comparison UnitTests/Linq/SelectTest.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.Windows.Forms;
5
6 using NUnit.Framework;
7
8 using BLToolkit.Data;
9 using BLToolkit.Data.DataProvider;
10 using BLToolkit.Data.Linq;
11 using BLToolkit.DataAccess;
12 using BLToolkit.Reflection;
13 using BLToolkit.Mapping;
14
15 namespace Data.Linq
16 {
17 using Model;
18
19 [TestFixture]
20 public class SelectTest : TestBase
21 {
22 [Test]
23 public void SimpleDirect()
24 {
25 TestJohn(db => db.Person);
26 }
27
28 [Test]
29 public void Simple()
30 {
31 TestJohn(db => from p in db.Person select p);
32 }
33
34 [Test]
35 public void SimpleDouble()
36 {
37 TestJohn(db => db.Person.Select(p => p).Select(p => p));
38 }
39
40 [Test]
41 public void New()
42 {
43 var expected = from p in Person select new { p.ID, p.FirstName };
44
45 ForEachProvider(db =>
46 {
47 var result = from p in db.Person select new { p.ID, p.FirstName };
48 Assert.IsTrue(result.ToList().SequenceEqual(expected));
49 });
50 }
51
52 void NewParam(IQueryable<Person> table, int i)
53 {
54 var expected = from p in Person select new { i, p.ID, p.FirstName };
55 var result = from p in table select new { i, p.ID, p.FirstName };
56
57 Assert.IsTrue(result.ToList().SequenceEqual(expected));
58 }
59
60 [Test]
61 public void NewParam()
62 {
63 ForEachProvider(db => { for (var i = 0; i < 5; i++) NewParam(db.Person, i); });
64 }
65
66 [Test]
67 public void InitObject()
68 {
69 TestJohn(db => from p in db.Person select new Person { ID = p.ID, FirstName = p.FirstName });
70 }
71
72 [Test]
73 public void NewObject()
74 {
75 TestJohn(db => from p in db.Person select new Person(p.ID, p.FirstName));
76 }
77
78 [Test]
79 public void NewInitObject()
80 {
81 TestJohn(db => from p in db.Person select new Person(p.ID) { FirstName = p.FirstName });
82 }
83
84 [Test]
85 public void NewWithExpr()
86 {
87 TestPerson(1, "John1", db => from p in db.Person select new Person(p.ID) { FirstName = (p.FirstName + "1\r\r\r").TrimEnd('\r') });
88 }
89
90 [Test]
91 public void MultipleSelect1()
92 {
93 TestJohn(db => db.Person
94 .Select(p => new { PersonID = p.ID, Name = p.FirstName })
95 .Select(p => new Person(p.PersonID) { FirstName = p.Name }));
96 }
97
98 [Test]
99 public void MultipleSelect2()
100 {
101 TestJohn(db =>
102 from p in db.Person
103 select new { PersonID = p.ID, Name = p.FirstName } into pp
104 select new Person(pp.PersonID) { FirstName = pp.Name });
105 }
106
107 [Test]
108 public void MultipleSelect3()
109 {
110 TestJohn(db => db.Person
111 .Select(p => new { PersonID = p.ID, Name = p.FirstName })
112 .Select(p => new Person { ID = p.PersonID, FirstName = p.Name })
113 .Select(p => new { PersonID = p.ID, Name = p.FirstName })
114 .Select(p => new Person { ID = p.PersonID, FirstName = p.Name }));
115 }
116
117 [Test]
118 public void MultipleSelect4()
119 {
120 TestJohn(db => db.Person
121 .Select(p1 => new { p1 })
122 .Select(p2 => new { p2 })
123 .Select(p3 => new Person { ID = p3.p2.p1.ID, FirstName = p3.p2.p1.FirstName }));
124 }
125
126 [Test]
127 public void MultipleSelect5()
128 {
129 TestJohn(db => db.Person
130 .Select(p1 => new { p1 })
131 .Select(p2 => new Person { ID = p2.p1.ID, FirstName = p2.p1.FirstName })
132 .Select(p3 => new { p3 })
133 .Select(p4 => new Person { ID = p4.p3.ID, FirstName = p4.p3.FirstName }));
134 }
135
136 [Test]
137 public void MultipleSelect6()
138 {
139 TestJohn(db => db.Person
140 .Select(p1 => new { p1 })
141 .Select(p2 => new Person { ID = p2.p1.ID, FirstName = p2.p1.FirstName })
142 .Select(p3 => p3)
143 .Select(p4 => new Person { ID = p4.ID, FirstName = p4.FirstName }));
144 }
145
146 [Test]
147 public void MultipleSelect7()
148 {
149 TestJohn(db => db.Person
150 .Select(p1 => new { ID = p1.ID + 1, p1.FirstName })
151 .Select(p2 => new Person { ID = p2.ID - 1, FirstName = p2.FirstName }));
152 }
153
154 [Test]
155 public void MultipleSelect8()
156 {
157 ForEachProvider(db =>
158 {
159 var person = (
160
161 db.Person
162 .Select(p1 => new Person { ID = p1.ID * 2, FirstName = p1.FirstName })
163 .Select(p2 => new { ID = p2.ID / "22".Length, p2.FirstName })
164
165 ).ToList().Where(p => p.ID == 1).First();
166 Assert.AreEqual(1, person.ID);
167 Assert.AreEqual("John", person.FirstName);
168 });
169 }
170
171 [Test]
172 public void MultipleSelect9()
173 {
174 TestJohn(db => db.Person
175 .Select(p1 => new { ID = p1.ID - 1, p1.FirstName })
176 .Select(p2 => new Person { ID = p2.ID + 1, FirstName = p2.FirstName })
177 .Select(p3 => p3)
178 .Select(p4 => new { ID = p4.ID * "22".Length, p4.FirstName })
179 .Select(p5 => new Person { ID = p5.ID / 2, FirstName = p5.FirstName }));
180 }
181
182 [Test]
183 public void MultipleSelect10()
184 {
185 TestJohn(db => db.Person
186 .Select(p1 => new { p1.ID, p1 })
187 .Select(p2 => new { p2.ID, p2.p1, p2 })
188 .Select(p3 => new { p3.ID, p3.p1.FirstName, p11 = p3.p2.p1, p3 })
189 .Select(p4 => new Person { ID = p4.p11.ID, FirstName = p4.p3.p1.FirstName }));
190 }
191
192 [Test]
193 public void MultipleSelect11([IncludeDataContexts("Sql2008", "Sql2012")] string context)
194 {
195 var dt = DateTime.Now;
196
197 using (var db = new TestDbManager(context))
198 {
199 var q =
200 from p in db.Parent
201 from g1 in p.GrandChildren.DefaultIfEmpty()
202 let c1 = g1.Child.ChildID
203 where c1 == 1
204 from g2 in p.GrandChildren.DefaultIfEmpty()
205 let c2 = g2.Child.ChildID
206 where c2 == 2
207 from g3 in p.GrandChildren.DefaultIfEmpty()
208 let c3 = g3.Child.ChildID
209 where c3 == 3
210 from g4 in p.GrandChildren.DefaultIfEmpty()
211 let c4 = g4.Child.ChildID
212 where c4 == 4
213 from g5 in p.GrandChildren.DefaultIfEmpty()
214 let c5 = g5.Child.ChildID
215 where c5 == 5
216 from g6 in p.GrandChildren.DefaultIfEmpty()
217 let c6 = g6.Child.ChildID
218 where c6 == 6
219 from g7 in p.GrandChildren.DefaultIfEmpty()
220 let c7 = g7.Child.ChildID
221 where c7 == 7
222 from g8 in p.GrandChildren.DefaultIfEmpty()
223 let c8 = g8.Child.ChildID
224 where c8 == 8
225 from g9 in p.GrandChildren.DefaultIfEmpty()
226 let c9 = g9.Child.ChildID
227 where c9 == 9
228 from g10 in p.GrandChildren.DefaultIfEmpty()
229 let c10 = g10.Child.ChildID
230 where c10 == 10
231 from g11 in p.GrandChildren.DefaultIfEmpty()
232 let c11 = g11.Child.ChildID
233 where c11 == 11
234 from g12 in p.GrandChildren.DefaultIfEmpty()
235 let c12 = g12.Child.ChildID
236 where c12 == 12
237 from g13 in p.GrandChildren.DefaultIfEmpty()
238 let c13 = g13.Child.ChildID
239 where c13 == 13
240 from g14 in p.GrandChildren.DefaultIfEmpty()
241 let c14 = g14.Child.ChildID
242 where c14 == 14
243 from g15 in p.GrandChildren.DefaultIfEmpty()
244 let c15 = g15.Child.ChildID
245 where c15 == 15
246 from g16 in p.GrandChildren.DefaultIfEmpty()
247 let c16 = g16.Child.ChildID
248 where c16 == 16
249 from g17 in p.GrandChildren.DefaultIfEmpty()
250 let c17 = g17.Child.ChildID
251 where c17 == 17
252 from g18 in p.GrandChildren.DefaultIfEmpty()
253 let c18 = g18.Child.ChildID
254 where c18 == 18
255 from g19 in p.GrandChildren.DefaultIfEmpty()
256 let c19 = g19.Child.ChildID
257 where c19 == 19
258 from g20 in p.GrandChildren.DefaultIfEmpty()
259 let c20 = g20.Child.ChildID
260 where c20 == 20
261 orderby c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20
262 select new
263 {
264 p,
265 cs = new [] { c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20 }
266 };
267
268 q.ToList();
269 }
270
271 Assert.IsTrue((DateTime.Now - dt).TotalSeconds < 30);
272 }
273
274 [Test]
275 public void MutiplySelect12([DataContexts(ExcludeLinqService = true)] string context)
276 {
277 using (var db = (TestDbManager)GetDataContext(context))
278 {
279 var q =
280 from grandChild in db.GrandChild
281 from child in db.Child
282 where grandChild.ChildID.HasValue
283 select grandChild;
284 q.ToList();
285
286 var selectCount = db.LastQuery.Split(' ', '\t', '\n', '\r').Count(s => s.Equals("select", StringComparison.InvariantCultureIgnoreCase));
287 Assert.AreEqual(1, selectCount, "Why do we need \"select from select\"??");
288 }
289 }
290
291 [Test]
292 public void Coalesce()
293 {
294 ForEachProvider(db =>
295 {
296 var q = (
297
298 from p in db.Person
299 where p.ID == 1
300 select new
301 {
302 p.ID,
303 FirstName = p.FirstName ?? "None",
304 MiddleName = p.MiddleName ?? "None"
305 }
306
307 ).ToList().First();
308
309 Assert.AreEqual(1, q.ID);
310 Assert.AreEqual("John", q.FirstName);
311 Assert.AreEqual("None", q.MiddleName);
312 });
313 }
314
315 [Test]
316 public void Coalesce2()
317 {
318 ForEachProvider(db =>
319 {
320 var q = (
321
322 from p in db.Person
323 where p.ID == 1
324 select new
325 {
326 p.ID,
327 FirstName = p.MiddleName ?? p.FirstName ?? "None",
328 LastName = p.LastName ?? p.FirstName ?? "None",
329 MiddleName = p.MiddleName ?? p.MiddleName ?? "None"
330 }
331
332 ).ToList().First();
333
334 Assert.AreEqual(1, q.ID);
335 Assert.AreEqual("John", q.FirstName);
336 Assert.AreEqual("Pupkin", q.LastName);
337 Assert.AreEqual("None", q.MiddleName);
338 });
339 }
340
341 class MyMapSchema : MappingSchema
342 {
343 public override void InitNullValues()
344 {
345 base.InitNullValues();
346 DefaultStringNullValue = null;
347 }
348 }
349
350 static readonly MyMapSchema _myMapSchema = new MyMapSchema();
351
352 [Test]
353 public void Coalesce3()
354 {
355 ForEachProvider(db =>
356 {
357 if (db is DbManager)
358 {
359 ((DbManager)db).MappingSchema = _myMapSchema;
360
361 var q = (
362
363 from p in db.Person
364 where p.ID == 1
365 select new
366 {
367 p.ID,
368 FirstName = p.MiddleName ?? p.FirstName ?? "None",
369 LastName = p.LastName ?? p.FirstName ?? "None",
370 MiddleName = p.MiddleName ?? p.MiddleName ?? "None"
371 }
372
373 ).ToList().First();
374
375 Assert.AreEqual(1, q.ID);
376 Assert.AreEqual("John", q.FirstName);
377 Assert.AreEqual("Pupkin", q.LastName);
378 Assert.AreEqual("None", q.MiddleName);
379 }
380 });
381 }
382
383 [Test]
384 public void Coalesce4()
385 {
386 ForEachProvider(new[] { ProviderName.SqlCe }, db => AreEqual(
387 from c in Child
388 select Sql.AsSql((from ch in Child where ch.ChildID == c.ChildID select ch.Parent.Value1).FirstOrDefault() ?? c.ChildID),
389 from c in db.Child
390 select Sql.AsSql((from ch in db.Child where ch.ChildID == c.ChildID select ch.Parent.Value1).FirstOrDefault() ?? c.ChildID)));
391 }
392
393 [Test]
394 public void Coalesce5()
395 {
396 ForEachProvider(new[] { ProviderName.SqlCe }, db => AreEqual(
397 from p in Parent select Sql.AsSql(p.Children.Max(c => (int?)c.ChildID) ?? p.Value1),
398 from p in db.Parent select Sql.AsSql(p.Children.Max(c => (int?)c.ChildID) ?? p.Value1)));
399 }
400
401 [Test]
402 public void Concatenation()
403 {
404 ForEachProvider(db =>
405 {
406 var q = from p in db.Person where p.ID == 1 select new { p.ID, FirstName = "123" + p.FirstName + "456" };
407 var f = q.Where(p => p.FirstName == "123John456").ToList().First();
408 Assert.AreEqual(1, f.ID);
409 });
410 }
411
412 IEnumerable<int> GetList(int i)
413 {
414 yield return i;
415 }
416
417 [Test]
418 public void SelectEnumerable()
419 {
420 ForEachProvider(db => AreEqual(
421 from p in Parent select new { Max = GetList(p.ParentID).Max() },
422 from p in db.Parent select new { Max = GetList(p.ParentID).Max() }));
423 }
424
425 [Test]
426 public void ConstractClass()
427 {
428 ForEachProvider(db =>
429 db.Parent.Select(f =>
430 new ListViewItem(new[] { "", f.ParentID.ToString(), f.Value1.ToString() })
431 {
432 Checked = true,
433 ImageIndex = 0,
434 Tag = f.ParentID
435 }).ToList());
436 }
437
438 static string ConvertString(string s, int? i, bool b, int n)
439 {
440 return s + "." + i + "." + b + "." + n;
441 }
442
443 [Test]
444 public void Index()
445 {
446 ForEachProvider(db =>
447 {
448 var q =
449 db.Child
450 .OrderByDescending(m => m.ChildID)
451 .Where(m => m.Parent != null && m.ParentID > 0);
452
453 var lines =
454 q.Select(
455 (m, i) =>
456 ConvertString(m.Parent.ParentID.ToString(), m.ChildID, i % 2 == 0, i)).ToArray();
457
458 Assert.AreEqual("7.77.True.0", lines[0]);
459
460 q =
461 db.Child
462 .OrderByDescending(m => m.ChildID)
463 .Where(m => m.Parent != null && m.ParentID > 0);
464
465 lines =
466 q.Select(
467 (m, i) =>
468 ConvertString(m.Parent.ParentID.ToString(), m.ChildID, i % 2 == 0, i)).ToArray();
469
470 Assert.AreEqual("7.77.True.0", lines[0]);
471 });
472 }
473
474 [Test]
475 public void InterfaceTest()
476 {
477 ForEachProvider(db =>
478 {
479 var q = from p in db.Parent2 select new { p.ParentID, p.Value1 };
480 q.ToList();
481 });
482 }
483
484 [Test]
485 public void ProjectionTest1()
486 {
487 ForEachProvider(db => AreEqual(
488 from c in Child select new { c.ChildID, ID = 0, ID1 = c.ParentID2.ParentID2, c.ParentID2.Value1, ID2 = c.ParentID },
489 from c in db.Child select new { c.ChildID, ID = 0, ID1 = c.ParentID2.ParentID2, c.ParentID2.Value1, ID2 = c.ParentID }));
490 }
491
492 [TableName("Person")]
493 [ObjectFactory(typeof(Factory))]
494 public class TestPersonObject
495 {
496 public class Factory : IObjectFactory
497 {
498 #region IObjectFactory Members
499
500 public object CreateInstance(TypeAccessor typeAccessor, InitContext context)
501 {
502 if (context == null)
503 throw new Exception("InitContext is null while mapping from DataReader!");
504
505 return typeAccessor.CreateInstance();
506 }
507
508 #endregion
509 }
510
511 public int PersonID;
512 public string FirstName;
513 }
514
515 [Test]
516 public void ObjectFactoryTest()
517 {
518 ForEachProvider(db => db.GetTable<TestPersonObject>().ToList());
519 }
520
521 [Test]
522 public void ProjectionTest2()
523 {
524 ForEachProvider(db => AreEqual(
525 from p in Person select p.Patient,
526 from p in db.Person select p.Patient));
527 }
528
529 [Test]
530 public void EqualTest1()
531 {
532 ForEachProvider(db =>
533 {
534 var q = (from p in db.Parent select new { p1 = p, p2 = p }).First();
535 Assert.AreSame(q.p1, q.p2);
536 });
537 }
538
539 [Test]
540 public void SelectEnumOnClient()
541 {
542 ForEachProvider(context =>
543 {
544 var arr = new List<Person> { new Person() };
545 var p = context.Person.Select(person => new { person.ID, Arr = arr.Take(1) }).FirstOrDefault();
546
547 p.Arr.Single();
548 });
549 }
550
551 [TableName("Parent")]
552 public class TestParent
553 {
554 [MapField("ParentID")] public int ParentID_;
555 [MapField("Value1")] public int? Value1_;
556 }
557
558 [Test]
559 public void SelectField()
560 {
561 using (var db = new TestDbManager())
562 {
563 var q =
564 from p in db.GetTable<TestParent>()
565 select p.Value1_;
566
567 var sql = q.ToString();
568
569 Assert.That(sql.IndexOf("ParentID_"), Is.LessThan(0));
570 }
571 }
572 }
573 }