comparison UnitTests/Linq/Common.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;
7 using BLToolkit.Data.Linq;
8
9 using NUnit.Framework;
10
11 namespace Data.Linq
12 {
13 using Model;
14
15 [TestFixture]
16 public class Common : TestBase
17 {
18 [Test]
19 public void AsQueryable()
20 {
21 ForEachProvider(db => AreEqual(
22 from p in Parent from ch in Child select p,
23 from p in db.Parent from ch in db.Child.AsQueryable() select p));
24 }
25
26 [Test]
27 public void Convert()
28 {
29 ForEachProvider(db => AreEqual(
30 from p in Parent from ch in Child select p,
31 from p in db.Parent from ch in ((IEnumerable<Child>)db.Child).AsQueryable() select p));
32 }
33
34 [Test]
35 public void NewCondition()
36 {
37 ForEachProvider(db => AreEqual(
38 from p in Parent select new { Value = p.Value1 != null ? p.Value1 : 100 },
39 from p in db.Parent select new { Value = p.Value1 != null ? p.Value1 : 100 }));
40 }
41
42 [Test]
43 public void NewCoalesce()
44 {
45 ForEachProvider(db => AreEqual(
46 from p in Parent select new { Value = p.Value1 ?? 100 },
47 from p in db.Parent select new { Value = p.Value1 ?? 100 }));
48 }
49
50 [Test]
51 public void CoalesceNew()
52 {
53 Child ch = null;
54
55 ForEachProvider(db => AreEqual(
56 from p in Parent select ch ?? new Child { ParentID = p.ParentID },
57 from p in db.Parent select ch ?? new Child { ParentID = p.ParentID }));
58 }
59
60 [Test]
61 public void ScalarCondition()
62 {
63 ForEachProvider(db => AreEqual(
64 from p in Parent select p.Value1 != null ? p.Value1 : 100,
65 from p in db.Parent select p.Value1 != null ? p.Value1 : 100));
66 }
67
68 [Test]
69 public void ScalarCoalesce()
70 {
71 ForEachProvider(db => AreEqual(
72 from p in Parent select p.Value1 ?? 100,
73 from p in db.Parent select p.Value1 ?? 100));
74 }
75
76 [Test]
77 public void ExprCoalesce()
78 {
79 ForEachProvider(db => AreEqual(
80 from p in Parent select (p.Value1 ?? 100) + 50,
81 from p in db.Parent select (p.Value1 ?? 100) + 50));
82 }
83
84 static int GetDefault1()
85 {
86 return 100;
87 }
88
89 [Test]
90 public void ClientCoalesce1()
91 {
92 ForEachProvider(db => AreEqual(
93 from p in Parent select p.Value1 ?? GetDefault1(),
94 from p in db.Parent select p.Value1 ?? GetDefault1()));
95 }
96
97 static int GetDefault2(int n)
98 {
99 return n;
100 }
101
102 [Test]
103 public void ClientCoalesce2()
104 {
105 ForEachProvider(db => AreEqual(
106 from p in Parent select p.Value1 ?? GetDefault2(p.ParentID),
107 from p in db.Parent select p.Value1 ?? GetDefault2(p.ParentID)));
108 }
109
110 [Test]
111 public void CoalesceLike([DataContexts] string context)
112 {
113 using (var db = GetDataContext(context))
114 AreEqual(
115 from p in Person
116 where
117 (p.FirstName == null ? (bool?)null : (bool?)p.FirstName.StartsWith("Jo")) == null ?
118 false :
119 (p.FirstName == null ? (bool?)null : p.FirstName.StartsWith("Jo")).Value
120 select p,
121 from p in db.Person
122 where
123 (p.FirstName == null ? (bool?)null : (bool?)p.FirstName.StartsWith("Jo")) == null ?
124 false :
125 (p.FirstName == null ? (bool?)null : p.FirstName.StartsWith("Jo")).Value
126 select p);
127 }
128
129 [Test]
130 public void PreferServerFunc1()
131 {
132 ForEachProvider(db => AreEqual(
133 from p in Person select p.FirstName.Length,
134 from p in db.Person select p.FirstName.Length));
135 }
136
137 [Test]
138 public void PreferServerFunc2()
139 {
140 ForEachProvider(db => AreEqual(
141 from p in Person select p.FirstName.Length + "".Length,
142 from p in db.Person select p.FirstName.Length + "".Length));
143 }
144
145 class Test
146 {
147 class Entity
148 {
149 public Test TestField = null;
150 }
151
152 public Test TestClosure(ITestDataContext db)
153 {
154 return db.Person.Select(_ => new Entity { TestField = this }).First().TestField;
155 }
156 }
157
158 [Test]
159 public void ClosureTest()
160 {
161 ForEachProvider(db => Assert.AreNotEqual(
162 new Test().TestClosure(db),
163 new Test().TestClosure(db)));
164 }
165
166 [Test]
167 public void ExecuteTest([IncludeDataContexts("Northwind")] string context)
168 {
169 using (var db = new NorthwindDB())
170 {
171 var emp = db.Employee;
172
173 Expression<Func<int>> m = () => emp.Count();
174
175 var exp = Expression.Call(((MethodCallExpression)m.Body).Method, emp.Expression);
176
177 var results = (int)((IQueryable)emp).Provider.Execute(exp);
178 }
179 }
180
181 class MyClass
182 {
183 public int ID;
184
185 public override bool Equals(object obj)
186 {
187 return ((MyClass)obj).ID == ID;
188 }
189
190 public override int GetHashCode()
191 {
192 return ID;
193 }
194 }
195
196 [Test]
197 public void NewObjectTest1()
198 {
199 ForEachProvider(db => AreEqual(
200 from p in Parent
201 select new { ID = new MyClass { ID = p.ParentID } } into p1
202 where p1.ID.ID == 1
203 select p1,
204 from p in db.Parent
205 select new { ID = new MyClass { ID = p.ParentID } } into p1
206 where p1.ID.ID == 1
207 select p1));
208 }
209
210 [Test]
211 public void NewObjectTest2()
212 {
213 ForEachProvider(db => AreEqual(
214 from p in Parent
215 select new { ID = new MyClass { ID = p.ParentID } } into p
216 join j in
217 from c in Child
218 select new { ID = new MyClass { ID = c.ParentID } }
219 on p.ID.ID equals j.ID.ID
220 where p.ID.ID == 1
221 select p,
222 from p in db.Parent
223 select new { ID = new MyClass { ID = p.ParentID } } into p
224 join j in
225 from c in db.Child
226 select new { ID = new MyClass { ID = c.ParentID } }
227 on p.ID.ID equals j.ID.ID
228 where p.ID.ID == 1
229 select p));
230 }
231
232 public Table<Person> People2(DbManager db)
233 {
234 return db.GetTable<Person>();
235 }
236
237 [Test]
238 public void TableAsMethod()
239 {
240 using (var db = new TestDbManager())
241 {
242 var q =
243 from d in db.Patient
244 from p in People2(db)
245 select p;
246
247 q.ToList();
248
249 q =
250 from d in db.Patient
251 from p in People2(db)
252 select p;
253
254 q.ToList();
255 }
256 }
257
258 [Test]
259 public void TableAsExtensionMethod()
260 {
261 using (var db = new TestDbManager())
262 {
263 var q =
264 from d in db.Patient
265 from p in db.People()
266 select p;
267
268 q.ToList();
269 }
270 }
271
272 [Test]
273 public void Condition1()
274 {
275 ForEachProvider(db => AreEqual(
276 from p in Person select new { Name = !string.IsNullOrEmpty(p.FirstName) ? p.FirstName : !string.IsNullOrEmpty(p.MiddleName) ? p.MiddleName : p.LastName },
277 from p in db.Person select new { Name = !string.IsNullOrEmpty(p.FirstName) ? p.FirstName : !string.IsNullOrEmpty(p.MiddleName) ? p.MiddleName : p.LastName }));
278 }
279
280 [Test]
281 public void Condition2()
282 {
283 ForEachProvider(db => AreEqual(
284 from p in Person select new { Name = !p.FirstName.IsNullOrEmpty() ? p.FirstName : !p.MiddleName.IsNullOrEmpty() ? p.MiddleName : p.LastName },
285 from p in db.Person select new { Name = !p.FirstName.IsNullOrEmpty() ? p.FirstName : !p.MiddleName.IsNullOrEmpty() ? p.MiddleName : p.LastName }));
286 }
287
288 enum PersonID
289 {
290 Person1 = 1,
291 Person2 = 2
292 }
293
294 [Test]
295 public void ConvertEnum1()
296 {
297 ForEachProvider(db => AreEqual(
298 from p in Person where p.ID == (int)PersonID.Person1 select p,
299 from p in db.Person where p.ID == (int)PersonID.Person1 select p));
300 }
301
302 [Test]
303 public void ConvertEnum2()
304 {
305 var id = PersonID.Person1;
306
307 ForEachProvider(db => AreEqual(
308 from p in Person where p.ID == (int)id select p,
309 from p in db.Person where p.ID == (int)id select p));
310 }
311
312 [Test]
313 public void GroupByUnion1()
314 {
315 ForEachProvider(db => AreEqual(
316 from t in (
317 from c in Child
318 where c.ParentID < 4
319 select new { c.ParentID, ID = c.ChildID })
320 .Concat(
321 from g in GrandChild
322 where g.ParentID >= 4
323 select new { ParentID = g.ParentID ?? 0, ID = g.GrandChildID ?? 0 })
324 group t by t.ParentID into gr
325 select new { ParentID = gr.Key, Sum = gr.Sum(i => i.ID) } into tt
326 where tt.Sum != 0
327 select tt
328 ,
329 from t in (
330 from c in db.Child
331 where c.ParentID < 4
332 select new { c.ParentID, ID = c.ChildID })
333 .Concat(
334 from g in db.GrandChild
335 where g.ParentID >= 4
336 select new { ParentID = g.ParentID ?? 0, ID = g.GrandChildID ?? 0 })
337 group t by t.ParentID into gr
338 select new { ParentID = gr.Key, Sum = gr.Sum(i => i.ID) } into tt
339 where tt.Sum != 0
340 select tt
341 ));
342 }
343
344 [Test]
345 public void GroupByUnion2()
346 {
347 ForEachProvider(db =>
348 {
349 var qe1 =
350 from t in (
351 from c in Child
352 where c.ParentID < 4
353 select new { c.ParentID, ID = c.ChildID })
354 .Concat(
355 from g in GrandChild
356 where g.ParentID >= 4
357 select new { ParentID = g.ParentID ?? 0, ID = g.GrandChildID ?? 0 })
358 group t by t.ParentID into gr
359 select new { ParentID = gr.Key, Sum = gr.Sum(i => i.ID) } into tt
360 where tt.Sum != 0
361 select tt;
362
363 var qe2 =
364 from p in Parent
365 join tt in qe1 on p.ParentID equals tt.ParentID into gr
366 from tt in gr.DefaultIfEmpty()
367 select new { p.ParentID };
368
369 var qr1 =
370 from t in (
371 from c in db.Child
372 where c.ParentID < 4
373 select new { c.ParentID, ID = c.ChildID })
374 .Concat(
375 from g in db.GrandChild
376 where g.ParentID >= 4
377 select new { ParentID = g.ParentID ?? 0, ID = g.GrandChildID ?? 0 })
378 group t by t.ParentID into gr
379 select new { ParentID = gr.Key, Sum = gr.Sum(i => i.ID) } into tt
380 where tt.Sum != 0
381 select tt;
382
383 var qr2 =
384 from p in db.Parent
385 join tt in qr1 on p.ParentID equals tt.ParentID into gr
386 from tt in gr.DefaultIfEmpty()
387 select new { p.ParentID };
388
389 AreEqual(qe2, qr2);
390 });
391 }
392
393 [Test]
394 public void GroupByLeftJoin1()
395 {
396 ForEachProvider(db => AreEqual(
397 from p in Parent
398 join tt in
399 from t in Child
400 group t by t.ParentID into gr
401 select new { ParentID = gr.Key, Sum = gr.Sum(i => i.ChildID) } into tt
402 where tt.Sum != 0
403 select tt
404 on p.ParentID equals tt.ParentID into gr
405 from tt in gr.DefaultIfEmpty()
406 select p.ParentID,
407 from p in db.Parent
408 join tt in
409 from t in db.Child
410 group t by t.ParentID into gr
411 select new { ParentID = gr.Key, Sum = gr.Sum(i => i.ChildID) } into tt
412 where tt.Sum != 0
413 select tt
414 on p.ParentID equals tt.ParentID into gr
415 from tt in gr.DefaultIfEmpty()
416 select p.ParentID));
417 }
418
419 void ProcessItem(ITestDataContext db, int id)
420 {
421 var hashQuery1 = Parent.Where(t => t.ParentID == id);
422
423 var groups1 = Child
424 .Where(p => hashQuery1.Any(e => e.ParentID == p.ParentID))
425 .GroupBy(e => e.ParentID)
426 .Select(g => g.Key);
427
428 var hashQuery2 = db.Parent.Where(t => t.ParentID == id);
429
430 var groups2 = db.Child
431 .Where(p => hashQuery2.Any(e => e.ParentID == p.ParentID))
432 .GroupBy(e => e.ParentID)
433 .Select(g => g.Key);
434
435 AreEqual(groups1, groups2);
436 }
437
438 [Test]
439 public void ParameterTest1()
440 {
441 ForEachProvider(db =>
442 {
443 ProcessItem(db, 1);
444 ProcessItem(db, 2);
445 });
446 }
447 }
448
449 static class Extender
450 {
451 public static Table<Person> People(this DbManager db)
452 {
453 return db.GetTable<Person>();
454 }
455
456 public static bool IsNullOrEmpty(this string value)
457 {
458 return string.IsNullOrEmpty(value);
459 }
460 }
461 }