Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing Unit test, models with complex types #185

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Source/EntityFramework.Extended.Test/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings>
<add name="EFExtendedCodeFirstTest" connectionString="Data Source=(LocalDb)\ProjectsV12;Initial Catalog=EFExtendedCodeFirstTest;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
using System;
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.Common;
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; }
[AuditPropertyFormat(typeof(TransactionFormat), "FormatMoney")]
public Money Money { get; set; }
public string Detail { get; set; }
}

public class TransactionFormat
{
public static object FormatMoney(AuditPropertyContext auditProperty)
{
DbDataRecord d = auditProperty.Value as DbDataRecord;
if (d == null)
return null;
try
{
Money m = new Money(d.GetDecimal(1),d.GetString(0));
return m;
}
catch (InvalidCastException)
{
return null;
}
}
}

public class EFExtendedEntities : DbContext
{
public EFExtendedEntities() : base("EFExtendedCodeFirstTest")
{

}
public DbSet<Transaction> Transactions { get; set; }
}

public class TestsInitializer : DropCreateDatabaseAlways<EFExtendedEntities>
{
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<Transaction>().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<Transaction>().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");


}

[TestMethod]
public void EFExtendedCodeFirst_Edit_Complex_type_property_Original_and_Current_not_the_same()
{
// 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<Transaction>().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.AreNotEqual(log.Entities[0].Properties[1].Original, log.Entities[0].Properties[1].Current,"Current and origional of string property Transaction.Detail are not the same.");
//cast complex type to DbDataRecord to access its property values.
Assert.AreNotEqual(log.Entities[0].Properties[2].Current, log.Entities[0].Properties[2].Original, "Current and origional of complex type property Transaction.Money.Amount are not the same.");

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core">
Expand All @@ -72,6 +73,10 @@
<Reference Include="System.Security" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Utile.Money, Version=1.0.1.1, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Utile.Money\Utile.Money\bin\Release\Utile.Money.dll</HintPath>
</Reference>
<Reference Include="xunit">
<HintPath>..\packages\xunit.1.9.2\lib\net20\xunit.dll</HintPath>
</Reference>
Expand All @@ -86,6 +91,7 @@
<Compile Include="Caching\CacheManagerTest.cs" />
<Compile Include="Caching\CachePolicyTest.cs" />
<Compile Include="Caching\CacheTagTest.cs" />
<Compile Include="CodeFirst\EFExtendedCodeFirstTest.cs" />
<Compile Include="ContainerTest.cs" />
<Compile Include="ExceptionAssert.cs" />
<Compile Include="IoCTest.cs" />
Expand All @@ -109,9 +115,14 @@
<DependentUpon>App.config</DependentUpon>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down