comparison UnitTests/Linq/Types.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.Data.Linq;
4 using System.Globalization;
5 using System.Linq;
6 using System.Linq.Expressions;
7 using System.Threading;
8
9 using BLToolkit.Data.DataProvider;
10 using BLToolkit.Data.Linq;
11 using BLToolkit.DataAccess;
12 using BLToolkit.Mapping;
13
14 using NUnit.Framework;
15
16 namespace Data.Linq
17 {
18 using Model;
19
20 [TestFixture]
21 public class Types : TestBase
22 {
23 [Test]
24 public void Bool1()
25 {
26 var value = true;
27
28 ForEachProvider(db => AreEqual(
29 from p in Parent where p.ParentID > 2 && value && true && !false select p,
30 from p in db.Parent where p.ParentID > 2 && value && true && !false select p));
31 }
32
33 [Test]
34 public void Bool2()
35 {
36 var value = true;
37
38 ForEachProvider(db => AreEqual(
39 from p in Parent where p.ParentID > 2 && value || true && !false select p,
40 from p in db.Parent where p.ParentID > 2 && value || true && !false select p));
41 }
42
43 [Test]
44 public void Bool3()
45 {
46 var values = new int[0];
47
48 ForEachProvider(db => AreEqual(
49 from p in Parent where values.Contains(p.ParentID) && !false || p.ParentID > 2 select p,
50 from p in db.Parent where values.Contains(p.ParentID) && !false || p.ParentID > 2 select p));
51 }
52
53 [Test]
54 public void BoolField1()
55 {
56 ForEachProvider(db => AreEqual(
57 from t in Types where t.BoolValue select t.MoneyValue,
58 from t in db.Types where t.BoolValue select t.MoneyValue));
59 }
60
61 [Test]
62 public void BoolField2()
63 {
64 ForEachProvider(db => AreEqual(
65 from t in Types where !t.BoolValue select t.MoneyValue,
66 from t in db.Types where !t.BoolValue select t.MoneyValue));
67 }
68
69 [Test]
70 public void BoolField3()
71 {
72 ForEachProvider(db => AreEqual(
73 from t in Types where t.BoolValue == true select t.MoneyValue,
74 from t in db.Types where t.BoolValue == true select t.MoneyValue));
75 }
76
77 [Test]
78 public void BoolField4()
79 {
80 ForEachProvider(db => AreEqual(
81 from t in Types where t.BoolValue == false select t.MoneyValue,
82 from t in db.Types where t.BoolValue == false select t.MoneyValue));
83 }
84
85 [Test]
86 public void BoolField5()
87 {
88 ForEachProvider(db => AreEqual(
89 from p in from t in Types select new { t.MoneyValue, b = !t.BoolValue } where p.b == false select p.MoneyValue,
90 from p in from t in db.Types select new { t.MoneyValue, b = !t.BoolValue } where p.b == false select p.MoneyValue));
91 }
92
93 [Test]
94 public void BoolField6()
95 {
96 ForEachProvider(db => AreEqual(
97 from p in from t in Types select new { t.MoneyValue, b = !t.BoolValue } where p.b select p.MoneyValue,
98 from p in from t in db.Types select new { t.MoneyValue, b = !t.BoolValue } where p.b select p.MoneyValue));
99 }
100
101 [Test]
102 public void BoolResult1()
103 {
104 ForEachProvider(db => AreEqual(
105 from p in Person select new { p.Patient, IsPatient = p.Patient != null },
106 from p in db.Person select new { p.Patient, IsPatient = p.Patient != null }));
107 }
108
109 [Test]
110 public void BoolResult2()
111 {
112 ForEachProvider(db => AreEqual(
113 from p in Person select new { IsPatient = Sql.AsSql(p.Patient != null) },
114 from p in db.Person select new { IsPatient = Sql.AsSql(p.Patient != null) }));
115 }
116
117 [Test]
118 public void BoolResult3()
119 {
120 ForEachProvider(db => AreEqual(
121 from p in Person select Sql.AsSql(p.ID == 1),
122 from p in db.Person select Sql.AsSql(p.ID == 1)));
123 }
124
125 [Test]
126 public void GuidNew()
127 {
128 ForEachProvider(db => AreEqual(
129 from p in Types where p.GuidValue != Guid.NewGuid() select p.GuidValue,
130 from p in db.Types where p.GuidValue != Guid.NewGuid() select p.GuidValue));
131 }
132
133 [Test]
134 public void Guid1()
135 {
136 ForEachProvider(db => AreEqual(
137 from p in Types where p.GuidValue == new Guid("D2F970C0-35AC-4987-9CD5-5BADB1757436") select p.GuidValue,
138 from p in db.Types where p.GuidValue == new Guid("D2F970C0-35AC-4987-9CD5-5BADB1757436") select p.GuidValue));
139 }
140
141 [Test]
142 public void Guid2()
143 {
144 var guid3 = new Guid("D2F970C0-35AC-4987-9CD5-5BADB1757436");
145 var guid4 = new Guid("40932fdb-1543-4e4a-ac2c-ca371604fb4b");
146
147 var parm = Expression.Parameter(typeof(LinqDataTypes), "p");
148
149 ForEachProvider(db =>
150 Assert.AreNotEqual(
151 db.Types
152 .Where(
153 Expression.Lambda<Func<LinqDataTypes,bool>>(
154 Expression.Equal(
155 Expression.PropertyOrField(parm, "GuidValue"),
156 Expression.Constant(guid3),
157 false,
158 typeof(Guid).GetMethod("op_Equality")),
159 new[] { parm }))
160 .Single().GuidValue,
161 db.Types
162 .Where(
163 Expression.Lambda<Func<LinqDataTypes,bool>>(
164 Expression.Equal(
165 Expression.PropertyOrField(parm, "GuidValue"),
166 Expression.Constant(guid4),
167 false,
168 typeof(Guid).GetMethod("op_Equality")),
169 new[] { parm }))
170 .Single().GuidValue)
171 );
172 }
173
174 [Test]
175 public void ContainsGuid()
176 {
177 var ids = new [] { new Guid("D2F970C0-35AC-4987-9CD5-5BADB1757436") };
178
179 ForEachProvider(db => AreEqual(
180 from p in Types where ids.Contains(p.GuidValue) select p.GuidValue,
181 from p in db.Types where ids.Contains(p.GuidValue) select p.GuidValue));
182 }
183
184 [Test]
185 public void NewGuid()
186 {
187 ForEachProvider(
188 new[] { ProviderName.DB2, ProviderName.Informix, ProviderName.Firebird, ProviderName.PostgreSQL, ProviderName.SQLite, ProviderName.Access },
189 db =>
190 {
191 db.Types.Delete(_ => _.ID > 1000);
192 db.Types.Insert(() => new LinqDataTypes
193 {
194 ID = 1001,
195 MoneyValue = 1001,
196 DateTimeValue = Sql.CurrentTimestamp,
197 BoolValue = true,
198 GuidValue = Sql.NewGuid(),
199 BinaryValue = new Binary(new byte[] { 1 }),
200 SmallIntValue = 1001
201 });
202
203 var guid = db.Types.Single(_ => _.ID == 1001).GuidValue;
204
205 Assert.AreEqual(1001, db.Types.Single(_ => _.GuidValue == guid).ID);
206
207 db.Types.Delete(_ => _.ID > 1000);
208 });
209 }
210
211 [Test]
212 public void BinaryLength()
213 {
214 ForEachProvider(
215 new[] { ProviderName.Access },
216 db =>
217 {
218 db.Types
219 .Where(t => t.ID == 1)
220 .Set(t => t.BinaryValue, new Binary(new byte[] { 1, 2, 3, 4, 5 }))
221 .Update();
222
223 Assert.That(
224 (from t in db.Types where t.ID == 1 select t.BinaryValue.Length).First(),
225 Is.EqualTo(5));
226
227 db.Types
228 .Where(t => t.ID == 1)
229 .Set(t => t.BinaryValue, (Binary)null)
230 .Update();
231 });
232 }
233
234 [Test]
235 public void InsertBinary1()
236 {
237 ForEachProvider(
238 new[] { ProviderName.DB2, ProviderName.Informix, ProviderName.Firebird, ProviderName.PostgreSQL, ProviderName.SQLite, ProviderName.Access },
239 db =>
240 {
241 Binary data = null;
242
243 db.Types.Delete(_ => _.ID > 1000);
244 db.Types.Insert(() => new LinqDataTypes
245 {
246 ID = 1001,
247 BinaryValue = data,
248 BoolValue = true,
249 });
250 db.Types.Delete(_ => _.ID > 1000);
251 });
252 }
253
254 [Test]
255 public void UpdateBinary1()
256 {
257 ForEachProvider(db =>
258 {
259 db.Types
260 .Where(t => t.ID == 1)
261 .Set(t => t.BinaryValue, new Binary(new byte[] { 1, 2, 3, 4, 5 }))
262 .Update();
263
264 var g = from t in db.Types where t.ID == 1 select t.BinaryValue;
265
266 foreach (var binary in g)
267 {
268 }
269 });
270 }
271
272 [Test]
273 public void UpdateBinary2()
274 {
275 ForEachProvider(new[] { ProviderName.SqlCe }, db =>
276 {
277 var ints = new[] { 1, 2 };
278 var binaries = new[] { new byte[] { 1, 2, 3, 4, 5 }, new byte[] { 5, 4, 3, 2, 1 } };
279
280 for (var i = 1; i <= 2; i++)
281 {
282 db.Types
283 .Where(t => t.ID == ints[i - 1])
284 .Set(t => t.BinaryValue, binaries[i - 1])
285 .Update();
286 }
287
288 var g = from t in db.Types where new[] { 1, 2 }.Contains(t.ID) select t;
289
290 foreach (var binary in g)
291 Assert.AreEqual(binaries[binary.ID - 1], binary.BinaryValue.ToArray());
292 });
293 }
294
295 [Test]
296 public void DateTime1()
297 {
298 var dt = Types2[3].DateTimeValue;
299
300 ForEachProvider(db => AreEqual(
301 from t in Types2 where t.DateTimeValue.Value.Date > dt.Value.Date select t,
302 from t in db.Types2 where t.DateTimeValue.Value.Date > dt.Value.Date select t));
303 }
304
305 [Test]
306 public void DateTime21([DataContexts(ProviderName.SQLite)] string context)
307 {
308 using (var db = GetDataContext(context))
309 {
310 var pdt = db.Types2.First(t => t.ID == 1).DateTimeValue;
311 var dt = DateTime.Parse("2010-12-14T05:00:07.4250141Z");
312
313 db.Types2.Update(t => t.ID == 1, t => new LinqDataTypes2 { DateTimeValue = dt });
314
315 var dt2 = db.Types2.First(t => t.ID == 1).DateTimeValue;
316
317 db.Types2.Update(t => t.ID == 1, t => new LinqDataTypes2 { DateTimeValue = pdt });
318
319 Assert.AreNotEqual(dt.Ticks, dt2.Value.Ticks);
320 }
321 }
322
323 [Test]
324 public void DateTime22()
325 {
326 ForEachProvider(
327 new[]
328 {
329 ProviderName.SqlCe, ProviderName.Access, "Sql2000", "Sql2005", ProviderName.DB2, ProviderName.Informix,
330 ProviderName.Firebird, "Oracle", "DevartOracle", ProviderName.PostgreSQL, ProviderName.MySql, ProviderName.Sybase
331 },
332 db =>
333 {
334 var pdt = db.Types2.First(t => t.ID == 1).DateTimeValue2;
335 var dt = DateTime.Parse("2010-12-14T05:00:07.4250141Z");
336
337 db.Types2.Update(t => t.ID == 1, t => new LinqDataTypes2 { DateTimeValue2 = dt });
338
339 var dt2 = db.Types2.First(t => t.ID == 1).DateTimeValue2;
340
341 db.Types2.Update(t => t.ID == 1, t => new LinqDataTypes2 { DateTimeValue2 = pdt });
342
343 Assert.AreEqual(dt, dt2);
344 });
345 }
346
347 [Test]
348 public void DateTime23()
349 {
350 ForEachProvider(
351 new[]
352 {
353 ProviderName.SqlCe, ProviderName.Access, "Sql2000", "Sql2005", ProviderName.DB2, ProviderName.Informix,
354 ProviderName.Firebird, "Oracle", "DevartOracle", ProviderName.PostgreSQL, ProviderName.MySql, ProviderName.Sybase
355 },
356 db =>
357 {
358 var pdt = db.Types2.First(t => t.ID == 1).DateTimeValue2;
359 var dt = DateTime.Parse("2010-12-14T05:00:07.4250141Z");
360
361 db.Types2
362 .Where(t => t.ID == 1)
363 .Set (_ => _.DateTimeValue2, dt)
364 .Update();
365
366 var dt2 = db.Types2.First(t => t.ID == 1).DateTimeValue2;
367
368 db.Types2.Update(t => t.ID == 1, t => new LinqDataTypes2 { DateTimeValue2 = pdt });
369
370 Assert.AreEqual(dt, dt2);
371 });
372 }
373
374 [Test]
375 public void DateTime24()
376 {
377 ForEachProvider(
378 new[]
379 {
380 ProviderName.SqlCe, ProviderName.Access, "Sql2000", "Sql2005", ProviderName.DB2, ProviderName.Informix,
381 ProviderName.Firebird, "Oracle", "DevartOracle", ProviderName.PostgreSQL, ProviderName.MySql, ProviderName.Sybase
382 },
383 db =>
384 {
385 var pdt = db.Types2.First(t => t.ID == 1).DateTimeValue2;
386 var dt = DateTime.Parse("2010-12-14T05:00:07.4250141Z");
387 var tt = db.Types2.First(t => t.ID == 1);
388
389 tt.DateTimeValue2 = dt;
390
391 db.Update(tt);
392
393 var dt2 = db.Types2.First(t => t.ID == 1).DateTimeValue2;
394
395 db.Types2.Update(t => t.ID == 1, t => new LinqDataTypes2 { DateTimeValue2 = pdt });
396
397 Assert.AreEqual(dt, dt2);
398 });
399 }
400
401 [Test]
402 public void DateTimeArray1()
403 {
404 ForEachProvider(db =>
405 AreEqual(
406 from t in Types2 where new DateTime?[] { new DateTime(2001, 1, 11, 1, 11, 21, 100) }.Contains(t.DateTimeValue) select t,
407 from t in db.Types2 where new DateTime?[] { new DateTime(2001, 1, 11, 1, 11, 21, 100) }.Contains(t.DateTimeValue) select t));
408 }
409
410 [Test]
411 public void DateTimeArray2()
412 {
413 var arr = new DateTime?[] { new DateTime(2001, 1, 11, 1, 11, 21, 100), new DateTime(2012, 11, 7, 19, 19, 29, 90) };
414
415 ForEachProvider(
416 new[] { ProviderName.Access },
417 db =>
418 AreEqual(
419 from t in Types2 where arr.Contains(t.DateTimeValue) select t,
420 from t in db.Types2 where arr.Contains(t.DateTimeValue) select t));
421 }
422
423 [Test]
424 public void DateTimeArray3()
425 {
426 var arr = new List<DateTime?> { new DateTime(2001, 1, 11, 1, 11, 21, 100) };
427
428 ForEachProvider(
429 new[] { ProviderName.Access },
430 db =>
431 AreEqual(
432 from t in Types2 where arr.Contains(t.DateTimeValue) select t,
433 from t in db.Types2 where arr.Contains(t.DateTimeValue) select t));
434 }
435
436 [Test]
437 public void DateTimeParams()
438 {
439 var arr = new List<DateTime?>
440 {
441 new DateTime(1992, 1, 11, 1, 11, 21, 100),
442 new DateTime(1993, 1, 11, 1, 11, 21, 100)
443 };
444
445 ForEachProvider(
446 db =>
447 {
448 foreach (var dateTime in arr)
449 {
450 var dt = DateTimeParams(db, dateTime);
451 Assert.AreEqual(dateTime, dt);
452 }
453 });
454 }
455
456 static DateTime DateTimeParams(ITestDataContext db, DateTime? dateTime)
457 {
458 var q =
459 from t in db.Types2
460 where t.DateTimeValue > dateTime
461 select new
462 {
463 t.DateTimeValue,
464 dateTime.Value
465 };
466
467 return q.First().Value;
468 }
469
470 [Test]
471 public void Nullable()
472 {
473 ForEachProvider(db => AreEqual(
474 from p in Parent select new { Value = p.Value1.GetValueOrDefault() },
475 from p in db.Parent select new { Value = p.Value1.GetValueOrDefault() }));
476 }
477
478 [Test]
479 public void Unicode()
480 {
481 ForEachProvider(new[] { "DevartOracle", ProviderName.Informix, ProviderName.Firebird, ProviderName.Sybase }, db =>
482 {
483 try
484 {
485 db.Person.Delete(p => p.ID > 2);
486
487 var id =
488 db.Person
489 .InsertWithIdentity(() => new Person
490 {
491 FirstName = "擊敗奴隸",
492 LastName = "Юникодкин",
493 Gender = Gender.Male
494 });
495
496 Assert.NotNull(id);
497
498 var person = db.Person.Single(p => p.FirstName == "擊敗奴隸" && p.LastName == "Юникодкин");
499
500 Assert.NotNull (person);
501 Assert.AreEqual(id, person.ID);
502 Assert.AreEqual("擊敗奴隸", person.FirstName);
503 Assert.AreEqual("Юникодкин", person.LastName);
504 }
505 finally
506 {
507 db.Person.Delete(p => p.ID > 2);
508 }
509 });
510 }
511
512 [Test]
513 public void TestCultureInfo([DataContexts] string context)
514 {
515 var current = Thread.CurrentThread.CurrentCulture;
516
517 Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
518
519 using (var db = GetDataContext(context))
520 AreEqual(
521 from t in Types where t.MoneyValue > 0.5m select t,
522 from t in db.Types where t.MoneyValue > 0.5m select t);
523
524 Thread.CurrentThread.CurrentCulture = current;
525 }
526
527 [Test]
528 public void SmallInt()
529 {
530 ForEachProvider(db => AreEqual(
531 from t1 in Types
532 join t2 in Types on t1.SmallIntValue equals t2.ID
533 select t1,
534 from t1 in db.Types
535 join t2 in db.Types on t1.SmallIntValue equals t2.ID
536 select t1)
537 );
538 }
539
540 [TableName("Person")]
541 public class PersonCharTest
542 {
543 public int PersonID;
544 public string FirstName;
545 public string LastName;
546 public string MiddleName;
547 public char Gender;
548 }
549
550 [Test]
551 public void CharTest1()
552 {
553 List<PersonCharTest> list;
554
555 using (var db = new TestDbManager())
556 list = db.GetTable<PersonCharTest>().ToList();
557
558 ForEachProvider(db => AreEqual(
559 from p in list where p.Gender == 'M' select p.PersonID,
560 from p in db.GetTable<PersonCharTest>() where p.Gender == 'M' select p.PersonID));
561 }
562
563 [Test]
564 public void CharTest2()
565 {
566 List<PersonCharTest> list;
567
568 using (var db = new TestDbManager())
569 list = db.GetTable<PersonCharTest>().ToList();
570
571 ForEachProvider(db => AreEqual(
572 from p in list where 'M' == p.Gender select p.PersonID,
573 from p in db.GetTable<PersonCharTest>() where 'M' == p.Gender select p.PersonID));
574 }
575
576 [TableName("Person")]
577 public class PersonBoolTest
578 {
579 public int PersonID;
580 public string FirstName;
581 public string LastName;
582 public string MiddleName;
583 [MapField("Gender"), MapValue(true, "M"), MapValue(false, "F")]
584 public bool IsMale;
585 }
586
587 //[Test]
588 public void BoolTest1()
589 {
590 List<PersonBoolTest> list;
591
592 using (var db = new TestDbManager())
593 list = db.GetTable<PersonBoolTest>().ToList();
594
595 ForEachProvider(db => AreEqual(
596 from p in list where p.IsMale select p.PersonID,
597 from p in db.GetTable<PersonBoolTest>() where p.IsMale select p.PersonID));
598 }
599
600 //[Test]
601 public void BoolTest2()
602 {
603 List<PersonBoolTest> list;
604
605 using (var db = new TestDbManager())
606 list = db.GetTable<PersonBoolTest>().ToList();
607
608 ForEachProvider(db => AreEqual(
609 from p in list where p.IsMale == true select p.PersonID,
610 from p in db.GetTable<PersonBoolTest>() where p.IsMale == true select p.PersonID));
611 }
612
613 [Test]
614 public void BoolTest31()
615 {
616 ForEachProvider(db => AreEqual(
617 from t in Types2 where (t.BoolValue ?? false) select t,
618 from t in db.Types2 where t.BoolValue.Value select t));
619 }
620
621 [Test]
622 public void BoolTest32()
623 {
624 ForEachProvider(db => AreEqual(
625 from t in Types2 where (t.BoolValue ?? false) select t,
626 from t in db.Types2 where t.BoolValue == true select t));
627 }
628
629 [Test]
630 public void BoolTest33()
631 {
632 ForEachProvider(db => AreEqual(
633 from t in Types2 where (t.BoolValue ?? false) select t,
634 from t in db.Types2 where true == t.BoolValue select t));
635 }
636
637 [Test]
638 public void LongTest1()
639 {
640 ForEachProvider(db =>
641 {
642 uint value = 0;
643
644 var q =
645 from t in db.Types2
646 where t.BigIntValue == value
647 select t;
648
649 q.ToList();
650 });
651 }
652
653 [Test]
654 public void CompareNullableInt()
655 {
656 int? param = null;
657
658 ForEachProvider(db => AreEqual(
659 from t in Parent where param == null || t.Value1 == param select t,
660 from t in db.Parent where param == null || t.Value1 == param select t));
661
662 param = 1;
663
664 ForEachProvider(db => AreEqual(
665 from t in Parent where param == null || t.Value1 == param select t,
666 from t in db.Parent where param == null || t.Value1 == param select t));
667 }
668
669 [Test]
670 public void CompareNullableBoolean1()
671 {
672 bool? param = null;
673
674 ForEachProvider(db => AreEqual(
675 from t in Types where param == null || t.BoolValue == param select t,
676 from t in db.Types where param == null || t.BoolValue == param select t));
677
678 param = true;
679
680 ForEachProvider(db => AreEqual(
681 from t in Types where param == null || t.BoolValue == param select t,
682 from t in db.Types where param == null || t.BoolValue == param select t));
683 }
684
685 [Test]
686 public void CompareNullableBoolean2()
687 {
688 short? param1 = null;
689 bool? param2 = null;
690
691 ForEachProvider(db => AreEqual(
692 from t1 in Types
693 join t2 in Types on t1.ID equals t2.ID
694 where (param1 == null || t1.SmallIntValue == param1) && (param2 == null || t1.BoolValue == param2)
695 select t1,
696 from t1 in db.Types
697 join t2 in db.Types on t1.ID equals t2.ID
698 where (param1 == null || t1.SmallIntValue == param1) && (param2 == null || t1.BoolValue == param2)
699 select t1));
700
701 //param1 = null;
702 param2 = false;
703
704 ForEachProvider(db => AreEqual(
705 from t1 in Types
706 join t2 in Types on t1.ID equals t2.ID
707 where (param1 == null || t1.SmallIntValue == param1) && (param2 == null || t1.BoolValue == param2)
708 select t1,
709 from t1 in db.Types
710 join t2 in db.Types on t1.ID equals t2.ID
711 where (param1 == null || t1.SmallIntValue == param1) && (param2 == null || t1.BoolValue == param2)
712 select t1));
713 }
714
715 [Test]
716 public void CompareNullableBoolean3()
717 {
718 short? param1 = null;
719 bool? param2 = false;
720
721 ForEachProvider(db => AreEqual(
722 from t in Types where (param1 == null || t.SmallIntValue == param1) && (param2 == null || t.BoolValue == param2) select t,
723 from t in db.Types where (param1 == null || t.SmallIntValue == param1) && (param2 == null || t.BoolValue == param2) select t));
724 }
725 }
726 }