From 8e986191b5e9e01f6d0bfa60abef737215118352 Mon Sep 17 00:00:00 2001 From: Padhraic Date: Wed, 18 Nov 2015 11:25:01 +0900 Subject: [PATCH 1/4] Add code first unit test --- .../EntityFramework.Extended.Test/App.config | 13 +- .../CodeFirst/EFExtendedCodeFirstTest.cs | 163 ++++++++++++++++++ ...EntityFramework.Extended.Test.net45.csproj | 13 +- 3 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 Source/EntityFramework.Extended.Test/CodeFirst/EFExtendedCodeFirstTest.cs diff --git a/Source/EntityFramework.Extended.Test/App.config b/Source/EntityFramework.Extended.Test/App.config index daf2d39..cb46948 100644 --- a/Source/EntityFramework.Extended.Test/App.config +++ b/Source/EntityFramework.Extended.Test/App.config @@ -2,8 +2,11 @@
- + + + + @@ -14,4 +17,12 @@ + + + + + + + + \ No newline at end of file diff --git a/Source/EntityFramework.Extended.Test/CodeFirst/EFExtendedCodeFirstTest.cs b/Source/EntityFramework.Extended.Test/CodeFirst/EFExtendedCodeFirstTest.cs new file mode 100644 index 0000000..2d94650 --- /dev/null +++ b/Source/EntityFramework.Extended.Test/CodeFirst/EFExtendedCodeFirstTest.cs @@ -0,0 +1,163 @@ +using System.ComponentModel.DataAnnotations; +using System.Data.Entity; +using System.Linq; +using EntityFramework.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.ComponentModel.DataAnnotations.Schema; +using EntityFramework.Audit; +using System.Data; +using System.Data.Entity.Core; + +//using Utile.Money; + +namespace EntityFramework.Test.CodeFirst +{ + [ComplexType] + public class Money + { + public Money() + { + CurrencyCode = "USD"; + Amount = 0; + } + public Money(decimal amount,string currencyCode) + { + CurrencyCode = currencyCode; + Amount = amount; + } + public string CurrencyCode { get; set; } + public decimal Amount { get; set; } + + } + public class Transaction + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int Id { get; set; } + public Money Money { get; set; } + public string Detail { get; set; } + } + + public class EFExtendedEntities : DbContext + { + public EFExtendedEntities() : base("EFExtendedCodeFirstTest") + { + + } + public DbSet Transactions { get; set; } + } + + public class TestsInitializer : DropCreateDatabaseAlways + { + protected override void Seed(EFExtendedEntities ctx) + { + //add a transaction + var trx = new Transaction + { + Money = new Money( 123456789012.3456m,"USD"), + Detail = "First Transaction" + }; + ctx.Transactions.Add(trx); + ctx.SaveChanges(); + base.Seed(ctx); + } + } + + [TestClass] + public class EFExtendedCodeFirstTest + { + private EFExtendedEntities _ctx; + + [TestInitialize] + public void Init() + { + var auditConfiguration = AuditConfiguration.Default; + + auditConfiguration.IncludeRelationships = true; + auditConfiguration.LoadRelationships = true; + auditConfiguration.DefaultAuditable = true; + + Database.SetInitializer(new TestsInitializer()); + _ctx = new EFExtendedEntities(); + } + + + [TestMethod] + public void EFExtendedCodeFirst_toXml() + { + // Arrange + var trx = new Transaction + { + Money = new Money(123456789012.3456m, "USD"),// complex type + Detail = "Another Transaction" + }; + var audit = _ctx.BeginAudit(); + _ctx.Transactions.Add(trx); + _ctx.SaveChanges(); + var log = audit.LastLog; + //Act + var xml = log.ToXml();//Exception thrown here + + // Assert + Assert.IsTrue(!string.IsNullOrEmpty(xml), "xml is not null or blank"); + } + + [TestMethod] + public void EFExtendedCodeFirst_Edit_Entities_not_empty_after_Complex_type_edit() + { + // Arrange + var trx = new Transaction + { + Money = new Money(123456789012.3456m, "USD"), + Detail = "Another Transaction" + }; + _ctx.Transactions.Add(trx); + _ctx.SaveChanges(); + + //Act + var audit = _ctx.BeginAudit(); + trx.Money.Amount = 10; + var t = _ctx.Set().FirstOrDefault(x => x.Id == trx.Id); + _ctx.Entry(t).CurrentValues.SetValues(trx); + _ctx.Entry(t).State = EntityState.Modified; + _ctx.SaveChanges(); + var log = audit.LastLog; + + // Assert + Assert.AreEqual(1, log.Entities.Count, "Change to Money was reconised by aduit"); + + } + + [TestMethod] + public void EFExtendedCodeFirst_Edit_Properties_contains_ComplexType_class() + { + // Arrange + var trx = new Transaction + { + Money = new Money(123456789012.3456m, "USD"), + Detail = "Another Transaction" + }; + _ctx.Transactions.Add(trx); + _ctx.SaveChanges(); + + //Act + var audit = _ctx.BeginAudit(); + //trx.Money.Amount = 10; + trx.Detail = "updated detail"; + var t = _ctx.Set().FirstOrDefault(x => x.Id == trx.Id); + _ctx.Entry(t).CurrentValues.SetValues(trx); + _ctx.Entry(t).State = EntityState.Modified; + _ctx.SaveChanges(); + var log = audit.LastLog; + + // Assert + + var obj = log.Entities[0].Properties; + Assert.AreEqual(3, obj.Count,"count equals number of properties in Transaction"); + + var tran = (IExtendedDataRecord) obj[2].Current; + + } + + } +} diff --git a/Source/EntityFramework.Extended.Test/EntityFramework.Extended.Test.net45.csproj b/Source/EntityFramework.Extended.Test/EntityFramework.Extended.Test.net45.csproj index 25227f2..81368f9 100644 --- a/Source/EntityFramework.Extended.Test/EntityFramework.Extended.Test.net45.csproj +++ b/Source/EntityFramework.Extended.Test/EntityFramework.Extended.Test.net45.csproj @@ -60,6 +60,7 @@ True + @@ -72,6 +73,10 @@ + + False + ..\..\..\Utile.Money\Utile.Money\bin\Debug\Utile.Money.dll + ..\packages\xunit.1.9.2\lib\net20\xunit.dll @@ -86,6 +91,7 @@ + @@ -109,9 +115,14 @@ App.config Always - + + Designer + + + +