0
|
1 using System;
|
|
2 using System.Linq;
|
|
3
|
|
4 using BLToolkit.Data.DataProvider;
|
|
5 using BLToolkit.Data.Linq;
|
|
6 using BLToolkit.DataAccess;
|
|
7 using BLToolkit.Mapping;
|
|
8
|
|
9 using NUnit.Framework;
|
|
10
|
|
11 namespace Data.Linq
|
|
12 {
|
|
13 using Model;
|
|
14
|
|
15 [TestFixture]
|
|
16 public class AssociationTest : TestBase
|
|
17 {
|
|
18 [Test]
|
|
19 public void Test1()
|
|
20 {
|
|
21 ForEachProvider(db => AreEqual(
|
|
22 from ch in Child where ch.ParentID == 1 select new { ch, ch.Parent },
|
|
23 from ch in db.Child where ch.ParentID == 1 select new { ch, ch.Parent }));
|
|
24 }
|
|
25
|
|
26 [Test]
|
|
27 public void Test2()
|
|
28 {
|
|
29 var expected =
|
|
30 from p in Parent
|
|
31 from ch in p.Children
|
|
32 where ch.ParentID < 4 || ch.ParentID >= 4
|
|
33 select new { p.ParentID, ch.ChildID };
|
|
34
|
|
35 ForEachProvider(db => AreEqual(expected,
|
|
36 from p in db.Parent
|
|
37 from ch in p.Children
|
|
38 where ch.ParentID < 4 || ch.ParentID >= 4
|
|
39 select new { p.ParentID, ch.ChildID }));
|
|
40 }
|
|
41
|
|
42 [Test]
|
|
43 public void Test3()
|
|
44 {
|
|
45 var expected =
|
|
46 from p in Parent
|
|
47 from ch in p.Children
|
|
48 where p.ParentID < 4 || p.ParentID >= 4
|
|
49 select new { p.ParentID };
|
|
50
|
|
51 ForEachProvider(db => AreEqual(expected,
|
|
52 from p in db.Parent
|
|
53 from ch in p.Children
|
|
54 where p.ParentID < 4 || p.ParentID >= 4
|
|
55 select new { p.ParentID }));
|
|
56 }
|
|
57
|
|
58 [Test]
|
|
59 public void Test4()
|
|
60 {
|
|
61 var expected =
|
|
62 from p in Parent
|
|
63 from ch in p.Children
|
|
64 where p.ParentID < 4 || p.ParentID >= 4
|
|
65 select new { p.ParentID, ch.ChildID };
|
|
66
|
|
67 ForEachProvider(db => AreEqual(expected,
|
|
68 from p in db.Parent
|
|
69 from ch in p.Children
|
|
70 where p.ParentID < 4 || p.ParentID >= 4
|
|
71 select new { p.ParentID, ch.ChildID }));
|
|
72 }
|
|
73
|
|
74 [Test]
|
|
75 public void Test5()
|
|
76 {
|
|
77 var expected =
|
|
78 from p in Parent
|
|
79 from ch in p.Children2
|
|
80 where ch.ParentID < 4 || ch.ParentID >= 4
|
|
81 select new { p.ParentID, ch.ChildID };
|
|
82
|
|
83 ForEachProvider(db => AreEqual(expected,
|
|
84 from p in db.Parent
|
|
85 from ch in p.Children2
|
|
86 where ch.ParentID < 4 || ch.ParentID >= 4
|
|
87 select new { p.ParentID, ch.ChildID }));
|
|
88 }
|
|
89
|
|
90 [Test]
|
|
91 public void SelectMany1()
|
|
92 {
|
|
93 ForEachProvider(db => AreEqual(
|
|
94 Parent.SelectMany(p => p.Children.Select(ch => p)),
|
|
95 db.Parent.SelectMany(p => p.Children.Select(ch => p))));
|
|
96 }
|
|
97
|
|
98 [Test]
|
|
99 public void SelectMany2()
|
|
100 {
|
|
101 ForEachProvider(db => AreEqual(
|
|
102 Parent.SelectMany(p => Child.Select(ch => p)),
|
|
103 db.Parent.SelectMany(p => db.Child.Select(ch => p))));
|
|
104 }
|
|
105
|
|
106 [Test]
|
|
107 public void SelectMany3()
|
|
108 {
|
|
109 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
|
|
110 Child
|
|
111 .GroupBy(ch => ch.Parent)
|
|
112 .Where(g => g.Count() > 2)
|
|
113 .SelectMany(g => g.Select(ch => ch.Parent)),
|
|
114 db.Child
|
|
115 .GroupBy(ch => ch.Parent)
|
|
116 .Where(g => g.Count() > 2)
|
|
117 .SelectMany(g => g.Select(ch => ch.Parent))));
|
|
118 }
|
|
119
|
|
120 [Test]
|
|
121 public void SelectMany4()
|
|
122 {
|
|
123 ForEachProvider(new[] { ProviderName.Access }, db => AreEqual(
|
|
124 Child
|
|
125 .GroupBy(ch => ch.Parent)
|
|
126 .Where(g => g.Count() > 2)
|
|
127 .SelectMany(g => g.Select(ch => ch.Parent.ParentID)),
|
|
128 db.Child
|
|
129 .GroupBy(ch => ch.Parent)
|
|
130 .Where(g => g.Count() > 2)
|
|
131 .SelectMany(g => g.Select(ch => ch.Parent.ParentID))));
|
|
132 }
|
|
133
|
|
134 [Test]
|
|
135 public void SelectMany5()
|
|
136 {
|
|
137 ForEachProvider(db => AreEqual(
|
|
138 Parent.SelectMany(p => p.Children.Select(ch => p.ParentID)),
|
|
139 db.Parent.SelectMany(p => p.Children.Select(ch => p.ParentID))));
|
|
140 }
|
|
141
|
|
142 [Test]
|
|
143 public void LeftJoin1()
|
|
144 {
|
|
145 ForEachProvider(db => AreEqual(
|
|
146 from p in Parent from c in p.Children.DefaultIfEmpty() where p.ParentID >= 4 select new { p, c },
|
|
147 from p in db.Parent from c in p.Children.DefaultIfEmpty() where p.ParentID >= 4 select new { p, c }));
|
|
148 }
|
|
149
|
|
150 [Test]
|
|
151 public void LeftJoin2()
|
|
152 {
|
|
153 ForEachProvider(db => AreEqual(
|
|
154 from p in Parent from c in p.Children.DefaultIfEmpty() where p.ParentID >= 4 select new { c, p },
|
|
155 from p in db.Parent from c in p.Children.DefaultIfEmpty() where p.ParentID >= 4 select new { c, p }));
|
|
156 }
|
|
157
|
|
158 [Test]
|
|
159 public void GroupBy1()
|
|
160 {
|
|
161 ForEachProvider(db => AreEqual(
|
|
162 from ch in Child group ch by ch.Parent into g select g.Key,
|
|
163 from ch in db.Child group ch by ch.Parent into g select g.Key));
|
|
164 }
|
|
165
|
|
166 [Test]
|
|
167 public void GroupBy2()
|
|
168 {
|
|
169 ForEachProvider(db => AreEqual(
|
|
170 (from ch in Child group ch by ch.Parent1).ToList().Select(g => g.Key),
|
|
171 (from ch in db.Child group ch by ch.Parent1).ToList().Select(g => g.Key)));
|
|
172 }
|
|
173
|
|
174 [Test]
|
|
175 public void GroupBy3()
|
|
176 {
|
|
177 ForEachProvider(db => AreEqual(
|
|
178 from p in Parent group p by p.Types.DateTimeValue.Year into g select g.Key,
|
|
179 from p in db.Parent group p by p.Types.DateTimeValue.Year into g select g.Key));
|
|
180 }
|
|
181
|
|
182 [Test]
|
|
183 public void GroupBy4()
|
|
184 {
|
|
185 ForEachProvider(db => AreEqual(
|
|
186 from p in Types group p by p.DateTimeValue.Year into g select g.Key,
|
|
187 from p in db.Types group p by p.DateTimeValue.Year into g select g.Key));
|
|
188 }
|
|
189
|
|
190 [Test]
|
|
191 public void EqualsNull1([IncludeDataContexts("Northwind")] string context)
|
|
192 {
|
|
193 using (var db = new NorthwindDB())
|
|
194 AreEqual(
|
|
195 from employee in Employee where employee.ReportsToEmployee != null select employee.EmployeeID,
|
|
196 from employee in db.Employee where employee.ReportsToEmployee != null select employee.EmployeeID);
|
|
197 }
|
|
198
|
|
199 [Test]
|
|
200 public void EqualsNull2([IncludeDataContexts("Northwind")] string context)
|
|
201 {
|
|
202 using (var db = new NorthwindDB())
|
|
203 AreEqual(
|
|
204 from employee in Employee where employee.ReportsToEmployee != null select employee,
|
|
205 from employee in db.Employee where employee.ReportsToEmployee != null select employee);
|
|
206 }
|
|
207
|
|
208 [Test]
|
|
209 public void EqualsNull3([IncludeDataContexts("Northwind")] string context)
|
|
210 {
|
|
211 using (var db = new NorthwindDB())
|
|
212 AreEqual(
|
|
213 from employee in Employee where employee.ReportsToEmployee != null select new { employee.ReportsToEmployee, employee },
|
|
214 from employee in db.Employee where employee.ReportsToEmployee != null select new { employee.ReportsToEmployee, employee });
|
|
215 }
|
|
216
|
|
217 [Test]
|
|
218 public void StackOverflow1([IncludeDataContexts("Northwind")] string context)
|
|
219 {
|
|
220 using (var db = new NorthwindDB())
|
|
221 Assert.AreEqual(
|
|
222 (from employee in Employee where employee.Employees.Count > 0 select employee).FirstOrDefault(),
|
|
223 (from employee in db.Employee where employee.Employees.Count > 0 select employee).FirstOrDefault());
|
|
224 }
|
|
225
|
|
226 [Test]
|
|
227 public void StackOverflow2()
|
|
228 {
|
|
229 ForEachProvider(new[] { ProviderName.SqlCe }, db => AreEqual(
|
|
230 from p in Parent5 where p.Children.Count != 0 select p,
|
|
231 from p in db.Parent5 where p.Children.Count != 0 select p));
|
|
232 }
|
|
233
|
|
234 [Test]
|
|
235 public void StackOverflow3()
|
|
236 {
|
|
237 ForEachProvider(new[] { ProviderName.SqlCe },
|
|
238 db => AreEqual(
|
|
239 from p in Parent5 where p.Children.Count() != 0 select p,
|
|
240 from p in db.Parent5 where p.Children.Count() != 0 select p));
|
|
241 }
|
|
242
|
|
243 [Test]
|
|
244 public void StackOverflow4()
|
|
245 {
|
|
246 ForEachProvider(new[] { ProviderName.SqlCe }, db => AreEqual(
|
|
247 from p in Parent5 select new { p.Children.Count },
|
|
248 from p in db.Parent5 select new { p.Children.Count }));
|
|
249 }
|
|
250
|
|
251 [Test]
|
|
252 public void DoubleJoin()
|
|
253 {
|
|
254 ForEachProvider(db => AreEqual(
|
|
255 from g in GrandChild where g.Child.Parent.Value1 == 1 select g,
|
|
256 from g in db.GrandChild where g.Child.Parent.Value1 == 1 select g));
|
|
257 }
|
|
258
|
|
259 [Test]
|
|
260 public void Projection1()
|
|
261 {
|
|
262 ForEachProvider(db => AreEqual(
|
|
263 from c in
|
|
264 from c in Child
|
|
265 where c.Parent.ParentID == 2
|
|
266 select c
|
|
267 join g in GrandChild on c.ParentID equals g.ParentID
|
|
268 where g.ChildID == 22
|
|
269 select new { c.Parent, c },
|
|
270 from c in
|
|
271 from c in db.Child
|
|
272 where c.Parent.ParentID == 2
|
|
273 select c
|
|
274 join g in db.GrandChild on c.ParentID equals g.ParentID
|
|
275 where g.ChildID == 22
|
|
276 select new { c.Parent, c }));
|
|
277 }
|
|
278
|
|
279 [TableName("Parent")]
|
|
280 public class Top
|
|
281 {
|
|
282 public int ParentID;
|
|
283 public int? Value1;
|
|
284
|
|
285 [Association(ThisKey = "ParentID", OtherKey = "ParentID", CanBeNull = true)]
|
|
286 public Middle Middle { get; set; }
|
|
287 }
|
|
288
|
|
289 [TableName("Child")]
|
|
290 public class Middle
|
|
291 {
|
|
292 [PrimaryKey] public int ParentID;
|
|
293 [PrimaryKey] public int ChildID;
|
|
294
|
|
295 [Association(ThisKey = "ChildID", OtherKey = "ChildID", CanBeNull = false)]
|
|
296 public Bottom Bottom { get; set; }
|
|
297
|
|
298 [Association(ThisKey = "ChildID", OtherKey = "ChildID", CanBeNull = true)]
|
|
299 public Bottom Bottom1 { get; set; }
|
|
300 }
|
|
301
|
|
302 [TableName("GrandChild")]
|
|
303 public class Bottom
|
|
304 {
|
|
305 public int ParentID;
|
|
306 public int ChildID;
|
|
307 public int GrandChildID;
|
|
308 }
|
|
309
|
|
310 [Test]
|
|
311 public void TestTernary1([DataContexts(ProviderName.SQLite, ProviderName.Access)] string context)
|
|
312 {
|
|
313 var ids = new[] { 1, 5 };
|
|
314
|
|
315 using (var db = GetDataContext(context))
|
|
316 {
|
|
317 var q =
|
|
318 from t in db.GetTable<Top>()
|
|
319 where ids.Contains(t.ParentID)
|
|
320 orderby t.ParentID
|
|
321 select t.Middle == null ? null : t.Middle.Bottom;
|
|
322
|
|
323 var list = q.ToList();
|
|
324
|
|
325 Assert.NotNull(list[0]);
|
|
326 Assert.Null (list[1]);
|
|
327 }
|
|
328 }
|
|
329
|
|
330 [Test]
|
|
331 public void TestTernary2()
|
|
332 {
|
|
333 var ids = new[] { 1, 5 };
|
|
334
|
|
335 ForEachProvider(
|
|
336 new[] { ProviderName.SQLite, ProviderName.Access },
|
|
337 db =>
|
|
338 {
|
|
339 var q =
|
|
340 from t in db.GetTable<Top>()
|
|
341 where ids.Contains(t.ParentID)
|
|
342 orderby t.ParentID
|
|
343 select t.Middle.Bottom;
|
|
344
|
|
345 var list = q.ToList();
|
|
346
|
|
347 Assert.NotNull(list[0]);
|
|
348 Assert.Null (list[1]);
|
|
349 });
|
|
350 }
|
|
351
|
|
352 [Test]
|
|
353 public void TestTernary3()
|
|
354 {
|
|
355 var ids = new[] { 1, 5 };
|
|
356
|
|
357 ForEachProvider(db =>
|
|
358 {
|
|
359 var q =
|
|
360 from t in db.GetTable<Top>()
|
|
361 where ids.Contains(t.ParentID)
|
|
362 orderby t.ParentID
|
|
363 select t.Middle.Bottom1;
|
|
364
|
|
365 var list = q.ToList();
|
|
366
|
|
367 Assert.NotNull(list[0]);
|
|
368 Assert.Null (list[1]);
|
|
369 });
|
|
370 }
|
|
371
|
|
372 [TableName("Child")]
|
|
373 [InheritanceMapping(Code = 1, IsDefault = true, Type = typeof(ChildForHeirarhy))]
|
|
374 public class ChildBaseForHeirarhy
|
|
375 {
|
|
376 [MapField(IsInheritanceDiscriminator = true)]
|
|
377 public int ChildID { get; set; }
|
|
378 }
|
|
379
|
|
380 public class ChildForHeirarhy : ChildBaseForHeirarhy
|
|
381 {
|
|
382 public int ParentID { get; set; }
|
|
383 [Association(ThisKey = "ParentID", OtherKey = "ParentID", CanBeNull = true)]
|
|
384 public Parent Parent { get; set; }
|
|
385 }
|
|
386
|
|
387 [Test]
|
|
388 public void AssociationInHeirarhy()
|
|
389 {
|
|
390 ForEachProvider(db =>
|
|
391 {
|
|
392 db.GetTable<ChildBaseForHeirarhy>()
|
|
393 .OfType<ChildForHeirarhy>()
|
|
394 .Select(ch => new ChildForHeirarhy { Parent = ch.Parent })
|
|
395 .ToList();
|
|
396 });
|
|
397 }
|
|
398
|
|
399 [Test]
|
|
400 public void LetTest1()
|
|
401 {
|
|
402 ForEachProvider(db =>
|
|
403 AreEqual(
|
|
404 from p in Parent
|
|
405 let chs = p.Children
|
|
406 select new { p.ParentID, Count = chs.Count() },
|
|
407 from p in db.Parent
|
|
408 let chs = p.Children
|
|
409 select new { p.ParentID, Count = chs.Count() }));
|
|
410 }
|
|
411
|
|
412 [Test]
|
|
413 public void LetTest2()
|
|
414 {
|
|
415 ForEachProvider(db =>
|
|
416 AreEqual(
|
|
417 from p in Parent
|
|
418 select new { p } into p
|
|
419 let chs = p.p.Children
|
|
420 select new { p.p.ParentID, Count = chs.Count() },
|
|
421 from p in db.Parent
|
|
422 select new { p } into p
|
|
423 let chs = p.p.Children
|
|
424 select new { p.p.ParentID, Count = chs.Count() }));
|
|
425 }
|
|
426 }
|
|
427 }
|