2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Test / Providers / Linq_101_Samples / String_Date_functions.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.Text;\r
31 using NUnit.Framework;\r
32 using Test_NUnit;\r
33 \r
34 using nwind;\r
35 \r
36 // test ns Linq_101_Samples\r
37 #if MYSQL\r
38     namespace Test_NUnit_MySql.Linq_101_Samples\r
39 #elif ORACLE && ODP\r
40     namespace Test_NUnit_OracleODP.Linq_101_Samples\r
41 #elif ORACLE\r
42     namespace Test_NUnit_Oracle.Linq_101_Samples\r
43 #elif POSTGRES\r
44     namespace Test_NUnit_PostgreSql.Linq_101_Samples\r
45 #elif SQLITE\r
46     namespace Test_NUnit_Sqlite.Linq_101_Samples\r
47 #elif INGRES\r
48     namespace Test_NUnit_Ingres.Linq_101_Samples\r
49 #elif MSSQL && MONO_STRICT\r
50     namespace Test_NUnit_MsSql_Strict.Linq_101_Samples\r
51 #elif MSSQL\r
52     namespace Test_NUnit_MsSql.Linq_101_Samples\r
53 #elif FIREBIRD\r
54     namespace Test_NUnit_Firebird.Linq_101_Samples\r
55 #endif\r
56 {\r
57     /// <summary>\r
58     /// Source:  http://msdn2.microsoft.com/en-us/vbasic/bb737930.aspx\r
59     /// manually translated from VB into C#.\r
60     /// </summary>\r
61     [TestFixture]\r
62     public class String_Date_functions : TestBase\r
63     {\r
64         [Test(Description = "String Concatenation. This sample uses the & operator to concatenate string fields and string literals in forming the Customers' calculated Location value")]\r
65         public void LinqToSqlString01()\r
66         {\r
67             Northwind db = CreateDB();\r
68 \r
69             var q = from c in db.Customers\r
70                     select new { c.CustomerID, Location = c.City + ", " + c.Country };\r
71 \r
72             //bool foundBerlin = false;\r
73             foreach (var v in q)\r
74             {\r
75                 if (v.Location == "Berlin, Germany")\r
76                 {\r
77                     //foundBerlin = true;\r
78                     return;\r
79                 }\r
80             }\r
81             Assert.Fail("Expected to find location 'Berlin, Germany'");\r
82         }\r
83 \r
84         [Test(Description = "String.Length. This sample uses the Length property to find all Products whose name is shorter than 10 characters.")]\r
85         public void LinqToSqlString02()\r
86         {\r
87             Northwind db = CreateDB();\r
88 \r
89             var q = from p in db.Products\r
90                     where p.ProductName.Length < 10\r
91                     select p;\r
92 \r
93             List<Product> prods = q.ToList();\r
94             Assert.IsTrue(prods.Count > 0, "Expected some products");\r
95         }\r
96 \r
97         [Test(Description = "String.Contains(substring).This sample uses the Contains method to find all Customers whose contact name contains 'Anders'.")]\r
98         public void LinqToSqlString03()\r
99         {\r
100             Northwind db = CreateDB();\r
101 \r
102             var q = from c in db.Customers\r
103                     where c.ContactName.Contains("Anders")\r
104                     select c;\r
105 \r
106             var list = q.ToList();\r
107             Assert.IsTrue(list.Count > 0);\r
108         }\r
109 \r
110         [Test(Description = "String.IndexOf(substring). This sample uses the IndexOf method to find the first instance of a space in each Customer's contact name.")]\r
111         public void LinqToSqlString04()\r
112         {\r
113             Northwind db = CreateDB();\r
114 \r
115             var q = from c in db.Customers\r
116                     select new { c.ContactName, SpacePos = c.ContactName.IndexOf(" ") };\r
117 \r
118             var list = q.ToList();\r
119             Assert.IsTrue(list.Count > 0);\r
120         }\r
121 \r
122         [Test(Description = "String.StartsWith(prefix). This sample uses the StartsWith method to find Customers whose contact name starts with 'Maria'.")]\r
123         public void LinqToSqlString05()\r
124         {\r
125             Northwind db = CreateDB();\r
126 \r
127             var q = from c in db.Customers\r
128                     where c.ContactName.StartsWith("Maria")\r
129                     select c;\r
130 \r
131             var list = q.ToList();\r
132             Assert.IsTrue(list.Count > 0);\r
133         }\r
134 \r
135         [Test(Description = "String.EndsWith(suffix). This sample uses the StartsWith method to find Customers whose contact name ends with 'Anders'.")]\r
136         public void LinqToSqlString06()\r
137         {\r
138             Northwind db = CreateDB();\r
139 \r
140             var q = from c in db.Customers\r
141                     where c.ContactName.EndsWith("Anders")\r
142                     select c;\r
143 \r
144             var list = q.ToList();\r
145             Assert.IsTrue(list.Count > 0);\r
146         }\r
147 \r
148         [Test(Description = "String.Substring(start). This sample uses the Substring method to return Product names starting from the fourth letter.")]\r
149         public void LinqToSqlString07()\r
150         {\r
151             Northwind db = CreateDB();\r
152 \r
153             var q = from p in db.Products\r
154                     select p.ProductName.Substring(3);\r
155 \r
156             var list = q.ToList();\r
157             Assert.IsTrue(list.Count > 0);\r
158         }\r
159 \r
160         [Test(Description = "String.Substring(start, length). This sample uses the Substring method to find Employees whose home phone numbers have '555' as the seventh through ninth digits.")]\r
161         public void LinqToSqlString08()\r
162         {\r
163             Northwind db = CreateDB();\r
164 \r
165             var q = from e in db.Employees\r
166                     where e.HomePhone.Substring(5, 3) == "555"\r
167                     select e;\r
168 \r
169             var list = q.ToList();\r
170             Assert.IsTrue(list.Count > 0);\r
171         }\r
172 \r
173         [Test(Description = "String.ToUpper(). This sample uses the ToUpper method to return Employee names where the last name has been converted to uppercase.")]\r
174         public void LinqToSqlString09()\r
175         {\r
176             Northwind db = CreateDB();\r
177 \r
178             var q = from e in db.Employees\r
179                     select new { LastName = e.LastName.ToUpper(), e.FirstName };\r
180 \r
181             var list = q.ToList();\r
182             Assert.IsTrue(list.Count > 0);\r
183 \r
184         }\r
185 \r
186         [Test(Description = "String.ToLower(). This sample uses the ToLower method to return Category names that have been converted to lowercase.")]\r
187         public void LinqToSqlString10()\r
188         {\r
189             Northwind db = CreateDB();\r
190 \r
191             var q = from c in db.Categories\r
192                     select c.CategoryName.ToLower();\r
193 \r
194             var list = q.ToList();\r
195             Assert.IsTrue(list.Count > 0);\r
196         }\r
197 \r
198         [Test(Description = "String.Trim(). This sample uses the Trim method to return the first five digits of Employee home phone numbers, with leading and trailing spaces removed.")]\r
199         public void LinqToSqlString11()\r
200         {\r
201             Northwind db = CreateDB();\r
202 \r
203             var q = from e in db.Employees\r
204                     where !("  "+(e.LastName)).Trim().Contains(" ")\r
205                     select e.LastName;\r
206 \r
207             var list = q.ToList();\r
208             Assert.IsTrue(list.Count > 0);\r
209         }\r
210 \r
211         [Test(Description = "String.Insert(pos, str). This sample uses the Insert method to return a sequence of employee phone numbers that have a ) in the fifth position, inserting a : after the ).")]\r
212         public void LinqToSqlString12()\r
213         {\r
214             Northwind db = CreateDB();\r
215 \r
216             var q = from e in db.Employees\r
217                     where e.HomePhone.Substring(4, 1) == ")"\r
218                     select e.HomePhone.Insert(5, ":");\r
219 \r
220             var list = q.ToList();\r
221             Assert.IsTrue(list.Count > 0);\r
222         }\r
223 \r
224         [Test(Description = "String.Remove(start). This sample uses the Insert method to return a sequence of employee phone numbers that have a ) in the fifth position, removing all characters starting from the tenth character.")]\r
225         public void LinqToSqlString13()\r
226         {\r
227             Northwind db = CreateDB();\r
228 \r
229             var q = from e in db.Employees\r
230                     where e.HomePhone.Substring(4, 1) == ")"\r
231                     select e.HomePhone.Remove(9);\r
232 \r
233             var list = q.ToList();\r
234             Assert.IsTrue(list.Count > 0);\r
235         }\r
236 \r
237         [Test(Description = "String.Remove(start, length). This sample uses the Insert method to return a sequence of employee phone numbers that have a ) in the fifth position, removing the first six characters.")]\r
238         public void LinqToSqlString14()\r
239         {\r
240             Northwind db = CreateDB();\r
241 \r
242             var q = from e in db.Employees\r
243                     where e.HomePhone.Substring(4, 1) == ")"\r
244                     select e.HomePhone.Remove(0, 6);\r
245 \r
246             var list = q.ToList();\r
247             Assert.IsTrue(list.Count > 0);\r
248         }\r
249 \r
250         [Test(Description = "String.Replace(find, replace). This sample uses the Replace method to return a sequence of Supplier information where the Country field has had UK replaced with United Kingdom and USA replaced with United States of America.")]\r
251         public void LinqToSqlString15()\r
252         {\r
253             Northwind db = CreateDB();\r
254 \r
255             var q = from s in db.Suppliers\r
256                     select new { s.CompanyName, Country = s.Country.Replace("UK", "United Kingdom").Replace("USA", "United States of America") };\r
257 \r
258             var list = q.ToList();\r
259             Assert.IsTrue(list.Count > 0);\r
260         }\r
261 \r
262         [Test(Description = "DateTime.Year. This sample uses the DateTime's Year property to find Orders placed in 1997.")]\r
263         public void LinqToSqlString16()\r
264         {\r
265             Northwind db = CreateDB();\r
266 \r
267             var q = from o in db.Orders\r
268                     where o.OrderDate.Value.Year == 1996\r
269                     select o;\r
270 \r
271             var list = q.ToList();\r
272             Assert.IsTrue(list.Count > 0);\r
273         }\r
274 \r
275         [Test(Description = "DateTime.Month. This sample uses the DateTime's Month property to find Orders placed in December.")]\r
276         public void LinqToSqlString17()\r
277         {\r
278             Northwind db = CreateDB();\r
279 \r
280             var q = from o in db.Orders\r
281                     where o.OrderDate.Value.Month == 10\r
282                     select o;\r
283 \r
284             var list = q.ToList();\r
285             Assert.IsTrue(list.Count > 0);\r
286         }\r
287 \r
288         [Test(Description = "DateTime.Day. This sample uses the DateTime's Day property to find Orders placed on the 31st day of the month.")]\r
289         public void LinqToSqlString18()\r
290         {\r
291             Northwind db = CreateDB();\r
292 \r
293             var q = from o in db.Orders\r
294                     where o.OrderDate.Value.Day == 16\r
295                     select o;\r
296 \r
297             var list = q.ToList();\r
298             Assert.IsTrue(list.Count > 0);\r
299         }\r
300 \r
301         [Test(Description = "DateTime.Seconds. This sample uses the DateTime's Day property to find Orders placed on the 31st day of the month.")]\r
302         public void LinqToSqlString19()\r
303         {\r
304             Northwind db = CreateDB();\r
305 \r
306             var q = (from o in db.Orders\r
307                     where o.OrderDate.Value.Second == 16\r
308                     select o).ToList();\r
309 \r
310  \r
311         }\r
312     }\r
313 }\r