2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Test / Providers / Linq_101_Samples / Advanced.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
8 // of this software and associated documentation files (the "Software"), to deal\r
9 // in the Software without restriction, including without limitation the rights\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
11 // copies of the Software, and to permit persons to whom the Software is\r
12 // furnished to do so, subject to the following conditions:\r
13 // \r
14 // The above copyright notice and this permission notice shall be included in\r
15 // all copies or substantial portions of the Software.\r
16 // \r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Collections.Generic;\r
29 using System.Linq;\r
30 using System.Linq.Expressions;\r
31 using System.Text;\r
32 using NUnit.Framework;\r
33 using Test_NUnit;\r
34 using Test_NUnit.Linq_101_Samples;\r
35 \r
36 using nwind;\r
37 \r
38 // test ns Linq_101_Samples\r
39 #if MYSQL\r
40     namespace Test_NUnit_MySql.Linq_101_Samples\r
41 #elif ORACLE && ODP\r
42     namespace Test_NUnit_OracleODP.Linq_101_Samples\r
43 #elif ORACLE\r
44     namespace Test_NUnit_Oracle.Linq_101_Samples\r
45 #elif POSTGRES\r
46     namespace Test_NUnit_PostgreSql.Linq_101_Samples\r
47 #elif SQLITE\r
48     namespace Test_NUnit_Sqlite.Linq_101_Samples\r
49 #elif INGRES\r
50     namespace Test_NUnit_Ingres.Linq_101_Samples\r
51 #elif MSSQL && L2SQL\r
52     namespace Test_NUnit_MsSql_Strict.Linq_101_Samples\r
53 #elif MSSQL\r
54     namespace Test_NUnit_MsSql.Linq_101_Samples\r
55 #elif FIREBIRD\r
56     namespace Test_NUnit_Firebird.Linq_101_Samples\r
57 #endif\r
58 {\r
59     /// <summary>\r
60     /// Source:  http://msdn2.microsoft.com/en-us/vbasic/bb737920.aspx\r
61     /// manually translated from VB into C#.\r
62     /// </summary>\r
63     [TestFixture]\r
64     public class AdvancedTest : TestBase\r
65     {\r
66         [Test(Description = "This sample builds a query dynamically to return the contact name of each customer.")]\r
67         public void LinqToSqlAdvanced01()\r
68         {\r
69             Northwind db = CreateDB();\r
70 \r
71             ParameterExpression param = Expression.Parameter(typeof(Customer), "c");\r
72             Expression selector = Expression.Property(param, typeof(Customer).GetProperty("ContactName"));\r
73             var pred = Expression.Lambda(selector, param);\r
74 \r
75             var custs = db.Customers;\r
76             var expr = Expression.Call(typeof(Queryable), "Select"\r
77                 , new Type[] { typeof(Customer), typeof(string) }, Expression.Constant(custs), pred);\r
78             var query = db.Customers.AsQueryable().Provider.CreateQuery<string>(expr);\r
79 \r
80             var list = query.ToList();\r
81             Assert.IsTrue(list.Count > 0);\r
82         }\r
83 \r
84         //TODO - 2,3,4,5\r
85 \r
86         [Test(Description = "This sample builds a query dynamically to filter for Customers in London.")]\r
87         public void LinqToSqlAdvanced02()\r
88         {\r
89             Northwind db = CreateDB();\r
90 \r
91             var custs = db.Customers;\r
92             var param = Expression.Parameter(typeof(Customer), "c");\r
93             var right = Expression.Constant("London");\r
94             var left = Expression.Property(param, typeof(Customer).GetProperty("City"));\r
95             var filter = Expression.Equal(left, right);\r
96             var pred = Expression.Lambda(filter, param);\r
97 \r
98             var expr = Expression.Call(typeof(Queryable), "Where", new Type[] { typeof(Customer) }, Expression.Constant(custs), pred);\r
99             var query = db.Customers.AsQueryable().Provider.CreateQuery<Customer>(expr);\r
100 \r
101             var list = query.ToList();\r
102             Assert.IsTrue(list.Count > 0, "Got London citiens > 0");\r
103         }\r
104 \r
105         [Test(Description = "This sample builds a query dynamically to filter for Customers in London and order them by ContactName.")]\r
106         public void LinqToSqlAdvanced03()\r
107         {\r
108             Northwind db = CreateDB();\r
109 \r
110             var param = Expression.Parameter(typeof(Customer), "c");\r
111 \r
112             var left = Expression.Property(param, typeof(Customer).GetProperty("City"));\r
113             var right = Expression.Constant("London");\r
114             var filter = Expression.Equal(left, right);\r
115             var pred = Expression.Lambda(filter, param);\r
116 \r
117             var selector = Expression.Property(param, typeof(Customer).GetProperty("ContactName"));\r
118             IQueryable custs = db.Customers;\r
119             var expr = Expression.Call(typeof(Queryable), "Where", new Type[] { typeof(Customer) }, Expression.Constant(custs), pred);\r
120             expr = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { typeof(Customer), typeof(String) }, custs.Expression, Expression.Lambda(Expression.Property(param, "ContactName"), param));\r
121             var query = db.Customers.AsQueryable().Provider.CreateQuery<Customer>(expr);\r
122 \r
123             var list = query.ToList();\r
124             Assert.IsTrue(list.Count > 0);\r
125         }\r
126 \r
127 \r
128         [Test(Description = "This sample dynamically builds a Union to return a sequence of all countries where either a customer or an employee live.")]\r
129         public void LinqToSqlAdvanced04()\r
130         {\r
131             Northwind db = CreateDB();\r
132 \r
133             var custs = db.Customers;\r
134             var param1 = Expression.Parameter(typeof(Customer), "e");\r
135             var left1 = Expression.Property(param1, typeof(Customer).GetProperty("City"));\r
136             var pred1 = Expression.Lambda(left1, param1);\r
137 \r
138             var employees = db.Employees;\r
139             var param2 = Expression.Parameter(typeof(Employee), "c");\r
140             var left2 = Expression.Property(param2, typeof(Employee).GetProperty("City"));\r
141             var pred2 = Expression.Lambda(left2, param2);\r
142 \r
143             var expr1 = Expression.Call(typeof(Queryable), "Select", new Type[] { typeof(Customer), typeof(String) }, Expression.Constant(custs), pred1);\r
144             var expr2 = Expression.Call(typeof(Queryable), "Select", new Type[] { typeof(Employee), typeof(String) }, Expression.Constant(employees), pred2);\r
145 \r
146             var q1 = db.Customers.AsQueryable().Provider.CreateQuery<String>(expr1);\r
147             var q2 = db.Employees.AsQueryable().Provider.CreateQuery<String>(expr2);\r
148 \r
149             var q3 = q1.Union(q2);\r
150 \r
151             Assert.Greater(q1.Count(), 0);\r
152             Assert.IsTrue(q1.Count() + q2.Count() >= q3.Count());\r
153 \r
154         }\r
155 \r
156 \r
157         [Linq101SamplesModified("Replaced Contact by Customer")]\r
158         [Test(Description="This sample demonstrates how we insert a new Contact and retrieve the newly assigned ContactID from the database.")]\r
159         public void LinqToSqlAdvanced05()\r
160         {\r
161             Northwind db = CreateDB();\r
162 \r
163             //PK Column should be autogenerated\r
164             var con = new Category() { CategoryName = "New Era", Description= "(123)-456-7890" };\r
165             db.Categories.InsertOnSubmit(con);\r
166 \r
167             \r
168             db.SubmitChanges();\r
169 \r
170             Console.WriteLine();\r
171             Console.WriteLine("The Category of the new record is {0}", con.CategoryID);\r
172 \r
173             Category customerReloaded=db.Categories.First(c=>c.CategoryID==con.CategoryID);\r
174             Assert.AreEqual(customerReloaded.CategoryName, con.CategoryName);\r
175             Assert.AreEqual(customerReloaded.Description, con.Description);\r
176 \r
177             // cleanup\r
178             db.Categories.DeleteOnSubmit(con);\r
179             db.SubmitChanges();\r
180         }\r
181 \r
182 \r
183 #if !DEBUG && (MSSQL && !L2SQL)\r
184         [Explicit]\r
185 #endif\r
186         [Test(Description = "This sample uses orderbyDescending and Take to return the discontinued products of the top 10 most expensive products")]\r
187         public void LinqToSqlAdvanced06()\r
188         {\r
189             Northwind db = CreateDB();\r
190 #if INGRES \r
191             var prods = from p in db.Products.OrderByDescending(p=> p.UnitPrice).Take(10) \r
192                        where p.Discontinued == 1 select p;\r
193 #else\r
194             var prods = from p in db.Products.OrderByDescending(p => p.UnitPrice).Take(10)\r
195                         where !p.Discontinued\r
196                         select p;\r
197 #endif\r
198 \r
199             var list = prods.ToList();\r
200             Assert.IsTrue(list.Count > 0);\r
201         }\r
202 \r
203 \r
204     }\r
205 }\r