comparison UnitTests/Linq/EnumMapping.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 System.Linq.Expressions;
5 using BLToolkit.Data.DataProvider;
6 using BLToolkit.Data.Linq;
7 using BLToolkit.DataAccess;
8 using BLToolkit.Mapping;
9 using NUnit.Framework;
10
11 namespace Data.Linq
12 {
13 [TestFixture, Category("MapValue")]
14 public class EnumMapping : TestBase
15 {
16 enum TestEnum1
17 {
18 [MapValue(11L)] Value1 = 3,
19 [MapValue(12L)] Value2,
20 }
21 enum TestEnum2
22 {
23 Value1 = 3,
24 Value2,
25 }
26 enum TestEnum3
27 {
28 Value1 = 3,
29 Value2,
30 }
31
32 [TableName("LinqDataTypes")]
33 class TestTable1
34 {
35 [PrimaryKey, MapField("ID")] public int Id;
36 [MapField("BigIntValue")] public TestEnum1 TestField;
37 }
38
39 [MapValue(TestEnum2.Value2, 12L)]
40 [TableName("LinqDataTypes")]
41 class TestTable2
42 {
43 [PrimaryKey, MapField("ID")]
44 public int Id;
45
46 [MapValue(TestEnum2.Value1, 11L)]
47 [MapField("BigIntValue")]
48 public TestEnum2 TestField;
49
50 [MapField("IntValue")]
51 public TestEnum3 Int32Field;
52 }
53
54 [TableName("LinqDataTypes")]
55 class NullableTestTable1
56 {
57 [PrimaryKey, MapField("ID")]
58 public int? Id;
59
60 [MapField("BigIntValue")]
61 public TestEnum1? TestField;
62 }
63
64 [MapValue(TestEnum2.Value2, 12L)]
65 [TableName("LinqDataTypes")]
66 class NullableTestTable2
67 {
68 [PrimaryKey, MapField("ID")]
69 public int? Id;
70
71 [MapValue(TestEnum2.Value1, 11L)]
72 [MapField("BigIntValue")]
73 public TestEnum2? TestField;
74
75 [MapField("IntValue")]
76 public TestEnum3? Int32Field;
77 }
78
79 [TableName("LinqDataTypes")]
80 class RawTable
81 {
82 [PrimaryKey, MapField("ID")]
83 public int Id;
84
85 [MapField("BigIntValue")]
86 public long TestField;
87
88 [MapField("IntValue")]
89 public int Int32Field;
90 }
91
92 class Cleaner : IDisposable
93 {
94 readonly ITestDataContext _db;
95
96 public Cleaner(ITestDataContext db)
97 {
98 _db = db;
99 Clean();
100 }
101
102 private void Clean()
103 {
104 _db.GetTable<RawTable>().Where(r => r.Id == RID).Delete();
105 }
106
107 public void Dispose()
108 {
109 try
110 {
111 // rollback emulation for WCF
112 Clean();
113 }
114 catch (Exception)
115 {
116 }
117 }
118 }
119
120 const long VAL2 = 12;
121 const long VAL1 = 11;
122 const int RID = 101;
123
124 [Test]
125 public void EnumMapInsert1()
126 {
127 ForEachProvider(db =>
128 {
129 using (new Cleaner(db))
130 {
131 db.GetTable<TestTable1>().Insert(() => new TestTable1
132 {
133 Id = RID,
134 TestField = TestEnum1.Value2
135 });
136
137 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Count());
138 }
139 });
140 }
141
142 [Test]
143 public void EnumMapInsert2()
144 {
145 ForEachProvider(db =>
146 {
147 using (new Cleaner(db))
148 {
149 db.GetTable<TestTable2>().Insert(() => new TestTable2
150 {
151 Id = RID,
152 TestField = TestEnum2.Value2
153 });
154
155 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Count());
156 }
157 });
158 }
159
160 [Test]
161 public void EnumMapInsert3()
162 {
163 ForEachProvider(db =>
164 {
165 using (new Cleaner(db))
166 {
167 db.GetTable<NullableTestTable1>().Insert(() => new NullableTestTable1
168 {
169 Id = RID,
170 TestField = TestEnum1.Value2
171 });
172
173 Assert.AreEqual(1, db.GetTable<RawTable>()
174 .Where(r => r.Id == RID && r.TestField == VAL2).Count());
175 }
176 });
177 }
178
179 [Test]
180 public void EnumMapInsert4()
181 {
182 ForEachProvider(db =>
183 {
184 using (new Cleaner(db))
185 {
186 db.GetTable<NullableTestTable2>().Insert(() => new NullableTestTable2
187 {
188 Id = RID,
189 TestField = TestEnum2.Value2
190 });
191
192 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Count());
193 }
194 });
195 }
196
197 [Test]
198 public void EnumMapWhere1()
199 {
200 ForEachProvider(db =>
201 {
202 using (new Cleaner(db))
203 {
204 db.GetTable<RawTable>().Insert(() => new RawTable
205 {
206 Id = RID,
207 TestField = VAL2
208 });
209
210 var result = db.GetTable<TestTable1>().Where(r => r.Id == RID && r.TestField == TestEnum1.Value2).Select(r => r.TestField).FirstOrDefault();
211 Assert.True(result == TestEnum1.Value2);
212 }
213 });
214 }
215
216 [Test]
217 public void EnumMapWhere2()
218 {
219 ForEachProvider(db =>
220 {
221 using (new Cleaner(db))
222 {
223 db.GetTable<RawTable>().Insert(() => new RawTable
224 {
225 Id = RID,
226 TestField = VAL2
227 });
228
229 var result = db.GetTable<TestTable2>().Where(r => r.Id == RID && r.TestField == TestEnum2.Value2).Select(r => r.TestField).FirstOrDefault();
230 Assert.True(result == TestEnum2.Value2);
231 }
232 });
233 }
234
235 [Test]
236 public void EnumMapWhere3()
237 {
238 ForEachProvider(db =>
239 {
240 using (new Cleaner(db))
241 {
242 db.GetTable<RawTable>().Insert(() => new RawTable
243 {
244 Id = RID,
245 TestField = VAL2
246 });
247
248 var result = db.GetTable<NullableTestTable1>()
249 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value2)
250 .Select(r => r.TestField).FirstOrDefault();
251 Assert.True(result == TestEnum1.Value2);
252 }
253 });
254 }
255
256 [Test]
257 public void EnumMapWhere4()
258 {
259 ForEachProvider(db =>
260 {
261 using (new Cleaner(db))
262 {
263 db.GetTable<RawTable>().Insert(() => new RawTable
264 {
265 Id = RID,
266 TestField = VAL2
267 });
268
269 var result = db.GetTable<NullableTestTable2>()
270 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value2)
271 .Select(r => r.TestField).FirstOrDefault();
272 Assert.True(result == TestEnum2.Value2);
273 }
274 });
275 }
276
277 [Test]
278 public void EnumMapUpdate1()
279 {
280 ForEachProvider(db =>
281 {
282 using (new Cleaner(db))
283 {
284 db.GetTable<RawTable>().Insert(() => new RawTable
285 {
286 Id = RID,
287 TestField = VAL1
288 });
289
290 db.GetTable<TestTable1>()
291 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value1)
292 .Update(r => new TestTable1 { TestField = TestEnum1.Value2 });
293
294 var result = db.GetTable<RawTable>()
295 .Where(r => r.Id == RID && r.TestField == VAL2)
296 .Select(r => r.TestField)
297 .FirstOrDefault();
298
299 Assert.True(result == VAL2);
300 }
301 });
302 }
303
304 [Test]
305 public void EnumMapUpdate2()
306 {
307 ForEachProvider(db =>
308 {
309 using (new Cleaner(db))
310 {
311 db.GetTable<RawTable>().Insert(() => new RawTable
312 {
313 Id = RID,
314 TestField = VAL1
315 });
316
317 db.GetTable<TestTable2>()
318 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value1)
319 .Update(r => new TestTable2 { TestField = TestEnum2.Value2 });
320
321 var result = db.GetTable<RawTable>()
322 .Where(r => r.Id == RID && r.TestField == VAL2)
323 .Select(r => r.TestField)
324 .FirstOrDefault();
325
326 Assert.True(result == VAL2);
327 }
328 });
329 }
330
331 [Test]
332 public void EnumMapUpdate3()
333 {
334 ForEachProvider(db =>
335 {
336 using (new Cleaner(db))
337 {
338 db.GetTable<RawTable>().Insert(() => new RawTable
339 {
340 Id = RID,
341 TestField = VAL1
342 });
343
344 db.GetTable<NullableTestTable1>()
345 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value1)
346 .Update(r => new NullableTestTable1 { TestField = TestEnum1.Value2 });
347
348 var result = db.GetTable<RawTable>()
349 .Where(r => r.Id == RID && r.TestField == VAL2)
350 .Select(r => r.TestField)
351 .FirstOrDefault();
352
353 Assert.True(result == VAL2);
354 }
355 });
356 }
357
358 [Test]
359 public void EnumMapUpdate4()
360 {
361 ForEachProvider(db =>
362 {
363 using (new Cleaner(db))
364 {
365 db.GetTable<RawTable>().Insert(() => new RawTable
366 {
367 Id = RID,
368 TestField = VAL1
369 });
370
371 db.GetTable<NullableTestTable2>()
372 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value1)
373 .Update(r => new NullableTestTable2 { TestField = TestEnum2.Value2 });
374
375 var result = db.GetTable<RawTable>()
376 .Where(r => r.Id == RID && r.TestField == VAL2)
377 .Select(r => r.TestField)
378 .FirstOrDefault();
379
380 Assert.True(result == VAL2);
381 }
382 });
383 }
384
385 [Test]
386 public void EnumMapSelectAnon1([DataContexts] string context)
387 {
388 using (var db = GetDataContext(context))
389 {
390 using (new Cleaner(db))
391 {
392 db.GetTable<RawTable>().Insert(() => new RawTable
393 {
394 Id = RID,
395 TestField = VAL2
396 });
397
398 var result = db.GetTable<TestTable1>()
399 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value2)
400 .Select(r => new { r.TestField })
401 .FirstOrDefault();
402
403 Assert.NotNull(result);
404 Assert.That(result.TestField, Is.EqualTo(TestEnum1.Value2));
405 }
406 }
407 }
408
409 [Test]
410 public void EnumMapSelectAnon2()
411 {
412 ForEachProvider(db =>
413 {
414 using (new Cleaner(db))
415 {
416 db.GetTable<RawTable>().Insert(() => new RawTable
417 {
418 Id = RID,
419 TestField = VAL2
420 });
421
422 var result = db.GetTable<TestTable2>()
423 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value2)
424 .Select(r => new { r.TestField })
425 .FirstOrDefault();
426
427 Assert.NotNull(result);
428 Assert.True(result.TestField == TestEnum2.Value2);
429 }
430 });
431 }
432
433 [Test]
434 public void EnumMapSelectAnon3()
435 {
436 ForEachProvider(db =>
437 {
438 using (new Cleaner(db))
439 {
440 db.GetTable<RawTable>().Insert(() => new RawTable
441 {
442 Id = RID,
443 TestField = VAL2
444 });
445
446 var result = db.GetTable<NullableTestTable1>()
447 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value2)
448 .Select(r => new { r.TestField })
449 .FirstOrDefault();
450
451 Assert.NotNull(result);
452 Assert.True(result.TestField == TestEnum1.Value2);
453 }
454 });
455 }
456
457 [Test]
458 public void EnumMapSelectAnon4()
459 {
460 ForEachProvider(db =>
461 {
462 using (new Cleaner(db))
463 {
464 db.GetTable<RawTable>().Insert(() => new RawTable
465 {
466 Id = RID,
467 TestField = VAL2
468 });
469
470 var result = db.GetTable<NullableTestTable2>()
471 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value2)
472 .Select(r => new { r.TestField })
473 .FirstOrDefault();
474
475 Assert.NotNull(result);
476 Assert.True(result.TestField == TestEnum2.Value2);
477 }
478 });
479 }
480
481 [Test]
482 public void EnumMapDelete1()
483 {
484 ForEachProvider(db =>
485 {
486 using (new Cleaner(db))
487 {
488 db.GetTable<RawTable>().Insert(() => new RawTable
489 {
490 Id = RID,
491 TestField = VAL2
492 });
493
494 Assert.True(1 == db.GetTable<TestTable1>().Delete(r => r.Id == RID && r.TestField == TestEnum1.Value2));
495 }
496 });
497 }
498
499 [Test]
500 public void EnumMapDelete2()
501 {
502 ForEachProvider(db =>
503 {
504 using (new Cleaner(db))
505 {
506 db.GetTable<RawTable>().Insert(() => new RawTable
507 {
508 Id = RID,
509 TestField = VAL2
510 });
511
512 Assert.True(1 == db.GetTable<TestTable2>().Delete(r => r.Id == RID && r.TestField == TestEnum2.Value2));
513 }
514 });
515 }
516
517 [Test]
518 public void EnumMapDelete3()
519 {
520 ForEachProvider(db =>
521 {
522 using (new Cleaner(db))
523 {
524 db.GetTable<RawTable>().Insert(() => new RawTable
525 {
526 Id = RID,
527 TestField = VAL2
528 });
529
530 Assert.True(1 == db.GetTable<NullableTestTable1>()
531 .Delete(r => r.Id == RID && r.TestField == TestEnum1.Value2));
532 }
533 });
534 }
535
536 [Test]
537 public void EnumMapDelete4()
538 {
539 ForEachProvider(db =>
540 {
541 using (new Cleaner(db))
542 {
543 db.GetTable<RawTable>().Insert(() => new RawTable
544 {
545 Id = RID,
546 TestField = VAL2
547 });
548
549 Assert.True(1 == db.GetTable<NullableTestTable2>()
550 .Delete(r => r.Id == RID && r.TestField == TestEnum2.Value2));
551 }
552 });
553 }
554
555 [Test]
556 public void EnumMapSet1()
557 {
558 ForEachProvider(db =>
559 {
560 using (new Cleaner(db))
561 {
562 db.GetTable<RawTable>().Insert(() => new RawTable
563 {
564 Id = RID,
565 TestField = VAL1
566 });
567
568 db.GetTable<TestTable1>()
569 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value1)
570 .Set(r => r.TestField, TestEnum1.Value2).Update();
571 var result = db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Select(r => r.TestField).FirstOrDefault();
572 Assert.True(result == VAL2);
573 }
574 });
575 }
576
577 [Test]
578 public void EnumMapSet2()
579 {
580 ForEachProvider(db =>
581 {
582 using (new Cleaner(db))
583 {
584 db.GetTable<RawTable>().Insert(() => new RawTable
585 {
586 Id = RID,
587 TestField = VAL1
588 });
589
590 db.GetTable<TestTable2>()
591 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value1)
592 .Set(r => r.TestField, TestEnum2.Value2).Update();
593 var result = db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Select(r => r.TestField).FirstOrDefault();
594 Assert.True(result == VAL2);
595 }
596 });
597 }
598
599 [Test]
600 public void EnumMapSet3()
601 {
602 ForEachProvider(db =>
603 {
604 using (new Cleaner(db))
605 {
606 db.GetTable<RawTable>().Insert(() => new RawTable
607 {
608 Id = RID,
609 Int32Field = 3
610 });
611
612 db.GetTable<TestTable2>()
613 .Where(r => r.Id == RID && r.Int32Field == TestEnum3.Value1)
614 .Set(r => r.Int32Field, () => TestEnum3.Value2).Update();
615 Assert.True(1 == db.GetTable<RawTable>().Where(r => r.Id == RID && r.Int32Field == 4).Count());
616 }
617 });
618 }
619
620 [Test]
621 public void EnumMapSet4()
622 {
623 ForEachProvider(db =>
624 {
625 using (new Cleaner(db))
626 {
627 db.GetTable<RawTable>().Insert(() => new RawTable
628 {
629 Id = RID,
630 TestField = VAL1
631 });
632
633 db.GetTable<NullableTestTable1>()
634 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value1)
635 .Set(r => r.TestField, TestEnum1.Value2).Update();
636 var result = db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Select(r => r.TestField).FirstOrDefault();
637 Assert.True(result == VAL2);
638 }
639 });
640 }
641
642 [Test]
643 public void EnumMapSet5()
644 {
645 ForEachProvider(db =>
646 {
647 using (new Cleaner(db))
648 {
649 db.GetTable<RawTable>().Insert(() => new RawTable
650 {
651 Id = RID,
652 TestField = VAL1
653 });
654
655 db.GetTable<NullableTestTable2>()
656 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value1)
657 .Set(r => r.TestField, TestEnum2.Value2).Update();
658 var result = db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Select(r => r.TestField).FirstOrDefault();
659 Assert.True(result == VAL2);
660 }
661 });
662 }
663
664 [Test]
665 public void EnumMapSet6()
666 {
667 ForEachProvider(db =>
668 {
669 using (new Cleaner(db))
670 {
671 db.GetTable<RawTable>().Insert(() => new RawTable
672 {
673 Id = RID,
674 Int32Field = 3
675 });
676
677 db.GetTable<NullableTestTable2>()
678 .Where(r => r.Id == RID && r.Int32Field == TestEnum3.Value1)
679 .Set(r => r.Int32Field, () => TestEnum3.Value2).Update();
680 Assert.True(1 == db.GetTable<RawTable>().Where(r => r.Id == RID && r.Int32Field == 4).Count());
681 }
682 });
683 }
684
685 [Test]
686 public void EnumMapContains1([DataContexts] string context)
687 {
688 using (var db = GetDataContext(context))
689 {
690 using (new Cleaner(db))
691 {
692 db.GetTable<RawTable>().Insert(() => new RawTable
693 {
694 Id = RID,
695 TestField = VAL2
696 });
697
698 Assert.True(1 == db.GetTable<TestTable1>()
699 .Where(r => r.Id == RID && new[] { TestEnum1.Value2 }.Contains(r.TestField)).Count());
700 }
701 }
702 }
703
704 [Test]
705 public void EnumMapContains2()
706 {
707 ForEachProvider(db =>
708 {
709 using (new Cleaner(db))
710 {
711 db.GetTable<RawTable>().Insert(() => new RawTable
712 {
713 Id = RID,
714 TestField = VAL2
715 });
716
717 Assert.That(db.GetTable<TestTable2>().Where(r => r.Id == RID && new[] { TestEnum2.Value2 }.Contains(r.TestField)).Count(), Is.EqualTo(1));
718 }
719 });
720 }
721
722 [Test]
723 public void EnumMapContains3()
724 {
725 ForEachProvider(db =>
726 {
727 using (new Cleaner(db))
728 {
729 db.GetTable<RawTable>().Insert(() => new RawTable
730 {
731 Id = RID,
732 TestField = VAL2
733 });
734
735 Assert.True(1 == db.GetTable<NullableTestTable1>()
736 .Where(r => r.Id == RID && new[] { (TestEnum1?)TestEnum1.Value2 }.Contains(r.TestField)).Count());
737 }
738 });
739 }
740
741 [Test]
742 public void EnumMapContains4()
743 {
744 ForEachProvider(db =>
745 {
746 using (new Cleaner(db))
747 {
748 db.GetTable<RawTable>().Insert(() => new RawTable
749 {
750 Id = RID,
751 TestField = VAL2
752 });
753
754 Assert.True(1 == db.GetTable<NullableTestTable2>()
755 .Where(r => r.Id == RID && new[] { (TestEnum2?)TestEnum2.Value2 }.Contains(r.TestField)).Count());
756 }
757 });
758 }
759
760 [Test]
761 public void EnumMapSelectNull1()
762 {
763 ForEachProvider(db =>
764 {
765 using (new Cleaner(db))
766 {
767 db.GetTable<RawTable>().Insert(() => new RawTable
768 {
769 Id = RID
770 });
771
772 var result = db.GetTable<NullableTestTable1>()
773 .Where(r => r.Id == RID)
774 .Select(r => new { r.TestField })
775 .FirstOrDefault();
776
777 Assert.NotNull(result);
778 Assert.True(result.TestField == null);
779 }
780 });
781 }
782
783 [Test]
784 public void EnumMapSelectNull2()
785 {
786 ForEachProvider(db =>
787 {
788 using (new Cleaner(db))
789 {
790 db.GetTable<RawTable>().Insert(() => new RawTable
791 {
792 Id = RID
793 });
794
795 var result = db.GetTable<NullableTestTable2>()
796 .Where(r => r.Id == RID)
797 .Select(r => new { r.TestField })
798 .FirstOrDefault();
799
800 Assert.NotNull(result);
801 Assert.True(result.TestField == null);
802 }
803 });
804 }
805
806 [Test]
807 public void EnumMapWhereNull1()
808 {
809 ForEachProvider(db =>
810 {
811 using (new Cleaner(db))
812 {
813 db.GetTable<RawTable>().Insert(() => new RawTable
814 {
815 Id = RID
816 });
817
818 var result = db.GetTable<NullableTestTable1>()
819 .Where(r => r.Id == RID && r.TestField == null)
820 .Select(r => new { r.TestField }).FirstOrDefault();
821 Assert.NotNull(result);
822 Assert.Null(result.TestField);
823 }
824 });
825 }
826
827 [Test]
828 public void EnumMapWhereNull2()
829 {
830 ForEachProvider(db =>
831 {
832 using (new Cleaner(db))
833 {
834 db.GetTable<RawTable>().Insert(() => new RawTable
835 {
836 Id = RID
837 });
838
839 var result = db.GetTable<NullableTestTable2>()
840 .Where(r => r.Id == RID && r.TestField == null)
841 .Select(r => new { r.TestField }).FirstOrDefault();
842 Assert.NotNull(result);
843 Assert.Null(result.TestField);
844 }
845 });
846 }
847
848 [Test]
849 public void EnumMapInsertObject1()
850 {
851 ForEachProvider(db =>
852 {
853 using (new Cleaner(db))
854 {
855 db.Insert(new TestTable1
856 {
857 Id = RID,
858 TestField = TestEnum1.Value2
859 });
860
861 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Count());
862 }
863 });
864 }
865
866 [Test]
867 public void EnumMapInsertObject2()
868 {
869 ForEachProvider(db =>
870 {
871 using (new Cleaner(db))
872 {
873 db.Insert(new TestTable2
874 {
875 Id = RID,
876 TestField = TestEnum2.Value2
877 });
878
879 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Count());
880 }
881 });
882 }
883
884 [Test]
885 public void EnumMapInsertObject3()
886 {
887 ForEachProvider(db =>
888 {
889 using (new Cleaner(db))
890 {
891 db.Insert(new NullableTestTable1
892 {
893 Id = RID,
894 TestField = TestEnum1.Value2
895 });
896
897 Assert.AreEqual(1, db.GetTable<RawTable>()
898 .Where(r => r.Id == RID && r.TestField == VAL2).Count());
899 }
900 });
901 }
902
903 [Test]
904 public void EnumMapInsertObject4()
905 {
906 ForEachProvider(db =>
907 {
908 using (new Cleaner(db))
909 {
910 db.Insert(new NullableTestTable2
911 {
912 Id = RID,
913 TestField = TestEnum2.Value2
914 });
915
916 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL2).Count());
917 }
918 });
919 }
920
921 [Test]
922 public void EnumMapInsertFromSelectWithParam1()
923 {
924 ForEachProvider(db =>
925 {
926 using (new Cleaner(db))
927 {
928 db.GetTable<RawTable>().Insert(() => new RawTable
929 {
930 Id = RID,
931 TestField = VAL2
932 });
933
934 var param = TestEnum1.Value1;
935
936 var result = db.GetTable<TestTable1>()
937 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value2)
938 .Select(r => new TestTable1
939 {
940 Id = r.Id,
941 TestField = param
942 })
943 .Insert(db.GetTable<TestTable1>(), r => r);
944
945 Assert.AreEqual(1, result);
946 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL1).Count());
947 }
948 });
949 }
950
951 [Test]
952 public void EnumMapInsertFromSelectWithParam2()
953 {
954 ForEachProvider(db =>
955 {
956 using (new Cleaner(db))
957 {
958 db.GetTable<RawTable>().Insert(() => new RawTable
959 {
960 Id = RID,
961 TestField = VAL2
962 });
963
964 var param = TestEnum2.Value1;
965
966 var result = db.GetTable<TestTable2>()
967 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value2)
968 .Select(r => new TestTable2
969 {
970 Id = r.Id,
971 TestField = param
972 })
973 .Insert(db.GetTable<TestTable2>(), r => r);
974
975 Assert.AreEqual(1, result);
976 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL1).Count());
977 }
978 });
979 }
980
981 [Test]
982 public void EnumMapInsertFromSelectWithParam3()
983 {
984 ForEachProvider(db =>
985 {
986 using (new Cleaner(db))
987 {
988 db.GetTable<RawTable>().Insert(() => new RawTable
989 {
990 Id = RID,
991 TestField = VAL2
992 });
993
994 var param = TestEnum1.Value1;
995
996 var result = db.GetTable<NullableTestTable1>()
997 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value2)
998 .Select(r => new NullableTestTable1
999 {
1000 Id = r.Id,
1001 TestField = param
1002 })
1003 .Insert(db.GetTable<NullableTestTable1>(), r => r);
1004
1005 Assert.AreEqual(1, result);
1006 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL1).Count());
1007 }
1008 });
1009 }
1010
1011 [Test]
1012 public void EnumMapInsertFromSelectWithParam4()
1013 {
1014 ForEachProvider(db =>
1015 {
1016 using (new Cleaner(db))
1017 {
1018 db.GetTable<RawTable>().Insert(() => new RawTable
1019 {
1020 Id = RID,
1021 TestField = VAL2
1022 });
1023
1024 var param = TestEnum2.Value1;
1025
1026 var result = db.GetTable<NullableTestTable2>()
1027 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value2)
1028 .Select(r => new NullableTestTable2
1029 {
1030 Id = r.Id,
1031 TestField = param
1032 })
1033 .Insert(db.GetTable<NullableTestTable2>(), r => r);
1034
1035 Assert.AreEqual(1, result);
1036 Assert.AreEqual(1, db.GetTable<RawTable>().Where(r => r.Id == RID && r.TestField == VAL1).Count());
1037 }
1038 });
1039 }
1040
1041 [Test]
1042 public void EnumMapDeleteEquals1()
1043 {
1044 ForEachProvider(db =>
1045 {
1046 using (new Cleaner(db))
1047 {
1048 db.GetTable<RawTable>().Insert(() => new RawTable
1049 {
1050 Id = RID,
1051 TestField = VAL2
1052 });
1053
1054 Assert.True(1 == db.GetTable<TestTable1>().Delete(r => r.Id == RID && r.TestField.Equals(TestEnum1.Value2)));
1055 }
1056 });
1057 }
1058
1059 [Test]
1060 public void EnumMapDeleteEquals2()
1061 {
1062 ForEachProvider(db =>
1063 {
1064 using (new Cleaner(db))
1065 {
1066 db.GetTable<RawTable>().Insert(() => new RawTable
1067 {
1068 Id = RID,
1069 TestField = VAL2
1070 });
1071
1072 Assert.True(1 == db.GetTable<TestTable2>().Delete(r => r.Id == RID && r.TestField.Equals(TestEnum2.Value2)));
1073 }
1074 });
1075 }
1076
1077 [Test]
1078 public void EnumMapDeleteEquals3()
1079 {
1080 ForEachProvider(db =>
1081 {
1082 using (new Cleaner(db))
1083 {
1084 db.GetTable<RawTable>().Insert(() => new RawTable
1085 {
1086 Id = RID,
1087 TestField = VAL2
1088 });
1089
1090 Assert.True(1 == db.GetTable<NullableTestTable1>()
1091 .Delete(r => r.Id == RID && r.TestField.Equals(TestEnum1.Value2)));
1092 }
1093 });
1094 }
1095
1096 [Test]
1097 public void EnumMapDeleteEquals4()
1098 {
1099 ForEachProvider(db =>
1100 {
1101 using (new Cleaner(db))
1102 {
1103 db.GetTable<RawTable>().Insert(() => new RawTable
1104 {
1105 Id = RID,
1106 TestField = VAL2
1107 });
1108
1109 Assert.True(1 == db.GetTable<NullableTestTable2>()
1110 .Delete(r => r.Id == RID && r.TestField.Equals(TestEnum2.Value2)));
1111 }
1112 });
1113 }
1114
1115 [Test]
1116 public void EnumMapCustomPredicate1([DataContexts] string context)
1117 {
1118 using (var db = GetDataContext(context))
1119 {
1120 using (new Cleaner(db))
1121 {
1122 db.GetTable<RawTable>().Insert(() => new RawTable
1123 {
1124 Id = RID,
1125 TestField = VAL2
1126 });
1127
1128 var entityParameter = Expression.Parameter(typeof(TestTable1), "entity"); // parameter name required for BLToolkit
1129 var filterExpression = Expression.Equal(Expression.Field(entityParameter, "TestField"), Expression.Constant(TestEnum1.Value2));
1130 var filterPredicate = Expression.Lambda<Func<TestTable1, bool>>(filterExpression, entityParameter);
1131 var result = db.GetTable<TestTable1>().Where(filterPredicate).ToList();
1132
1133 Assert.AreEqual(1, result.Count);
1134 }
1135 }
1136 }
1137
1138 [Test]
1139 public void EnumMapCustomPredicate2([DataContexts] string context)
1140 {
1141 using (var db = GetDataContext(context))
1142 {
1143 using (new Cleaner(db))
1144 {
1145 db.GetTable<RawTable>().Insert(() => new RawTable
1146 {
1147 Id = RID,
1148 TestField = VAL2
1149 });
1150
1151 var entityParameter = Expression.Parameter(typeof(TestTable2), "entity"); // parameter name required for BLToolkit
1152 var filterExpression = Expression.Equal(Expression.Field(entityParameter, "TestField"), Expression.Constant(TestEnum2.Value2));
1153 var filterPredicate = Expression.Lambda<Func<TestTable2, bool>>(filterExpression, entityParameter);
1154 var result = db.GetTable<TestTable2>().Where(filterPredicate).ToList();
1155
1156 Assert.AreEqual(1, result.Count);
1157 }
1158 }
1159 }
1160
1161 [TableName("LinqDataTypes")]
1162 class TestTable3
1163 {
1164 [PrimaryKey]
1165 public int ID;
1166
1167 [MapField("BigIntValue")]
1168 public TestEnum1? TargetType;
1169
1170 [MapField("IntValue")]
1171 public int? TargetID;
1172 }
1173
1174 struct ObjectReference
1175 {
1176 public TestEnum1 TargetType;
1177 public int TargetID;
1178 public ObjectReference(TestEnum1 targetType, int tagetId)
1179 {
1180 TargetType = targetType;
1181 TargetID = tagetId;
1182 }
1183 }
1184
1185 [Test]
1186 public void Test_4_1_18_Regression1()
1187 {
1188 ForEachProvider(db =>
1189 {
1190 using (new Cleaner(db))
1191 {
1192 db.GetTable<RawTable>().Insert(() => new RawTable()
1193 {
1194 Id = RID,
1195 TestField = VAL2,
1196 Int32Field = 10
1197 });
1198
1199 var result = db.GetTable<TestTable3>().Where(r => r.ID == RID).Select(_ => new
1200 {
1201 Target = _.TargetType != null && _.TargetID != null
1202 ? new ObjectReference(_.TargetType.Value, _.TargetID.Value)
1203 : default(ObjectReference?)
1204 })
1205 .ToArray();
1206
1207 Assert.AreEqual(1, result.Length);
1208 Assert.NotNull(result[0].Target);
1209 Assert.AreEqual(10, result[0].Target.Value.TargetID);
1210 Assert.AreEqual(TestEnum1.Value2, result[0].Target.Value.TargetType);
1211 }
1212 });
1213 }
1214
1215 [TableName("LinqDataTypes")]
1216 class TestTable4
1217 {
1218 [PrimaryKey]
1219 public int ID;
1220
1221 [MapField("BigIntValue")]
1222 public TestEnum2? TargetType;
1223
1224 [MapField("IntValue")]
1225 public int? TargetID;
1226 }
1227
1228 struct ObjectReference2
1229 {
1230 public TestEnum2 TargetType;
1231 public int TargetID;
1232 public ObjectReference2(TestEnum2 targetType, int tagetId)
1233 {
1234 TargetType = targetType;
1235 TargetID = tagetId;
1236 }
1237 }
1238
1239 [Test]
1240 public void Test_4_1_18_Regression2()
1241 {
1242 ForEachProvider(db =>
1243 {
1244 using (new Cleaner(db))
1245 {
1246 db.GetTable<RawTable>().Insert(() => new RawTable()
1247 {
1248 Id = RID,
1249 TestField = (long)TestEnum2.Value2,
1250 Int32Field = 10
1251 });
1252
1253 var result = db.GetTable<TestTable4>().Where(r => r.ID == RID).Select(_ => new
1254 {
1255 Target = _.TargetType != null && _.TargetID != null
1256 ? new ObjectReference2(_.TargetType.Value, _.TargetID.Value)
1257 : default(ObjectReference2?)
1258 })
1259 .ToArray();
1260
1261 Assert.AreEqual(1, result.Length);
1262 Assert.NotNull(result[0].Target);
1263 Assert.AreEqual(10, result[0].Target.Value.TargetID);
1264 Assert.AreEqual(TestEnum2.Value2, result[0].Target.Value.TargetType);
1265 }
1266 });
1267 }
1268
1269 class NullableResult
1270 {
1271 public TestEnum1? Value;
1272 }
1273
1274 [Test]
1275 public void EnumMapSelectNull([DataContexts] string context)
1276 {
1277 using (var db = GetDataContext(context))
1278 {
1279 using (new Cleaner(db))
1280 {
1281 db.GetTable<RawTable>().Insert(() => new RawTable
1282 {
1283 Id = RID
1284 });
1285
1286 var result = db.GetTable<TestTable1>()
1287 .Where(r => r.Id == RID)
1288 .Select(r => new NullableResult {Value = r.TestField })
1289 .FirstOrDefault();
1290
1291 Assert.NotNull(result);
1292 Assert.Null(result.Value);
1293 }
1294 }
1295 }
1296
1297 private TestEnum1 Convert(TestEnum1 val)
1298 {
1299 return val;
1300 }
1301
1302 [Test]
1303 public void EnumMapSelectNull_Regression([DataContexts] string context)
1304 {
1305 using (var db = GetDataContext(context))
1306 {
1307 using (new Cleaner(db))
1308 {
1309 db.GetTable<RawTable>().Insert(() => new RawTable
1310 {
1311 Id = RID,
1312 TestField = VAL2
1313 });
1314
1315 var result = db.GetTable<TestTable1>()
1316 .Where(r => r.Id == RID)
1317 .Select(r => new NullableResult { Value = Convert(r.TestField) })
1318 .FirstOrDefault();
1319
1320 Assert.NotNull(result);
1321 Assert.That(result.Value, Is.EqualTo(TestEnum1.Value2));
1322 }
1323 }
1324 }
1325
1326 [Flags]
1327 enum TestFlag
1328 {
1329 Value1 = 0x1,
1330 Value2 = 0x2
1331 }
1332
1333 [TableName("LinqDataTypes")]
1334 class TestTable5
1335 {
1336 public int ID;
1337 public TestFlag IntValue;
1338 }
1339
1340 [Test]
1341 public void TestFlagEnum([DataContexts(ProviderName.Access)] string context)
1342 {
1343 using (var db = GetDataContext(context))
1344 {
1345 var result =
1346 from t in db.GetTable<TestTable5>()
1347 where (t.IntValue & TestFlag.Value1) != 0
1348 select t;
1349
1350 var sql = result.ToString();
1351
1352 Assert.That(sql, Is.Not.Contains("Convert"));
1353 }
1354 }
1355
1356 [Test]
1357 public void EnumMapContainsList1()
1358 {
1359 ForEachProvider(db =>
1360 {
1361 using (new Cleaner(db))
1362 {
1363 db.GetTable<RawTable>().Insert(() => new RawTable
1364 {
1365 Id = RID,
1366 TestField = VAL2
1367 });
1368
1369 var set = new HashSet<TestEnum1>();
1370 set.Add(TestEnum1.Value2);
1371
1372 Assert.That(db.GetTable<TestTable1>()
1373 .Where(r => r.Id == RID && set.Contains(r.TestField)).Count(), Is.EqualTo(1));
1374 Assert.That(db.GetTable<TestTable1>()
1375 .Where(r => r.Id == RID && !set.Contains(r.TestField)).Count(), Is.EqualTo(0));
1376 }
1377 });
1378 }
1379
1380 [Test]
1381 public void EnumMapContainsList2()
1382 {
1383 ForEachProvider(db =>
1384 {
1385 using (new Cleaner(db))
1386 {
1387 db.GetTable<RawTable>().Insert(() => new RawTable
1388 {
1389 Id = RID,
1390 TestField = VAL2
1391 });
1392
1393 var set = new HashSet<TestEnum2>();
1394 set.Add(TestEnum2.Value2);
1395
1396 Assert.That(db.GetTable<TestTable2>().Where(r => r.Id == RID && set.Contains(r.TestField)).Count(), Is.EqualTo(1));
1397 Assert.That(db.GetTable<TestTable2>().Where(r => r.Id == RID && !set.Contains(r.TestField)).Count(), Is.EqualTo(0));
1398 }
1399 });
1400 }
1401
1402 [Test]
1403 public void EnumMapContainsList3()
1404 {
1405 ForEachProvider(db =>
1406 {
1407 using (new Cleaner(db))
1408 {
1409 db.GetTable<RawTable>().Insert(() => new RawTable
1410 {
1411 Id = RID,
1412 TestField = VAL2
1413 });
1414
1415 var set = new HashSet<TestEnum1?>();
1416 set.Add(TestEnum1.Value2);
1417
1418 Assert.That(db.GetTable<NullableTestTable1>()
1419 .Where(r => r.Id == RID && set.Contains(r.TestField)).Count(), Is.EqualTo(1));
1420 Assert.That(db.GetTable<NullableTestTable1>()
1421 .Where(r => r.Id == RID && !set.Contains(r.TestField)).Count(), Is.EqualTo(0));
1422 }
1423 });
1424 }
1425
1426 [Test]
1427 public void EnumMapContainsList4()
1428 {
1429 ForEachProvider(db =>
1430 {
1431 using (new Cleaner(db))
1432 {
1433 db.GetTable<RawTable>().Insert(() => new RawTable
1434 {
1435 Id = RID,
1436 TestField = VAL2
1437 });
1438
1439 var set = new HashSet<TestEnum2?>();
1440 set.Add(TestEnum2.Value2);
1441
1442 Assert.That(db.GetTable<NullableTestTable2>()
1443 .Where(r => r.Id == RID && set.Contains(r.TestField)).Count(), Is.EqualTo(1));
1444 Assert.That(db.GetTable<NullableTestTable2>()
1445 .Where(r => r.Id == RID && !set.Contains(r.TestField)).Count(), Is.EqualTo(0));
1446 }
1447 });
1448 }
1449
1450 [Test]
1451 public void EnumMapIntermediateObject1()
1452 {
1453 ForEachProvider(db =>
1454 {
1455 using (new Cleaner(db))
1456 {
1457 db.GetTable<RawTable>().Insert(() => new RawTable
1458 {
1459 Id = RID,
1460 TestField = VAL2
1461 });
1462
1463 Assert.That(
1464 db.GetTable<TestTable1>()
1465 .Select(r => new {r.Id, r.TestField})
1466 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value2).Count(), Is.EqualTo(1));
1467 }
1468 });
1469 }
1470
1471 //////[Test]
1472 public void EnumMapIntermediateObject2()
1473 {
1474 ForEachProvider(db =>
1475 {
1476 using (new Cleaner(db))
1477 {
1478 db.GetTable<RawTable>().Insert(() => new RawTable
1479 {
1480 Id = RID,
1481 TestField = VAL2
1482 });
1483
1484 Assert.That(
1485 db.GetTable<TestTable2>()
1486 .Select(r => new { r.Id, r.TestField })
1487 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value2).Count(), Is.EqualTo(1));
1488 }
1489 });
1490 }
1491
1492 [Test]
1493 public void EnumMapIntermediateObject3()
1494 {
1495 ForEachProvider(db =>
1496 {
1497 using (new Cleaner(db))
1498 {
1499 db.GetTable<RawTable>().Insert(() => new RawTable
1500 {
1501 Id = RID,
1502 TestField = VAL2
1503 });
1504
1505 Assert.That(
1506 db.GetTable<NullableTestTable1>()
1507 .Select(r => new { r.Id, r.TestField })
1508 .Where(r => r.Id == RID && r.TestField == TestEnum1.Value2).Count(), Is.EqualTo(1));
1509 }
1510 });
1511 }
1512
1513 //////[Test]
1514 public void EnumMapIntermediateObject4()
1515 {
1516 ForEachProvider(db =>
1517 {
1518 using (new Cleaner(db))
1519 {
1520 db.GetTable<RawTable>().Insert(() => new RawTable
1521 {
1522 Id = RID,
1523 TestField = VAL2
1524 });
1525
1526 Assert.That(
1527 db.GetTable<NullableTestTable2>()
1528 .Select(r => new { r.Id, r.TestField })
1529 .Where(r => r.Id == RID && r.TestField == TestEnum2.Value2).Count(), Is.EqualTo(1));
1530 }
1531 });
1532 }
1533 }
1534 }