comparison UnitTests/Linq/ConvertExpression.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.Linq;
3
4 using BLToolkit.Data.DataProvider;
5
6 using NUnit.Framework;
7
8 namespace Data.Linq
9 {
10 [TestFixture]
11 public class ConvertExpression : TestBase
12 {
13 [Test]
14 public void Select1()
15 {
16 ForEachProvider(db => AreEqual(
17 from p in Parent
18 let children = p.Children.Where(c => c.ParentID > 1)
19 select children.Sum(c => c.ChildID),
20 from p in db.Parent
21 let children = p.Children.Where(c => c.ParentID > 1)
22 select children.Sum(c => c.ChildID)));
23 }
24
25 [Test]
26 public void Select2()
27 {
28 ForEachProvider(db => AreEqual(
29 from p in Parent
30 let children1 = p.Children.Where(c => c.ParentID > 1)
31 let children2 = children1.Where(c => c.ParentID < 10)
32 select children2.Sum(c => c.ChildID),
33 from p in db.Parent
34 let children1 = p.Children.Where(c => c.ParentID > 1)
35 let children2 = children1.Where(c => c.ParentID < 10)
36 select children2.Sum(c => c.ChildID)));
37 }
38
39 [Test]
40 public void Select3()
41 {
42 ForEachProvider(db => AreEqual(
43 Parent
44 .Select(p => new { children1 = p.Children. Where(c => c.ParentID > 1) })
45 .Select(t => new { children2 = t.children1.Where(c => c.ParentID < 10) })
46 .Select(t => t.children2.Sum(c => c.ChildID)),
47 db.Parent
48 .Select(p => new { children1 = p.Children. Where(c => c.ParentID > 1) })
49 .Select(t => new { children2 = t.children1.Where(c => c.ParentID < 10) })
50 .Select(t => t.children2.Sum(c => c.ChildID))));
51 }
52
53 [Test]
54 public void Select4()
55 {
56 ForEachProvider(db => AreEqual(
57 Parent
58 .Select(p => p.Children. Where(c => c.ParentID > 1))
59 .Select(t => t.Where(c => c.ParentID < 10))
60 .Select(t => t.Sum(c => c.ChildID)),
61 db.Parent
62 .Select(p => p.Children. Where(c => c.ParentID > 1))
63 .Select(t => t.Where(c => c.ParentID < 10))
64 .Select(t => t.Sum(c => c.ChildID))));
65 }
66
67 [Test]
68 public void Where1()
69 {
70 ForEachProvider(db => AreEqual(
71 from p in Parent
72 let children1 = p.Children.Where(c => c.ParentID > 1)
73 let children2 = children1.Where(c => c.ParentID < 10)
74 where children1.Any()
75 select children2.Sum(c => c.ChildID),
76 from p in db.Parent
77 let children1 = p.Children.Where(c => c.ParentID > 1)
78 let children2 = children1.Where(c => c.ParentID < 10)
79 where children1.Any()
80 select children2.Sum(c => c.ChildID)));
81 }
82
83 [Test]
84 public void Where2()
85 {
86 ForEachProvider(db => AreEqual(
87 from p in Parent
88 let children1 = p.Children.Where(c => c.ParentID > 1)
89 where children1.Any()
90 let children2 = children1.Where(c => c.ParentID < 10)
91 select children2.Sum(c => c.ChildID),
92 from p in db.Parent
93 let children1 = p.Children.Where(c => c.ParentID > 1)
94 where children1.Any()
95 let children2 = children1.Where(c => c.ParentID < 10)
96 select children2.Sum(c => c.ChildID)));
97 }
98
99 [Test]
100 public void Where3()
101 {
102 ForEachProvider(db => AreEqual(
103 from p in Parent
104 let children1 = p.Children.Where(c => c.ParentID > 1)
105 let children2 = children1.Where(c => c.ParentID < 10)
106 where children2.Any()
107 select children2.Sum(c => c.ChildID),
108 from p in db.Parent
109 let children1 = p.Children.Where(c => c.ParentID > 1)
110 let children2 = children1.Where(c => c.ParentID < 10)
111 where children2.Any()
112 select children2.Sum(c => c.ChildID)));
113 }
114
115 //[Test]
116 public void Where4()
117 {
118 ForEachProvider(db => AreEqual(
119 Parent
120 .Select(p => new { p, children1 = p.Children. Where(c => c.ParentID > 1) })
121 .Where (t => t.children1.Any()),
122 db.Parent
123 .Select(p => new { p, children1 = p.Children. Where(c => c.ParentID > 1) })
124 .Where (t => t.children1.Any())));
125 }
126
127 //[Test]
128 public void Where5()
129 {
130 ForEachProvider(db => AreEqual(
131 Parent
132 .Select(p => new { children1 = p.Children. Where(c => c.ParentID > 1) })
133 .Where (t => t.children1.Any()),
134 db.Parent
135 .Select(p => new { children1 = p.Children. Where(c => c.ParentID > 1) })
136 .Where (t => t.children1.Any())));
137 }
138
139 //[Test]
140 public void Where6()
141 {
142 ForEachProvider(db => AreEqual(
143 Parent
144 .Select(p => p.Children. Where(c => c.ParentID > 1))
145 .Where (t => t.Any()),
146 db.Parent
147 .Select(p => p.Children. Where(c => c.ParentID > 1))
148 .Where (t => t.Any())));
149 }
150
151 [Test]
152 public void Any1()
153 {
154 ForEachProvider(db => Assert.AreEqual(
155 Parent
156 .Select(p => new { p, children1 = p.Children.Where(c => c.ParentID > 1) })
157 .Any(p => p.children1.Any()),
158 db.Parent
159 .Select(p => new { p, children1 = p.Children.Where(c => c.ParentID > 1) })
160 .Any(p => p.children1.Any())));
161 }
162
163 [Test]
164 public void Any2()
165 {
166 ForEachProvider(db => Assert.AreEqual(
167 Parent
168 .Select(p => p.Children.Where(c => c.ParentID > 1))
169 .Any(p => p.Any()),
170 db.Parent
171 .Select(p => p.Children.Where(c => c.ParentID > 1))
172 .Any(p => p.Any())));
173 }
174
175 [Test]
176 public void Any3()
177 {
178 ForEachProvider(db => Assert.AreEqual(
179 Parent
180 .Select(p => new { p, children1 = p.Children.Where(c => c.ParentID > 1) })
181 .Where(p => p.children1.Any())
182 .Any(),
183 db.Parent
184 .Select(p => new { p, children1 = p.Children.Where(c => c.ParentID > 1) })
185 .Where(p => p.children1.Any())
186 .Any()));
187 }
188
189 //[Test]
190 public void Any4()
191 {
192 ForEachProvider(db => Assert.AreEqual(
193 Parent
194 .Select(p => new { children1 = p.Children.Where(c => c.ParentID > 1) })
195 .Where(p => p.children1.Any())
196 .Any(),
197 db.Parent
198 .Select(p => new { children1 = p.Children.Where(c => c.ParentID > 1) })
199 .Where(p => p.children1.Any())
200 .Any()));
201 }
202
203
204 [Test]
205 public void LetTest1([DataContexts(ProviderName.SqlCe, ProviderName.Informix, ProviderName.Sybase)] string context)
206 {
207 using (var db = GetDataContext(context))
208 AreEqual(
209 from p in Parent
210 let ch = p.Children
211 where ch.FirstOrDefault() != null
212 select ch.FirstOrDefault().ParentID
213 ,
214 from p in db.Parent
215 let ch = p.Children
216 where ch.FirstOrDefault() != null
217 select ch.FirstOrDefault().ParentID);
218 }
219
220 [Test]
221 public void LetTest2()
222 {
223 ForEachProvider(
224 new[] { ProviderName.SqlCe, ProviderName.Informix, ProviderName.Sybase },
225 db => AreEqual(
226 from p in Parent
227 let ch = p.Children
228 where ch.FirstOrDefault() != null
229 select p
230 ,
231 from p in db.Parent
232 let ch = p.Children
233 where ch.FirstOrDefault() != null
234 select p));
235 }
236
237 [Test]
238 public void LetTest3()
239 {
240 ForEachProvider(
241 new[] { ProviderName.Informix, ProviderName.Sybase },
242 db => AreEqual(
243 from p in Parent
244 let ch = Child
245 select ch.FirstOrDefault().ParentID
246 ,
247 from p in db.Parent
248 let ch = db.Child
249 select ch.FirstOrDefault().ParentID));
250 }
251
252 [Test]
253 public void LetTest4()
254 {
255 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
256
257 ForEachProvider(
258 new[] { ProviderName.Informix, ProviderName.Sybase },
259 db => AreEqual(
260 from p in Parent
261 let ch1 = Child.Where(c => c.ParentID == p.ParentID)
262 let ch2 = ch1.Where(c => c.ChildID > -100)
263 select new
264 {
265 Any = ch2.Any(),
266 Count = ch2.Count(),
267 First1 = ch2.FirstOrDefault(c => c.ParentID > 0),
268 First2 = ch2.FirstOrDefault()
269 }
270 ,
271 from p in db.Parent
272 let ch1 = db.Child.Where(c => c.ParentID == p.ParentID)
273 let ch2 = ch1.Where(c => c.ChildID > -100)
274 select new
275 {
276 Any = ch2.Any(),
277 Count = ch2.Count(),
278 First1 = ch2.FirstOrDefault(c => c.ParentID > 0),
279 First2 = ch2.FirstOrDefault()
280 }));
281
282 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
283 }
284
285 [Test]
286 public void LetTest5()
287 {
288 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
289
290 ForEachProvider(
291 new[] { ProviderName.Informix, ProviderName.Sybase },
292 db => AreEqual(
293 from p in Parent
294 let ch1 = Child.Where(c => c.ParentID == p.ParentID)
295 let ch2 = ch1.Where(c => c.ChildID > -100)
296 select new
297 {
298 Any = ch2.Any(),
299 Count = ch2.Count(),
300 First1 = ch2.FirstOrDefault(c => c.ParentID > 0) == null ? 0 : ch2.FirstOrDefault(c => c.ParentID > 0).ParentID,
301 First2 = ch2.FirstOrDefault()
302 }
303 ,
304 from p in db.Parent
305 let ch1 = db.Child.Where(c => c.ParentID == p.ParentID)
306 let ch2 = ch1.Where(c => c.ChildID > -100)
307 select new
308 {
309 Any = ch2.Any(),
310 Count = ch2.Count(),
311 First1 = ch2.FirstOrDefault(c => c.ParentID > 0).ParentID,
312 First2 = ch2.FirstOrDefault()
313 }));
314
315 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
316 }
317
318 [Test]
319 public void LetTest6()
320 {
321 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
322 //BLToolkit.Common.Configuration.Linq.GenerateExpressionTest = true;
323
324 ForEachProvider(
325 new[] { ProviderName.Informix, ProviderName.Sybase },
326 db => AreEqual(
327 (
328 from p in Parent
329 let ch1 = Child.Where(c => c.ParentID == p.ParentID)
330 let ch2 = ch1.Where(c => c.ChildID > -100)
331 select new
332 {
333 p.ParentID,
334 Any = ch2.Any(),
335 Count = ch2.Count(),
336 First1 = ch2.FirstOrDefault(c => c.ParentID > 0) == null ? 0 : ch2.FirstOrDefault(c => c.ParentID > 0).ParentID,
337 First2 = ch2.FirstOrDefault()
338 }
339 ).Where(t => t.ParentID > 0)
340 ,
341 (
342 from p in db.Parent
343 let ch1 = db.Child.Where(c => c.ParentID == p.ParentID)
344 let ch2 = ch1.Where(c => c.ChildID > -100)
345 select new
346 {
347 p.ParentID,
348 Any = ch2.Any(),
349 Count = ch2.Count(),
350 First1 = ch2.FirstOrDefault(c => c.ParentID > 0).ParentID,
351 First2 = ch2.FirstOrDefault()
352 }
353 ).Where(t => t.ParentID > 0)));
354
355 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
356 }
357
358 [Test]
359 public void LetTest7()
360 {
361 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
362
363 ForEachProvider(
364 new[] { ProviderName.Informix, ProviderName.Sybase },
365 db => AreEqual(
366 (
367 from p in Parent
368 let ch1 = Child.Where(c => c.ParentID == p.ParentID)
369 let ch2 = ch1.Where(c => c.ChildID > -100)
370 select new
371 {
372 p.ParentID,
373 Any = ch2.Any(),
374 Count = ch2.Count(),
375 First1 = ch2.FirstOrDefault(c => c.ParentID > 0) == null ? 0 : ch2.FirstOrDefault(c => c.ParentID > 0).ParentID,
376 First2 = ch2.FirstOrDefault()
377 }
378 ).Where(t => t.ParentID > 0).Take(5000)
379 ,
380 (
381 from p in db.Parent
382 let ch1 = db.Child.Where(c => c.ParentID == p.ParentID)
383 let ch2 = ch1.Where(c => c.ChildID > -100)
384 select new
385 {
386 p.ParentID,
387 Any = ch2.Any(),
388 Count = ch2.Count(),
389 First1 = ch2.FirstOrDefault(c => c.ParentID > 0).ParentID,
390 First2 = ch2.FirstOrDefault()
391 }
392 ).Where(t => t.ParentID > 0).Take(5000)));
393
394 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
395 }
396
397 [Test]
398 public void LetTest8()
399 {
400 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
401
402 ForEachProvider(
403 db => AreEqual(
404 from p in Parent
405 let ch1 = Child.Where(c => c.ParentID == p.ParentID)
406 let ch2 = ch1.Where(c => c.ChildID > -100)
407 let ch3 = ch2.FirstOrDefault(c => c.ParentID > 0)
408 select new
409 {
410 First1 = ch3 == null ? 0 : ch3.ParentID,
411 Any = ch2.Any(),
412 Count = ch2.Count(),
413 First2 = ch2.FirstOrDefault()
414 }
415 ,
416 from p in db.Parent
417 let ch1 = db.Child.Where(c => c.ParentID == p.ParentID)
418 let ch2 = ch1.Where(c => c.ChildID > -100)
419 let ch3 = ch2.FirstOrDefault(c => c.ParentID > 0)
420 select new
421 {
422 First1 = ch3 == null ? 0 : ch3.ParentID,
423 Any = ch2.Any(),
424 Count = ch2.Count(),
425 First2 = ch2.FirstOrDefault()
426 }));
427
428 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
429 }
430
431 [Test]
432 public void LetTest9()
433 {
434 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
435
436 ForEachProvider(
437 db => AreEqual(
438 (
439 from p in Parent
440 let ch1 = Child.Where(c => c.ParentID == p.ParentID)
441 select new
442 {
443 First = ch1.FirstOrDefault()
444 }
445 ).Take(10)
446 ,
447 (
448 from p in db.Parent
449 let ch1 = db.Child.Where(c => c.ParentID == p.ParentID)
450 select new
451 {
452 First = ch1.FirstOrDefault()
453 }
454 ).Take(10)));
455
456 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
457 }
458
459 [Test]
460 public void LetTest10()
461 {
462 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
463
464 ForEachProvider(
465 db => Assert.AreEqual(
466 (
467 from p in Parent
468 let ch1 = Child.Where(c => c.ParentID == p.ParentID)
469 select new
470 {
471 First = ch1.FirstOrDefault()
472 }
473 ).Any()
474 ,
475 (
476 from p in db.Parent
477 let ch1 = db.Child.Where(c => c.ParentID == p.ParentID)
478 select new
479 {
480 First = ch1.FirstOrDefault()
481 }
482 ).Any()));
483
484 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
485 }
486
487 [Test]
488 public void LetTest11([DataContexts] string context)
489 {
490 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = true;
491
492 using (var db = GetDataContext(context))
493 AreEqual(
494 from p in Parent
495 let ch1 = Child.FirstOrDefault(c => c.ParentID > 0)
496 let ch2 = Child.Where(c => c.ChildID > -100)
497 select new
498 {
499 First1 = ch1 == null ? 0 : ch1.ParentID,
500 First2 = ch2.FirstOrDefault()
501 }
502 ,
503 from p in db.Parent
504 let ch1 = db.Child.FirstOrDefault(c => c.ParentID > 0)
505 let ch2 = Child.Where(c => c.ChildID > -100)
506 select new
507 {
508 First1 = ch1 == null ? 0 : ch1.ParentID,
509 First2 = ch2.FirstOrDefault()
510 });
511
512 BLToolkit.Common.Configuration.Linq.AllowMultipleQuery = false;
513 }
514 }
515 }