-
-
Notifications
You must be signed in to change notification settings - Fork 384
Expand file tree
/
Copy pathEFExtendedCodeFirstTest.cs
More file actions
211 lines (184 loc) · 6.53 KB
/
EFExtendedCodeFirstTest.cs
File metadata and controls
211 lines (184 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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.");
}
}
}