/mcs/class/System.Data.Linq/src/DbLinq/Test/Providers/Linq_101_Samples/Count_Sum_Min_Max_Avg.cs

https://github.com/ccflo/mono · C# · 271 lines · 183 code · 28 blank · 60 comment · 29 complexity · 23bbbcb6f5d9386842f901a070bdfa61 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. using Test_NUnit;
  7. using nwind;
  8. // test ns Linq_101_Samples
  9. #if MYSQL
  10. namespace Test_NUnit_MySql.Linq_101_Samples
  11. #elif ORACLE && ODP
  12. namespace Test_NUnit_OracleODP.Linq_101_Samples
  13. #elif ORACLE
  14. namespace Test_NUnit_Oracle.Linq_101_Samples
  15. #elif POSTGRES
  16. namespace Test_NUnit_PostgreSql.Linq_101_Samples
  17. #elif SQLITE
  18. namespace Test_NUnit_Sqlite.Linq_101_Samples
  19. #elif INGRES
  20. namespace Test_NUnit_Ingres.Linq_101_Samples
  21. #elif MSSQL && L2SQL
  22. namespace Test_NUnit_MsSql_Strict.Linq_101_Samples
  23. #elif MSSQL
  24. namespace Test_NUnit_MsSql.Linq_101_Samples
  25. #elif FIREBIRD
  26. namespace Test_NUnit_Firebird.Linq_101_Samples
  27. #endif
  28. {
  29. /// <summary>
  30. /// Source: http://msdn2.microsoft.com/en-us/vbasic/bb737922.aspx
  31. /// manually translated from VB into C#.
  32. /// </summary>
  33. [TestFixture]
  34. public class Count_Sum_Min_Max_Avg : TestBase
  35. {
  36. [Test]
  37. public void LinqToSqlCount01()
  38. {
  39. Northwind db = CreateDB();
  40. var q = db.Customers.Count();
  41. Assert.IsTrue(q > 0, "Expect non-zero count");
  42. }
  43. #if !DEBUG && (MSSQL && !L2SQL)
  44. [Explicit]
  45. #endif
  46. [Test]
  47. public void LinqToSqlCount02()
  48. {
  49. Northwind db = CreateDB();
  50. #if INGRES && !MONO_STRICT
  51. var q = (from p in db.Products where p.Discontinued == "N" select p)
  52. .Count();
  53. #else
  54. var q = (from p in db.Products where !p.Discontinued select p)
  55. .Count();
  56. #endif
  57. Assert.IsTrue(q > 0, "Expect non-zero count");
  58. }
  59. [Test(Description = "This sample uses Sum to find the total freight over all Orders.")]
  60. public void LinqToSqlCount03()
  61. {
  62. Northwind db = CreateDB();
  63. var q = (from o in db.Orders select o.Freight).Sum();
  64. Assert.IsTrue(q > 0, "Freight sum must be > 0");
  65. }
  66. [Test(Description = "This sample uses Sum to find the total number of units on order over all Products.")]
  67. public void LinqToSqlCount04()
  68. {
  69. Northwind db = CreateDB();
  70. var q = (from p in db.Products select (int)p.UnitsOnOrder.Value).Sum();
  71. Assert.IsTrue(q > 0, "Freight sum must be > 0");
  72. }
  73. [Test(Description = "This sample uses Min to find the lowest unit price of any Product")]
  74. public void LinqToSqlCount05()
  75. {
  76. Northwind db = CreateDB();
  77. var q = (from p in db.Products select p.UnitsOnOrder).Min();
  78. Assert.IsTrue(q == 0, "Min UnitsOnOrder must be 0");
  79. }
  80. [Test(Description = "This sample uses Min to find the lowest freight of any Order.")]
  81. public void LinqToSqlCount06()
  82. {
  83. Northwind db = CreateDB();
  84. var q = (from o in db.Orders select o.Freight).Min();
  85. Assert.IsTrue(q > 0, "Freight sum must be > 0");
  86. }
  87. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
  88. [Explicit]
  89. #endif
  90. [Test(Description = "This sample uses Min to find the Products that have the lowest unit price in each category")]
  91. public void LinqToSqlCount07()
  92. {
  93. #region SHOW_MICROSOFT_GENERATED_SQL
  94. /*
  95. //the one Linq statement below gets translated into 9 SQL statements
  96. SELECT MIN([t0].[UnitPrice]) AS [value], [t0].[CategoryID]
  97. FROM [dbo].[Products] AS [t0]
  98. GROUP BY [t0].[CategoryID]
  99. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  100. FROM [dbo].[Products] AS [t0]
  101. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  102. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
  103. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [4.5000]
  104. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  105. FROM [dbo].[Products] AS [t0]
  106. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  107. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [2]
  108. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [10.0000]
  109. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  110. FROM [dbo].[Products] AS [t0]
  111. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  112. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [3]
  113. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [9.2000]
  114. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  115. FROM [dbo].[Products] AS [t0]
  116. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  117. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [4]
  118. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [2.5000]
  119. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  120. FROM [dbo].[Products] AS [t0]
  121. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  122. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [5]
  123. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [7.0000]
  124. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  125. FROM [dbo].[Products] AS [t0]
  126. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  127. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [6]
  128. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [7.4500]
  129. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  130. FROM [dbo].[Products] AS [t0]
  131. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  132. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [7]
  133. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [10.0000]
  134. SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
  135. FROM [dbo].[Products] AS [t0]
  136. WHERE ([t0].[UnitPrice] = @x2) AND (((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
  137. -- @x1: Input Int (Size = 0; Prec = 0; Scale = 0) [8]
  138. -- @x2: Input Money (Size = 0; Prec = 19; Scale = 4) [6.0000]
  139. */
  140. #endregion
  141. Northwind db = CreateDB();
  142. var categories = (from p in db.Products
  143. group p by p.CategoryID into g
  144. select new
  145. {
  146. CategoryID = g.Key,
  147. CheapestProducts = from p2 in g
  148. where p2.UnitPrice == g.Min(p3 => p3.UnitPrice)
  149. select p2
  150. });
  151. var list = categories.ToList();
  152. Assert.IsTrue(list.Count > 0, "Expected count > 0");
  153. }
  154. [Test(Description = "This sample uses Max to find the latest hire date of any Employee")]
  155. public void LinqToSqlCount08()
  156. {
  157. Northwind db = CreateDB();
  158. var q = (from e in db.Employees select e.HireDate).Max();
  159. Assert.IsTrue(q > new DateTime(1990, 1, 1), "Hire date must be > 2000");
  160. }
  161. [Test(Description = "This sample uses Max to find the most units in stock of any Product")]
  162. public void LinqToSqlCount09()
  163. {
  164. Northwind db = CreateDB();
  165. var q = (from p in db.Products select p.UnitsInStock).Max();
  166. Assert.IsTrue(q > 0, "Max UnitsInStock must be > 0");
  167. }
  168. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
  169. [Explicit]
  170. #endif
  171. [Test(Description = "This sample uses Max to find the Products that have the highest unit price in each category")]
  172. public void LinqToSqlCount10()
  173. {
  174. //Miscrosoft translates this query into multiple SQL statements
  175. Northwind db = CreateDB();
  176. var q = from p in db.Products
  177. group p by p.CategoryID into g
  178. select new
  179. {
  180. g,
  181. MostExpensiveProducts = from p2 in g
  182. where p2.UnitPrice == g.Max(p3 => p3.UnitPrice)
  183. select p2
  184. };
  185. var list = q.ToList();
  186. Assert.IsTrue(list.Count > 0, "Got most expensive items > 0");
  187. }
  188. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
  189. [Explicit]
  190. #endif
  191. [Test(Description = "This sample uses Average to find the average freight of all Orders.")]
  192. public void LinqToSqlCount11()
  193. {
  194. Northwind db = CreateDB();
  195. var q = (from o in db.Orders
  196. select o.Freight).Average();
  197. Console.WriteLine(q);
  198. Assert.IsTrue(q > 0, "Avg orders'freight must be > 0");
  199. }
  200. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
  201. [Explicit]
  202. #endif
  203. [Test(Description = "This sample uses Average to find the average unit price of all Products.")]
  204. public void LinqToSqlCount12()
  205. {
  206. Northwind db = CreateDB();
  207. var q = (from p in db.Products
  208. select p.UnitPrice).Average();
  209. Console.WriteLine(q);
  210. Console.WriteLine(q);
  211. Assert.IsTrue(q > 0, "Avg products'unitPrice must be > 0");
  212. }
  213. #if !INGRES
  214. #if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
  215. [Explicit]
  216. #endif
  217. [Test(Description = "This sample uses Average to find the Products that have unit price higher than the average unit price of the category for each category.")]
  218. public void LinqToSqlCount13()
  219. {
  220. Northwind db = CreateDB();
  221. var categories = from p in db.Products
  222. group p by p.CategoryID into g
  223. select new
  224. {
  225. g,
  226. ExpensiveProducts = from p2 in g
  227. where (p2.UnitPrice > g.Average(p3 => p3.UnitPrice))
  228. select p2
  229. };
  230. var list = categories.ToList();
  231. Assert.IsTrue(list.Count > 0, "Got categorized products > 0");
  232. }
  233. #endif
  234. }
  235. }