5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\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
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\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
28 using System.Collections.Generic;
\r
31 using NUnit.Framework;
\r
36 // test ns Linq_101_Samples
\r
38 namespace Test_NUnit_MySql.Linq_101_Samples
\r
40 namespace Test_NUnit_OracleODP.Linq_101_Samples
\r
42 namespace Test_NUnit_Oracle.Linq_101_Samples
\r
44 namespace Test_NUnit_PostgreSql.Linq_101_Samples
\r
46 namespace Test_NUnit_Sqlite.Linq_101_Samples
\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
52 namespace Test_NUnit_MsSql.Linq_101_Samples
\r
54 namespace Test_NUnit_Firebird.Linq_101_Samples
\r
58 /// Source: http://msdn2.microsoft.com/en-us/vbasic/bb737930.aspx
\r
59 /// manually translated from VB into C#.
\r
62 public class String_Date_functions : TestBase
\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
67 Northwind db = CreateDB();
\r
69 var q = from c in db.Customers
\r
70 select new { c.CustomerID, Location = c.City + ", " + c.Country };
\r
72 //bool foundBerlin = false;
\r
73 foreach (var v in q)
\r
75 if (v.Location == "Berlin, Germany")
\r
77 //foundBerlin = true;
\r
81 Assert.Fail("Expected to find location 'Berlin, Germany'");
\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
87 Northwind db = CreateDB();
\r
89 var q = from p in db.Products
\r
90 where p.ProductName.Length < 10
\r
93 List<Product> prods = q.ToList();
\r
94 Assert.IsTrue(prods.Count > 0, "Expected some products");
\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
100 Northwind db = CreateDB();
\r
102 var q = from c in db.Customers
\r
103 where c.ContactName.Contains("Anders")
\r
106 var list = q.ToList();
\r
107 Assert.IsTrue(list.Count > 0);
\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
113 Northwind db = CreateDB();
\r
115 var q = from c in db.Customers
\r
116 select new { c.ContactName, SpacePos = c.ContactName.IndexOf(" ") };
\r
118 var list = q.ToList();
\r
119 Assert.IsTrue(list.Count > 0);
\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
125 Northwind db = CreateDB();
\r
127 var q = from c in db.Customers
\r
128 where c.ContactName.StartsWith("Maria")
\r
131 var list = q.ToList();
\r
132 Assert.IsTrue(list.Count > 0);
\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
138 Northwind db = CreateDB();
\r
140 var q = from c in db.Customers
\r
141 where c.ContactName.EndsWith("Anders")
\r
144 var list = q.ToList();
\r
145 Assert.IsTrue(list.Count > 0);
\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
151 Northwind db = CreateDB();
\r
153 var q = from p in db.Products
\r
154 select p.ProductName.Substring(3);
\r
156 var list = q.ToList();
\r
157 Assert.IsTrue(list.Count > 0);
\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
163 Northwind db = CreateDB();
\r
165 var q = from e in db.Employees
\r
166 where e.HomePhone.Substring(5, 3) == "555"
\r
169 var list = q.ToList();
\r
170 Assert.IsTrue(list.Count > 0);
\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
176 Northwind db = CreateDB();
\r
178 var q = from e in db.Employees
\r
179 select new { LastName = e.LastName.ToUpper(), e.FirstName };
\r
181 var list = q.ToList();
\r
182 Assert.IsTrue(list.Count > 0);
\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
189 Northwind db = CreateDB();
\r
191 var q = from c in db.Categories
\r
192 select c.CategoryName.ToLower();
\r
194 var list = q.ToList();
\r
195 Assert.IsTrue(list.Count > 0);
\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
201 Northwind db = CreateDB();
\r
203 var q = from e in db.Employees
\r
204 where !(" "+(e.LastName)).Trim().Contains(" ")
\r
207 var list = q.ToList();
\r
208 Assert.IsTrue(list.Count > 0);
\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
214 Northwind db = CreateDB();
\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
220 var list = q.ToList();
\r
221 Assert.IsTrue(list.Count > 0);
\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
227 Northwind db = CreateDB();
\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
233 var list = q.ToList();
\r
234 Assert.IsTrue(list.Count > 0);
\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
240 Northwind db = CreateDB();
\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
246 var list = q.ToList();
\r
247 Assert.IsTrue(list.Count > 0);
\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
253 Northwind db = CreateDB();
\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
258 var list = q.ToList();
\r
259 Assert.IsTrue(list.Count > 0);
\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
265 Northwind db = CreateDB();
\r
267 var q = from o in db.Orders
\r
268 where o.OrderDate.Value.Year == 1996
\r
271 var list = q.ToList();
\r
272 Assert.IsTrue(list.Count > 0);
\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
278 Northwind db = CreateDB();
\r
280 var q = from o in db.Orders
\r
281 where o.OrderDate.Value.Month == 10
\r
284 var list = q.ToList();
\r
285 Assert.IsTrue(list.Count > 0);
\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
291 Northwind db = CreateDB();
\r
293 var q = from o in db.Orders
\r
294 where o.OrderDate.Value.Day == 16
\r
297 var list = q.ToList();
\r
298 Assert.IsTrue(list.Count > 0);
\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
304 Northwind db = CreateDB();
\r
306 var q = (from o in db.Orders
\r
307 where o.OrderDate.Value.Second == 16
\r
308 select o).ToList();
\r