comparison UnitTests/Linq/SelectManyTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Linq.Expressions;
5
6 using BLToolkit.Data.DataProvider;
7
8 using NUnit.Framework;
9
10 namespace Data.Linq
11 {
12 using Model;
13
14 [TestFixture]
15 public class SelectManyTest : TestBase
16 {
17 [Test]
18 public void Basic1()
19 {
20 ForEachProvider(db => AreEqual(
21 Parent.SelectMany(p => Child),
22 db.Parent.SelectMany(p => db.Child)));
23 }
24
25 [Test]
26 public void Basic1_1()
27 {
28 ForEachProvider(db => AreEqual(
29 Parent.SelectMany(p => Child.SelectMany(t => GrandChild)),
30 db.Parent.SelectMany(p => db.Child.SelectMany(t => db.GrandChild))));
31 }
32
33 [Test]
34 public void Basic2()
35 {
36 ForEachProvider(db => AreEqual(
37 Parent.SelectMany(p => Child.Select(_ => _.ParentID + 1)),
38 db.Parent.SelectMany(p => db.Child.Select(_ => _.ParentID + 1))));
39 }
40
41 [Test]
42 public void Basic3()
43 {
44 ForEachProvider(db => AreEqual(
45 Parent.SelectMany(p => Child.Select(_ => _.ParentID + 1).Where(_ => _ > 1)),
46 db.Parent.SelectMany(p => db.Child.Select(_ => _.ParentID + 1).Where(_ => _ > 1))));
47 }
48
49 [Test]
50 public void Basic4()
51 {
52 ForEachProvider(db => AreEqual(
53 Parent.SelectMany(p => Child.Select(_ => _.ParentID + 1).Where(_ => p.ParentID == _)),
54 db.Parent.SelectMany(p => db.Child.Select(_ => _.ParentID + 1).Where(_ => p.ParentID == _))));
55 }
56
57 [Test]
58 public void Basic5()
59 {
60 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
61 Child.SelectMany(t => t.Parent.GrandChildren),
62 db.Child.SelectMany(t => t.Parent.GrandChildren)));
63 }
64
65 [Test]
66 public void Basic6()
67 {
68 ForEachProvider(db => AreEqual(
69 Parent.SelectMany(p => p.Children.Select(_ => _.ParentID + 1).Where(_ => _ > 1)),
70 db.Parent.SelectMany(p => p.Children.Select(_ => _.ParentID + 1).Where(_ => _ > 1))));
71 }
72
73 [Test]
74 public void Basic61()
75 {
76 ForEachProvider(db => AreEqual(
77 Parent.SelectMany(p => p.Children.Select(_ => _.ParentID + 1).Where(_ => _ > 1 || _ > 2)).Where(_ => _ > 0 || _ > 3),
78 db.Parent.SelectMany(p => p.Children.Select(_ => _.ParentID + 1).Where(_ => _ > 1 || _ > 2)).Where(_ => _ > 0 || _ > 3)));
79 }
80
81 [Test]
82 public void Basic62()
83 {
84 ForEachProvider(new[] { ProviderName.Access },
85 db => AreEqual(
86 Parent.SelectMany(p => p.Children.Select(_ => _.ParentID + p.ParentID).Where(_ => _ > 1)),
87 db.Parent.SelectMany(p => p.Children.Select(_ => _.ParentID + p.ParentID).Where(_ => _ > 1))));
88 }
89
90 [Test]
91 public void Basic7()
92 {
93 ForEachProvider(db => AreEqual(
94 Parent.SelectMany(p => p.Children),
95 db.Parent.SelectMany(p => p.Children)));
96 }
97
98 [Test]
99 public void Basic8()
100 {
101 ForEachProvider(db => AreEqual(
102 Parent.SelectMany(p => p.Children.SelectMany(t => t.GrandChildren)),
103 db.Parent.SelectMany(p => p.Children.SelectMany(t => t.GrandChildren))));
104 }
105
106 [Test]
107 public void Basic9()
108 {
109 ForEachProvider(db => AreEqual(
110 Parent.SelectMany(p => p.Children.SelectMany(t => p.GrandChildren)),
111 db.Parent.SelectMany(p => p.Children.SelectMany(t => p.GrandChildren))));
112 }
113
114 [Test]
115 public void Basic10()
116 {
117 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
118 Child.GroupBy(o => o.ParentID2).SelectMany(g => g.Select(o => o.Parent)),
119 db.Child.GroupBy(o => o.ParentID2).SelectMany(g => g.Select(o => o.Parent))));
120 }
121
122 [Test]
123 public void Basic11()
124 {
125 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
126 Child
127 .GroupBy(o => o.ParentID2)
128 .SelectMany(g => g.Select(o => o.ParentID)),
129 db.Child
130 .GroupBy(o => o.ParentID2)
131 .SelectMany(g => db.Child.Where(o => o.ParentID2 == g.Key).Select(o => o.ParentID))));
132 }
133
134 [Test]
135 public void Test1()
136 {
137 TestJohn(db =>
138 {
139 var q = db.Person.Select(p => p);
140
141 return db.Person
142 .SelectMany(p1 => q, (p1, p2) => new { p1, p2 })
143 .Where (t => t.p1.ID == t.p2.ID && t.p1.ID == 1)
144 .Select (t => new Person { ID = t.p1.ID, FirstName = t.p2.FirstName });
145 });
146 }
147
148 [Test]
149 public void Test11()
150 {
151 TestJohn(db => db.Person
152 .SelectMany(p1 => db.Person.Select(p => p), (p1, p2) => new { p1, p2 })
153 .Where (t => t.p1.ID == t.p2.ID && t.p1.ID == 1)
154 .Select (t => new Person { ID = t.p1.ID, FirstName = t.p2.FirstName }));
155 }
156
157 [Test]
158 public void Test21()
159 {
160 TestJohn(db =>
161 from p1 in from p in db.Person select new { ID1 = p.ID, p.LastName }
162 from p2 in from p in db.Person select new { ID2 = p.ID, p.FirstName }
163 from p3 in from p in db.Person select new { ID3 = p.ID, p.LastName }
164 where p1.ID1 == p2.ID2 && p1.LastName == p3.LastName && p1.ID1 == 1
165 select new Person { ID = p1.ID1, FirstName = p2.FirstName, LastName = p3.LastName } );
166 }
167
168 [Test]
169 public void Test22()
170 {
171 TestJohn(db =>
172 from p1 in from p in db.Person select p
173 from p2 in from p in db.Person select p
174 from p3 in from p in db.Person select p
175 where p1.ID == p2.ID && p1.LastName == p3.LastName && p1.ID == 1
176 select new Person { ID = p1.ID, FirstName = p2.FirstName, LastName = p3.LastName } );
177 }
178
179 [Test]
180 public void Test31()
181 {
182 TestJohn(db =>
183 from p in
184 from p in
185 from p in db.Person
186 where p.ID == 1
187 select new { p, ID = p.ID + 1 }
188 where p.ID == 2
189 select new { p, ID = p.ID + 1 }
190 where p.ID == 3
191 select p.p.p);
192 }
193
194 [Test]
195 public void Test32()
196 {
197 ForEachProvider(db =>
198 {
199 var q =
200 from p in
201 from p in
202 from p in db.Person
203 where p.ID == 1
204 select new { p, ID = p.ID + 1 }
205 where p.ID == 2
206 select new { p, ID = p.ID + 1 }
207 where p.ID == 3
208 select new { p.p.p };
209
210 var list = q.ToList();
211
212 Assert.AreEqual(1, list.Count);
213
214 var person = list[0].p;
215
216 Assert.AreEqual(1, person.ID);
217 Assert.AreEqual("John", person.FirstName);
218 });
219 }
220
221 [Test]
222 public void SubQuery1()
223 {
224 TestJohn(db =>
225 {
226 var id = 1;
227 var q = from p in db.Person where p.ID == id select p;
228
229 return
230 from p1 in db.Person
231 from p2 in q
232 where p1.ID == p2.ID
233 select new Person { ID = p1.ID, FirstName = p2.FirstName };
234 });
235 }
236
237 public void SubQuery2(ITestDataContext db)
238 {
239 var q1 = from p in db.Person where p.ID == 1 || p.ID == 2 select p;
240 var q2 = from p in db.Person where !(p.ID == 2) select p;
241
242 var q =
243 from p1 in q1
244 from p2 in q2
245 where p1.ID == p2.ID
246 select new Person { ID = p1.ID, FirstName = p2.FirstName };
247
248 foreach (var person in q)
249 {
250 Assert.AreEqual(1, person.ID);
251 Assert.AreEqual("John", person.FirstName);
252 }
253 }
254
255 [Test]
256 public void SubQuery2()
257 {
258 ForEachProvider(db =>
259 {
260 SubQuery2(db);
261 SubQuery2(db);
262 });
263 }
264
265 IQueryable<Person> GetPersonQuery(ITestDataContext db, int id)
266 {
267 return from p in db.Person where p.ID == id select new Person { ID = p.ID + 1, FirstName = p.FirstName };
268 }
269
270 [Test]
271 public void SubQuery3()
272 {
273 TestJohn(db =>
274 {
275 var q = GetPersonQuery(db, 1);
276
277 return
278 from p1 in db.Person
279 from p2 in q
280 where p1.ID == p2.ID - 1
281 select new Person { ID = p1.ID, FirstName = p2.FirstName };
282 });
283 }
284
285 [Test]
286 public void OneParam1()
287 {
288 TestJohn(db => db.Person.SelectMany(p => db.Person).Where(t => t.ID == 1).Select(t => t));
289 }
290
291 [Test]
292 public void OneParam2()
293 {
294 ForEachProvider(db => AreEqual(
295 Parent.SelectMany(p => p.Children).Where(t => t.ParentID == 1).Select(t => t),
296 db.Parent.SelectMany(p => p.Children).Where(t => t.ParentID == 1).Select(t => t)));
297 }
298
299 [Test]
300 public void OneParam3()
301 {
302 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
303 Child.SelectMany(p => p.Parent.GrandChildren).Where(t => t.ParentID == 1).Select(t => t),
304 db.Child.SelectMany(p => p.Parent.GrandChildren).Where(t => t.ParentID == 1).Select(t => t)));
305 }
306
307 [Test]
308 public void ScalarQuery()
309 {
310 TestJohn(db =>
311 from p1 in db.Person
312 from p2 in (from p in db.Person select p.ID)
313 where p1.ID == p2
314 select new Person { ID = p2, FirstName = p1.FirstName }
315 );
316 }
317
318 [Test]
319 public void SelectManyLeftJoin1()
320 {
321 ForEachProvider(db => Assert.AreEqual(
322 (from p in Parent
323 from c in p.Children.Select(o => new { o.ChildID, p.ParentID }).DefaultIfEmpty()
324 select new { p.Value1, o = c }).Count(),
325 (from p in db.Parent
326 from c in p.Children.Select(o => new { o.ChildID, p.ParentID }).DefaultIfEmpty()
327 select new { p.Value1, o = c }).AsEnumerable().Count()));
328 }
329
330 [Test]
331 public void SelectManyLeftJoin2()
332 {
333 ForEachProvider(db => AreEqual(
334 from p in Parent
335 from ch in (from c in Child where p.ParentID == c.ParentID select c).DefaultIfEmpty()
336 select ch,
337 from p in db.Parent
338 from ch in (from c in db.Child where p.ParentID == c.ParentID select c).DefaultIfEmpty()
339 select ch));
340 }
341
342 [Test]
343 public void SelectManyLeftJoin3()
344 {
345 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
346 from p in Parent
347 from ch in Child.DefaultIfEmpty()
348 where p.ParentID == ch.ParentID
349 select ch,
350 from p in db.Parent
351 from ch in db.Child.DefaultIfEmpty()
352 where p.ParentID == ch.ParentID
353 select ch));
354 }
355
356 [Test]
357 public void SelectManyLeftJoin4()
358 {
359 ForEachProvider(db => AreEqual(
360 from p in Parent
361 from ch in (from c in Child where p.ParentID == c.ParentID select c).DefaultIfEmpty()
362 select new { p.ParentID, ch },
363 from p in db.Parent
364 from ch in (from c in db.Child where p.ParentID == c.ParentID select c).DefaultIfEmpty()
365 select new { p.ParentID, ch }));
366 }
367
368 [Test]
369 public void SelectManyLeftJoinCount()
370 {
371 var expected =
372 from p in Parent
373 from c in p.Children.Select(o => new { o.ChildID, p.ParentID }).DefaultIfEmpty()
374 select new { p.Value1, o = c };
375
376 ForEachProvider(db => Assert.AreEqual(expected.Count(),
377 (from p in db.Parent
378 from c in p.Children.Select(o => new { o.ChildID, p.ParentID }).DefaultIfEmpty()
379 select new { p.Value1, n = c.ChildID + 1, o = c }).Count()));
380 }
381
382 [Test]
383 public void TestJoin1()
384 {
385 ForEachProvider(db => AreEqual(
386 from p in
387 from p in Parent
388 from g in p.GrandChildren
389 join c in Child on g.ChildID equals c.ChildID
390 join t in Types on c.ParentID equals t.ID
391 select c
392 join t in Person on p.ParentID equals t.ID
393 select p,
394 from p in
395 from p in db.Parent
396 from g in p.GrandChildren
397 join c in db.Child on g.ChildID equals c.ChildID
398 join t in db.Types on c.ParentID equals t.ID
399 select c
400 join t in db.Person on p.ParentID equals t.ID
401 select p));
402 }
403
404 [Test]
405 public void Test3()
406 {
407 ForEachProvider(new[] { ProviderName.Access }, db => Assert.AreEqual(
408 (from p in Parent
409 from g in p.GrandChildren
410 from t in Person
411 let c = g.Child
412 select c).Count(),
413 (from p in db.Parent
414 from g in p.GrandChildren
415 from t in db.Person
416 let c = g.Child
417 select c).Count()));
418 }
419
420 [Test]
421 public void Test4()
422 {
423 ForEachProvider(db => Assert.AreEqual(
424 (from p in Parent
425 from g in p.GrandChildren
426 join c in db.Child on g.ChildID equals c.ChildID
427 join t in db.Types on c.ParentID equals t.ID
428 select c).Count(),
429 (from p in db.Parent
430 from g in p.GrandChildren
431 join c in db.Child on g.ChildID equals c.ChildID
432 join t in db.Types on c.ParentID equals t.ID
433 select c).Count()));
434 }
435
436 [Test]
437 public void Test5()
438 {
439 ForEachProvider(new[] { ProviderName.Access }, db =>
440 {
441 var q3 =
442 from p in db.Parent
443 from g in db.GrandChild
444 from c in db.Parent2
445 select g.Child;
446
447 q3.ToList();
448 });
449 }
450
451 [Test]
452 public void Test6()
453 {
454 ForEachProvider(new[] { ProviderName.Access }, db =>
455 {
456 var q3 =
457 from p in db.Parent
458 from g in db.GrandChild
459 from c in db.Parent2
460 let r = g.Child
461 select g.Child;
462
463 q3.ToList();
464 });
465 }
466
467 [Test]
468 public void Test7()
469 {
470 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
471 from p in db.Parent
472 from g in p.GrandChildren
473 from c in db.Parent2
474 let r = g.Child
475 where p.ParentID == g.ParentID
476 select r,
477 from p in db.Parent
478 from g in p.GrandChildren
479 from c in db.Parent2
480 let r = g.Child
481 where p.ParentID == g.ParentID
482 select r));
483 }
484
485 [Test]
486 public void Test8()
487 {
488 ForEachProvider(new[] { ProviderName.Access }, db =>
489 {
490 var q2 =
491 from p in
492 from p in db.Parent
493 join c in db.GrandChild on p.ParentID equals c.ParentID
494 select p
495 from g in p.GrandChildren
496 from c in db.Parent2
497 let r = g.Child
498 where
499 p.ParentID == g.ParentID
500 select r;
501
502 q2.ToList();
503 });
504 }
505
506 [Test]
507 public void Test81()
508 {
509 ForEachProvider(db =>
510 {
511 var q2 =
512 from p in
513 from p in db.Parent
514 join c in db.GrandChild on p.ParentID equals c.ParentID
515 select p
516 from g in p.GrandChildren
517 //from c in db.Parent2
518 let r = g.Child
519 where
520 p.ParentID == g.ParentID
521 select r;
522
523 q2.ToList();
524 });
525 }
526
527 [Test]
528 public void Test9()
529 {
530 ForEachProvider(new[] { ProviderName.Access }, db =>
531 {
532 var q1 = db.Types.Where(_ => _.ID > 1).Where(_ => _.ID > 2);
533
534 var q2 =
535 from p in db.Parent
536 join c in db.GrandChild on p.ParentID equals c.ParentID
537 join t in q1 on c.ParentID equals t.ID
538 where p.ParentID == 1
539 select p;
540
541 q2 = q2.Distinct().OrderBy(_ => _.ParentID);
542
543 var q3 =
544 from p in q2
545 from g in p.GrandChildren
546 from c in db.Parent2
547 let r = g.Child
548 where
549 p.ParentID == g.ParentID && g.ParentID == c.ParentID
550 select r;
551
552 q3 = q3.Where(_ => _.ChildID == 1);
553
554 q3.ToList();
555 });
556 }
557
558 [Test]
559 public void Test91()
560 {
561 ForEachProvider(new[] { ProviderName.Access }, db =>
562 {
563 var q2 =
564 from p in db.Parent
565 join c in db.GrandChild on p.ParentID equals c.ParentID
566 where p.ParentID == 1
567 select p;
568
569 q2 = q2.Distinct();
570
571 var q3 =
572 from p in q2
573 from g in p.GrandChildren
574 let r = g.Child
575 where
576 p.ParentID == g.ParentID
577 select r;
578
579 q3.ToList();
580 });
581 }
582
583 /////[Test]
584 public void Test92()
585 {
586 ForEachProvider(db => AreEqual(
587 db.Parent
588 .SelectMany(c => c.Children, (c, p) => new { c, p, })
589 .Select(_ => new { _.c, p = new Child { ParentID = _.c.ParentID, ChildID = _.p.ChildID } })
590 .SelectMany(ch => ch.p.GrandChildren, (ch, t) => new { t, ch }),
591 db.Parent
592 .SelectMany(c => c.Children, (c, p) => new { c, p, })
593 .Select(_ => new { _.c, p = new Child { ParentID = _.c.ParentID, ChildID = _.p.ChildID } })
594 .SelectMany(ch => ch.p.GrandChildren, (ch, t) => new { t, ch })));
595 }
596
597 [Test]
598 public void DoubleConntectionTest()
599 {
600 ForEachProvider(db =>
601 {
602 var p1 = 1;
603 var p2 = 1;
604
605 var q1 = db.Parent
606 .GroupJoin(
607 db.Child,
608 x => new { x.ParentID },
609 y => new { y.ParentID },
610 (x,y) => new { Parent = x, Child = y })
611 .SelectMany(
612 y => y.Child.DefaultIfEmpty(),
613 (x,y) => new { x.Parent, Child = x.Child.FirstOrDefault() })
614 .Where(x => x.Parent.Value1 > p1 && x.Parent.ParentID > p2)
615 .OrderBy(x => x.Parent.Value1);
616
617 var q2 =
618 from x in db.Parent
619 join y in db.Child on new { x.ParentID } equals new { y.ParentID } into g
620 from y in g.DefaultIfEmpty()
621 where x.Value1 > p1 && x.ParentID > p2
622 orderby x.Value1
623 select new
624 {
625 x, y
626 };
627
628 var q3 = db.Parent
629 .GroupJoin(
630 db.Child,
631 x => new { x.ParentID },
632 y => new { y.ParentID },
633 (x,y) => new { Parent = x, Child = y })
634 .SelectMany(
635 y => y.Child.DefaultIfEmpty(),
636 (x,y) => new { x.Parent, Child = y })
637 .Where (x => x.Parent.Value1 > p1 && x.Parent.ParentID > p2)
638 .OrderBy(x => x.Parent.Value1)
639 /*.Select (x => new
640 {
641 x.Parent,
642 x.Child
643 })*/;
644
645 var q4 =
646 from x in db.Parent
647 where x.Value1 > p1 && x.ParentID > p2
648 orderby x.Value1
649 select new
650 {
651 x, y = x.Children.FirstOrDefault()
652 };
653
654 foreach (var item in q1)
655 {
656 }
657 });
658 }
659
660 void Foo(Expression<Func<object[],object>> func)
661 {
662 /*
663 ParameterExpression ps;
664 Expression.Lambda<Func<object[],object>>(
665 Expression.Add(
666 Expression.Convert(
667 Expression.ArrayIndex(
668 ps = Expression.Parameter(typeof(object[]), "p"),
669 Expression.Constant(0, typeof(int))),
670 typeof(string)),
671 Expression.Convert(
672 Expression.Convert(
673 Expression.ArrayIndex(
674 ps,
675 Expression.Constant(1, typeof(int))),
676 typeof(int)),
677 typeof(object)),
678 (MethodInfo)methodof(string.Concat)),
679 new ParameterExpression[] { ps });
680 */
681 }
682
683 Dictionary<string,string> _dic = new Dictionary<string,string>();
684
685 void Bar()
686 {
687 Foo(p => (string)p[0] + (int)p[1]);
688 }
689
690 //[Test]
691 public void Test___()
692 {
693 Bar();
694 }
695 }
696 }