comparison UnitTests/Fluent/FluentMapTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using BLToolkit.Data;
5 using BLToolkit.Data.Linq;
6 using BLToolkit.DataAccess;
7 using BLToolkit.Fluent.Test.MockDataBase;
8 using BLToolkit.Mapping;
9 using BLToolkit.Mapping.Fluent;
10 using BLToolkit.Reflection.Extension;
11 using Microsoft.VisualStudio.TestTools.UnitTesting;
12
13 namespace BLToolkit.Fluent.Test
14 {
15 /// <summary>
16 /// Тестирование FluentMap
17 /// </summary>
18 [TestClass]
19 public class FluentMapTest
20 {
21 [TestInitialize]
22 public void Initialize()
23 {
24 DbManager.AddDataProvider(typeof(MockDataProvider));
25 }
26
27 /// <summary>
28 /// TableName mapping
29 /// </summary>
30 [TestMethod]
31 public void ShouldMapTableName()
32 {
33 // db config
34 var conn = new MockDb()
35 .NewReader("Field1")
36 .NewRow(1);
37
38 using (conn)
39 using (var db = new DbManager(conn))
40 {
41 // fluent config
42 new FluentMap<TableNameDbo>()
43 .TableName("TableNameDboT1")
44 .MapTo(db);
45
46 // when
47 db.GetTable<TableNameDbo>().ToArray();
48
49 // then
50 conn.Commands[0]
51 .Assert().AreTable("TableNameDboT1", "Fail mapping");
52 }
53 }
54
55 /// <summary>
56 /// TableName mapping fro child
57 /// </summary>
58 [TestMethod]
59 public void ShouldMapTableNameForChild()
60 {
61 // db config
62 var conn = new MockDb()
63 .NewReader("Field1")
64 .NewRow(1);
65
66 using (conn)
67 using (var db = new DbManager(conn))
68 {
69 // fluent config
70 new FluentMap<TableNameDbo>()
71 .TableName("TableNameDboT1")
72 .MapTo(db);
73
74 // when
75 db.GetTable<TableNameChildDbo>().ToArray();
76
77 // then
78 conn.Commands[0]
79 .Assert().AreTable("TableNameDboT1", "Fail mapping");
80 }
81 }
82
83 /// <summary>
84 /// MapField mapping
85 /// </summary>
86 [TestMethod]
87 public void ShouldMapField()
88 {
89 // db config
90 var conn = new MockDb()
91 .NewNonQuery();
92
93 using (conn)
94 using (var db = new DbManager(conn))
95 {
96 // fluent config
97 new FluentMap<MapFieldDbo>()
98 .MapField(_ => _.Field1, "f1")
99 .MapTo(db);
100
101 // when
102 db.GetTable<MapFieldDbo>().Insert(() => new MapFieldDbo { Field1 = 1 });
103
104 // then
105 conn.Commands[0]
106 .Assert().AreField("f1", "Fail mapping");
107 }
108 }
109
110 /// <summary>
111 /// MapField mapping for child
112 /// </summary>
113 [TestMethod]
114 public void ShouldMapFieldforChild()
115 {
116 // db config
117 var conn = new MockDb()
118 .NewNonQuery();
119
120 using (conn)
121 using (var db = new DbManager(conn))
122 {
123 // fluent config
124 new FluentMap<MapFieldDbo>()
125 .MapField(_ => _.Field1, "f1")
126 .MapTo(db);
127
128 // when
129 db.GetTable<MapFieldChild1Dbo>().Insert(() => new MapFieldChild1Dbo { Field1 = 1 });
130
131 // then
132 conn.Commands[0]
133 .Assert().AreField("f1", "Fail mapping");
134 }
135 }
136
137 /// <summary>
138 /// MapField mapping for child override
139 /// </summary>
140 [TestMethod]
141 public void ShouldMapFieldforChildOverride()
142 {
143 // db config
144 var conn = new MockDb()
145 .NewNonQuery();
146
147 using (conn)
148 using (var db = new DbManager(conn))
149 {
150 // fluent config
151 new FluentMap<MapFieldDbo>()
152 .MapField(_ => _.Field1, "f1")
153 .MapTo(db);
154 new FluentMap<MapFieldChild2Dbo>()
155 .MapField(_ => _.Field1, "fc1")
156 .MapTo(db);
157
158 // when
159 db.GetTable<MapFieldChild2Dbo>().Insert(() => new MapFieldChild2Dbo { Field1 = 1 });
160
161 // then
162 conn.Commands[0]
163 .Assert().AreField("fc1", "Fail mapping");
164 }
165 }
166
167 /// <summary>
168 /// PrimaryKey mapping
169 /// </summary>
170 [TestMethod]
171 public void ShouldMapPrimaryKey()
172 {
173 // db config
174 var conn = new MockDb()
175 .NewReader("Field1", "Field2")
176 .NewRow(1, 2);
177
178 using (conn)
179 using (var db = new DbManager(conn))
180 {
181 // fluent config
182 new FluentMap<PrimaryKeyDbo>()
183 .PrimaryKey(_ => _.Field2)
184 .MapTo(db);
185
186 // when
187 AssertExceptionEx.AreNotException<Exception>(() => new SqlQuery<PrimaryKeyDbo>(db).SelectByKey(1)
188 , "Fail query");
189
190 // then
191 Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
192 conn.Commands[0]
193 .Assert().AreField("Field2", 2, "Not where part");
194 }
195 }
196
197 /// <summary>
198 /// PrimaryKey mapping for child
199 /// </summary>
200 [TestMethod]
201 public void ShouldMapPrimaryKeyForChild()
202 {
203 // db config
204 var conn = new MockDb()
205 .NewReader("Field1", "Field2")
206 .NewRow(1, 2);
207
208 using (conn)
209 using (var db = new DbManager(conn))
210 {
211 // fluent config
212 new FluentMap<PrimaryKeyDbo>()
213 .PrimaryKey(_ => _.Field2)
214 .MapTo(db);
215
216 // when
217 AssertExceptionEx.AreNotException<Exception>(() => new SqlQuery<PrimaryKeyChildDbo>(db).SelectByKey(1)
218 , "Fail query");
219
220 // then
221 Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
222 conn.Commands[0]
223 .Assert().AreField("Field2", 2, "Not where part");
224 }
225 }
226
227 /// <summary>
228 /// NonUpdatable use
229 /// </summary>
230 [TestMethod]
231 public void ShouldMapNonUpdatable()
232 {
233 // db config
234 var conn = new MockDb()
235 .NewNonQuery();
236
237 using (conn)
238 using (var db = new DbManager(conn))
239 {
240 // fluent config
241 new FluentMap<NonUpdatableDbo>()
242 .NonUpdatable(_ => _.Field1)
243 .MapTo(db);
244
245 // when
246 new SqlQuery<NonUpdatableDbo>(db).Insert(new NonUpdatableDbo { Field1 = 10, Field2 = 1 });
247
248 // then
249 Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
250 }
251 }
252
253 /// <summary>
254 /// NonUpdatable use for child
255 /// </summary>
256 [TestMethod]
257 public void ShouldMapNonUpdatableForChild()
258 {
259 // db config
260 var conn = new MockDb()
261 .NewNonQuery();
262
263 using (conn)
264 using (var db = new DbManager(conn))
265 {
266 // fluent config
267 new FluentMap<NonUpdatableDbo>()
268 .NonUpdatable(_ => _.Field1)
269 .MapTo(db);
270
271 // when
272 new SqlQuery<NonUpdatableChildDbo>(db).Insert(new NonUpdatableChildDbo { Field1 = 10, Field2 = 1 });
273
274 // then
275 Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
276 }
277 }
278
279 /// <summary>
280 /// SqlIgnore mapping on insert
281 /// </summary>
282 [TestMethod]
283 public void ShouldMapSqlIgnoreInsert()
284 {
285 // db config
286 var conn = new MockDb()
287 .NewNonQuery();
288
289 using (conn)
290 using (var db = new DbManager(conn))
291 {
292 // fluent config
293 new FluentMap<SqlIgnoreInsertDbo>()
294 .SqlIgnore(_ => _.Field2)
295 .MapTo(db);
296
297 // when / then
298 new SqlQuery<SqlIgnoreInsertDbo>(db).Insert(new SqlIgnoreInsertDbo { Field1 = 20, Field2 = 2 });
299 AssertExceptionEx.AreException<LinqException>(
300 () => db.GetTable<SqlIgnoreInsertDbo>().Insert(() => new SqlIgnoreInsertDbo { Field1 = 10, Field2 = 1 })
301 , "Fail for linq");
302
303 // then
304 conn.Commands[0]
305 .Assert().AreNotField("Field2", "Field exists");
306 Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
307 }
308 }
309
310 /// <summary>
311 /// SqlIgnore mapping on select
312 /// </summary>
313 [TestMethod]
314 public void ShouldMapSqlIgnoreSelect()
315 {
316 // db config
317 var conn = new MockDb()
318 .NewReader("Field1")
319 .NewRow(10);
320
321 using (conn)
322 using (var db = new DbManager(conn))
323 {
324 // fluent config
325 new FluentMap<SqlIgnoreSelectDbo>()
326 .SqlIgnore(_ => _.Field2)
327 .MapTo(db);
328
329 var table = db.GetTable<SqlIgnoreSelectDbo>();
330
331 // when
332 (from t in table where t.Field1 == 10 select t).First();
333
334 // then
335 conn.Commands[0]
336 .Assert().AreNotField("Field2", "Field exists");
337 }
338 }
339
340 /// <summary>
341 /// SqlIgnore mapping for child
342 /// </summary>
343 [TestMethod]
344 public void ShouldMapSqlIgnoreForChild()
345 {
346 // db config
347 var conn = new MockDb()
348 .NewReader("Field1")
349 .NewRow(10);
350
351 using (conn)
352 using (var db = new DbManager(conn))
353 {
354 // fluent config
355 new FluentMap<SqlIgnoreSelectDbo>()
356 .SqlIgnore(_ => _.Field2)
357 .MapTo(db);
358
359 var table = db.GetTable<SqlIgnoreChildDbo>();
360
361 // when
362 (from t in table where t.Field1 == 10 select t).First();
363
364 // then
365 conn.Commands[0]
366 .Assert().AreNotField("Field2", "Field exists");
367 }
368 }
369
370 /// <summary>
371 /// MapIgnore mapping on insert
372 /// </summary>
373 [TestMethod]
374 public void ShouldMapIgnoreInsert()
375 {
376 // db config
377 var conn = new MockDb()
378 .NewNonQuery();
379
380 using (conn)
381 using (var db = new DbManager(conn))
382 {
383 // fluent config
384 new FluentMap<MapIgnoreInsertDbo>()
385 .MapIgnore(_ => _.Field2)
386 .MapTo(db);
387
388 // when / then
389 new SqlQuery<MapIgnoreInsertDbo>(db).Insert(new MapIgnoreInsertDbo { Field1 = 20, Field2 = 2 });
390
391 AssertExceptionEx.AreException<LinqException>(
392 () => db.GetTable<MapIgnoreInsertDbo>().Insert(() => new MapIgnoreInsertDbo { Field1 = 10, Field2 = 1 })
393 , "Fail for linq");
394
395 // then
396 conn.Commands[0]
397 .Assert().AreNotField("Field2", "Field exists");
398 Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
399 }
400 }
401
402 /// <summary>
403 /// MapIgnore mapping on select
404 /// </summary>
405 [TestMethod]
406 public void ShouldMapIgnoreSelect()
407 {
408 // db config
409 var conn = new MockDb()
410 .NewReader("Field1")
411 .NewRow(10, 1);
412
413 using (conn)
414 using (var db = new DbManager(conn))
415 {
416 // fluent config
417 new FluentMap<MapIgnoreSelectDbo>()
418 .MapIgnore(_ => _.Field2)
419 .MapTo(db);
420
421 var table = db.GetTable<MapIgnoreSelectDbo>();
422
423 // when
424 (from t in table where t.Field1 == 10 select t).First();
425
426 // then
427 conn.Commands[0]
428 .Assert().AreNotField("Field2", "Field exists");
429 }
430 }
431
432 /// <summary>
433 /// MapIgnore mapping for child
434 /// </summary>
435 [TestMethod]
436 public void ShouldMapIgnoreForChild()
437 {
438 // db config
439 var conn = new MockDb()
440 .NewReader("Field1")
441 .NewRow(10, 1);
442
443 using (conn)
444 using (var db = new DbManager(conn))
445 {
446 // fluent config
447 new FluentMap<MapIgnoreSelectDbo>()
448 .MapIgnore(_ => _.Field2)
449 .MapTo(db);
450
451 var table = db.GetTable<MapIgnoreChildDbo>();
452
453 // when
454 (from t in table where t.Field1 == 10 select t).First();
455
456 // then
457 conn.Commands[0]
458 .Assert().AreNotField("Field2", "Field exists");
459 }
460 }
461
462 /// <summary>
463 /// Trimmable mapping
464 /// </summary>
465 [TestMethod]
466 public void ShouldMapTrimmable()
467 {
468 // db config
469 var conn = new MockDb()
470 .NewReader("Field1")
471 .NewRow("test ");
472
473 using (conn)
474 using (var db = new DbManager(conn))
475 {
476 // fluent config
477 new FluentMap<TrimmableDbo>()
478 .Trimmable(_ => _.Field1)
479 .MapTo(db);
480
481 var table = db.GetTable<TrimmableDbo>();
482
483 // when
484 var dbo = (from t in table select t).First();
485
486 // then
487 Assert.AreEqual("test", dbo.Field1, "Not trimmable");
488 }
489 }
490
491 /// <summary>
492 /// Trimmable mapping for child
493 /// </summary>
494 [TestMethod]
495 public void ShouldMapTrimmableForChild()
496 {
497 // db config
498 var conn = new MockDb()
499 .NewReader("Field1")
500 .NewRow("test ");
501
502 using (conn)
503 using (var db = new DbManager(conn))
504 {
505 // fluent config
506 new FluentMap<TrimmableDbo>()
507 .Trimmable(_ => _.Field1)
508 .MapTo(db);
509
510 var table = db.GetTable<TrimmableChildDbo>();
511
512 // when
513 var dbo = (from t in table select t).First();
514
515 // then
516 Assert.AreEqual("test", dbo.Field1, "Not trimmable");
517 }
518 }
519
520 /// <summary>
521 /// MapValue mapping for member
522 /// </summary>
523 [TestMethod]
524 public void ShouldMapValueForMember()
525 {
526 // db config
527 var conn = new MockDb()
528 .NewReader("Field1", "Field2")
529 .NewRow(true, false);
530
531 using (conn)
532 using (var db = new DbManager(conn))
533 {
534 #warning bug for maping TO db
535 // fluent config
536 new FluentMap<MapValueMemberDbo>()
537 .MapField(_ => _.Field1)
538 .MapValue("result true", true)
539 .MapValue("result false", false)
540 .MapField(_ => _.Field2)
541 .MapValue("value true", true)
542 .MapValue("value false", false)
543 .MapTo(db);
544
545 var table = db.GetTable<MapValueMemberDbo>();
546
547 // when
548 var dbo = (from t in table select t).First();
549
550 // then
551 Assert.AreEqual("result true", dbo.Field1, "Not map from value1");
552 Assert.AreEqual("value false", dbo.Field2, "Not map from value2");
553 }
554 }
555
556 /// <summary>
557 /// MapValue mapping for member for child
558 /// </summary>
559 [TestMethod]
560 public void ShouldMapValueForMemberForChild()
561 {
562 // db config
563 var conn = new MockDb()
564 .NewReader("Field1", "Field2")
565 .NewRow(true, false);
566
567 using (conn)
568 using (var db = new DbManager(conn))
569 {
570 #warning bug for maping TO db
571 // fluent config
572 new FluentMap<MapValueMemberDbo>()
573 .MapField(_ => _.Field1)
574 .MapValue("result true", true)
575 .MapValue("result false", false)
576 .MapField(_ => _.Field2)
577 .MapValue("value true", true)
578 .MapValue("value false", false)
579 .MapTo(db);
580
581 var table = db.GetTable<MapValueMemberChildDbo>();
582
583 // when
584 var dbo = (from t in table select t).First();
585
586 // then
587 Assert.AreEqual("result true", dbo.Field1, "Not map from value1");
588 Assert.AreEqual("value false", dbo.Field2, "Not map from value2");
589 }
590 }
591
592 /// <summary>
593 /// MapValue mapping for enum
594 /// </summary>
595 [TestMethod]
596 public void ShouldMapValueForEnum()
597 {
598 // db config
599 var conn = new MockDb()
600 .NewReader("Field1", "Field2", "Field3", "Field4")
601 .NewRow("ok", "super", "yes", 10);
602
603 using (conn)
604 using (var db = new DbManager(conn))
605 {
606 #warning bug for maping TO db
607 // fluent config
608 new FluentMap<MapValueEnumDbo>()
609 .MapValue(MapValueEnum.Value1, "ok", "yes")
610 .MapValue(MapValueEnum.Value2, "super")
611 .MapTo(db);
612
613 var table = db.GetTable<MapValueEnumDbo>();
614
615 // when
616 var dbo = (from t in table select t).First();
617
618 // then
619 Assert.AreEqual(MapValueEnum.Value1, dbo.Field1, "Not map from value1");
620 Assert.AreEqual(MapValueEnum.Value2, dbo.Field2, "Not map from value2");
621 Assert.AreEqual(MapValueEnum.Value1, dbo.Field3, "Not map from value3");
622 }
623 }
624
625 /// <summary>
626 /// MapValue mapping for enum for child
627 /// </summary>
628 [TestMethod]
629 public void ShouldMapValueForEnumForChild()
630 {
631 // db config
632 var conn = new MockDb()
633 .NewReader("Field1", "Field2", "Field3", "Field4")
634 .NewRow("ok", "super", "yes", 10);
635
636 using (conn)
637 using (var db = new DbManager(conn))
638 {
639 #warning bug for maping TO db
640 // fluent config
641 new FluentMap<MapValueEnumDbo>()
642 .MapValue(MapValueEnum.Value1, "ok", "yes")
643 .MapValue(MapValueEnum.Value2, "super")
644 .MapTo(db);
645
646 var table = db.GetTable<MapValueEnumChildDbo>();
647
648 // when
649 var dbo = (from t in table select t).First();
650
651 // then
652 Assert.AreEqual(MapValueEnum.Value1, dbo.Field1, "Not map from value1");
653 Assert.AreEqual(MapValueEnum.Value2, dbo.Field2, "Not map from value2");
654 Assert.AreEqual(MapValueEnum.Value1, dbo.Field3, "Not map from value3");
655 }
656 }
657
658 /// <summary>
659 /// MapValue mapping for type
660 /// </summary>
661 [TestMethod]
662 public void ShouldMapValueForType()
663 {
664 // db config
665 var conn = new MockDb()
666 .NewReader("Field1", "Field2", "Field3")
667 .NewRow("one", "two", true);
668
669 using (conn)
670 using (var db = new DbManager(conn))
671 {
672 #warning bug for property any different types
673 #warning bug for maping TO db
674 // fluent config
675 new FluentMap<MapValueTypeDbo>()
676 .MapValue(1, "one", "1")
677 .MapValue(2, "two")
678 .MapValue(3, true)
679 .MapTo(db);
680
681 var table = db.GetTable<MapValueTypeDbo>();
682
683 // when
684 var dbo = (from t in table select t).First();
685
686 // then
687 Assert.AreEqual(1, dbo.Field1, "Not map from value1");
688 Assert.AreEqual(2, dbo.Field2, "Not map from value2");
689 Assert.AreEqual(3, dbo.Field3, "Not map from value3");
690 }
691 }
692
693 /// <summary>
694 /// MapValue mapping for type for child
695 /// </summary>
696 [TestMethod]
697 public void ShouldMapValueForTypeForChild()
698 {
699 // db config
700 var conn = new MockDb()
701 .NewReader("Field1", "Field2", "Field3")
702 .NewRow("one", "two", true);
703
704 using (conn)
705 using (var db = new DbManager(conn))
706 {
707 #warning bug for property any different types
708 #warning bug for maping TO db
709 // fluent config
710 new FluentMap<MapValueTypeDbo>()
711 .MapValue(1, "one", "1")
712 .MapValue(2, "two")
713 .MapValue(3, true)
714 .MapTo(db);
715
716 var table = db.GetTable<MapValueTypeChildDbo>();
717
718 // when
719 var dbo = (from t in table select t).First();
720
721 // then
722 Assert.AreEqual(1, dbo.Field1, "Not map from value1");
723 Assert.AreEqual(2, dbo.Field2, "Not map from value2");
724 Assert.AreEqual(3, dbo.Field3, "Not map from value3");
725 }
726 }
727
728 /// <summary>
729 /// InheritanceMapping mapping
730 /// </summary>
731 [TestMethod]
732 public void ShouldMapInheritanceMapping()
733 {
734 // db config
735 var conn = new MockDb()
736 .NewReader("Field1", "Field2")
737 .NewRow(1, 1)
738 .NewRow(2, 2);
739
740 using (conn)
741 using (var db = new DbManager(conn))
742 {
743 // fluent config
744 new FluentMap<InheritanceMappingDbo>()
745 .InheritanceMapping<InheritanceMappingDbo1>(1)
746 .InheritanceMapping<InheritanceMappingDbo2>(2)
747 .InheritanceField(_ => _.Field2)
748 .MapTo(db);
749
750 var table = db.GetTable<InheritanceMappingDbo>();
751
752 // when
753 var dbos = (from t in table select t).ToArray();
754
755 // then
756 Assert.IsInstanceOfType(dbos[0], typeof(InheritanceMappingDbo1), "Invalid type1");
757 Assert.IsInstanceOfType(dbos[1], typeof(InheritanceMappingDbo2), "Invalid type2");
758 }
759 }
760
761 /// <summary>
762 /// InheritanceMapping mapping select only child
763 /// </summary>
764 [TestMethod]
765 public void ShouldMapInheritanceMappingSelectChild()
766 {
767 // db config
768 var conn = new MockDb()
769 .NewReader("Field1", "Field2")
770 .NewRow(1, 1);
771
772 using (conn)
773 using (var db = new DbManager(conn))
774 {
775 // fluent config
776 new FluentMap<InheritanceMappingChDbo>()
777 .TableName("tt")
778 .InheritanceMapping<InheritanceMappingChDbo1>(11111)
779 .InheritanceMapping<InheritanceMappingChDbo2>(22222)
780 .InheritanceField(_ => _.Field2)
781 .MapTo(db);
782
783 var table = db.GetTable<InheritanceMappingChDbo1>();
784
785 // when
786 var dbos = (from t in table select t).ToArray();
787
788 // then
789 Assert.IsInstanceOfType(dbos[0], typeof(InheritanceMappingChDbo1), "Invalid type");
790 Assert.IsTrue(conn.Commands[0].CommandText.ToLower().Contains("where"), "Not condition");
791 Assert.IsTrue(conn.Commands[0].CommandText.ToLower().Contains("11111"), "Fail condition value");
792 conn.Commands[0]
793 .Assert().AreField("Field2", 2, "Not in where part");
794 }
795 }
796
797 /// <summary>
798 /// Association mapping to many
799 /// </summary>
800 [TestMethod]
801 public void ShouldMapAssociationToMany()
802 {
803 // db config
804 var conn = new MockDb()
805 .NewReader("Field4")
806 .NewRow("TestMany");
807
808 using (conn)
809 using (var db = new DbManager(conn))
810 {
811 // fluent config
812 new FluentMap<AssociationThis1Dbo>()
813 .MapField(_ => _.FieldThis1, "ThisId")
814 .MapField(_ => _.FieldThis2)
815 .Association(_ => _.FieldThis1).ToMany((AssociationOtherDbo _) => _.FieldOther2)
816 .MapTo(db);
817
818 var table = db.GetTable<AssociationThis1Dbo>();
819
820 // when
821 var dbo = (from t in table select t.FieldThis2.First().FieldOther4).First();
822
823 // then
824 Assert.AreEqual("TestMany", dbo, "Fail result for many");
825 conn.Commands[0]
826 .Assert().AreField("ThisId", "Fail this key");
827 conn.Commands[0]
828 .Assert().AreField("FieldOther2", "Fail other key");
829 conn.Commands[0]
830 .Assert().AreField("FieldOther4", "Fail other result");
831 Assert.AreEqual(3, conn.Commands[0].Fields.Count, "More fields");
832 }
833 }
834
835 /// <summary>
836 /// Association mapping to one
837 /// </summary>
838 [TestMethod]
839 public void ShouldMapAssociationToOne()
840 {
841 // db config
842 var conn = new MockDb()
843 .NewReader("Field4")
844 .NewRow("TestOne");
845
846 using (conn)
847 using (var db = new DbManager(conn))
848 {
849 // fluent config
850 new FluentMap<AssociationThis2Dbo>()
851 .MapField(_ => _.FieldThis1, "ThisId")
852 .MapField(_ => _.FieldThis3)
853 .Association(_ => _.FieldThis1).ToOne(_ => _.FieldOther3)
854 .MapTo(db);
855
856 var table = db.GetTable<AssociationThis2Dbo>();
857
858 // when
859 var dbo = (from t in table select t.FieldThis3.FieldOther4).First();
860
861 // then
862 Assert.AreEqual("TestOne", dbo, "Fail result for many");
863 conn.Commands[0]
864 .Assert().AreField("ThisId", "Fail this key");
865 conn.Commands[0]
866 .Assert().AreField("FieldOther3", "Fail other key");
867 conn.Commands[0]
868 .Assert().AreField("FieldOther4", "Fail other result");
869 Assert.AreEqual(3, conn.Commands[0].Fields.Count, "More fields");
870 }
871 }
872
873 /// <summary>
874 /// Association mapping for child
875 /// </summary>
876 [TestMethod]
877 public void ShouldMapAssociationForChild()
878 {
879 // db config
880 var conn = new MockDb()
881 .NewReader("Field4")
882 .NewRow("TestOne");
883
884 using (conn)
885 using (var db = new DbManager(conn))
886 {
887 // fluent config
888 new FluentMap<AssociationThis2Dbo>()
889 .MapField(_ => _.FieldThis1, "ThisId")
890 .MapField(_ => _.FieldThis3)
891 .Association(_ => _.FieldThis1).ToOne(_ => _.FieldOther3)
892 .MapTo(db);
893
894 var table = db.GetTable<AssociationThis2ChildDbo>();
895
896 // when
897 var dbo = (from t in table select t.FieldThis3.FieldOther4).First();
898
899 // then
900 Assert.AreEqual("TestOne", dbo, "Fail result for many");
901 conn.Commands[0]
902 .Assert().AreField("ThisId", "Fail this key");
903 conn.Commands[0]
904 .Assert().AreField("FieldOther3", "Fail other key");
905 conn.Commands[0]
906 .Assert().AreField("FieldOther4", "Fail other result");
907 Assert.AreEqual(3, conn.Commands[0].Fields.Count, "More fields");
908 }
909 }
910
911 /// <summary>
912 /// Relation mapping
913 /// </summary>
914 [TestMethod]
915 public void ShouldMapRelation()
916 {
917 // given
918 List<Parent> parents = new List<Parent>();
919 MapResultSet[] sets = new MapResultSet[3];
920
921 sets[0] = new MapResultSet(typeof(Parent), parents);
922 sets[1] = new MapResultSet(typeof(Child));
923 sets[2] = new MapResultSet(typeof(Grandchild));
924
925 // db config
926 var conn = new MockDb()
927 .NewReader("ParentID")
928 .NewRow(1)
929 .NewRow(2)
930 .NextResult("ChildID", "ParentID")
931 .NewRow(4, 1)
932 .NewRow(5, 2)
933 .NewRow(6, 2)
934 .NewRow(7, 1)
935 .NextResult("GrandchildID", "ChildID")
936 .NewRow(1, 4)
937 .NewRow(2, 4)
938 .NewRow(3, 5)
939 .NewRow(4, 5)
940 .NewRow(5, 6)
941 .NewRow(6, 6)
942 .NewRow(7, 7)
943 .NewRow(8, 7);
944
945 using (conn)
946 using (var db = new DbManager(conn))
947 {
948 // fluent config
949 new FluentMap<Parent>()
950 .MapField(_ => _.ID, "ParentID").PrimaryKey()
951 .MapField(_ => _.Children).Relation()
952 .MapTo(db);
953 new FluentMap<Child>()
954 .MapField(_ => _.Parent.ID, "ParentID")
955 .MapField(_ => _.ID, "ChildID").PrimaryKey()
956 .MapField(_ => _.Parent).Relation()
957 .MapField(_ => _.Grandchildren).Relation()
958 .MapTo(db);
959 new FluentMap<Grandchild>()
960 .MapField(_ => _.Child.ID, "ChildID")
961 .MapField(_ => _.ID, "GrandchildID").PrimaryKey()
962 .MapField(_ => _.Child).Relation()
963 .MapTo(db);
964
965 // when
966 db.SetCommand("select *").ExecuteResultSet(sets);
967 }
968
969 // then
970 Assert.IsTrue(parents.Any());
971
972 foreach (Parent parent in parents)
973 {
974 Assert.IsNotNull(parent);
975 Assert.IsTrue(parent.Children.Any());
976
977 foreach (Child child in parent.Children)
978 {
979 Assert.AreEqual(parent, child.Parent);
980 Assert.IsTrue(child.Grandchildren.Any());
981
982 foreach (Grandchild grandchild in child.Grandchildren)
983 {
984 Assert.AreEqual(child, grandchild.Child);
985 Assert.AreEqual(parent, grandchild.Child.Parent);
986 }
987 }
988 }
989 }
990
991 /// <summary>
992 /// Relation mapping for child
993 /// </summary>
994 [TestMethod]
995 public void ShouldMapRelationForChild()
996 {
997 // given
998 List<Parent2Child> parents = new List<Parent2Child>();
999 MapResultSet[] sets = new MapResultSet[2];
1000
1001 sets[0] = new MapResultSet(typeof(Parent2Child), parents);
1002 sets[1] = new MapResultSet(typeof(Child2));
1003
1004 // db config
1005 var conn = new MockDb()
1006 .NewReader("ParentID")
1007 .NewRow(1)
1008 .NewRow(2)
1009 .NextResult("ChildID", "ParentID")
1010 .NewRow(4, 1)
1011 .NewRow(5, 2)
1012 .NewRow(6, 2)
1013 .NewRow(7, 1);
1014
1015 using (conn)
1016 using (var db = new DbManager(conn))
1017 {
1018 // fluent config
1019 new FluentMap<Parent2>()
1020 .MapField(_ => _.ID, "ParentID").PrimaryKey()
1021 .MapField(_ => _.Children).Relation()
1022 .MapTo(db);
1023 new FluentMap<Child2>()
1024 .MapField(_ => _.Parent.ID, "ParentID")
1025 .MapField(_ => _.ID, "ChildID").PrimaryKey()
1026 .MapField(_ => _.Parent).Relation()
1027 .MapTo(db);
1028
1029 // when
1030 db.SetCommand("select *").ExecuteResultSet(sets);
1031 }
1032
1033 // then
1034 Assert.IsTrue(parents.Any());
1035
1036 foreach (Parent2Child parent in parents)
1037 {
1038 Assert.IsNotNull(parent);
1039 Assert.IsTrue(parent.Children.Any());
1040
1041 foreach (Child2 child in parent.Children)
1042 {
1043 Assert.AreEqual(parent, child.Parent);
1044 }
1045 }
1046 }
1047
1048 #region Dbo
1049 public class TableNameDbo
1050 {
1051 public int Field1 { get; set; }
1052 }
1053 public class TableNameChildDbo : TableNameDbo
1054 {
1055 }
1056 public class MapFieldDbo
1057 {
1058 public int Field1 { get; set; }
1059 }
1060 public class MapFieldChild1Dbo : MapFieldDbo
1061 {
1062 }
1063 public class MapFieldChild2Dbo : MapFieldDbo
1064 {
1065 }
1066 public class PrimaryKeyDbo
1067 {
1068 public int Field1 { get; set; }
1069 public int Field2 { get; set; }
1070 }
1071 public class PrimaryKeyChildDbo : PrimaryKeyDbo
1072 {
1073 }
1074 public class NonUpdatableDbo
1075 {
1076 public int Field1 { get; set; }
1077 public int Field2 { get; set; }
1078 }
1079 public class NonUpdatableChildDbo : NonUpdatableDbo
1080 {
1081 }
1082 public class SqlIgnoreInsertDbo
1083 {
1084 public int Field1 { get; set; }
1085 public int Field2 { get; set; }
1086 }
1087 public class SqlIgnoreSelectDbo
1088 {
1089 public int Field1 { get; set; }
1090 public int Field2 { get; set; }
1091 }
1092 public class SqlIgnoreChildDbo : SqlIgnoreSelectDbo
1093 {
1094 }
1095 public class MapIgnoreInsertDbo
1096 {
1097 public int Field1 { get; set; }
1098 public int Field2 { get; set; }
1099 }
1100 public class MapIgnoreSelectDbo
1101 {
1102 public int Field1 { get; set; }
1103 public int Field2 { get; set; }
1104 }
1105 public class MapIgnoreChildDbo : MapIgnoreSelectDbo
1106 {
1107 }
1108 public class TrimmableDbo
1109 {
1110 public string Field1 { get; set; }
1111 }
1112 public class TrimmableChildDbo : TrimmableDbo
1113 {
1114 }
1115 public class MapValueMemberDbo
1116 {
1117 public string Field1 { get; set; }
1118 public string Field2 { get; set; }
1119 }
1120 public class MapValueMemberChildDbo : MapValueMemberDbo
1121 {
1122 }
1123 public class MapValueEnumDbo
1124 {
1125 public MapValueEnum Field1 { get; set; }
1126 public MapValueEnum Field2 { get; set; }
1127 public MapValueEnum Field3 { get; set; }
1128 public int Field4 { get; set; }
1129 }
1130 public class MapValueEnumChildDbo : MapValueEnumDbo
1131 {
1132 }
1133 public enum MapValueEnum
1134 {
1135 Value1,
1136 Value2
1137 }
1138 public class MapValueTypeDbo
1139 {
1140 public int Field1 { get; set; }
1141 public int Field2 { get; set; }
1142 public int Field3 { get; set; }
1143 }
1144 public class MapValueTypeChildDbo : MapValueTypeDbo
1145 {
1146 }
1147 public abstract class InheritanceMappingDbo
1148 {
1149 public int Field1 { get; set; }
1150 public int Field2 { get; set; }
1151 }
1152 public class InheritanceMappingDbo1 : InheritanceMappingDbo
1153 {
1154 }
1155 public class InheritanceMappingDbo2 : InheritanceMappingDbo
1156 {
1157 }
1158 public abstract class InheritanceMappingChDbo
1159 {
1160 public int Field1 { get; set; }
1161 public int Field2 { get; set; }
1162 }
1163 public class InheritanceMappingChDbo1 : InheritanceMappingChDbo
1164 {
1165 }
1166 public class InheritanceMappingChDbo2 : InheritanceMappingChDbo
1167 {
1168 }
1169 public class AssociationThis1Dbo
1170 {
1171 public int FieldThis1 { get; set; }
1172 public List<AssociationOtherDbo> FieldThis2 { get; set; }
1173 }
1174 public class AssociationThis2Dbo
1175 {
1176 public int FieldThis1 { get; set; }
1177 public AssociationOtherDbo FieldThis3 { get; set; }
1178 }
1179 public class AssociationThis2ChildDbo : AssociationThis2Dbo
1180 {
1181 }
1182 public class AssociationOtherDbo
1183 {
1184 public int FieldOther1 { get; set; }
1185 public int FieldOther2 { get; set; }
1186 public int FieldOther3 { get; set; }
1187 public string FieldOther4 { get; set; }
1188 }
1189 public class Parent
1190 {
1191 public int ID;
1192 public List<Child> Children = new List<Child>();
1193 }
1194 public class Child
1195 {
1196 public int ID;
1197 public Parent Parent = new Parent();
1198 public List<Grandchild> Grandchildren = new List<Grandchild>();
1199 }
1200 public class Grandchild
1201 {
1202 public int ID;
1203 public Child Child = new Child();
1204 }
1205 public class Parent2
1206 {
1207 public int ID;
1208 public List<Child2> Children = new List<Child2>();
1209 }
1210 public class Parent2Child : Parent2
1211 {
1212 }
1213 public class Child2
1214 {
1215 public int ID;
1216 public Parent2Child Parent = new Parent2Child();
1217 }
1218 #endregion
1219 }
1220 }