Mercurial > pub > bltoolkit
comparison UnitTests/Linq/Inheritance.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.DataProvider; | |
| 5 using BLToolkit.Data.Linq; | |
| 6 using BLToolkit.Data.Sql.SqlProvider; | |
| 7 using BLToolkit.DataAccess; | |
| 8 using BLToolkit.Mapping; | |
| 9 | |
| 10 using NUnit.Framework; | |
| 11 | |
| 12 namespace Data.Linq | |
| 13 { | |
| 14 using Model; | |
| 15 | |
| 16 [TestFixture] | |
| 17 public class Inheritance : TestBase | |
| 18 { | |
| 19 [Test] | |
| 20 public void Test1() | |
| 21 { | |
| 22 ForEachProvider(db => AreEqual(ParentInheritance, db.ParentInheritance)); | |
| 23 } | |
| 24 | |
| 25 [Test] | |
| 26 public void Test2() | |
| 27 { | |
| 28 ForEachProvider(db => AreEqual(ParentInheritance, db.ParentInheritance.Select(p => p))); | |
| 29 } | |
| 30 | |
| 31 [Test] | |
| 32 public void Test3() | |
| 33 { | |
| 34 ForEachProvider(db => AreEqual( | |
| 35 from p in ParentInheritance where p is ParentInheritance1 select p, | |
| 36 from p in db.ParentInheritance where p is ParentInheritance1 select p)); | |
| 37 } | |
| 38 | |
| 39 [Test] | |
| 40 public void Test4() | |
| 41 { | |
| 42 ForEachProvider(db => AreEqual( | |
| 43 from p in ParentInheritance where !(p is ParentInheritanceNull) select p, | |
| 44 from p in db.ParentInheritance where !(p is ParentInheritanceNull) select p)); | |
| 45 } | |
| 46 | |
| 47 [Test] | |
| 48 public void Test5() | |
| 49 { | |
| 50 ForEachProvider(db => AreEqual( | |
| 51 from p in ParentInheritance where p is ParentInheritanceValue select p, | |
| 52 from p in db.ParentInheritance where p is ParentInheritanceValue select p)); | |
| 53 } | |
| 54 | |
| 55 [Test] | |
| 56 public void Test6() | |
| 57 { | |
| 58 ForEachProvider(db => | |
| 59 { | |
| 60 var q = from p in db.ParentInheritance2 where p is ParentInheritance12 select p; | |
| 61 q.ToList(); | |
| 62 }); | |
| 63 } | |
| 64 | |
| 65 [Test] | |
| 66 public void Test7() | |
| 67 { | |
| 68 #pragma warning disable 183 | |
| 69 var expected = from p in ParentInheritance where p is ParentInheritanceBase select p; | |
| 70 ForEachProvider(db => AreEqual(expected, from p in db.ParentInheritance where p is ParentInheritanceBase select p)); | |
| 71 #pragma warning restore 183 | |
| 72 } | |
| 73 | |
| 74 [Test] | |
| 75 public void Test8() | |
| 76 { | |
| 77 ForEachProvider(db => AreEqual( | |
| 78 ParentInheritance.OfType<ParentInheritance1>(), | |
| 79 db.ParentInheritance.OfType<ParentInheritance1>())); | |
| 80 } | |
| 81 | |
| 82 [Test] | |
| 83 public void Test9() | |
| 84 { | |
| 85 ForEachProvider(db => | |
| 86 AreEqual( | |
| 87 ParentInheritance | |
| 88 .Where(p => p.ParentID == 1 || p.ParentID == 2 || p.ParentID == 4) | |
| 89 .OfType<ParentInheritanceNull>(), | |
| 90 db.ParentInheritance | |
| 91 .Where(p => p.ParentID == 1 || p.ParentID == 2 || p.ParentID == 4) | |
| 92 .OfType<ParentInheritanceNull>())); | |
| 93 } | |
| 94 | |
| 95 [Test] | |
| 96 public void Test10() | |
| 97 { | |
| 98 var expected = ParentInheritance.OfType<ParentInheritanceValue>(); | |
| 99 ForEachProvider(db => AreEqual(expected, db.ParentInheritance.OfType<ParentInheritanceValue>())); | |
| 100 } | |
| 101 | |
| 102 [Test] | |
| 103 public void Test11() | |
| 104 { | |
| 105 ForEachProvider(db => | |
| 106 { | |
| 107 var q = from p in db.ParentInheritance3 where p is ParentInheritance13 select p; | |
| 108 q.ToList(); | |
| 109 }); | |
| 110 } | |
| 111 | |
| 112 [Test] | |
| 113 public void Test12() | |
| 114 { | |
| 115 ForEachProvider(db => AreEqual( | |
| 116 from p in ParentInheritance1 where p.ParentID == 1 select p, | |
| 117 from p in db.ParentInheritance1 where p.ParentID == 1 select p)); | |
| 118 } | |
| 119 | |
| 120 //[Test] | |
| 121 public void Test13() | |
| 122 { | |
| 123 ForEachProvider(db => AreEqual( | |
| 124 from p in ParentInheritance4 | |
| 125 join c in Child on p.ParentID equals c.ParentID | |
| 126 select p, | |
| 127 from p in db.ParentInheritance4 | |
| 128 join c in db.Child on p.ParentID equals c.ParentID | |
| 129 select p)); | |
| 130 } | |
| 131 | |
| 132 [Test] | |
| 133 public void TypeCastAsTest1([IncludeDataContexts("Northwind")] string context) | |
| 134 { | |
| 135 using (var db = new NorthwindDB()) | |
| 136 AreEqual( | |
| 137 DiscontinuedProduct.ToList() | |
| 138 .Select(p => p as Northwind.Product) | |
| 139 .Select(p => p == null ? "NULL" : p.ProductName), | |
| 140 db.DiscontinuedProduct | |
| 141 .Select(p => p as Northwind.Product) | |
| 142 .Select(p => p == null ? "NULL" : p.ProductName)); | |
| 143 } | |
| 144 | |
| 145 [Test] | |
| 146 public void TypeCastAsTest11([IncludeDataContexts("Northwind")] string context) | |
| 147 { | |
| 148 using (var db = new NorthwindDB()) | |
| 149 AreEqual( | |
| 150 DiscontinuedProduct.ToList() | |
| 151 .Select(p => new { p = p as Northwind.Product }) | |
| 152 .Select(p => p.p == null ? "NULL" : p.p.ProductName), | |
| 153 db.DiscontinuedProduct | |
| 154 .Select(p => new { p = p as Northwind.Product }) | |
| 155 .Select(p => p.p == null ? "NULL" : p.p.ProductName)); | |
| 156 } | |
| 157 | |
| 158 [Test] | |
| 159 public void TypeCastAsTest2([IncludeDataContexts("Northwind")] string context) | |
| 160 { | |
| 161 using (var db = new NorthwindDB()) | |
| 162 AreEqual( | |
| 163 Product.ToList() | |
| 164 .Select(p => p as Northwind.DiscontinuedProduct) | |
| 165 .Select(p => p == null ? "NULL" : p.ProductName), | |
| 166 db.Product | |
| 167 .Select(p => p as Northwind.DiscontinuedProduct) | |
| 168 .Select(p => p == null ? "NULL" : p.ProductName)); | |
| 169 } | |
| 170 | |
| 171 [Test] | |
| 172 public void FirstOrDefault([IncludeDataContexts("Northwind")] string context) | |
| 173 { | |
| 174 using (var db = new NorthwindDB()) | |
| 175 Assert.AreEqual( | |
| 176 DiscontinuedProduct.FirstOrDefault().ProductID, | |
| 177 db.DiscontinuedProduct.FirstOrDefault().ProductID); | |
| 178 } | |
| 179 | |
| 180 [Test] | |
| 181 public void Cast1() | |
| 182 { | |
| 183 ForEachProvider(db => AreEqual( | |
| 184 ParentInheritance.OfType<ParentInheritance1>().Cast<ParentInheritanceBase>(), | |
| 185 db.ParentInheritance.OfType<ParentInheritance1>().Cast<ParentInheritanceBase>())); | |
| 186 } | |
| 187 | |
| 188 class ParentEx : Parent | |
| 189 { | |
| 190 [MapIgnore] | |
| 191 protected bool Field1; | |
| 192 | |
| 193 public static void Test(Inheritance inheritance) | |
| 194 { | |
| 195 inheritance.ForEachProvider(db => inheritance.AreEqual( | |
| 196 inheritance.Parent.Select(p => new ParentEx { Field1 = true, ParentID = p.ParentID, Value1 = p.Value1 }).Cast<Parent>(), | |
| 197 db.Parent.Select(p => new ParentEx { Field1 = true, ParentID = p.ParentID, Value1 = p.Value1 }).Cast<Parent>())); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 [Test] | |
| 202 public void Cast2() | |
| 203 { | |
| 204 ParentEx.Test(this); | |
| 205 } | |
| 206 | |
| 207 [TableName("Person")] | |
| 208 class PersonEx : Person | |
| 209 { | |
| 210 } | |
| 211 | |
| 212 [Test] | |
| 213 public void SimplTest() | |
| 214 { | |
| 215 using (var db = new TestDbManager()) | |
| 216 Assert.AreEqual(1, db.GetTable<PersonEx>().Where(_ => _.FirstName == "John").Select(_ => _.ID).Single()); | |
| 217 } | |
| 218 | |
| 219 [InheritanceMapping(Code = 1, Type = typeof(Parent222))] | |
| 220 [TableName("Parent")] | |
| 221 public class Parent111 | |
| 222 { | |
| 223 [MapField(IsInheritanceDiscriminator = true)] | |
| 224 public int ParentID; | |
| 225 } | |
| 226 | |
| 227 [MapField("Value1", "Value.ID")] | |
| 228 public class Parent222 : Parent111 | |
| 229 { | |
| 230 [MapIgnore] | |
| 231 public Value111 Value; | |
| 232 } | |
| 233 | |
| 234 public class Value111 | |
| 235 { | |
| 236 public int ID; | |
| 237 } | |
| 238 | |
| 239 [Test] | |
| 240 public void InheritanceMappingIssueTest() | |
| 241 { | |
| 242 using (var db = new TestDbManager()) | |
| 243 { | |
| 244 var q1 = db.GetTable<Parent222>(); | |
| 245 var q = q1.Where(_ => _.Value.ID == 1); | |
| 246 | |
| 247 var sql = ((ExpressionQuery<Parent222>)q).SqlText; | |
| 248 Assert.IsNotEmpty(sql); | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 [Test] | |
| 253 public void ReferenceNavigation([IncludeDataContexts("Northwind")] string context) | |
| 254 { | |
| 255 using (var db = new NorthwindDB()) | |
| 256 { | |
| 257 var result = | |
| 258 from od in db.OrderDetail | |
| 259 where od.Product.Category.CategoryName == "Seafood" | |
| 260 select new { od.Order, od.Product }; | |
| 261 | |
| 262 var list = result.ToList(); | |
| 263 | |
| 264 Assert.AreEqual(330, list.Count); | |
| 265 | |
| 266 foreach (var item in list) | |
| 267 { | |
| 268 Assert.IsNotNull(item); | |
| 269 Assert.IsNotNull(item.Order); | |
| 270 Assert.IsNotNull(item.Product); | |
| 271 Assert.IsTrue( | |
| 272 item.Product.Discontinued && item.Product is Northwind.DiscontinuedProduct || | |
| 273 !item.Product.Discontinued && item.Product is Northwind.ActiveProduct); | |
| 274 } | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 [Test] | |
| 279 public void TypeCastIsChildConditional1([IncludeDataContexts("Northwind")] string context) | |
| 280 { | |
| 281 using (var db = new NorthwindDB()) | |
| 282 { | |
| 283 var result = db.Product. Select(x => x is Northwind.DiscontinuedProduct ? x : null); | |
| 284 var expected = db.Product.ToList().Select(x => x is Northwind.DiscontinuedProduct ? x : null); | |
| 285 | |
| 286 var list = result.ToList(); | |
| 287 | |
| 288 Assert.Greater(list.Count, 0); | |
| 289 Assert.AreEqual(expected.Count(), list.Count); | |
| 290 Assert.IsTrue(list.Except(expected).Count() == 0); | |
| 291 Assert.IsTrue(list.Contains(null)); | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 [Test] | |
| 296 public void TypeCastIsChildConditional2([IncludeDataContexts("Northwind")] string context) | |
| 297 { | |
| 298 using (var db = new NorthwindDB()) | |
| 299 { | |
| 300 var result = db.Product. Select(x => x is Northwind.DiscontinuedProduct); | |
| 301 var expected = db.Product.ToList().Select(x => x is Northwind.DiscontinuedProduct); | |
| 302 | |
| 303 var list = result.ToList(); | |
| 304 | |
| 305 Assert.Greater(list.Count, 0); | |
| 306 Assert.AreEqual(expected.Count(), list.Count); | |
| 307 Assert.IsTrue(list.Except(expected).Count() == 0); | |
| 308 } | |
| 309 } | |
| 310 | |
| 311 [Test] | |
| 312 public void TypeCastIsChild([IncludeDataContexts("Northwind")] string context) | |
| 313 { | |
| 314 using (var db = new NorthwindDB()) | |
| 315 { | |
| 316 var result = db.Product.Where(x => x is Northwind.DiscontinuedProduct).ToList(); | |
| 317 var expected = Product.Where(x => x is Northwind.DiscontinuedProduct).ToList(); | |
| 318 | |
| 319 Assert.Greater(result.Count, 0); | |
| 320 Assert.AreEqual(result.Count, expected.Count); | |
| 321 } | |
| 322 } | |
| 323 | |
| 324 #region Models for Test14 | |
| 325 | |
| 326 interface IChildTest14 | |
| 327 { | |
| 328 int ChildID { get; set; } | |
| 329 } | |
| 330 | |
| 331 [TableName("Child")] | |
| 332 class ChildTest14 : IChildTest14 | |
| 333 { | |
| 334 [PrimaryKey] | |
| 335 public int ChildID { get; set; } | |
| 336 | |
| 337 } | |
| 338 | |
| 339 T FindById<T>(IQueryable<T> queryable, int id) | |
| 340 where T : IChildTest14 | |
| 341 { | |
| 342 return queryable.Where(x => x.ChildID == id).FirstOrDefault(); | |
| 343 } | |
| 344 | |
| 345 #endregion | |
| 346 | |
| 347 [Test] | |
| 348 public void Test14() | |
| 349 { | |
| 350 ForEachProvider(db => | |
| 351 { | |
| 352 var q = db.GetTable<ChildTest14>().Select(c => new ChildTest14() { ChildID = c.ChildID }); | |
| 353 FindById(q, 10); | |
| 354 }); | |
| 355 } | |
| 356 | |
| 357 [Test] | |
| 358 public void Test15([IncludeDataContexts("Northwind")] string context) | |
| 359 { | |
| 360 using (var db = new NorthwindDB()) | |
| 361 { | |
| 362 var result = db.DiscontinuedProduct.Select(p => p).ToList(); | |
| 363 var expected = DiscontinuedProduct.Select(p => p).ToList(); | |
| 364 | |
| 365 Assert.That(result.Count, Is.Not.EqualTo(0).And.EqualTo(expected.Count)); | |
| 366 } | |
| 367 } | |
| 368 | |
| 369 [Test] | |
| 370 public void Test16([IncludeDataContexts("Northwind")] string context) | |
| 371 { | |
| 372 using (var db = new NorthwindDB()) | |
| 373 { | |
| 374 var result = db.DiscontinuedProduct.ToList(); | |
| 375 var expected = DiscontinuedProduct.ToList(); | |
| 376 | |
| 377 Assert.That(result.Count, Is.Not.EqualTo(0).And.EqualTo(expected.Count)); | |
| 378 } | |
| 379 } | |
| 380 | |
| 381 public enum TypeCodeEnum | |
| 382 { | |
| 383 Base, | |
| 384 A, | |
| 385 A1, | |
| 386 A2, | |
| 387 } | |
| 388 | |
| 389 [TableName("LinqDataTypes")] | |
| 390 public abstract class InheritanceBase | |
| 391 { | |
| 392 public Guid GuidValue { get; set; } | |
| 393 | |
| 394 [MapField("ID")] | |
| 395 public virtual TypeCodeEnum TypeCode | |
| 396 { | |
| 397 get { return TypeCodeEnum.Base; } | |
| 398 } | |
| 399 } | |
| 400 | |
| 401 [InheritanceMapping(Code = TypeCodeEnum.A1, Type = typeof(InheritanceA1), IsDefault = false)] | |
| 402 [InheritanceMapping(Code = TypeCodeEnum.A2, Type = typeof(InheritanceA2), IsDefault = true)] | |
| 403 public abstract class InheritanceA : InheritanceBase | |
| 404 { | |
| 405 [Association(CanBeNull = true, ThisKey = "GuidValue", OtherKey = "GuidValue")] | |
| 406 public List<InheritanceB> Bs { get; set; } | |
| 407 | |
| 408 [MapField("ID", IsInheritanceDiscriminator = true)] | |
| 409 public override TypeCodeEnum TypeCode | |
| 410 { | |
| 411 get { return TypeCodeEnum.A; } | |
| 412 } | |
| 413 } | |
| 414 | |
| 415 class InheritanceA1 : InheritanceA | |
| 416 { | |
| 417 [MapField("ID", IsInheritanceDiscriminator = true)] | |
| 418 public override TypeCodeEnum TypeCode | |
| 419 { | |
| 420 get { return TypeCodeEnum.A1; } | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 class InheritanceA2 : InheritanceA | |
| 425 { | |
| 426 [MapField("ID", IsInheritanceDiscriminator = true)] | |
| 427 public override TypeCodeEnum TypeCode | |
| 428 { | |
| 429 get { return TypeCodeEnum.A2; } | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 public class InheritanceB : InheritanceBase | |
| 434 { | |
| 435 } | |
| 436 | |
| 437 [Test] | |
| 438 public void GuidTest() | |
| 439 { | |
| 440 using (var db = new TestDbManager()) | |
| 441 { | |
| 442 var list = db.GetTable<InheritanceA>().Where(a => a.Bs.Any()).ToList(); | |
| 443 } | |
| 444 } | |
| 445 | |
| 446 [Test] | |
| 447 public void QuerySyntaxSimpleTest() | |
| 448 { | |
| 449 ForEachProvider(db => | |
| 450 { | |
| 451 // db.GetTable<Parent111>().OfType<Parent222>().ToList(); - it's work!!! | |
| 452 (from p in db.GetTable<Parent111>().OfType<Parent222>() select p).ToList(); | |
| 453 }); | |
| 454 } | |
| 455 | |
| 456 [TableName("Person")] | |
| 457 [InheritanceMapping(Code = 1, Type = typeof(Test17John))] | |
| 458 [InheritanceMapping(Code = 2, Type = typeof(Test17Tester))] | |
| 459 public class Test17Person | |
| 460 { | |
| 461 [MapField(IsInheritanceDiscriminator = true)] | |
| 462 public int PersonID { get; set; } | |
| 463 } | |
| 464 | |
| 465 public class Test17John : Test17Person | |
| 466 { | |
| 467 public string FirstName { get; set; } | |
| 468 } | |
| 469 | |
| 470 public class Test17Tester : Test17Person | |
| 471 { | |
| 472 public string LastName { get; set; } | |
| 473 } | |
| 474 | |
| 475 [Test] | |
| 476 public void Test17() | |
| 477 { | |
| 478 ForEachProvider(context => | |
| 479 { | |
| 480 if (context is TestDbManager) | |
| 481 { | |
| 482 var db = (TestDbManager)context; | |
| 483 db.GetTable<Test17Person>().OfType<Test17John>().ToList(); | |
| 484 Assert.False(db.LastQuery.ToLowerInvariant().Contains("lastname"), "Why select LastName field??"); | |
| 485 } | |
| 486 }); | |
| 487 } | |
| 488 | |
| 489 [TableName("Person")] | |
| 490 [InheritanceMapping(Code = Gender.Male, Type = typeof(Test18Male))] | |
| 491 [InheritanceMapping(Code = Gender.Female, Type = typeof(Test18Female))] | |
| 492 public class Test18Person | |
| 493 { | |
| 494 [PrimaryKey, NonUpdatable(IsIdentity = true, OnInsert = true, OnUpdate = true), SequenceName("PERSONID")] | |
| 495 public int PersonID { get; set; } | |
| 496 [MapField(IsInheritanceDiscriminator = true)] | |
| 497 public Gender Gender { get; set; } | |
| 498 } | |
| 499 | |
| 500 public class Test18Male : Test18Person | |
| 501 { | |
| 502 public string FirstName { get; set; } | |
| 503 } | |
| 504 | |
| 505 public class Test18Female : Test18Person | |
| 506 { | |
| 507 public string FirstName { get; set; } | |
| 508 public string LastName { get; set; } | |
| 509 } | |
| 510 | |
| 511 [Test] | |
| 512 public void Test18() | |
| 513 { | |
| 514 ForEachProvider(db => | |
| 515 { | |
| 516 var ids = Enumerable.Range(0, 10).ToList(); | |
| 517 var q = | |
| 518 from p1 in db.GetTable<Test18Person>() | |
| 519 where ids.Contains(p1.PersonID) | |
| 520 join p2 in db.GetTable<Test18Person>() on p1.PersonID equals p2.PersonID | |
| 521 select p1; | |
| 522 | |
| 523 var list = q.Distinct().OfType<Test18Female>().ToList(); | |
| 524 }); | |
| 525 } | |
| 526 | |
| 527 [Test] | |
| 528 public void Test19() | |
| 529 { | |
| 530 ForEachProvider(db => | |
| 531 { | |
| 532 var ids = Enumerable.Range(0, 10).ToList(); | |
| 533 var q = | |
| 534 from p1 in db.GetTable<Test18Person>() | |
| 535 where ids.Contains(p1.PersonID) | |
| 536 join p2 in db.GetTable<Test18Person>() on p1.PersonID equals p2.PersonID | |
| 537 select p1; | |
| 538 | |
| 539 IQueryable iq = q.Distinct(); | |
| 540 var list = iq.OfType<Test18Female>().ToList(); | |
| 541 }); | |
| 542 } | |
| 543 } | |
| 544 } |
