Mercurial > pub > bltoolkit
comparison UnitTests/Linq/ParserTest.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 using System.Linq.Expressions; | |
4 using System.Reflection; | |
5 | |
6 using BLToolkit.Data.Linq; | |
7 using BLToolkit.Data.Linq.Builder; | |
8 using BLToolkit.Data.Sql; | |
9 | |
10 using Data.Linq.Model; | |
11 | |
12 using NUnit.Framework; | |
13 | |
14 namespace Data.Linq | |
15 { | |
16 [TestFixture] | |
17 public class ParserTest : TestBase | |
18 { | |
19 static ParserTest() | |
20 { | |
21 ExpressionBuilder.AddBuilder(new ContextParser()); | |
22 } | |
23 | |
24 #region IsExpressionTable | |
25 | |
26 [Test] | |
27 public void IsExpressionTable1() | |
28 { | |
29 using (var db = new TestDbManager()) | |
30 { | |
31 var ctx = db.Parent | |
32 .Select (p1 => p1.ParentID) | |
33 .GetContext(); | |
34 | |
35 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
36 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
37 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
38 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
39 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
40 } | |
41 } | |
42 | |
43 [Test] | |
44 public void IsExpressionTable2() | |
45 { | |
46 using (var db = new TestDbManager()) | |
47 { | |
48 var ctx = db.Parent | |
49 .Select (p1 => p1.ParentID + 1) | |
50 .GetContext(); | |
51 | |
52 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
53 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
54 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
55 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
56 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
57 } | |
58 } | |
59 | |
60 [Test] | |
61 public void IsExpressionTable3() | |
62 { | |
63 using (var db = new TestDbManager()) | |
64 { | |
65 var ctx = db.Parent | |
66 .Select (p1 => p1) | |
67 .GetContext(); | |
68 | |
69 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
70 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
71 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
72 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
73 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
74 } | |
75 } | |
76 | |
77 #endregion | |
78 | |
79 #region IsExpressionScalar | |
80 | |
81 [Test] | |
82 public void IsExpressionScalar1() | |
83 { | |
84 using (var db = new TestDbManager()) | |
85 { | |
86 var ctx = db.Parent | |
87 .Select (p1 => p1.ParentID) | |
88 .Select (p2 => p2) | |
89 .GetContext(); | |
90 | |
91 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
92 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
93 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
94 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
95 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
96 } | |
97 } | |
98 | |
99 [Test] | |
100 public void IsExpressionScalar2() | |
101 { | |
102 using (var db = new TestDbManager()) | |
103 { | |
104 var ctx = db.Parent | |
105 .Select (p1 => p1.ParentID + 1) | |
106 .Select (p2 => p2) | |
107 .GetContext(); | |
108 | |
109 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
110 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
111 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
112 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
113 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
114 } | |
115 } | |
116 | |
117 [Test] | |
118 public void IsExpressionScalar3() | |
119 { | |
120 using (var db = new TestDbManager()) | |
121 { | |
122 var ctx = db.Parent | |
123 .Select (p1 => p1) | |
124 .Select (p2 => p2) | |
125 .GetContext(); | |
126 | |
127 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
128 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
129 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
130 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
131 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
132 } | |
133 } | |
134 | |
135 [Test] | |
136 public void IsExpressionScalar4() | |
137 { | |
138 using (var db = new TestDbManager()) | |
139 { | |
140 var ctx = db.Parent | |
141 .Select (p1 => p1.ParentID + 1) | |
142 .Where (p3 => p3 == 1) | |
143 .Select (p2 => p2) | |
144 .GetContext(); | |
145 | |
146 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
147 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
148 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
149 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
150 //Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.SubQuery)); | |
151 } | |
152 } | |
153 | |
154 [Test] | |
155 public void IsExpressionScalar5() | |
156 { | |
157 using (var db = new TestDbManager()) | |
158 { | |
159 var ctx = db.Parent | |
160 .Select (p1 => p1) | |
161 .Select (p2 => p2.ParentID) | |
162 .GetContext(); | |
163 | |
164 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
165 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
166 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
167 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
168 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
169 } | |
170 } | |
171 | |
172 [Test] | |
173 public void IsExpressionScalar6() | |
174 { | |
175 using (var db = new TestDbManager()) | |
176 { | |
177 var ctx = db.Child | |
178 .Select (p => p.Parent) | |
179 .Select (p => p) | |
180 .GetContext(); | |
181 | |
182 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
183 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
184 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
185 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
186 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
187 } | |
188 } | |
189 | |
190 [Test] | |
191 public void IsExpressionScalar7() | |
192 { | |
193 using (var db = new TestDbManager()) | |
194 { | |
195 var ctx = db.Child | |
196 .Select (p => p) | |
197 .Select (p => p) | |
198 .Select (p => p.Parent) | |
199 .GetContext(); | |
200 | |
201 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
202 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
203 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
204 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
205 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
206 } | |
207 } | |
208 | |
209 [Test] | |
210 public void IsExpressionScalar8() | |
211 { | |
212 using (var db = new TestDbManager()) | |
213 { | |
214 var ctx = db.Child | |
215 .Select (p => p) | |
216 .Select (p3 => new { p1 = new { p2 = new { p = p3 } } }) | |
217 .Select (p => p.p1.p2.p.Parent) | |
218 .GetContext(); | |
219 | |
220 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
221 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
222 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
223 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
224 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
225 } | |
226 } | |
227 | |
228 [Test] | |
229 public void IsExpressionScalar9() | |
230 { | |
231 using (var db = new TestDbManager()) | |
232 { | |
233 var ctx = db.Child | |
234 .Select (p => p) | |
235 .Select (p3 => new { p1 = new { p2 = new { p = p3.Parent } } }) | |
236 .Select (p => p.p1.p2.p) | |
237 .GetContext(); | |
238 | |
239 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
240 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
241 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
242 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
243 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
244 } | |
245 } | |
246 | |
247 | |
248 [Test] | |
249 public void IsExpressionScalar10() | |
250 { | |
251 using (var db = new TestDbManager()) | |
252 { | |
253 var ctx = db.Child | |
254 .Select (p => p) | |
255 .Select (p => new { p = new { p } }) | |
256 .Select (p => p.p) | |
257 .GetContext(); | |
258 | |
259 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
260 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
261 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
262 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
263 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
264 } | |
265 } | |
266 | |
267 [Test] | |
268 public void IsExpressionScalar11() | |
269 { | |
270 using (var db = new TestDbManager()) | |
271 { | |
272 var ctx = db.Child | |
273 .Select (p => p) | |
274 .Select (p => new { p = new Child { ChildID = p.ChildID } }) | |
275 .Select (p => p.p) | |
276 .GetContext(); | |
277 | |
278 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
279 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
280 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
281 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
282 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.SubQuery). Result); | |
283 } | |
284 } | |
285 | |
286 #endregion | |
287 | |
288 #region IsExpressionSelect | |
289 | |
290 [Test] | |
291 public void IsExpressionSelect1() | |
292 { | |
293 using (var db = new TestDbManager()) | |
294 { | |
295 var ctx = db.Parent | |
296 .Select (p1 => new { p1.ParentID }) | |
297 .Select (p2 => p2.ParentID) | |
298 .GetContext(); | |
299 | |
300 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
301 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
302 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
303 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
304 } | |
305 } | |
306 | |
307 [Test] | |
308 public void IsExpressionSelect2() | |
309 { | |
310 using (var db = new TestDbManager()) | |
311 { | |
312 var ctx = db.Parent | |
313 .Select (p1 => new { p = p1.ParentID + 1 }) | |
314 .Select (p2 => p2.p) | |
315 .GetContext(); | |
316 | |
317 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
318 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
319 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
320 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
321 } | |
322 } | |
323 | |
324 [Test] | |
325 public void IsExpressionSelect3() | |
326 { | |
327 using (var db = new TestDbManager()) | |
328 { | |
329 var ctx = db.Parent | |
330 .Select (p1 => new { p1 }) | |
331 .Select (p2 => p2.p1) | |
332 .GetContext(); | |
333 | |
334 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
335 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
336 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
337 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
338 } | |
339 } | |
340 | |
341 [Test] | |
342 public void IsExpressionSelect4() | |
343 { | |
344 using (var db = new TestDbManager()) | |
345 { | |
346 var ctx = db.Parent | |
347 .Select (p1 => new { p = p1.ParentID + 1 }) | |
348 .Where (p3 => p3.p == 1) | |
349 .Select (p2 => p2.p) | |
350 .GetContext(); | |
351 | |
352 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
353 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
354 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
355 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
356 } | |
357 } | |
358 | |
359 [Test] | |
360 public void IsExpressionSelect42() | |
361 { | |
362 using (var db = new TestDbManager()) | |
363 { | |
364 var ctx = db.Parent | |
365 .Select (p1 => new { p = p1.ParentID + 1 }) | |
366 .Where (p3 => p3.p == 1) | |
367 .Select (p2 => p2) | |
368 .GetContext(); | |
369 | |
370 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
371 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
372 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
373 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
374 } | |
375 } | |
376 | |
377 [Test] | |
378 public void IsExpressionSelect5() | |
379 { | |
380 using (var db = new TestDbManager()) | |
381 { | |
382 var ctx = db.Parent | |
383 .Select (p1 => new { p1 }) | |
384 .Select (p2 => p2.p1.ParentID) | |
385 .GetContext(); | |
386 | |
387 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
388 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
389 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
390 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
391 } | |
392 } | |
393 | |
394 [Test] | |
395 public void IsExpressionSelect6() | |
396 { | |
397 using (var db = new TestDbManager()) | |
398 { | |
399 var ctx = db.Parent | |
400 .Select (p => new { p }) | |
401 .Select (p => p) | |
402 .GetContext(); | |
403 | |
404 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
405 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
406 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
407 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
408 } | |
409 } | |
410 | |
411 [Test] | |
412 public void IsExpressionSelect7() | |
413 { | |
414 using (var db = new TestDbManager()) | |
415 { | |
416 var ctx = db.Child | |
417 .Select (p => new { p, p.Parent }) | |
418 .Select (p => new { p.Parent, p.p.ChildID }) | |
419 .Select (p => p.Parent) | |
420 .GetContext(); | |
421 | |
422 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
423 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
424 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
425 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
426 } | |
427 } | |
428 | |
429 [Test] | |
430 public void IsExpressionSelect8() | |
431 { | |
432 using (var db = new TestDbManager()) | |
433 { | |
434 var ctx = db.Child | |
435 .Select (p => new { p, p.Parent }) | |
436 .Select (p => new { p.Parent.ParentID, p.p.ChildID }) | |
437 .Select (p => p.ParentID) | |
438 .GetContext(); | |
439 | |
440 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
441 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
442 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
443 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
444 } | |
445 } | |
446 | |
447 [Test] | |
448 public void IsExpressionSelect9() | |
449 { | |
450 using (var db = new TestDbManager()) | |
451 { | |
452 var ctx = db.GrandChild | |
453 .Select (p => new { p, p.Child }) | |
454 .Select (p => new { p.Child.Parent.ParentID, p.p.ChildID }) | |
455 .Select (p => p.ParentID) | |
456 .GetContext(); | |
457 | |
458 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
459 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
460 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
461 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
462 } | |
463 } | |
464 | |
465 [Test] | |
466 public void IsExpressionSelect10() | |
467 { | |
468 using (var db = new TestDbManager()) | |
469 { | |
470 var ctx = db.Parent | |
471 .Select (p => p.Children.Max(c => (int?)c.ChildID) ?? p.Value1) | |
472 .Select (p => p) | |
473 .GetContext(); | |
474 | |
475 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Association).Result); | |
476 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Object). Result); | |
477 Assert.IsFalse(ctx.IsExpression(null, 0, RequestFor.Field). Result); | |
478 Assert.IsTrue (ctx.IsExpression(null, 0, RequestFor.Expression). Result); | |
479 } | |
480 } | |
481 | |
482 #endregion | |
483 | |
484 #region ConvertToIndexTable | |
485 | |
486 [Test] | |
487 public void ConvertToIndexTable1() | |
488 { | |
489 using (var db = new TestDbManager()) | |
490 { | |
491 var ctx = db.Parent1 | |
492 .Select (t => t) | |
493 .GetContext(); | |
494 | |
495 Assert.AreEqual(new[] { 0, 1 }, ctx.ConvertToIndex(null, 0, ConvertFlags.All).Select(_ => _.Index).ToArray()); | |
496 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Key).Select(_ => _.Index).ToArray()); | |
497 } | |
498 } | |
499 | |
500 [Test] | |
501 public void ConvertToIndexTable2() | |
502 { | |
503 using (var db = new TestDbManager()) | |
504 { | |
505 var ctx = db.Parent | |
506 .Select (t => t) | |
507 .GetContext(); | |
508 | |
509 Assert.AreEqual(new[] { 0, 1 }, ctx.ConvertToIndex(null, 0, ConvertFlags.All).Select(_ => _.Index).ToArray()); | |
510 Assert.AreEqual(new[] { 0, 1 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Key).Select(_ => _.Index).ToArray()); | |
511 } | |
512 } | |
513 | |
514 [Test] | |
515 public void ConvertToIndexTable3() | |
516 { | |
517 using (var db = new TestDbManager()) | |
518 { | |
519 var ctx = db.Parent | |
520 .Select (t => t.ParentID) | |
521 .GetContext(); | |
522 | |
523 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Field).Select(_ => _.Index).ToArray()); | |
524 } | |
525 } | |
526 | |
527 [Test] | |
528 public void ConvertToIndexTable4() | |
529 { | |
530 using (var db = new TestDbManager()) | |
531 { | |
532 var ctx = db.Parent | |
533 .Select (t => t.Value1) | |
534 .GetContext(); | |
535 | |
536 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Field).Select(_ => _.Index).ToArray()); | |
537 } | |
538 } | |
539 | |
540 [Test] | |
541 public void ConvertToIndexTable5() | |
542 { | |
543 using (var db = new TestDbManager()) | |
544 { | |
545 var ctx = db.Parent | |
546 .Select (t => new { t = new { t } }) | |
547 .Select (t => t.t.t.ParentID) | |
548 .Select (t => t) | |
549 .GetContext(); | |
550 | |
551 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Field).Select(_ => _.Index).ToArray()); | |
552 } | |
553 } | |
554 | |
555 #endregion | |
556 | |
557 #region ConvertToIndex | |
558 | |
559 [Test] | |
560 public void ConvertToIndexScalar1() | |
561 { | |
562 using (var db = new TestDbManager()) | |
563 { | |
564 var ctx = db.Parent | |
565 .Select (p1 => p1.ParentID) | |
566 .Select (p2 => p2) | |
567 .GetContext(); | |
568 | |
569 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Field).Select(_ => _.Index).ToArray()); | |
570 } | |
571 } | |
572 | |
573 [Test] | |
574 public void ConvertToIndexScalar2() | |
575 { | |
576 using (var db = new TestDbManager()) | |
577 { | |
578 var ctx = db.Parent | |
579 .Select (p1 => p1.ParentID + 1) | |
580 .Select (p2 => p2) | |
581 .GetContext(); | |
582 | |
583 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Field).Select(_ => _.Index).ToArray()); | |
584 } | |
585 } | |
586 | |
587 [Test] | |
588 public void ConvertToIndexScalar3() | |
589 { | |
590 using (var db = new TestDbManager()) | |
591 { | |
592 var ctx = db.Parent | |
593 .Select (p1 => p1.ParentID + 1) | |
594 .Where (p3 => p3 == 1) | |
595 .Select (p2 => p2) | |
596 .GetContext(); | |
597 | |
598 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Field).Select(_ => _.Index).ToArray()); | |
599 } | |
600 } | |
601 | |
602 [Test] | |
603 public void ConvertToIndexScalar4() | |
604 { | |
605 using (var db = new TestDbManager()) | |
606 { | |
607 var ctx = db.Parent | |
608 .Select (p1 => new { p = new { p = p1.ParentID } }) | |
609 .Select (p2 => p2.p.p) | |
610 .GetContext(); | |
611 | |
612 Assert.AreEqual(new[] { 0 }, ctx.ConvertToIndex(null, 0, ConvertFlags.Field).Select(_ => _.Index).ToArray()); | |
613 } | |
614 } | |
615 | |
616 [Test] | |
617 public void ConvertToIndexJoin1() | |
618 { | |
619 using (var db = new TestDbManager()) | |
620 { | |
621 var q2 = | |
622 from gc1 in db.GrandChild | |
623 join max in | |
624 from gch in db.GrandChild | |
625 group gch by gch.ChildID into g | |
626 select g.Max(c => c.GrandChildID) | |
627 on gc1.GrandChildID equals max | |
628 select gc1; | |
629 | |
630 var result = | |
631 from ch in db.Child | |
632 join p in db.Parent on ch.ParentID equals p.ParentID | |
633 join gc2 in q2 on p.ParentID equals gc2.ParentID into g | |
634 from gc3 in g.DefaultIfEmpty() | |
635 select gc3; | |
636 | |
637 var ctx = result.GetContext(); | |
638 var idx = ctx.ConvertToIndex(null, 0, ConvertFlags.Key); | |
639 | |
640 Assert.AreEqual(new[] { 0, 1, 2 }, idx.Select(_ => _.Index).ToArray()); | |
641 } | |
642 } | |
643 | |
644 [Test] | |
645 public void ConvertToIndexJoin2() | |
646 { | |
647 using (var db = new TestDbManager()) | |
648 { | |
649 var result = | |
650 from ch in db.Child | |
651 join gc2 in db.GrandChild on ch.ParentID equals gc2.ParentID into g | |
652 from gc3 in g.DefaultIfEmpty() | |
653 select gc3; | |
654 | |
655 var ctx = result.GetContext(); | |
656 var idx = ctx.ConvertToIndex(null, 0, ConvertFlags.Key); | |
657 | |
658 Assert.AreEqual(new[] { 0, 1, 2 }, idx.Select(_ => _.Index).ToArray()); | |
659 | |
660 idx = ctx.ConvertToIndex(null, 0, ConvertFlags.All); | |
661 | |
662 Assert.AreEqual(new[] { 0, 1, 2 }, idx.Select(_ => _.Index).ToArray()); | |
663 } | |
664 } | |
665 | |
666 #endregion | |
667 | |
668 #region ConvertToSql | |
669 | |
670 [Test] | |
671 public void ConvertToSql1() | |
672 { | |
673 using (var db = new TestDbManager()) | |
674 { | |
675 var ctx = db.Parent | |
676 .Select (p1 => new { p1.ParentID }) | |
677 .Select (p2 => p2.ParentID) | |
678 .GetContext(); | |
679 | |
680 var sql = ctx.ConvertToSql(null, 0, ConvertFlags.Field); | |
681 | |
682 Assert.AreEqual (1, sql.Length); | |
683 Assert.IsAssignableFrom(typeof(SqlField), sql[0].Sql); | |
684 Assert.AreEqual ("ParentID", ((SqlField)sql[0].Sql).Name); | |
685 } | |
686 } | |
687 | |
688 [Test] | |
689 public void ConvertToSql2() | |
690 { | |
691 using (var db = new TestDbManager()) | |
692 { | |
693 var ctx = db.Parent | |
694 .Select (p1 => new { p = p1.ParentID + 1 }) | |
695 .Select (p2 => p2.p) | |
696 .GetContext(); | |
697 | |
698 var sql = ctx.ConvertToSql(null, 0, ConvertFlags.Field); | |
699 | |
700 Assert.AreEqual (1, sql.Length); | |
701 Assert.IsAssignableFrom(typeof(SqlBinaryExpression), sql[0].Sql); | |
702 } | |
703 } | |
704 | |
705 [Test] | |
706 public void ConvertToSql3() | |
707 { | |
708 using (var db = new TestDbManager()) | |
709 { | |
710 var ctx = db.Parent | |
711 .Select (p1 => new { p = p1.ParentID + 1 }) | |
712 .Where (p3 => p3.p == 1) | |
713 .Select (p2 => p2.p) | |
714 .GetContext(); | |
715 | |
716 var sql = ctx.ConvertToSql(null, 0, ConvertFlags.Field); | |
717 | |
718 Assert.AreEqual (1, sql.Length); | |
719 Assert.IsAssignableFrom(typeof(SqlQuery.Column), sql[0].Sql); | |
720 } | |
721 } | |
722 | |
723 [Test] | |
724 public void ConvertToSql4() | |
725 { | |
726 using (var db = new TestDbManager()) | |
727 { | |
728 var ctx = db.Parent | |
729 .Select (p1 => new { p1 }) | |
730 .Select (p2 => p2.p1.ParentID) | |
731 .GetContext(); | |
732 | |
733 var sql = ctx.ConvertToSql(null, 0, ConvertFlags.Field); | |
734 | |
735 Assert.AreEqual (1, sql.Length); | |
736 Assert.IsAssignableFrom(typeof(SqlField), sql[0].Sql); | |
737 Assert.AreEqual ("ParentID", ((SqlField)sql[0].Sql).Name); | |
738 } | |
739 } | |
740 | |
741 [Test] | |
742 public void ConvertToSql5() | |
743 { | |
744 using (var db = new TestDbManager()) | |
745 { | |
746 var ctx = db.Child | |
747 .Select (p => new { p, p.Parent }) | |
748 .Select (p => new { p.Parent.ParentID, p.p.ChildID }) | |
749 .Select (p => p.ParentID) | |
750 .GetContext(); | |
751 | |
752 var sql = ctx.ConvertToSql(null, 0, ConvertFlags.Field); | |
753 | |
754 Assert.AreEqual (1, sql.Length); | |
755 Assert.IsAssignableFrom(typeof(SqlField), sql[0].Sql); | |
756 Assert.AreEqual ("ParentID", ((SqlField)sql[0].Sql).Name); | |
757 } | |
758 } | |
759 | |
760 #endregion | |
761 | |
762 #region SqlTest | |
763 | |
764 [Test] | |
765 public void Join1() | |
766 { | |
767 using (var db = new TestDbManager()) | |
768 { | |
769 var q = | |
770 from t in | |
771 from ch in db.Child | |
772 join p in db.Parent on ch.ParentID equals p.ParentID | |
773 select ch.ParentID + p.ParentID | |
774 where t > 2 | |
775 select t; | |
776 | |
777 var ctx = q.GetContext(); | |
778 ctx.BuildExpression(null, 0); | |
779 | |
780 Assert.AreEqual(1, ctx.SqlQuery.Select.Columns.Count); | |
781 } | |
782 } | |
783 | |
784 [Test] | |
785 public void Join2() | |
786 { | |
787 using (var db = new TestDbManager()) | |
788 { | |
789 var q = | |
790 from t in | |
791 from ch in db.Child | |
792 join p in db.Parent on ch.ParentID equals p.ParentID | |
793 select new { ID = ch.ParentID + p.ParentID } | |
794 where t.ID > 2 | |
795 select t; | |
796 | |
797 var ctx = q.GetContext(); | |
798 ctx.BuildExpression(null, 0); | |
799 | |
800 Assert.AreEqual(2, ctx.SqlQuery.Select.Columns.Count); | |
801 } | |
802 } | |
803 | |
804 public class MyClass | |
805 { | |
806 public int ID; | |
807 } | |
808 | |
809 [Test] | |
810 public void Join3() | |
811 { | |
812 using (var db = new TestDbManager()) | |
813 { | |
814 var q = | |
815 from p in db.Parent | |
816 join j in db.Child on p.ParentID equals j.ParentID | |
817 select p; | |
818 | |
819 var ctx = q.GetContext(); | |
820 ctx.BuildExpression(null, 0); | |
821 | |
822 Assert.AreEqual(2, ctx.SqlQuery.Select.Columns.Count); | |
823 } | |
824 } | |
825 | |
826 [Test] | |
827 public void Join4() | |
828 { | |
829 using (var db = new TestDbManager()) | |
830 { | |
831 var q = | |
832 from p in db.Parent | |
833 select new { ID = new MyClass { ID = p.ParentID } } | |
834 into p | |
835 join j in | |
836 from c in db.Child | |
837 select new { ID = new MyClass { ID = c.ParentID } } | |
838 on p.ID.ID equals j.ID.ID | |
839 where p.ID.ID == 1 | |
840 select p; | |
841 | |
842 var ctx = q.GetContext(); | |
843 ctx.BuildExpression(null, 0); | |
844 | |
845 Assert.AreEqual(1, ctx.SqlQuery.Select.Columns.Count); | |
846 } | |
847 } | |
848 | |
849 [Test] | |
850 public void Join5() | |
851 { | |
852 using (var db = new TestDbManager()) | |
853 { | |
854 var q = | |
855 from p in db.Parent | |
856 join c in db.Child on p.ParentID equals c.ParentID | |
857 join g in db.GrandChild on p.ParentID equals g.ParentID | |
858 select new { p, c, g } into x | |
859 select x.c.ParentID; | |
860 | |
861 var ctx = q.GetContext(); | |
862 var sql = ctx.ConvertToSql(null, 0, ConvertFlags.All); | |
863 | |
864 Assert.AreEqual(1, sql.Length); | |
865 } | |
866 } | |
867 | |
868 [Test] | |
869 public void Join6([IncludeDataContexts("Sql2008", "Sql2012")] string context) | |
870 { | |
871 using (var db = new TestDbManager(context)) | |
872 { | |
873 var q = | |
874 from g in db.GrandChild | |
875 join p in db.Parent4 on g.Child.ParentID equals p.ParentID | |
876 select g; | |
877 | |
878 var ctx = q.GetContext(); | |
879 | |
880 ctx.BuildExpression(null, 0); | |
881 | |
882 var sql = db.GetSqlText(ctx.SqlQuery); | |
883 | |
884 CompareSql(sql, @" | |
885 SELECT | |
886 [g].[ParentID], | |
887 [g].[ChildID], | |
888 [g].[GrandChildID] | |
889 FROM | |
890 [GrandChild] [g] | |
891 LEFT JOIN [Child] [t1] ON [g].[ParentID] = [t1].[ParentID] AND [g].[ChildID] = [t1].[ChildID] | |
892 INNER JOIN [Parent] [p] ON [t1].[ParentID] = [p].[ParentID]"); | |
893 } | |
894 } | |
895 | |
896 #endregion | |
897 } | |
898 | |
899 class ContextParser : ISequenceBuilder | |
900 { | |
901 public int BuildCounter { get; set; } | |
902 | |
903 public bool CanBuild(ExpressionBuilder builder, BuildInfo buildInfo) | |
904 { | |
905 var call = buildInfo.Expression as MethodCallExpression; | |
906 return call != null && call.Method.Name == "GetContext"; | |
907 } | |
908 | |
909 public IBuildContext BuildSequence(ExpressionBuilder builder, BuildInfo buildInfo) | |
910 { | |
911 var call = (MethodCallExpression)buildInfo.Expression; | |
912 return new Context(builder.BuildSequence(new BuildInfo(buildInfo, call.Arguments[0]))); | |
913 } | |
914 | |
915 public SequenceConvertInfo Convert(ExpressionBuilder builder, BuildInfo buildInfo, ParameterExpression param) | |
916 { | |
917 return null; | |
918 } | |
919 | |
920 public bool IsSequence(ExpressionBuilder builder, BuildInfo buildInfo) | |
921 { | |
922 return builder.IsSequence(new BuildInfo(buildInfo, ((MethodCallExpression)buildInfo.Expression).Arguments[0])); | |
923 } | |
924 | |
925 public class Context : PassThroughContext | |
926 { | |
927 public Context(IBuildContext context) : base(context) | |
928 { | |
929 } | |
930 | |
931 public override void BuildQuery<T>(Query<T> query, ParameterExpression queryParameter) | |
932 { | |
933 query.GetElement = (ctx,db,expr,ps) => this; | |
934 } | |
935 } | |
936 } | |
937 | |
938 static class Extensions | |
939 { | |
940 public static ContextParser.Context GetContext<T>(this IQueryable<T> source) | |
941 { | |
942 if (source == null) throw new ArgumentNullException("source"); | |
943 | |
944 return source.Provider.Execute<ContextParser.Context>( | |
945 Expression.Call( | |
946 null, | |
947 ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(new[] { typeof(T) }), | |
948 new[] { source.Expression })); | |
949 } | |
950 | |
951 static public Expression Unwrap(this Expression ex) | |
952 { | |
953 if (ex == null) | |
954 return null; | |
955 | |
956 switch (ex.NodeType) | |
957 { | |
958 case ExpressionType.Quote : | |
959 case ExpressionType.Convert : | |
960 case ExpressionType.ConvertChecked : return ((UnaryExpression)ex).Operand.Unwrap(); | |
961 } | |
962 | |
963 return ex; | |
964 } | |
965 } | |
966 } |