-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathOwnedCollectionBehaviours.cs
385 lines (334 loc) · 15.6 KB
/
OwnedCollectionBehaviours.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System.Collections.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RefactorThis.GraphDiff.Tests.Models;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
using System;
namespace RefactorThis.GraphDiff.Tests.Tests
{
[TestClass]
public class OwnedCollectionBehaviours : TestBase
{
[TestMethod]
public void ShouldUpdateItemInOwnedCollection()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "Hello" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
node1.OneToManyOwned.First().Title = "What's up";
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.OwnedCollection(p => p.OneToManyOwned));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
var owned = node2.OneToManyOwned.First();
Assert.IsTrue(owned.OneParent == node2 && owned.Title == "What's up");
}
}
[TestMethod]
public void ShouldUpdateItemInNestedOwnedCollection()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel
{
Title = "Hello",
OneToManyOneToManyOwned = new Collection<OneToManyOneToManyOwnedModel>
{
new OneToManyOneToManyOwnedModel {Title = "BeforeUpdate"}
}
}
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
var oneToManyOneToManyOwned = node1.OneToManyOwned.Single().OneToManyOneToManyOwned.Single();
var expectedId = oneToManyOneToManyOwned.Id;
const string expectedTitle = "AfterUpdate";
oneToManyOneToManyOwned.Title = expectedTitle;
using (var context = new TestDbContext())
{
// Setup mapping
node1 = context.UpdateGraph(node1, map => map.OwnedCollection(p => p.OneToManyOwned,
with => with.OwnedCollection(p => p.OneToManyOneToManyOwned)));
context.SaveChanges();
Assert.AreEqual(1, node1.OneToManyOwned.Count);
Assert.AreEqual(1, node1.OneToManyOwned.Single().OneToManyOneToManyOwned.Count);
oneToManyOneToManyOwned = node1.OneToManyOwned.Single().OneToManyOneToManyOwned.Single();
Assert.AreEqual(expectedId, oneToManyOneToManyOwned.Id);
Assert.AreEqual(expectedTitle, oneToManyOneToManyOwned.Title);
var node1Reloaded = context.Nodes
.Include("OneToManyOwned.OneToManyOneToManyOwned")
.Single(n => n.Id == node1.Id);
Assert.AreEqual(1, node1Reloaded.OneToManyOwned.Count);
Assert.AreEqual(1, node1Reloaded.OneToManyOwned.Single().OneToManyOneToManyOwned.Count);
oneToManyOneToManyOwned = node1Reloaded.OneToManyOwned.Single().OneToManyOneToManyOwned.Single();
Assert.AreEqual(expectedId, oneToManyOneToManyOwned.Id);
Assert.AreEqual(expectedTitle, oneToManyOneToManyOwned.Title);
}
}
[TestMethod]
public void ShouldAddNewItemInOwnedCollection()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "Hello" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
var newModel = new OneToManyOwnedModel { Title = "Hi" };
node1.OneToManyOwned.Add(newModel);
using (var context = new TestDbContext())
{
node1 = context.UpdateGraph(node1, map => map.OwnedCollection(p => p.OneToManyOwned));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.AreEqual(2, node2.OneToManyOwned.Count);
var ownedId = node1.OneToManyOwned.Skip(1).Select(o => o.Id).Single();
var owned = context.OneToManyOwnedModels.Single(p => p.Id == ownedId);
Assert.IsTrue(owned.OneParent == node2 && owned.Title == "Hi");
}
}
[TestMethod]
public void ShouldAddNewItemInOwnedCollectionWithoutChangingRequiredAssociate()
{
var root = new RootEntity { RequiredAssociate = new RequiredAssociate(), Sources = new List<RootEntity>() };
var requiredAssociate = new RequiredAssociate();
using (var context = new TestDbContext())
{
context.RootEntities.Add(root);
context.RequiredAssociates.Add(requiredAssociate);
context.SaveChanges();
} // Simulate detach
var expectedAssociateId = requiredAssociate.Id;
var owned = new RootEntity { RequiredAssociate = requiredAssociate };
root.Sources.Add(owned);
using (var context = new TestDbContext())
{
root = context.UpdateGraph(root, map => map.OwnedCollection(r => r.Sources, with => with.AssociatedEntity(s => s.RequiredAssociate)));
context.SaveChanges();
var ownedAfterSave = root.Sources.FirstOrDefault();
Assert.IsNotNull(ownedAfterSave);
Assert.IsNotNull(ownedAfterSave.RequiredAssociate);
Assert.AreEqual(expectedAssociateId, ownedAfterSave.RequiredAssociate.Id);
var ownedReloaded = context.RootEntities.Single(r => r.Id == ownedAfterSave.Id);
Assert.IsNotNull(ownedReloaded.RequiredAssociate);
Assert.AreEqual(expectedAssociateId, ownedReloaded.RequiredAssociate.Id);
}
}
[TestMethod]
public void ShouldAddTwoNewOwnedItemsWithSharedRequiredAssociate()
{
var root = new RootEntity { RequiredAssociate = new RequiredAssociate(), Sources = new List<RootEntity>() };
var requiredAssociate = new RequiredAssociate();
using (var context = new TestDbContext())
{
context.RootEntities.Add(root);
context.RequiredAssociates.Add(requiredAssociate);
context.SaveChanges();
} // Simulate detach
var expectedAssociateId = requiredAssociate.Id;
var ownedOne = new RootEntity { RequiredAssociate = requiredAssociate };
root.Sources.Add(ownedOne);
var ownedTwo = new RootEntity { RequiredAssociate = requiredAssociate };
root.Sources.Add(ownedTwo);
using (var context = new TestDbContext())
{
root = context.UpdateGraph(root, map => map.OwnedCollection(r => r.Sources, with => with.AssociatedEntity(s => s.RequiredAssociate)));
context.SaveChanges();
Assert.IsTrue(root.Sources.All(s => s.RequiredAssociate.Id == expectedAssociateId));
var sourceIds = root.Sources.Select(s => s.Id).ToArray();
var sourcesReloaded = context.RootEntities.Where(r => sourceIds.Contains(r.Id)).ToList();
Assert.IsTrue(sourcesReloaded.All(s => s.RequiredAssociate != null && s.RequiredAssociate.Id == expectedAssociateId));
}
}
[TestMethod]
public void ShouldNotChangeRequiredAssociateEvenIfItIsUsedTwice()
{
var requiredAssociate = new RequiredAssociate();
var root = new RootEntity { RequiredAssociate = requiredAssociate, Sources = new List<RootEntity>() };
using (var context = new TestDbContext())
{
context.RootEntities.Add(root);
context.RequiredAssociates.Add(requiredAssociate);
context.SaveChanges();
} // Simulate detach
var expectedAssociateId = requiredAssociate.Id;
var owned = new RootEntity { RequiredAssociate = requiredAssociate };
root.Sources.Add(owned);
using (var context = new TestDbContext())
{
root = context.UpdateGraph(root, map => map.OwnedCollection(r => r.Sources, with => with.AssociatedEntity(s => s.RequiredAssociate)));
context.SaveChanges();
var ownedAfterSave = root.Sources.FirstOrDefault();
Assert.IsNotNull(ownedAfterSave);
Assert.IsNotNull(ownedAfterSave.RequiredAssociate);
Assert.AreEqual(expectedAssociateId, ownedAfterSave.RequiredAssociate.Id);
var ownedReloaded = context.RootEntities.Single(r => r.Id == ownedAfterSave.Id);
Assert.IsNotNull(ownedReloaded.RequiredAssociate);
Assert.AreEqual(expectedAssociateId, ownedReloaded.RequiredAssociate.Id);
}
}
[TestMethod]
public void ShouldRemoveItemsInOwnedCollection()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "Hello" },
new OneToManyOwnedModel { Title = "Hello2" },
new OneToManyOwnedModel { Title = "Hello3" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.OwnedCollection(p => p.OneToManyOwned));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.IsTrue(node2.OneToManyOwned.Count == 0);
}
}
[TestMethod]
public void ShouldRemoveItemsInOwnedCollectionWhenSetToNull()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "Hello" },
new OneToManyOwnedModel { Title = "Hello2" },
new OneToManyOwnedModel { Title = "Hello3" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
node1.OneToManyOwned = null;
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.OwnedCollection(p => p.OneToManyOwned));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
Assert.IsTrue(node2.OneToManyOwned.Count == 0);
}
}
[TestMethod]
public void ShouldMergeTwoCollectionsAndDecideOnUpdatesDeletesAndAdds()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "This" },
new OneToManyOwnedModel { Title = "Is" },
new OneToManyOwnedModel { Title = "A" },
new OneToManyOwnedModel { Title = "Test" }
}
};
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
} // Simulate detach
node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
node1.OneToManyOwned.First().Title = "Hello";
node1.OneToManyOwned.Add(new OneToManyOwnedModel { Title = "Finish" });
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.OwnedCollection(p => p.OneToManyOwned));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
var list = node2.OneToManyOwned.ToList();
Assert.IsTrue(list[0].Title == "Hello");
Assert.IsTrue(list[1].Title == "A");
Assert.IsTrue(list[2].Title == "Test");
Assert.IsTrue(list[3].Title == "Finish");
}
}
[TestMethod]
public void ShouldUpdateItemInOwnedCollectionWithCustomKey()
{
var node1 = new TestNode
{
Title = "New Node",
OneToManyOwned = new List<OneToManyOwnedModel>
{
new OneToManyOwnedModel { Title = "Hello", UniqueId = new Guid("DA6B78FF-BB7F-4FA1-8659-F64AC6457D14") }
}
};
int originalOwnedId;
using (var context = new TestDbContext())
{
context.Nodes.Add(node1);
context.SaveChanges();
originalOwnedId = node1.OneToManyOwned.First().Id;
} // Simulate detach
node1.OneToManyOwned.First().Title = "What's up";
node1.OneToManyOwned.First().Id = 0; //We will try to update on Guid
using (var context = new TestDbContext())
{
// Setup mapping
context.UpdateGraph(node1, map => map
.OwnedCollection(p => p.OneToManyOwned),
keysConfiguration: new KeysConfiguration()
.ForEntity<OneToManyOwnedModel>(e => e.UniqueId));
context.SaveChanges();
var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
Assert.IsNotNull(node2);
var owned = node2.OneToManyOwned.First();
Assert.IsTrue(owned.OneParent == node2 && owned.Title == "What's up" && owned.Id == originalOwnedId);
}
}
}
}