Mercurial > pub > bltoolkit
comparison UnitTests/Linq/TestBase.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; | |
| 3 using System.Collections.Generic; | |
| 4 using System.Diagnostics; | |
| 5 using System.IO; | |
| 6 using System.Linq; | |
| 7 using System.Reflection; | |
| 8 using System.ServiceModel; | |
| 9 using System.ServiceModel.Description; | |
| 10 | |
| 11 using BLToolkit.Data.DataProvider; | |
| 12 using BLToolkit.Common; | |
| 13 using BLToolkit.Data; | |
| 14 using BLToolkit.Data.Linq; | |
| 15 using BLToolkit.Data.Sql.SqlProvider; | |
| 16 using BLToolkit.Mapping; | |
| 17 using BLToolkit.ServiceModel; | |
| 18 | |
| 19 using NUnit.Framework; | |
| 20 | |
| 21 namespace Data.Linq | |
| 22 { | |
| 23 using Model; | |
| 24 | |
| 25 // fix for failing tests due to use of "," vs "." in numbers parsing for some cultures | |
| 26 [SetCulture("")] | |
| 27 public class TestBase | |
| 28 { | |
| 29 static TestBase() | |
| 30 { | |
| 31 var providerListFile = | |
| 32 File.Exists(@"..\..\UserDataProviders.txt") ? | |
| 33 @"..\..\UserDataProviders.txt" : | |
| 34 @"..\..\DefaultDataProviders.txt"; | |
| 35 | |
| 36 UserProviders.AddRange( | |
| 37 File.ReadAllLines(providerListFile) | |
| 38 .Select(s => s.Trim()) | |
| 39 .Where (s => s.Length > 0 && !s.StartsWith("--"))); | |
| 40 | |
| 41 AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => | |
| 42 { | |
| 43 string assembly; | |
| 44 | |
| 45 if (args.Name.IndexOf("Sybase.AdoNet2.AseClient") >= 0) assembly = @"Sybase\Sybase.AdoNet2.AseClient.dll"; | |
| 46 else if (args.Name.IndexOf("Oracle.DataAccess") >= 0) assembly = @"Oracle\Oracle.DataAccess.dll"; | |
| 47 else if (args.Name.IndexOf("IBM.Data.DB2") >= 0) assembly = @"IBM\IBM.Data.DB2.dll"; | |
| 48 else if (args.Name.IndexOf("Npgsql.resources") >= 0) return null; | |
| 49 else if (args.Name.IndexOf("Npgsql") >= 0) assembly = @"PostgreSql\Npgsql.dll"; | |
| 50 else if (args.Name.IndexOf("Mono.Security") >= 0) assembly = @"PostgreSql\Mono.Security.dll"; | |
| 51 else if (args.Name.IndexOf("System.Data.SqlServerCe,") >= 0) assembly = @"SqlCe\System.Data.SqlServerCe.dll"; | |
| 52 else | |
| 53 return null; | |
| 54 | |
| 55 assembly = @"..\..\..\..\Redist\" + assembly; | |
| 56 | |
| 57 if (!File.Exists(assembly)) | |
| 58 assembly = @"..\..\" + assembly; | |
| 59 | |
| 60 return Assembly.LoadFrom(assembly); | |
| 61 }; | |
| 62 | |
| 63 DbManager.TurnTraceSwitchOn(); | |
| 64 | |
| 65 PostgreSQLSqlProvider.QuoteIdentifiers = true; | |
| 66 | |
| 67 var path = Path.GetDirectoryName(typeof(DbManager).Assembly.CodeBase.Replace("file:///", "")); | |
| 68 | |
| 69 foreach (var info in Providers) | |
| 70 { | |
| 71 try | |
| 72 { | |
| 73 Type type; | |
| 74 | |
| 75 if (info.Assembly == null) | |
| 76 { | |
| 77 type = typeof(DbManager).Assembly.GetType(info.Type, true); | |
| 78 } | |
| 79 else | |
| 80 { | |
| 81 #if FW4 | |
| 82 var fileName = info.Assembly + ".4.dll"; | |
| 83 #else | |
| 84 var fileName = info.Assembly + ".3.dll"; | |
| 85 #endif | |
| 86 | |
| 87 var assembly = Assembly.LoadFile(Path.Combine(path, fileName)); | |
| 88 | |
| 89 type = assembly.GetType(info.Type, true); | |
| 90 } | |
| 91 | |
| 92 DbManager.AddDataProvider(type); | |
| 93 | |
| 94 info.Loaded = true; | |
| 95 } | |
| 96 catch (Exception) | |
| 97 { | |
| 98 info.Loaded = false; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 LinqService.TypeResolver = str => | |
| 103 { | |
| 104 switch (str) | |
| 105 { | |
| 106 //case "Data.Linq.Model.Gender" : return typeof(Gender); | |
| 107 case "Data.Linq.Model.Person": return typeof(Person); | |
| 108 default : return null; | |
| 109 } | |
| 110 }; | |
| 111 } | |
| 112 | |
| 113 const int StartIP = 12345; | |
| 114 static int _lastIP = StartIP; | |
| 115 | |
| 116 static int GetIP(string config) | |
| 117 { | |
| 118 int ip; | |
| 119 | |
| 120 if (_ips.TryGetValue(config, out ip)) | |
| 121 return ip; | |
| 122 | |
| 123 _lastIP++; | |
| 124 | |
| 125 var host = new ServiceHost(new LinqService(config) { AllowUpdates = true }, new Uri("net.tcp://localhost:" + _lastIP)); | |
| 126 | |
| 127 host.Description.Behaviors.Add(new ServiceMetadataBehavior()); | |
| 128 host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true; | |
| 129 host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); | |
| 130 host.AddServiceEndpoint( | |
| 131 typeof(ILinqService), | |
| 132 new NetTcpBinding(SecurityMode.None) | |
| 133 { | |
| 134 MaxReceivedMessageSize = 10000000, | |
| 135 MaxBufferPoolSize = 10000000, | |
| 136 MaxBufferSize = 10000000, | |
| 137 CloseTimeout = new TimeSpan(00, 01, 00), | |
| 138 OpenTimeout = new TimeSpan(00, 01, 00), | |
| 139 ReceiveTimeout = new TimeSpan(00, 10, 00), | |
| 140 SendTimeout = new TimeSpan(00, 10, 00), | |
| 141 }, | |
| 142 "LinqOverWCF"); | |
| 143 | |
| 144 host.Open(); | |
| 145 | |
| 146 _ips.Add(config, _lastIP); | |
| 147 | |
| 148 return _lastIP; | |
| 149 } | |
| 150 | |
| 151 public class ProviderInfo | |
| 152 { | |
| 153 public ProviderInfo(string name, string assembly, string type) | |
| 154 { | |
| 155 Name = name; | |
| 156 Assembly = assembly; | |
| 157 Type = type; | |
| 158 } | |
| 159 | |
| 160 public readonly string Name; | |
| 161 public readonly string Assembly; | |
| 162 public readonly string Type; | |
| 163 public bool Loaded; | |
| 164 public int IP; | |
| 165 public bool Skip; | |
| 166 } | |
| 167 | |
| 168 public static readonly List<string> UserProviders = new List<string>(); | |
| 169 public static readonly List<ProviderInfo> Providers = new List<ProviderInfo> | |
| 170 { | |
| 171 new ProviderInfo("Sql2008", null, "BLToolkit.Data.DataProvider.Sql2008DataProvider"), | |
| 172 new ProviderInfo("Sql2012", null, "BLToolkit.Data.DataProvider.Sql2012DataProvider"), | |
| 173 new ProviderInfo(ProviderName.SqlCe, "BLToolkit.Data.DataProvider.SqlCe", "BLToolkit.Data.DataProvider.SqlCeDataProvider"), | |
| 174 new ProviderInfo(ProviderName.SQLite, "BLToolkit.Data.DataProvider.SQLite", "BLToolkit.Data.DataProvider.SQLiteDataProvider"), | |
| 175 new ProviderInfo(ProviderName.Access, null, "BLToolkit.Data.DataProvider.AccessDataProvider"), | |
| 176 new ProviderInfo("Sql2000", null, "BLToolkit.Data.DataProvider.Sql2000DataProvider"), | |
| 177 new ProviderInfo("Sql2005", null, "BLToolkit.Data.DataProvider.SqlDataProvider"), | |
| 178 new ProviderInfo(ProviderName.DB2, "BLToolkit.Data.DataProvider.DB2", "BLToolkit.Data.DataProvider.DB2DataProvider"), | |
| 179 new ProviderInfo(ProviderName.Informix, "BLToolkit.Data.DataProvider.Informix", "BLToolkit.Data.DataProvider.InformixDataProvider"), | |
| 180 new ProviderInfo(ProviderName.Firebird, "BLToolkit.Data.DataProvider.Firebird", "BLToolkit.Data.DataProvider.FdpDataProvider"), | |
| 181 new ProviderInfo("Oracle", "BLToolkit.Data.DataProvider.Oracle", "BLToolkit.Data.DataProvider.OdpDataProvider"), | |
| 182 new ProviderInfo("DevartOracle", "BLToolkit.Data.DataProvider.DevartOracle", "BLToolkit.Data.DataProvider.DevartOracleDataProvider"), | |
| 183 //new ProviderInfo("Oracle", "BLToolkit.Data.DataProvider.OracleManaged", "BLToolkit.Data.DataProvider.OdpManagedDataProvider"), | |
| 184 new ProviderInfo(ProviderName.PostgreSQL, "BLToolkit.Data.DataProvider.PostgreSQL", "BLToolkit.Data.DataProvider.PostgreSQLDataProvider"), | |
| 185 new ProviderInfo(ProviderName.MySql, "BLToolkit.Data.DataProvider.MySql", "BLToolkit.Data.DataProvider.MySqlDataProvider"), | |
| 186 new ProviderInfo(ProviderName.Sybase, "BLToolkit.Data.DataProvider.Sybase", "BLToolkit.Data.DataProvider.SybaseDataProvider"), | |
| 187 }; | |
| 188 | |
| 189 static IEnumerable<ITestDataContext> GetProviders(IEnumerable<string> exceptList) | |
| 190 { | |
| 191 var list = UserProviders.Concat(UserProviders.Select(p => p + ".LinqService")); | |
| 192 | |
| 193 foreach (var info in Providers.Where(p => list.Contains(p.Name))) | |
| 194 { | |
| 195 if (exceptList.Contains(info.Name)) | |
| 196 continue; | |
| 197 | |
| 198 Debug.WriteLine(info.Name, "Provider "); | |
| 199 | |
| 200 if (!info.Loaded) | |
| 201 continue; | |
| 202 | |
| 203 yield return new TestDbManager(info.Name); | |
| 204 | |
| 205 var ip = GetIP(info.Name); | |
| 206 var dx = new TestServiceModelDataContext(ip); | |
| 207 | |
| 208 Debug.WriteLine(((IDataContext)dx).ContextID, "Provider "); | |
| 209 | |
| 210 yield return dx; | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] | |
| 215 public class DataContextsAttribute : ValuesAttribute | |
| 216 { | |
| 217 public DataContextsAttribute(params string[] except) | |
| 218 { | |
| 219 Except = except; | |
| 220 } | |
| 221 | |
| 222 const bool IncludeLinqService = true; | |
| 223 | |
| 224 public string[] Except { get; set; } | |
| 225 public string[] Include { get; set; } | |
| 226 public bool ExcludeLinqService { get; set; } | |
| 227 | |
| 228 public override IEnumerable GetData(ParameterInfo parameter) | |
| 229 { | |
| 230 if (Include != null) | |
| 231 { | |
| 232 var list = Include.Intersect( | |
| 233 IncludeLinqService ? | |
| 234 UserProviders.Concat(UserProviders.Select(p => p + ".LinqService")) : | |
| 235 UserProviders). | |
| 236 ToArray(); | |
| 237 | |
| 238 return list; | |
| 239 } | |
| 240 | |
| 241 var providers = new List<string>(); | |
| 242 | |
| 243 foreach (var info in Providers) | |
| 244 { | |
| 245 if (info.Skip && Include == null) | |
| 246 continue; | |
| 247 | |
| 248 if (Except != null && Except.Contains(info.Name)) | |
| 249 continue; | |
| 250 | |
| 251 if (!UserProviders.Contains(info.Name)) | |
| 252 continue; | |
| 253 | |
| 254 providers.Add(info.Name); | |
| 255 | |
| 256 if (IncludeLinqService && !ExcludeLinqService) | |
| 257 { | |
| 258 providers.Add(info.Name + ".LinqService"); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 return providers.ToArray(); | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 public class IncludeDataContextsAttribute : DataContextsAttribute | |
| 267 { | |
| 268 public IncludeDataContextsAttribute(params string[] include) | |
| 269 { | |
| 270 Include = include; | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 static readonly Dictionary<string,int> _ips = new Dictionary<string,int>(); | |
| 275 | |
| 276 protected ITestDataContext GetDataContext(string configuration) | |
| 277 { | |
| 278 if (configuration.EndsWith(".LinqService")) | |
| 279 { | |
| 280 var str = configuration.Substring(0, configuration.Length - ".LinqService".Length); | |
| 281 var ip = GetIP(str); | |
| 282 var dx = new TestServiceModelDataContext(ip); | |
| 283 | |
| 284 Debug.WriteLine(((IDataContext)dx).ContextID, "Provider "); | |
| 285 | |
| 286 return dx; | |
| 287 } | |
| 288 | |
| 289 Debug.WriteLine(configuration, "Provider "); | |
| 290 | |
| 291 return new TestDbManager(configuration); | |
| 292 } | |
| 293 | |
| 294 protected void ForEachProvider(Type expectedException, string[] exceptList, Action<ITestDataContext> func) | |
| 295 { | |
| 296 Exception ex = null; | |
| 297 | |
| 298 foreach (var db in GetProviders(exceptList)) | |
| 299 { | |
| 300 try | |
| 301 { | |
| 302 if (db is DbManager) | |
| 303 ((DbManager)db).BeginTransaction(); | |
| 304 | |
| 305 func(db); | |
| 306 } | |
| 307 catch (Exception e) | |
| 308 { | |
| 309 if (expectedException == null || e.GetType() != expectedException) | |
| 310 throw; | |
| 311 | |
| 312 ex = e; | |
| 313 } | |
| 314 finally | |
| 315 { | |
| 316 db.Dispose(); | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 if (ex != null) | |
| 321 throw ex; | |
| 322 } | |
| 323 | |
| 324 protected void ForEachProvider(string[] exceptList, Action<ITestDataContext> func) | |
| 325 { | |
| 326 ForEachProvider(null, exceptList, func); | |
| 327 } | |
| 328 | |
| 329 protected void ForEachProvider(Action<ITestDataContext> func) | |
| 330 { | |
| 331 ForEachProvider(Array<string>.Empty, func); | |
| 332 } | |
| 333 | |
| 334 protected void ForEachProvider(Type expectedException, Action<ITestDataContext> func) | |
| 335 { | |
| 336 ForEachProvider(expectedException, Array<string>.Empty, func); | |
| 337 } | |
| 338 | |
| 339 protected void Not0ForEachProvider(Func<ITestDataContext, int> func) | |
| 340 { | |
| 341 ForEachProvider(db => Assert.Less(0, func(db))); | |
| 342 } | |
| 343 | |
| 344 protected void TestPerson(int id, string firstName, Func<ITestDataContext,IQueryable<Person>> func) | |
| 345 { | |
| 346 ForEachProvider(db => | |
| 347 { | |
| 348 var person = func(db).ToList().Where(p => p.ID == id).First(); | |
| 349 | |
| 350 Assert.AreEqual(id, person.ID); | |
| 351 Assert.AreEqual(firstName, person.FirstName); | |
| 352 }); | |
| 353 } | |
| 354 | |
| 355 protected void TestJohn(Func<ITestDataContext,IQueryable<Person>> func) | |
| 356 { | |
| 357 TestPerson(1, "John", func); | |
| 358 } | |
| 359 | |
| 360 protected void TestOnePerson(string[] exceptList, int id, string firstName, Func<ITestDataContext,IQueryable<Person>> func) | |
| 361 { | |
| 362 ForEachProvider(exceptList, db => | |
| 363 { | |
| 364 var list = func(db).ToList(); | |
| 365 | |
| 366 Assert.AreEqual(1, list.Count); | |
| 367 | |
| 368 var person = list[0]; | |
| 369 | |
| 370 Assert.AreEqual(id, person.ID); | |
| 371 Assert.AreEqual(firstName, person.FirstName); | |
| 372 }); | |
| 373 } | |
| 374 | |
| 375 protected void TestOnePerson(int id, string firstName, Func<ITestDataContext,IQueryable<Person>> func) | |
| 376 { | |
| 377 TestOnePerson(Array<string>.Empty, id, firstName, func); | |
| 378 } | |
| 379 | |
| 380 protected void TestOneJohn(string[] exceptList, Func<ITestDataContext,IQueryable<Person>> func) | |
| 381 { | |
| 382 TestOnePerson(exceptList, 1, "John", func); | |
| 383 } | |
| 384 | |
| 385 protected void TestOneJohn(Func<ITestDataContext,IQueryable<Person>> func) | |
| 386 { | |
| 387 TestOnePerson(Array<string>.Empty, 1, "John", func); | |
| 388 } | |
| 389 | |
| 390 private List<LinqDataTypes> _types; | |
| 391 protected IEnumerable<LinqDataTypes> Types | |
| 392 { | |
| 393 get | |
| 394 { | |
| 395 if (_types == null) | |
| 396 using (var db = new TestDbManager()) | |
| 397 _types = db.Types.ToList(); | |
| 398 | |
| 399 foreach (var type in _types) | |
| 400 yield return type; | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 private List<LinqDataTypes2> _types2; | |
| 405 protected List<LinqDataTypes2> Types2 | |
| 406 { | |
| 407 get | |
| 408 { | |
| 409 if (_types2 == null) | |
| 410 using (var db = new TestDbManager()) | |
| 411 _types2 = db.Types2.ToList(); | |
| 412 return _types2; | |
| 413 } | |
| 414 } | |
| 415 | |
| 416 private List<Person> _person; | |
| 417 protected IEnumerable<Person> Person | |
| 418 { | |
| 419 get | |
| 420 { | |
| 421 if (_person == null) | |
| 422 { | |
| 423 using (var db = new TestDbManager()) | |
| 424 _person = db.Person.ToList(); | |
| 425 | |
| 426 foreach (var p in _person) | |
| 427 p.Patient = Patient.SingleOrDefault(ps => p.ID == ps.PersonID); | |
| 428 } | |
| 429 | |
| 430 foreach (var item in _person) | |
| 431 yield return item; | |
| 432 } | |
| 433 } | |
| 434 | |
| 435 private List<Patient> _patient; | |
| 436 protected List<Patient> Patient | |
| 437 { | |
| 438 get | |
| 439 { | |
| 440 if (_patient == null) | |
| 441 { | |
| 442 using (var db = new TestDbManager()) | |
| 443 _patient = db.Patient.ToList(); | |
| 444 | |
| 445 foreach (var p in _patient) | |
| 446 p.Person = Person.Single(ps => ps.ID == p.PersonID); | |
| 447 } | |
| 448 | |
| 449 return _patient; | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 private List<Doctor> _doctor; | |
| 454 protected List<Doctor> Doctor | |
| 455 { | |
| 456 get | |
| 457 { | |
| 458 if (_doctor == null) | |
| 459 { | |
| 460 using (var db = new TestDbManager()) | |
| 461 _doctor = db.Doctor.ToList(); | |
| 462 } | |
| 463 | |
| 464 return _doctor; | |
| 465 } | |
| 466 } | |
| 467 | |
| 468 #region Parent/Child Model | |
| 469 | |
| 470 private List<Parent> _parent; | |
| 471 protected IEnumerable<Parent> Parent | |
| 472 { | |
| 473 get | |
| 474 { | |
| 475 if (_parent == null) | |
| 476 using (var db = new TestDbManager()) | |
| 477 { | |
| 478 db.Parent.Delete(c => c.ParentID >= 1000); | |
| 479 _parent = db.Parent.ToList(); | |
| 480 db.Close(); | |
| 481 | |
| 482 foreach (var p in _parent) | |
| 483 { | |
| 484 p.Children = Child. Where(c => c.ParentID == p.ParentID).ToList(); | |
| 485 p.GrandChildren = GrandChild.Where(c => c.ParentID == p.ParentID).ToList(); | |
| 486 p.Types = Types.First(t => t.ID == p.ParentID); | |
| 487 } | |
| 488 } | |
| 489 | |
| 490 foreach (var parent in _parent) | |
| 491 yield return parent; | |
| 492 } | |
| 493 } | |
| 494 | |
| 495 private List<Parent1> _parent1; | |
| 496 protected IEnumerable<Parent1> Parent1 | |
| 497 { | |
| 498 get | |
| 499 { | |
| 500 if (_parent1 == null) | |
| 501 _parent1 = Parent.Select(p => new Parent1 { ParentID = p.ParentID, Value1 = p.Value1 }).ToList(); | |
| 502 | |
| 503 foreach (var parent in _parent1) | |
| 504 yield return parent; | |
| 505 } | |
| 506 } | |
| 507 | |
| 508 private List<Parent4> _parent4; | |
| 509 protected List<Parent4> Parent4 | |
| 510 { | |
| 511 get | |
| 512 { | |
| 513 return _parent4 ?? (_parent4 = Parent.Select(p => new Parent4 { ParentID = p.ParentID, Value1 = Map.ToEnum<TypeValue>(p.Value1) }).ToList()); | |
| 514 } | |
| 515 } | |
| 516 | |
| 517 private List<Parent5> _parent5; | |
| 518 protected List<Parent5> Parent5 | |
| 519 { | |
| 520 get | |
| 521 { | |
| 522 if (_parent5 == null) | |
| 523 { | |
| 524 _parent5 = Parent.Select(p => new Parent5 { ParentID = p.ParentID, Value1 = p.Value1}).ToList(); | |
| 525 | |
| 526 foreach (var p in _parent5) | |
| 527 p.Children = _parent5.Where(c => c.Value1 == p.ParentID).ToList(); | |
| 528 } | |
| 529 | |
| 530 return _parent5; | |
| 531 } | |
| 532 } | |
| 533 | |
| 534 private List<ParentInheritanceBase> _parentInheritance; | |
| 535 protected IEnumerable<ParentInheritanceBase> ParentInheritance | |
| 536 { | |
| 537 get | |
| 538 { | |
| 539 if (_parentInheritance == null) | |
| 540 _parentInheritance = Parent.Select(p => | |
| 541 p.Value1 == null ? new ParentInheritanceNull { ParentID = p.ParentID } : | |
| 542 p.Value1.Value == 1 ? new ParentInheritance1 { ParentID = p.ParentID, Value1 = p.Value1.Value } : | |
| 543 (ParentInheritanceBase) new ParentInheritanceValue { ParentID = p.ParentID, Value1 = p.Value1.Value } | |
| 544 ).ToList(); | |
| 545 | |
| 546 foreach (var item in _parentInheritance) | |
| 547 yield return item; | |
| 548 } | |
| 549 } | |
| 550 | |
| 551 private List<ParentInheritanceValue> _parentInheritanceValue; | |
| 552 protected List<ParentInheritanceValue> ParentInheritanceValue | |
| 553 { | |
| 554 get | |
| 555 { | |
| 556 return _parentInheritanceValue ?? (_parentInheritanceValue = | |
| 557 ParentInheritance.Where(p => p is ParentInheritanceValue).Cast<ParentInheritanceValue>().ToList()); | |
| 558 } | |
| 559 } | |
| 560 | |
| 561 private List<ParentInheritance1> _parentInheritance1; | |
| 562 protected List<ParentInheritance1> ParentInheritance1 | |
| 563 { | |
| 564 get | |
| 565 { | |
| 566 return _parentInheritance1 ?? (_parentInheritance1 = | |
| 567 ParentInheritance.Where(p => p is ParentInheritance1).Cast<ParentInheritance1>().ToList()); | |
| 568 } | |
| 569 } | |
| 570 | |
| 571 private List<ParentInheritanceBase4> _parentInheritance4; | |
| 572 protected List<ParentInheritanceBase4> ParentInheritance4 | |
| 573 { | |
| 574 get | |
| 575 { | |
| 576 return _parentInheritance4 ?? (_parentInheritance4 = Parent | |
| 577 .Where(p => p.Value1.HasValue && (new[] { 1, 2 }.Contains(p.Value1.Value))) | |
| 578 .Select(p => p.Value1 == 1 ? | |
| 579 (ParentInheritanceBase4)new ParentInheritance14 { ParentID = p.ParentID } : | |
| 580 (ParentInheritanceBase4)new ParentInheritance24 { ParentID = p.ParentID } | |
| 581 ).ToList()); | |
| 582 } | |
| 583 } | |
| 584 | |
| 585 private List<Child> _child; | |
| 586 protected IEnumerable<Child> Child | |
| 587 { | |
| 588 get | |
| 589 { | |
| 590 if (_child == null) | |
| 591 using (var db = new TestDbManager()) | |
| 592 { | |
| 593 db.Child.Delete(c => c.ParentID >= 1000); | |
| 594 _child = db.Child.ToList(); | |
| 595 db.Clone(); | |
| 596 | |
| 597 foreach (var ch in _child) | |
| 598 { | |
| 599 ch.Parent = Parent. Single(p => p.ParentID == ch.ParentID); | |
| 600 ch.Parent1 = Parent1.Single(p => p.ParentID == ch.ParentID); | |
| 601 ch.ParentID2 = new Parent3 { ParentID2 = ch.Parent.ParentID, Value1 = ch.Parent.Value1 }; | |
| 602 ch.GrandChildren = GrandChild.Where(c => c.ParentID == ch.ParentID && c.ChildID == ch.ChildID).ToList(); | |
| 603 } | |
| 604 } | |
| 605 | |
| 606 foreach (var child in _child) | |
| 607 yield return child; | |
| 608 } | |
| 609 } | |
| 610 | |
| 611 private List<GrandChild> _grandChild; | |
| 612 protected IEnumerable<GrandChild> GrandChild | |
| 613 { | |
| 614 get | |
| 615 { | |
| 616 if (_grandChild == null) | |
| 617 using (var db = new TestDbManager()) | |
| 618 { | |
| 619 _grandChild = db.GrandChild.ToList(); | |
| 620 db.Close(); | |
| 621 | |
| 622 foreach (var ch in _grandChild) | |
| 623 ch.Child = Child.Single(c => c.ParentID == ch.ParentID && c.ChildID == ch.ChildID); | |
| 624 } | |
| 625 | |
| 626 foreach (var grandChild in _grandChild) | |
| 627 yield return grandChild; | |
| 628 } | |
| 629 } | |
| 630 | |
| 631 private List<GrandChild1> _grandChild1; | |
| 632 protected IEnumerable<GrandChild1> GrandChild1 | |
| 633 { | |
| 634 get | |
| 635 { | |
| 636 if (_grandChild1 == null) | |
| 637 using (var db = new TestDbManager()) | |
| 638 { | |
| 639 _grandChild1 = db.GrandChild1.ToList(); | |
| 640 | |
| 641 foreach (var ch in _grandChild1) | |
| 642 { | |
| 643 ch.Parent = Parent1.Single(p => p.ParentID == ch.ParentID); | |
| 644 ch.Child = Child. Single(c => c.ParentID == ch.ParentID && c.ChildID == ch.ChildID); | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 foreach (var grandChild in _grandChild1) | |
| 649 yield return grandChild; | |
| 650 } | |
| 651 } | |
| 652 | |
| 653 #endregion | |
| 654 | |
| 655 #region Northwind | |
| 656 | |
| 657 private List<Northwind.Category> _category; | |
| 658 public List<Northwind.Category> Category | |
| 659 { | |
| 660 get | |
| 661 { | |
| 662 if (_category == null) | |
| 663 using (var db = new NorthwindDB()) | |
| 664 _category = db.Category.ToList(); | |
| 665 return _category; | |
| 666 } | |
| 667 } | |
| 668 | |
| 669 private List<Northwind.Customer> _customer; | |
| 670 public List<Northwind.Customer> Customer | |
| 671 { | |
| 672 get | |
| 673 { | |
| 674 if (_customer == null) | |
| 675 { | |
| 676 using (var db = new NorthwindDB()) | |
| 677 _customer = db.Customer.ToList(); | |
| 678 | |
| 679 foreach (var c in _customer) | |
| 680 c.Orders = (from o in Order where o.CustomerID == c.CustomerID select o).ToList(); | |
| 681 } | |
| 682 | |
| 683 return _customer; | |
| 684 } | |
| 685 } | |
| 686 | |
| 687 private List<Northwind.Employee> _employee; | |
| 688 public List<Northwind.Employee> Employee | |
| 689 { | |
| 690 get | |
| 691 { | |
| 692 if (_employee == null) | |
| 693 { | |
| 694 using (var db = new NorthwindDB()) | |
| 695 { | |
| 696 _employee = db.Employee.ToList(); | |
| 697 | |
| 698 foreach (var employee in _employee) | |
| 699 { | |
| 700 employee.Employees = (from e in _employee where e.ReportsTo == employee.EmployeeID select e).ToList(); | |
| 701 employee.ReportsToEmployee = (from e in _employee where e.EmployeeID == employee.ReportsTo select e).SingleOrDefault(); | |
| 702 } | |
| 703 } | |
| 704 } | |
| 705 | |
| 706 return _employee; | |
| 707 } | |
| 708 } | |
| 709 | |
| 710 private List<Northwind.EmployeeTerritory> _employeeTerritory; | |
| 711 public List<Northwind.EmployeeTerritory> EmployeeTerritory | |
| 712 { | |
| 713 get | |
| 714 { | |
| 715 if (_employeeTerritory == null) | |
| 716 using (var db = new NorthwindDB()) | |
| 717 _employeeTerritory = db.EmployeeTerritory.ToList(); | |
| 718 return _employeeTerritory; | |
| 719 } | |
| 720 } | |
| 721 | |
| 722 private List<Northwind.OrderDetail> _orderDetail; | |
| 723 public List<Northwind.OrderDetail> OrderDetail | |
| 724 { | |
| 725 get | |
| 726 { | |
| 727 if (_orderDetail == null) | |
| 728 using (var db = new NorthwindDB()) | |
| 729 _orderDetail = db.OrderDetail.ToList(); | |
| 730 return _orderDetail; | |
| 731 } | |
| 732 } | |
| 733 | |
| 734 private List<Northwind.Order> _order; | |
| 735 public List<Northwind.Order> Order | |
| 736 { | |
| 737 get | |
| 738 { | |
| 739 if (_order == null) | |
| 740 { | |
| 741 using (var db = new NorthwindDB()) | |
| 742 _order = db.Order.ToList(); | |
| 743 | |
| 744 foreach (var o in _order) | |
| 745 { | |
| 746 o.Customer = Customer.Single(c => o.CustomerID == c.CustomerID); | |
| 747 o.Employee = Employee.Single(e => o.EmployeeID == e.EmployeeID); | |
| 748 } | |
| 749 } | |
| 750 | |
| 751 return _order; | |
| 752 } | |
| 753 } | |
| 754 | |
| 755 private IEnumerable<Northwind.Product> _product; | |
| 756 public IEnumerable<Northwind.Product> Product | |
| 757 { | |
| 758 get | |
| 759 { | |
| 760 if (_product == null) | |
| 761 using (var db = new NorthwindDB()) | |
| 762 _product = db.Product.ToList(); | |
| 763 | |
| 764 foreach (var product in _product) | |
| 765 yield return product; | |
| 766 } | |
| 767 } | |
| 768 | |
| 769 private List<Northwind.ActiveProduct> _activeProduct; | |
| 770 public List<Northwind.ActiveProduct> ActiveProduct | |
| 771 { | |
| 772 get { return _activeProduct ?? (_activeProduct = Product.OfType<Northwind.ActiveProduct>().ToList()); } | |
| 773 } | |
| 774 | |
| 775 public IEnumerable<Northwind.DiscontinuedProduct> DiscontinuedProduct | |
| 776 { | |
| 777 get { return Product.OfType<Northwind.DiscontinuedProduct>(); } | |
| 778 } | |
| 779 | |
| 780 private List<Northwind.Region> _region; | |
| 781 public List<Northwind.Region> Region | |
| 782 { | |
| 783 get | |
| 784 { | |
| 785 if (_region == null) | |
| 786 using (var db = new NorthwindDB()) | |
| 787 _region = db.Region.ToList(); | |
| 788 return _region; | |
| 789 } | |
| 790 } | |
| 791 | |
| 792 private List<Northwind.Shipper> _shipper; | |
| 793 public List<Northwind.Shipper> Shipper | |
| 794 { | |
| 795 get | |
| 796 { | |
| 797 if (_shipper == null) | |
| 798 using (var db = new NorthwindDB()) | |
| 799 _shipper = db.Shipper.ToList(); | |
| 800 return _shipper; | |
| 801 } | |
| 802 } | |
| 803 | |
| 804 private List<Northwind.Supplier> _supplier; | |
| 805 public List<Northwind.Supplier> Supplier | |
| 806 { | |
| 807 get | |
| 808 { | |
| 809 if (_supplier == null) | |
| 810 using (var db = new NorthwindDB()) | |
| 811 _supplier = db.Supplier.ToList(); | |
| 812 return _supplier; | |
| 813 } | |
| 814 } | |
| 815 | |
| 816 private List<Northwind.Territory> _territory; | |
| 817 public List<Northwind.Territory> Territory | |
| 818 { | |
| 819 get | |
| 820 { | |
| 821 if (_territory == null) | |
| 822 using (var db = new NorthwindDB()) | |
| 823 _territory = db.Territory.ToList(); | |
| 824 return _territory; | |
| 825 } | |
| 826 } | |
| 827 | |
| 828 #endregion | |
| 829 | |
| 830 protected void AreEqual<T>(IEnumerable<T> expected, IEnumerable<T> result) | |
| 831 { | |
| 832 var resultList = result. ToList(); | |
| 833 var expectedList = expected.ToList(); | |
| 834 | |
| 835 Assert.AreNotEqual(0, expectedList.Count); | |
| 836 Assert.AreEqual(expectedList.Count, resultList.Count, "Expected and result lists are different. Lenght: "); | |
| 837 | |
| 838 var exceptExpectedList = resultList. Except(expectedList).ToList(); | |
| 839 var exceptResultList = expectedList.Except(resultList). ToList(); | |
| 840 | |
| 841 var exceptExpected = exceptExpectedList.Count; | |
| 842 var exceptResult = exceptResultList. Count; | |
| 843 | |
| 844 if (exceptResult != 0 || exceptExpected != 0) | |
| 845 for (var i = 0; i < resultList.Count; i++) | |
| 846 Debug.WriteLine("{0} {1} --- {2}", Equals(expectedList[i], resultList[i]) ? " " : "-", expectedList[i], resultList[i]); | |
| 847 | |
| 848 Assert.AreEqual(0, exceptExpected); | |
| 849 Assert.AreEqual(0, exceptResult); | |
| 850 } | |
| 851 | |
| 852 protected void AreEqual<T>(IEnumerable<IEnumerable<T>> expected, IEnumerable<IEnumerable<T>> result) | |
| 853 { | |
| 854 var resultList = result. ToList(); | |
| 855 var expectedList = expected.ToList(); | |
| 856 | |
| 857 Assert.AreNotEqual(0, expectedList.Count); | |
| 858 Assert.AreEqual(expectedList.Count, resultList.Count, "Expected and result lists are different. Lenght: "); | |
| 859 | |
| 860 for (var i = 0; i < resultList.Count; i++) | |
| 861 { | |
| 862 var elist = expectedList[i].ToList(); | |
| 863 var rlist = resultList [i].ToList(); | |
| 864 | |
| 865 if (elist.Count > 0 || rlist.Count > 0) | |
| 866 AreEqual(elist, rlist); | |
| 867 } | |
| 868 } | |
| 869 | |
| 870 protected void AreSame<T>(IEnumerable<T> expected, IEnumerable<T> result) | |
| 871 { | |
| 872 var resultList = result. ToList(); | |
| 873 var expectedList = expected.ToList(); | |
| 874 | |
| 875 Assert.AreNotEqual(0, expectedList.Count); | |
| 876 Assert.AreEqual(expectedList.Count, resultList.Count); | |
| 877 | |
| 878 var b = expectedList.SequenceEqual(resultList); | |
| 879 | |
| 880 if (!b) | |
| 881 for (var i = 0; i < resultList.Count; i++) | |
| 882 Debug.WriteLine(string.Format("{0} {1} --- {2}", Equals(expectedList[i], resultList[i]) ? " " : "-", expectedList[i], resultList[i])); | |
| 883 | |
| 884 Assert.IsTrue(b); | |
| 885 } | |
| 886 | |
| 887 protected void CompareSql(string result, string expected) | |
| 888 { | |
| 889 var ss = expected.Trim('\r', '\n').Split('\n'); | |
| 890 | |
| 891 while (ss.All(_ => _.Length > 0 && _[0] == '\t')) | |
| 892 for (var i = 0; i < ss.Length; i++) | |
| 893 ss[i] = ss[i].Substring(1); | |
| 894 | |
| 895 Assert.AreEqual(string.Join("\n", ss), result.Trim('\r', '\n')); | |
| 896 } | |
| 897 } | |
| 898 } |
