PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/LLBL Pro v4.2/AW.Test/NorthwindTest.cs

#
C# | 591 lines | 413 code | 79 blank | 99 comment | 22 complexity | 112beb343f403aa1cbf17691ecbe92e5 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MIT, GPL-2.0, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Dynamic;
  5. using AW.Helper.LLBL;
  6. using AW.Test.Helpers;
  7. using FluentAssertions;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Northwind.DAL;
  10. using Northwind.DAL.DTO;
  11. using Northwind.DAL.EntityClasses;
  12. using Northwind.DAL.Linq;
  13. using Northwind.DAL.Linq.Filters;
  14. using Northwind.DAL.SqlServer;
  15. using SD.LLBLGen.Pro.LinqSupportClasses;
  16. using SD.LLBLGen.Pro.ORMSupportClasses;
  17. namespace AW.Tests
  18. {
  19. public class GroupByYear
  20. {
  21. public int Year { get; set; }
  22. public int Month { get; set; }
  23. }
  24. public class CountryRegion : KeyCount
  25. {
  26. public string ShipCountry { get; set; }
  27. public string ShipRegion { get; set; }
  28. }
  29. public class KeyCount
  30. {
  31. public object Key { get; set; }
  32. public int Count { get; set; }
  33. }
  34. /// <summary>
  35. /// Summary description for NorthwindTest
  36. /// </summary>
  37. [TestClass]
  38. public class NorthwindTest
  39. {
  40. /// <summary>
  41. /// Gets or sets the test context which provides information about and functionality for the current test run.
  42. /// </summary>
  43. public TestContext TestContext { get; set; }
  44. #region Additional test attributes
  45. //
  46. // You can use the following additional attributes as you write your tests:
  47. //
  48. // Use ClassInitialize to run code before running the first test in the class
  49. // [ClassInitialize()]
  50. // public static void MyClassInitialize(TestContext testContext) { }
  51. //
  52. // Use ClassCleanup to run code after all tests in a class have run
  53. // [ClassCleanup()]
  54. // public static void MyClassCleanup() { }
  55. //
  56. // Use TestInitialize to run code before running each test
  57. // [TestInitialize()]
  58. // public void MyTestInitialize() { }
  59. //
  60. // Use TestCleanup to run code after each test has run
  61. // [TestCleanup()]
  62. // public void MyTestCleanup() { }
  63. //
  64. #endregion
  65. public static LinqMetaData GetNorthwindLinqMetaData()
  66. {
  67. return new LinqMetaData(new DataAccessAdapter(), DataAccessAdapter.StaticCustomFunctionMappings);
  68. }
  69. [TestMethod, Description("tests whether you can have a typed null field in a anonymous projection")]
  70. public void CustomerAnonProjection()
  71. {
  72. var queryable = from c in GetNorthwindLinqMetaData().Customer
  73. select new {c.Address, c.City, CompanyName = (string) null, c.ContactName, c.ContactTitle, c.Country, c.CustomerId, c.Fax, c.Phone, c.PostalCode, c.Region};
  74. Assert.IsNotNull(queryable.ToList());
  75. }
  76. [TestMethod, Description("tests whether you can pass a null parameter to a constructor in a projection")]
  77. public void CustomerVMProjection()
  78. {
  79. var queryable = from c in GetNorthwindLinqMetaData().Customer
  80. select new CustomerVM(c.Address, c.City, null, c.ContactName, c.ContactTitle, c.Country, c.CustomerId, c.Fax, c.Phone, c.PostalCode, c.Region);
  81. Assert.IsNotNull(queryable.ToList());
  82. }
  83. [TestMethod, Description("tests whether you can OrderBy after a Projection with Entity Instance")]
  84. public void CustomerVMProjectionEntityInstance()
  85. {
  86. var queryable = CustomerVM.CustomerVmFactoryEntityInstance(GetNorthwindLinqMetaData().Customer).OrderBy(c => c.City);
  87. Assert.IsNotNull(queryable.ToList());
  88. }
  89. [TestMethod, Description("tests whether you can OrderBy after a Projection with Entity Instance")]
  90. public void CustomerVMProjectionConstructor()
  91. {
  92. var queryable = CustomerVM.CustomerVmFactoryConstructor(GetNorthwindLinqMetaData().Customer).OrderBy(c => c.City);
  93. Assert.IsNotNull(queryable.ToList());
  94. }
  95. /// <summary>
  96. /// <see cref="http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=19256" />
  97. /// </summary>
  98. [TestProperty("Bug", "Fixed"), TestMethod, Description("tests whether you Left Join from Customer to CustomerDemographic")]
  99. public void CustomerLeftJoinCustomerDemographic()
  100. {
  101. var queryable = from c in GetNorthwindLinqMetaData().Customer
  102. from ccd in c.CustomerCustomerDemos.DefaultIfEmpty()
  103. select new {c.ContactName, ccd.CustomerId, ccd.CustomerDemographic.CustomerDesc};
  104. var result = queryable.ToList();
  105. Assert.AreEqual(1, result.Count);
  106. }
  107. /// <summary>
  108. /// <see cref="http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=19256" />
  109. /// </summary>
  110. [TestMethod, Description("tests whether you Left Join from Customer to CustomerDemographic")]
  111. public void CustomerExplicitLeftJoinCustomerDemographic()
  112. {
  113. var queryable = from c in GetNorthwindLinqMetaData().Customer
  114. from ccd in c.CustomerCustomerDemos.DefaultIfEmpty()
  115. join cd in GetNorthwindLinqMetaData().CustomerDemographic on ccd.CustomerTypeId equals cd.CustomerTypeId into customerDemographics
  116. from cd in customerDemographics.DefaultIfEmpty()
  117. select new {c.ContactName, ccd.CustomerId, cd.CustomerDesc};
  118. var result = queryable.ToList();
  119. Assert.AreEqual(91, result.Count);
  120. }
  121. /// <summary>
  122. /// <see cref="http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=19256" />
  123. /// </summary>
  124. [TestProperty("Bug", "Fixed"), TestMethod, Description("tests whether you Left Join from Customer to CustomerDemographic")]
  125. public void CustomerLeftJoinCustomerDemographicViaMany()
  126. {
  127. var queryable = from c in GetNorthwindLinqMetaData().Customer
  128. from cd in c.CustomerDemographics.DefaultIfEmpty()
  129. select new {c.ContactName, c.CustomerId, cd.CustomerDesc};
  130. var result = queryable.ToList();
  131. Assert.AreEqual(1, result.Count);
  132. }
  133. /// <summary>
  134. /// <see cref="http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=19256" />
  135. /// </summary>
  136. [TestMethod, Description("tests whether you Left Join from Customer to CustomerDemographic")]
  137. public void CustomerLeftJoinCustomerDemographicLinqToObject()
  138. {
  139. var cus = GetNorthwindLinqMetaData().Customer.PrefetchCustomerCustomerDemosCustomerDemographic().ToEntityCollection2();
  140. var cusproj = from c in cus
  141. from ccd in c.CustomerCustomerDemos.DefaultIfEmpty()
  142. select
  143. new
  144. {
  145. c.ContactName,
  146. CustomerId = ccd == null ? null : ccd.CustomerId,
  147. CustomerDesc = ccd == null ? null : ccd.CustomerDemographic.CustomerDesc
  148. };
  149. Assert.AreEqual(91, cusproj.Count());
  150. }
  151. /// <summary>
  152. /// Tests whether a second m:n prefetch results in duplicate
  153. /// </summary>
  154. [TestMethod]
  155. public void ManyToManyPrefetchContextBugTest()
  156. {
  157. AssertOneCustomerDemographicAfterSecondPrefetch(null); //Passes
  158. AssertOneCustomerDemographicAfterSecondPrefetch(new Context()); //Fails
  159. }
  160. /// <summary>
  161. /// Tests that there is only one CustomerDemographic after a second prefetch CustomerCustomerDemo Table has 1 row: ALFKI
  162. /// 1 CustomerDemographic Table has 1 row: xxx 1
  163. /// </summary>
  164. /// <param name="contextToUse"> The context to use. </param>
  165. private static void AssertOneCustomerDemographicAfterSecondPrefetch(Context contextToUse)
  166. {
  167. var northwindLinqMetaData = GetNorthwindLinqMetaData();
  168. northwindLinqMetaData.ContextToUse = contextToUse;
  169. const string alfki = "ALFKI";
  170. if (!northwindLinqMetaData.CustomerDemographic.Any())
  171. {
  172. var customerDemographicEntity = new CustomerDemographicEntity("1") {CustomerDesc = "CustomerDesc"};
  173. var customerCustomerDemos = customerDemographicEntity.CustomerCustomerDemos.AddNew();
  174. customerCustomerDemos.CustomerId = alfki;
  175. northwindLinqMetaData.AdapterToUse.SaveEntity(customerDemographicEntity);
  176. }
  177. AssertOneCustomerDemographicAfterPrefetch(northwindLinqMetaData, alfki);
  178. AssertOneCustomerDemographicAfterPrefetch(northwindLinqMetaData, alfki);
  179. }
  180. private static void AssertOneCustomerDemographicAfterPrefetch(LinqMetaData northwindLinqMetaData, string alfki)
  181. {
  182. var cus = northwindLinqMetaData.Customer.PrefetchCustomerDemographics().Single(c => c.CustomerId == alfki);
  183. Assert.AreEqual(alfki, cus.CustomerId);
  184. Assert.AreEqual(1, cus.CustomerDemographics.Count());
  185. if (northwindLinqMetaData.ContextToUse != null)
  186. northwindLinqMetaData.ContextToUse.Add(cus.CustomerDemographics);
  187. }
  188. [TestMethod]
  189. public void PrefetchWithContext()
  190. {
  191. var metaData = GetNorthwindLinqMetaData();
  192. metaData.ContextToUse = new Context();
  193. const string customerToSearch = "ALFKI";
  194. // first time
  195. var customer = (from c in metaData.Customer
  196. where c.CustomerId == customerToSearch
  197. select c)
  198. .PrefetchEmployeesViaOrders()
  199. .Single();
  200. Assert.AreEqual(customerToSearch, customer.CustomerId);
  201. var employeesCountToTest = customer.EmployeesViaOrders.Count;
  202. // add the related collection to the context
  203. metaData.ContextToUse.Add(customer.EmployeesViaOrders);
  204. //Assert.AreEqual(1, customer.EmployeesViaOrders.First().CustomersViaOrders);
  205. // second time
  206. customer = (from c in metaData.Customer
  207. where c.CustomerId == customerToSearch
  208. select c)
  209. .PrefetchEmployeesViaOrders()
  210. .Single();
  211. Assert.AreEqual(customerToSearch, customer.CustomerId);
  212. Assert.AreEqual(employeesCountToTest, customer.EmployeesViaOrders.Count);
  213. }
  214. /// <summary>
  215. /// http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=19954
  216. /// </summary>
  217. [TestProperty("Bug", "ByDesign"), TestCategory("Failing"), TestMethod,
  218. Description("After a prefetch of a ManyToMany relationship can I navigate to an entity at the end of that relationship then navigate back to the root entity")]
  219. public void BiDirectionalManyToMany()
  220. {
  221. var metaData = GetNorthwindLinqMetaData();
  222. var customer = metaData.Customer.PrefetchEmployeesViaOrders().First();
  223. Assert.AreNotEqual(0, customer.EmployeesViaOrders.Count);
  224. Assert.AreEqual(1, customer.EmployeesViaOrders.First().CustomersViaOrders.Count); //Fails
  225. }
  226. [TestMethod, Description("After a prefetch of a 1:n intermediate m:1 relationship can I navigate to an entity at the end of that relationship then navigate back to the root entity")]
  227. public void BiDirectionalManyToManyInCode()
  228. {
  229. var metaData = GetNorthwindLinqMetaData();
  230. var customer = metaData.Customer.PrefetchOrdersEmployee().First();
  231. Assert.AreNotEqual(0, customer.Orders.Count);
  232. Assert.AreNotEqual(0, customer.EmployeesViaOrdersInCode.Count());
  233. Assert.AreEqual(1, customer.EmployeesViaOrdersInCode.First().CustomersViaOrdersInCode.Count());
  234. Assert.AreEqual(customer, customer.EmployeesViaOrdersInCode.First().CustomersViaOrdersInCode.Single());
  235. }
  236. [TestMethod]
  237. public void BiDirectionalOneToMany()
  238. {
  239. var metaData = GetNorthwindLinqMetaData();
  240. var customerEntities = metaData.Customer;
  241. var customer = customerEntities.PrefetchOrders().First();
  242. Assert.AreNotEqual(0, customer.Orders.Count);
  243. Assert.AreEqual(customer, customer.Orders.First().Customer);
  244. }
  245. /// <summary>
  246. /// http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=20595
  247. /// </summary>
  248. [TestMethod, Description("SQL bind exception with two Where predicates with the same Entity")]
  249. public void SQLBindExceptionWithTwoWherePredicatesWithTheSameEntity()
  250. {
  251. var metaData = GetNorthwindLinqMetaData();
  252. var employees = from e in metaData.Employee
  253. where e.Orders.Any(o => o.ShipCity == "Reims") || e.Orders.Any(o => o.ShipCity == "Lyon")
  254. select e;
  255. const int expected = 7;
  256. employees.ToEntityCollection2().Count.Should().BeInRange(6, expected);
  257. employees.Count().Should().BeInRange(6, expected);
  258. employees = from e in metaData.Employee
  259. from order in e.Orders
  260. from et in e.EmployeeTerritories
  261. where e.Orders.Any(o => o.ShipCity == "Reims") || e.Orders.Any(o => o.ShipCity == "Lyon")
  262. select e;
  263. employees.ToEntityCollection2().Count.Should().BeInRange(6, expected);
  264. employees.CountColumn(e => e.EmployeeId, true).Should().BeInRange(6, expected);
  265. // This one throws 'The multi-part identifier "LPLA_4.EmployeeID" could not be bound.'
  266. employees = from e in metaData.Employee
  267. from et in e.EmployeeTerritories
  268. where e.Orders.Any(o => o.ShipCity == "Reims") || e.Orders.Any(o => o.ShipCity == "Lyon")
  269. select e;
  270. employees.ToEntityCollection2().Count.Should().BeInRange(6, expected);
  271. employees.CountColumn(e => e.EmployeeId, true).Should().BeInRange(6, expected);
  272. }
  273. [TestMethod, TestProperty("Bug", "Fixed")]
  274. public void PrefetchBeforeCriteria()
  275. {
  276. var metaData = GetNorthwindLinqMetaData();
  277. //var productEntities = (from c in metaData.Product.PrefetchOrderDetailOrderCustomer()
  278. // where c.Supplier.City == "xx" && c.UnitPrice == 100
  279. // select c);
  280. metaData.Customer.FilterByCountry("NZ").FilterByEmployeeId(100).EmptySelect().PrefetchCustomerDemographics().ToEntityCollection2();
  281. // metaData.Customer.FilterByCountry("NZ").FilterByEmployeeId(100).EmptySelect().CountColumn(c=>c.CustomerId);
  282. metaData.Customer.PrefetchCustomerDemographics().FilterByEmployeeId(100).FilterByCountry("NZ").ToEntityCollection2();
  283. metaData.Customer.EmptySelect().PrefetchCustomerDemographics().FilterByEmployeeId(100).FilterByCountry("NZ").ToEntityCollection2();
  284. }
  285. /// <summary>
  286. /// Tests for filter by an interface.
  287. /// </summary>
  288. [TestMethod, Description("http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=22914")]
  289. public void TestFilterByDiscontinued()
  290. {
  291. var metaData = GetNorthwindLinqMetaData();
  292. Assert.IsTrue(metaData.Product.FilterByDiscontinued(true).Any());
  293. Assert.IsTrue(metaData.Product.FilterByDiscontinued(false).Any());
  294. }
  295. /// <summary>
  296. /// Tests for filter by an interface.
  297. /// </summary>
  298. [TestMethod, Description("http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=22914"), ExpectedException(typeof(ORMQueryConstructionException))]
  299. public void TestFilterByDiscontinuedI()
  300. {
  301. var metaData = GetNorthwindLinqMetaData();
  302. Assert.IsTrue(metaData.Product.FilterByDiscontinuedI(true).Any());
  303. }
  304. [TestMethod, Description("http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=22914"), ExpectedException(typeof(ORMQueryConstructionException))]
  305. public void TestFilterBySupplierIdI()
  306. {
  307. var metaData = GetNorthwindLinqMetaData();
  308. Assert.IsTrue(metaData.Product.FilterBySupplierIdI(1).ToList().Any()); //Still gives NullReferenceException
  309. }
  310. [TestMethod, TestProperty("Bug", "Fixed")]
  311. public void TestFilterByProductName()
  312. {
  313. var metaData = GetNorthwindLinqMetaData();
  314. metaData.Product.FilterByProductName("FilterByProductName").ToEntityCollection2();
  315. }
  316. [TestMethod, Description("tests whether you can OrderBy after a projection when the field has a different name")]
  317. public void TestProductViewDto()
  318. {
  319. var productEntities = GetNorthwindLinqMetaData().Product;
  320. var queryable = ProductViewDto.ProductViewDtoFactoryPropertyProjection(productEntities).FilterByDiscontinued(true).OrderBy(p => p.ReorderLevelZzz);
  321. Assert.IsNotNull(queryable.ToList());
  322. queryable = ProductViewDto.ProductViewDtoFactoryPropertiesViaConstructor(productEntities).FilterByDiscontinued(true).OrderBy(p => p.ReorderLevelZzz);
  323. Assert.IsNotNull(queryable.ToList());
  324. var pagedQueryable = ProductViewDto.ProductViewDtoFactoryEntityConstructor(productEntities).FilterByDiscontinued(true).OrderBy(p => p.UnitPrice).TakePage(2, 10);
  325. Assert.IsNotNull(pagedQueryable.ToList());
  326. }
  327. [TestMethod, Description("tests whether you can OrderBy after a projection")]
  328. public void TestEmployeeViewDto()
  329. {
  330. var employeeEntities = GetNorthwindLinqMetaData().Employee;
  331. var queryable = EmployeeViewDto.EmployeeViewDtoFactoryPropertyProjection(employeeEntities).OrderBy(e => e.FirstName);
  332. Assert.IsNotNull(queryable.ToList());
  333. queryable = EmployeeViewDto.EmployeeViewDtoFactoryPropertiesViaConstructor(employeeEntities).OrderBy(e => e.FirstName);
  334. Assert.IsNotNull(queryable.ToList());
  335. Assert.IsNotNull(EmployeeViewDto.EmployeeViewDtoFactoryEntityInstance(employeeEntities).OrderBy(e => e.FirstName).TakePage(2, 10).ToList());
  336. Assert.IsNotNull(EmployeeViewDto.EmployeeViewDtoFactoryPropertiesViaConstructor(employeeEntities.Where(e => e.City != "x").Where(e => e.City != "x")).OrderBy(e => e.FirstName).ToList());
  337. Assert.IsNotNull(EmployeeViewDto.EmployeeViewDtoFactoryEntityInstance(employeeEntities.EmptySelect().EmptySelect()).OrderBy(e => e.FirstName).TakePage(2, 10).ToList());
  338. }
  339. [TestMethod, Description("tests FilterByCustomerTypeId().FilterByManagersOrder(1)")]
  340. public void TestFilterByCustomerTypeIdFilterByManagersOrder()
  341. {
  342. var employeeEntities = GetNorthwindLinqMetaData().Employee;
  343. employeeEntities.FilterByCustomerTypeId("ALFKI").FilterByManagersOrder(1).ToEntityCollection2();
  344. // employeeEntities.FilterByCustomerTypeId("ALFKI").FilterByManagersOrder(1).CountColumn(e => e.EmployeeId);
  345. employeeEntities.FilterByCustomerTypeIdViaOrders("ALFKI").FilterByOrder(1).ToEntityCollection2();
  346. // employeeEntities.FilterByCustomerTypeIdViaOrders("ALFKI").FilterByOrder(1).CountColumn(e => e.EmployeeId);
  347. employeeEntities.FilterByOrder(1).FilterByCustomerTypeIdViaOrders("ALFKI").ToEntityCollection2();
  348. // employeeEntities.FilterByOrder(1).FilterByCustomerTypeIdViaOrders("ALFKI").CountColumn(e => e.EmployeeId);
  349. }
  350. [TestMethod, Description("http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=21560")]
  351. public void TestFirstInWhereAndProjection()
  352. {
  353. var linqMetaData = GetNorthwindLinqMetaData();
  354. var orders = from o in linqMetaData.Order
  355. select new
  356. {
  357. o.CustomerId,
  358. o.OrderId
  359. };
  360. var customers = from c in linqMetaData.Customer
  361. where c.CustomerId == orders.First().CustomerId
  362. select new
  363. {
  364. orders.First().OrderId,
  365. c.Address,
  366. c.City,
  367. c.CompanyName
  368. };
  369. customers.First();
  370. }
  371. [TestMethod, Description("http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=22376")]
  372. public void TestGroupByOrderDateYear()
  373. {
  374. var linqMetaData = GetNorthwindLinqMetaData();
  375. var ordersGroupedByYear = linqMetaData.Order.GroupBy(o => o.OrderDate.Value.Year);
  376. Assert.AreEqual(3, ordersGroupedByYear.Select(o => o.Key).Count());
  377. //Fails Assert.AreEqual(3, ordersGroupedByYear.Count());
  378. var ordersWithYear = from o in linqMetaData.Order
  379. select new {o.OrderDate.Value.Year, o};
  380. var ordersGroupedByYear2 = from o in ordersWithYear
  381. group o by o.Year
  382. into orderYear
  383. select orderYear;
  384. /// Assert.AreEqual(1, ordersGroupedByYear2.Count());
  385. }
  386. [TestMethod]
  387. public void TestGroupByOrderDatesNamedType()
  388. {
  389. var linqMetaData = GetNorthwindLinqMetaData();
  390. //IQueryable<IGrouping<object, OrderEntity>> ordersGroupedByYear = from o in linqMetaData.Order
  391. // group o by new {o.OrderDate.Value.Year, o.OrderDate.Value.Month}
  392. // into orderYear
  393. // select orderYear;
  394. IQueryable<IGrouping<object, OrderEntity>> ordersGroupedByYear = linqMetaData.Order.GroupBy(o => new {o.OrderDate.Value.Year, o.OrderDate.Value.Month});
  395. Assert.AreEqual(23, ordersGroupedByYear.Select(o => o.Key).Count());
  396. var ordersGroupedByYearNamed = linqMetaData.Order.GroupBy(o => new GroupByYear {Year = o.OrderDate.Value.Year, Month = o.OrderDate.Value.Month});
  397. Assert.AreEqual(23, ordersGroupedByYearNamed.Select(o => new {o.Key.Year, o.Key.Month, count = o.Count()}).Count());
  398. }
  399. [TestMethod]
  400. public void TestGroupByOrderNamedType()
  401. {
  402. var linqMetaData = GetNorthwindLinqMetaData();
  403. //var ordersGroupedByCountry = from o in linqMetaData.Order
  404. // group o by new CountryRegion {ShipCountry = o.ShipCountry, ShipRegion = o.ShipRegion}
  405. // into orderCountry
  406. // select orderCountry;
  407. var ordersGroupedByCountry = linqMetaData.Order.GroupBy(o => new CountryRegion {ShipCountry = o.ShipCountry, ShipRegion = o.ShipRegion});
  408. var ordersGroupedByCountryAnon = linqMetaData.Order.GroupBy(o => new {o.ShipCountry, o.ShipRegion});
  409. var ordersGroupedByCountryList = ordersGroupedByCountry.Select(o => new CountryRegion {ShipCountry = o.Key.ShipCountry, ShipRegion = o.Key.ShipRegion, Count = o.Count()}).ToList();
  410. Assert.AreEqual(35, ordersGroupedByCountryList.Count());
  411. var ordersGroupedByCountryList2Anon = ordersGroupedByCountryAnon.Select(o => new CountryRegion {ShipCountry = string.IsNullOrEmpty(o.Key.ShipCountry) ? "empty" : o.Key.ShipCountry}).ToList();
  412. Assert.AreEqual(35, ordersGroupedByCountryList2Anon.Count());
  413. var ordersGroupedByCountryList2 = ordersGroupedByCountry.Select(o => new CountryRegion {ShipCountry = string.IsNullOrEmpty(o.Key.ShipCountry) ? "empty" : o.Key.ShipCountry}).ToList();
  414. Assert.AreEqual(35, ordersGroupedByCountryList2.Count());
  415. var ordersGroupedByCountryKeyList = ordersGroupedByCountry.Select(o => new CountryRegion {Key = o.Key, Count = o.Count()}).ToList();
  416. Assert.AreEqual(35, ordersGroupedByCountryKeyList.Count());
  417. var ordersGroupedByCountryDynamic = (IQueryable<IGrouping<DynamicClass, OrderEntity>>) linqMetaData.Order.GroupBy("new(ShipCountry, ShipRegion)", "it");
  418. var countryRegions = new List<CountryRegion>();
  419. foreach (var grouping in ordersGroupedByCountryDynamic)
  420. {
  421. var countryRegion = new CountryRegion();
  422. dynamic countryRegionDyn = grouping.Key;
  423. countryRegion.ShipCountry = countryRegionDyn.ShipCountry;
  424. countryRegion.ShipRegion = countryRegionDyn.ShipRegion;
  425. countryRegions.Add(countryRegion);
  426. }
  427. var groupedByCountryWithCountDynamic = (IQueryable<DynamicClass>) ordersGroupedByCountryDynamic.Select("new(Key.ShipCountry, Key.ShipRegion, Count() as Count)");
  428. var groupedByCountryWithCountDynamicList = groupedByCountryWithCountDynamic.ToList();
  429. Assert.AreEqual(35, groupedByCountryWithCountDynamicList.Count());
  430. var groupedByCountryWithCount = ordersGroupedByCountryDynamic.Select(x => new {x.Key, Count = x.Count()});
  431. var groupedByCountryWithCountList = groupedByCountryWithCount.ToList();
  432. //Assert.AreEqual(35, groupedByCountryWithCountList.Count());
  433. //MapKeysToProperties(groupedByCountryWithCountList, MapKeyToProperties);
  434. }
  435. private static void MapKeysToProperties(IEnumerable<CountryRegion> dynamicClasses, Action<CountryRegion> keyMapper)
  436. {
  437. foreach (var dynamicClass in dynamicClasses)
  438. {
  439. keyMapper(dynamicClass);
  440. }
  441. }
  442. private static void MapKeyToProperties(CountryRegion dynamicClass)
  443. {
  444. dynamic countryRegionDyn = dynamicClass.Key;
  445. dynamicClass.ShipCountry = countryRegionDyn.ShipCountry;
  446. dynamicClass.Count = dynamicClass.Count;
  447. }
  448. [TestMethod]
  449. public void TestGroupByOrderDynamic()
  450. {
  451. var linqMetaData = GetNorthwindLinqMetaData();
  452. var ordersGroupedByCountry = from o in linqMetaData.Order
  453. group o by o.ShipCountry;
  454. Assert.AreEqual(21, ordersGroupedByCountry.Select(o => o.Key).Count());
  455. var ordersGroupedByCountryDynamic = (IQueryable<IGrouping<object, OrderEntity>>) linqMetaData.Order.GroupBy(OrderFieldIndex.ShipCountry.ToString(), "it");
  456. Assert.AreEqual(21, ordersGroupedByCountryDynamic.Select(o => o.Key).Count());
  457. // var ordersGroupedByCountryDynamic = linqMetaData.Order
  458. //.GroupBy(o => MetaDataHelper.GetPropertyValue(o, OrderFieldIndex.ShipCountry.ToString()))
  459. //.Select(orderCountry => orderCountry);
  460. // Assert.AreEqual(21, ordersGroupedByCountryDynamic.Select(o => o.Key).Count());
  461. }
  462. /// <summary>
  463. /// http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=23043
  464. /// </summary>
  465. [TestMethod, TestProperty("Bug", "Fixed"), Description("LINQ - Invalid SQL when CountColumn")]
  466. public void TestCountColumn()
  467. {
  468. GetNorthwindLinqMetaData().Customer.FilterByShipCountry("NZ").FilterByCountry("NZ").CountColumn(c => c.CustomerId);
  469. }
  470. /// <summary>
  471. /// http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=23043
  472. /// </summary>
  473. [TestMethod, TestProperty("Bug", "Fixed"), Description("LINQ - Invalid SQL when CountColumn")]
  474. public void TestAny()
  475. {
  476. var northwindLinqMetaData = GetNorthwindLinqMetaData();
  477. Assert.IsFalse(northwindLinqMetaData.Customer.FilterByShipCountry("NZ").FilterByCountry("NZ").Any());
  478. Assert.IsFalse(northwindLinqMetaData.Employee.FilterByOrders(2).FilterByManagersOrder(1).Any());
  479. // ReSharper disable once ReplaceWithSingleCallToAny
  480. Assert.IsFalse(northwindLinqMetaData.Order.FilterByProducts(2).Where(c => c.Customer.Country == "NZ").Any());
  481. }
  482. /// <summary>
  483. /// https://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=23273
  484. /// </summary>
  485. [TestMethod, TestCategory("Failing"), TestProperty("Bug", "UnFixed"), Description("LINQ - Invalid SQL with Linq on interface query join")]
  486. public void TestCrossJoinWithInterfaceQuery()
  487. {
  488. var northwindLinqMetaData = GetNorthwindLinqMetaData();
  489. // Assert.AreEqual(0, northwindLinqMetaData.Product.FilterByUnitsInStock(2).FilterByOrderQuery(northwindLinqMetaData.Order.Where(o => o.ShipCountry == "France")).Count());
  490. //Assert.AreEqual(53, northwindLinqMetaData.Order.FilterByIProductFullGenericQuery(northwindLinqMetaData.Product.FilterByUnitsInStock(10)).Count());
  491. Assert.AreEqual(29, northwindLinqMetaData.Supplier.FilterByIProductGenericJoinQuery(northwindLinqMetaData.Product).ToEntityCollection2().Count());
  492. Assert.AreEqual(29, northwindLinqMetaData.Supplier.FilterByIProductJoinQuery(northwindLinqMetaData.Product).ToList().Count());
  493. //Assert.AreEqual(53, northwindLinqMetaData.Order.FilterByIProductFullQuery(northwindLinqMetaData.Product).ToEntityCollection2().Count());
  494. }
  495. [TestMethod, Description("LINQ - Invalid SQL with Linq on interface query join")]
  496. public void TestSupplierCrossJoinProduct()
  497. {
  498. var northwindLinqMetaData = GetNorthwindLinqMetaData();
  499. Assert.AreEqual(29, northwindLinqMetaData.Supplier.FilterByIProductGenericQuery(northwindLinqMetaData.Product).ToEntityCollection2().Count());
  500. }
  501. [TestMethod, TestProperty("Bug", "UnFixed"), Description("Blowup filtering on 2 1:M related tables and a n:m table.")]
  502. //http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=23591
  503. public void TestEmployeeCrossJoinTerritory()
  504. {
  505. var northwindLinqMetaData = GetNorthwindLinqMetaData();
  506. Assert.AreEqual(0,
  507. northwindLinqMetaData.Employee.FilterByOrder(1)
  508. .Where(p => p.Staff.Any())
  509. .FilterByTerritories(northwindLinqMetaData.Territory.Where(t => t.RegionId != 1)).Count());
  510. }
  511. }
  512. }