File: ReadTest_Complex.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (633 lines) | stat: -rw-r--r-- 22,068 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#region MIT license
// 
// MIT license
//
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
#endregion
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using NUnit.Framework;
using Test_NUnit;

using nwind;

#if ORACLE
using Id = System.Decimal;
#else
using Id = System.Int32;
#endif

// test ns 
#if MYSQL
    namespace Test_NUnit_MySql
#elif ORACLE && ODP
    namespace Test_NUnit_OracleODP
#elif ORACLE
    namespace Test_NUnit_Oracle
#elif POSTGRES
    namespace Test_NUnit_PostgreSql
#elif SQLITE
    namespace Test_NUnit_Sqlite
#elif INGRES
    namespace Test_NUnit_Ingres
#elif MSSQL && L2SQL
    namespace Test_NUnit_MsSql_Strict
#elif MSSQL
    namespace Test_NUnit_MsSql
#elif FIREBIRD
    namespace Test_NUnit_Firebird
#endif
{
    [TestFixture]
    public class ReadTest_Complex : TestBase
    {
        Northwind db;

        public ReadTest_Complex()
        {
            db = CreateDB();

        }

        #region 'D' tests exercise 'local object constants'
        [Test]
        public void D0_SelectPensByLocalProperty()
        {
            //reported by Andrus.
            //http://groups.google.com/group/dblinq/browse_thread/thread/c25527cbed93d265

            Northwind db = CreateDB();

            Product localProduct = new Product { ProductName = "Chai" };
            var q = from p in db.Products where p.ProductName == localProduct.ProductName select p;

            List<Product> products = q.ToList();
            int productCount = products.Count;
            Assert.AreEqual(productCount, 1, "Expected one pen, got count=" + productCount);
        }

        [Test]
        public void D1_SelectPensByLocalProperty()
        {
            Northwind db = CreateDB();
            var pen = new { Name = "Chai" };
            var q = from p in db.Products where p.ProductName == pen.Name select p;

            List<Product> products = q.ToList();
            int productCount = products.Count;
            Assert.AreEqual(productCount, 1, "Expected one pen, got count=" + productCount);
        }

        [Test]
        public void D2_SelectProductByLocalPropertyAndConstant()
        {

            Northwind db = CreateDB();
            string product = "Carnarvon Tigers";
            var q = from p in db.Products
                    where p.ProductName == product &&
                        p.QuantityPerUnit.StartsWith("16")
                    select p;
            List<Product> products = q.ToList();
            int productCount = products.Count;
            Assert.AreEqual(1, productCount, "Expected one product, got count=" + productCount);
        }

        [Test]
        public void D3_ArrayContains()
        {
            Northwind db = CreateDB();

            var data = from p in db.Customers
                       where new string[] { "ALFKI", "WARTH" }.Contains(p.CustomerID)
                       select new { p.CustomerID, p.Country };

            var dataList = data.ToList();
            //Assert.AreEqual(productCount, 1, "Expected one pen, got count=" + productCount);
        }
        #endregion

        #region Tests 'F' work on aggregation
        [Test]
        public void F1_ProductCount()
        {
            var q = from p in db.Products select p;
            int productCount = q.Count();
            Assert.Greater(productCount, 0, "Expected non-zero product count");
        }

        [Test]
        public void F2_ProductCount_Projected()
        {
            var q = from p in db.Products select p.ProductID;
            int productCount = q.Count();
            Assert.Greater(productCount, 0, "Expected non-zero product count");
            Console.WriteLine();
        }
        [Test]
        public void F2_ProductCount_Clause()
        {
            var q = from p in db.Products select p.ProductID;
            int productCount = q.Count(i => i < 3);
            Assert.Greater(productCount, 0, "Expected non-zero product count");
            Assert.IsTrue(productCount < 4, "Expected product count < 3");
        }

        [Test]
        public void F3_MaxProductId()
        {
            var q = from p in db.Products select p.ProductID;
            var maxID = q.Max();
            Assert.Greater(maxID, 0, "Expected non-zero product count");
        }

        [Test]
        public void F4_MinProductId()
        {
            var q = from p in db.Products select p.ProductID;
            var minID = q.Min();
            Assert.Greater(minID, 0, "Expected non-zero product count");
        }

#if !ORACLE // picrap: this test causes an internal buffer overflow when marshaling with oracle win32 driver

        [Test]
        public void F5_AvgProductId()
        {
            var q = from p in db.Products select p.ProductID;
            double avg = q.Average();
            Assert.Greater(avg, 0, "Expected non-zero productID average");
        }

#endif

        [Test]
        public void F7_ExplicitJoin()
        {
            //a nice and light nonsense join:
            //bring in rows such as {Chai,AIRBU}
            var q =
                from p in db.Products
                join c in db.Categories on p.ProductID equals c.CategoryID
                select new { p.ProductName, c.CategoryName };

            int rowCount = 0;
            foreach (var v in q)
            {
                rowCount++;
                Assert.IsTrue(v.ProductName != null);
                Assert.IsTrue(v.CategoryName != null);
            }
            Assert.IsTrue(rowCount > 2);
        }

        [Test]
        public void F7b_ExplicitJoin()
        {
            var q =
                from c in db.Customers
                join o in db.Orders on c.CustomerID equals o.CustomerID
                where c.City == "London"
                select o;
        }

#if INCLUDING_CLAUSE
        //Including() clause discontinued in Studio Orcas?
        [Test]
        public void F8_IncludingClause()
        {
            var q = (
	            from c in db.Customers
	            where c.City == "London"
	            select c)
	            .Including(c => c.Orders);
        }

        [Test]
        public void F8_Including_Nested()
        {
            var q = (
	            from c in db.Customers
	            where c.City == "London"
	            select c)
	            .Including(c => c.Orders.Including(o => o.OrderDetails));
        }
#endif

        [Test]
        public void F9_Project_AndContinue()
        {
            var q =
                from c in db.Customers
                where c.City == "London"
                select new { Name = c.ContactName, c.Phone } into x
                orderby x.Name
                select x;
        }

        [Test]
        public void F10_DistinctCity()
        {
            var q1 = from c in db.Customers select c.City;
            var q2 = q1.Distinct();

            int numLondon = 0;
            foreach (string city in q2)
            {
                if (city == "London") { numLondon++; }
            }
            Assert.AreEqual(1, numLondon, "Expected to see London once");
        }

        [Test]
        public void F11_ConcatString()
        {
            var q4 = from p in db.Products select p.ProductName + p.ProductID;
            //var q4 = from p in db.Products select p.ProductID;
            var q5 = q4.ToList();
            Assert.Greater(q5.Count, 2, "Expected to see some concat strings");
            foreach (string s0 in q5)
            {
                bool startWithLetter = Char.IsLetter(s0[0]);
                bool endsWithDigit = Char.IsDigit(s0[s0.Length - 1]);
                Assert.IsTrue(startWithLetter && endsWithDigit, "String must start with letter and end with digit");
            }
        }

#if !DEBUG && POSTGRES
        [Explicit]
#endif
        [Test]
        public void F12_ConcatString_2()
        {
            var q4 = from p in db.Products
                     where (p.ProductName + p.ProductID).Contains("e")
                     select p.ProductName+p.ProductID;
            //var q4 = from p in db.Products select p.ProductID;
            //var q5 = q4.ToList();
            Assert.Greater( q4.Count(), 2, "Expected to see some concat strings");
            foreach(string s0 in q4)
            {
                bool startWithLetter = Char.IsLetter(s0[0]);
                bool endsWithDigit = Char.IsDigit(s0[s0.Length-1]);
                Assert.IsTrue(startWithLetter && endsWithDigit, "String must start with letter and end with digit");
            }
        }
        #endregion

        const string obsoleteError = @"Since beta2 in Linq2Sql to project a new entity (ie: select new Order(3)) is forbidden for coherence reasons, so this tests doesn't mimic the Linq2Sql behavior and it is obsolete and should be modified. If you apply such test cases to Linq2Sql you'll get Test_NUnit_MsSql_Strict.DynamicLinqTest.DL5_NestedObjectSelect:
        System.NotSupportedException : Explicit construction of entity type 'MsNorthwind.XX' in query is not allowed.\n\nMore Info in: http://linqinaction.net/blogs/roller/archive/2007/11/27/explicit-construction-of-entity-type-in-query-is-not-allowed.aspx";
        [Test]
        public void F13_NewCustomer()
        {
            Assert.Ignore(obsoleteError);
            Northwind db = CreateDB();
            IQueryable<Customer> q = (from c in db.Customers
                                      select
                                      new Customer
                                      {
                                          CustomerID = c.CustomerID
                                      });
            var list = q.ToList();
            Assert.Greater(list.Count(), 0, "Expected list");
            //Assert.Greater(list.Count(), 0, "Expected list");
            Assert.Ignore("test passed but: theoretically constructions of entity types are not allowed");
        }

        [Test]
        public void F14_NewCustomer_Order()
        {
            Assert.Ignore(obsoleteError);
            Northwind db = CreateDB();
            IQueryable<Customer> q = (from c in db.Customers
                                      select
                                      new Customer
                                      {
                                          CustomerID = c.CustomerID
                                      });
            //this OrderBy clause messes up the SQL statement
            var q2 = q.OrderBy(c => c.CustomerID);
            var list = q2.ToList();
            Assert.Greater(list.Count(), 0, "Expected list");
            //Assert.Greater(list.Count(), 0, "Expected list");
        }


        [Test]
        public void F15_OrderByCoalesce()
        {
            Northwind db = CreateDB();
            var q = from c in db.Customers
                    orderby c.ContactName ?? ""
                    select c;
            var list = q.ToList();
            Assert.Greater(list.Count(), 0, "Expected list");
        }

        [Test(Description = "Non-dynamic version of DL5_NestedObjectSelect")]
        public void F16_NestedObjectSelect()
        {
            Assert.Ignore(obsoleteError);
            Northwind db = CreateDB();
            var q = from o in db.Orders
                    select new Order() { OrderID = o.OrderID, Customer = new Customer() { ContactName = o.Customer.ContactName } };
            var list = q.ToList();
        }

        [Test(Description = "Non-dynamic version of DL5_NestedObjectSelect")]
        public void F17_NestedObjectSelect_Ver2()
        {
            Assert.Ignore(obsoleteError);
            Northwind db = CreateDB();
            var query = from order in db.Orders
                        select new Order
                        {
                            OrderID = order.OrderID,
                            Customer = new Customer
                            {
                                ContactName = order.Customer.ContactName,
                                ContactTitle = order.Customer.ContactTitle
                            }
                        };
            var list = query.ToList();
            Assert.IsTrue(list.Count > 0);
        }

#if !DEBUG && POSTGRES
        [Explicit]
#endif
        [Test(Description = "byte[] test")]
        public void F18_ByteArrayAssignmentTest()
        {
            var db = CreateDB();

            var picture = new byte[] { 1, 2, 3, 4 };

            var nc = new Category { CategoryName = "test", Picture = picture };
            db.Categories.InsertOnSubmit(nc);
            db.SubmitChanges();

            var q = from c in db.Categories 
                    where c.CategoryName == "test"
                    select new { c.Picture };
            var l = q.ToList();
            Assert.IsTrue(l.Count > 0);
            Assert.IsTrue(picture.SequenceEqual(l[0].Picture.ToArray()));

            db.Categories.DeleteOnSubmit(nc);
            db.SubmitChanges();
        }


        [Test]
        public void F19_ExceptWithCount_ViaToList()
        {
            var db = CreateDB();

            var toExclude = from t in db.GetTable<Territory>()
                            where t.TerritoryDescription.StartsWith("A")
                            select t;
            var universe = from t in db.GetTable<Territory>() select t;
            var toTake = universe.Except(toExclude);
            
            int toListCount = toTake.ToList().Count;
            Assert.AreEqual(51, toListCount);
        }

        [Test]
        public void F20_ExceptWithCount()
        {
            var db = CreateDB();

            var toExclude = from t in db.GetTable<Territory>()
                            where t.TerritoryDescription.StartsWith("A")
                            select t;
            var universe = from t in db.GetTable<Territory>() select t;
            var toTake = universe.Except(toExclude).Except(db.Territories.Where(terr => terr.TerritoryDescription.StartsWith("B")));

            int toTakeCount = toTake.Count();
            Assert.AreEqual(44, toTakeCount);
        }

#if !DEBUG && (SQLITE)
        [Explicit]
#endif
        [Test]
        public void F21_CountNestedExcepts()
        {
            var db = CreateDB();

            var toExclude1 = from t in db.GetTable<Territory>()
                            where t.TerritoryDescription.StartsWith("A")
                            select t;
            var toExclude2 = toExclude1.Except(db.GetTable<Territory>().Where(terr => terr.TerritoryDescription.Contains("i")));

            var universe = from t in db.GetTable<Territory>() select t;

            var toTake = universe.Except(toExclude2);

            int toTakeCount = toTake.Count();
            Assert.AreEqual(52, toTakeCount);
        }

#if !DEBUG && (SQLITE)
        [Explicit]
#endif
        [Test]
        public void F22_AnyNestedExcepts()
        {
            var db = CreateDB();

            var toExclude1 = from t in db.GetTable<Territory>()
                             where t.TerritoryDescription.StartsWith("A")
                             select t;
            var toExclude2 = toExclude1.Except(db.GetTable<Territory>().Where(terr => terr.TerritoryDescription.Contains("i")));

            var universe = from t in db.GetTable<Territory>() select t;

            var toTake = universe.Except(toExclude2);

            Assert.IsTrue(toTake.Any());
        }

#if !DEBUG && SQLITE
        [Explicit]
#endif
        [Test]
        public void F23_AnyNestedExcepts_WithParameter()
        {
            var db = CreateDB();

            var toExclude1 = from t in db.GetTable<Territory>()
                             where t.TerritoryDescription.StartsWith("A")
                             select t;
            var toExclude2 = toExclude1.Except(db.GetTable<Territory>().Where(terr => terr.TerritoryDescription.Contains("i")));

            var universe = from t in db.GetTable<Territory>() select t;

            var toTake = universe.Except(toExclude2);

            Assert.IsTrue(toTake.Any(t => t.TerritoryDescription.Contains("i")));
        }

#if !DEBUG && SQLITE
        [Explicit]
#endif
        [Test]
        public void F24_CountNestedExcepts_WithParameter()
        {
            var db = CreateDB();

            var toExclude1 = from t in db.GetTable<Territory>()
                             where t.TerritoryDescription.StartsWith("A")
                             select t;
            var toExclude2 = toExclude1.Except(db.GetTable<Territory>().Where(terr => terr.TerritoryDescription.Contains("i")));

            var universe = from t in db.GetTable<Territory>() select t;

            var toTake = universe.Except(toExclude2);

            int toTakeCount = toTake.Count(t => t.TerritoryDescription.Contains("o"));
            Assert.AreEqual(34, toTakeCount);
        }

        [Test]
        public void F25_DistinctUnion()
        {
            var db = CreateDB();

            var toInclude1 = from t in db.GetTable<Territory>()
                             where t.TerritoryDescription.StartsWith("A")
                             select t;
            var toInclude2 = toInclude1.Concat(db.GetTable<Territory>().Where(terr => terr.TerritoryDescription.Contains("i")));

            var toTake = toInclude2.Distinct();

            int count = toTake.ToList().Count;

            Assert.AreEqual(27, count);
        }

        [Test]
        public void F26_DistinctUnion_Count()
        {
            var db = CreateDB();

            var toInclude1 = from t in db.GetTable<Territory>()
                             where t.TerritoryDescription.StartsWith("A")
                             select t;
            var toInclude2 = toInclude1.Concat(db.GetTable<Territory>().Where(terr => terr.TerritoryDescription.Contains("i")));

            var toTake = toInclude2.Distinct();

            int count = toTake.Count();

            Assert.AreEqual(27, count);
        }

#if MSSQL || L2SQL

#if L2SQL
        [Explicit]
#endif
        [Test]
        public void F27_SelectEmployee_Identifier()
        {
            var db = CreateDB();
            var q = from e in db.GetTable<EmployeeWithStringIdentifier>() where e.Identifier == "7" select e;
            EmployeeWithStringIdentifier em = q.Single();

            Assert.AreEqual("King", em.LastName);
        }

#endif

        /// <summary>
        /// the following three tests are from Jahmani's page
        /// LinqToSQL: Comprehensive Support for SQLite, MS Access, SQServer2000/2005
        /// http://www.codeproject.com/KB/linq/linqToSql_7.aspx?msg=2428251
        /// </summary>
        [Test(Description = "list of customers who have place orders that have all been shipped to the customers city.")]
        public void O1_OperatorAll()
        {
            var q = from c in db.Customers
                    where (from o in c.Orders
                           select o).All(o => o.ShipCity == c.City)
                    select new { c.CustomerID, c.ContactName };
            var list = q.ToList();
        }

        [Test(Description = "list of customers who have placed no orders")]
        public void O2_OperatorAny()
        {
            //SELECT  t0.CustomerID, t0.ContactName
            //FROM Customers AS t0
            //WHERE  NOT  (
            //(    SELECT  COUNT(*) 
            //    FROM Orders AS t1
            //    WHERE (t1.CustomerID = t0.CustomerID)
            //) > 0
            //)
            var q = from customer in db.Customers
                    where !customer.Orders.Any()
                    select new { customer.CustomerID, customer.ContactName };
            //var q = from customer in db.Customers
            //        where customer.Orders.Count() == 0
            //        select new { customer.CustomerID, customer.ContactName };
            var list = q.ToList();
        }

        [Test(Description = "provide a list of customers and employees who live in London.")]
        public void O3_OperatorUnion()
        {
            var q = (from c in db.Customers.Where(d => d.City == "London")
                     select new { ContactName = c.ContactName })
              .Union(from e in db.Employees.Where(f => f.City == "London")
                     select new { ContactName = e.LastName });
            var list = q.ToList();
            Assert.IsTrue(list.Count > 0, "Expected some customers and employees from London");
        }

        [Test]
        public void O4_OperatorContains()
        {
            var ids = new Id[] { 1, 2, 3 };
            Northwind db = CreateDB();

            //var q = from p in db.Products select p.ProductID;
            //int productCount = q.Count();

            var products = from p in db.Products
                           where ids.Contains((Id) p.ProductID)
                           select p;

            Assert.AreEqual(3, products.Count());

        }


    }
}