Mercurial > pub > bltoolkit
comparison UnitTests/Linq/SelectTest.cs.orig @ 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 using BLToolkit.DataAccess; | |
6 using BLToolkit.Reflection; | |
7 using NUnit.Framework; | |
8 | |
9 using BLToolkit.Data; | |
10 using BLToolkit.Data.DataProvider; | |
11 using BLToolkit.Data.Linq; | |
12 using BLToolkit.Mapping; | |
13 | |
14 namespace Data.Linq | |
15 { | |
16 using Model; | |
17 | |
18 [TestFixture] | |
19 public class SelectTest : TestBase | |
20 { | |
21 [Test] | |
22 public void SimpleDirect() | |
23 { | |
24 TestJohn(db => db.Person); | |
25 } | |
26 | |
27 [Test] | |
28 public void Simple() | |
29 { | |
30 TestJohn(db => from p in db.Person select p); | |
31 } | |
32 | |
33 [Test] | |
34 public void SimpleDouble() | |
35 { | |
36 TestJohn(db => db.Person.Select(p => p).Select(p => p)); | |
37 } | |
38 | |
39 [Test] | |
40 public void New() | |
41 { | |
42 var expected = from p in Person select new { p.ID, p.FirstName }; | |
43 | |
44 ForEachProvider(db => | |
45 { | |
46 var result = from p in db.Person select new { p.ID, p.FirstName }; | |
47 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
48 }); | |
49 } | |
50 | |
51 void NewParam(IQueryable<Person> table, int i) | |
52 { | |
53 var expected = from p in Person select new { i, p.ID, p.FirstName }; | |
54 var result = from p in table select new { i, p.ID, p.FirstName }; | |
55 | |
56 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
57 } | |
58 | |
59 [Test] | |
60 public void NewParam() | |
61 { | |
62 ForEachProvider(db => { for (int i = 0; i < 5; i++) NewParam(db.Person, i); }); | |
63 } | |
64 | |
65 [Test] | |
66 public void InitObject() | |
67 { | |
68 TestJohn(db => from p in db.Person select new Person { ID = p.ID, FirstName = p.FirstName }); | |
69 } | |
70 | |
71 [Test] | |
72 public void NewObject() | |
73 { | |
74 TestJohn(db => from p in db.Person select new Person(p.ID, p.FirstName)); | |
75 } | |
76 | |
77 [Test] | |
78 public void NewInitObject() | |
79 { | |
80 TestJohn(db => from p in db.Person select new Person(p.ID) { FirstName = p.FirstName }); | |
81 } | |
82 | |
83 [Test] | |
84 public void NewWithExpr() | |
85 { | |
86 TestPerson(1, "John1", db => from p in db.Person select new Person(p.ID) { FirstName = (p.FirstName + "1\r\r\r").TrimEnd('\r') }); | |
87 } | |
88 | |
89 [Test] | |
90 public void MultipleSelect1() | |
91 { | |
92 TestJohn(db => db.Person | |
93 .Select(p => new { PersonID = p.ID, Name = p.FirstName }) | |
94 .Select(p => new Person(p.PersonID) { FirstName = p.Name })); | |
95 } | |
96 | |
97 [Test] | |
98 public void MultipleSelect2() | |
99 { | |
100 TestJohn(db => | |
101 from p in db.Person | |
102 select new { PersonID = p.ID, Name = p.FirstName } into pp | |
103 select new Person(pp.PersonID) { FirstName = pp.Name }); | |
104 } | |
105 | |
106 [Test] | |
107 public void MultipleSelect3() | |
108 { | |
109 TestJohn(db => db.Person | |
110 .Select(p => new { PersonID = p.ID, Name = p.FirstName }) | |
111 .Select(p => new Person { ID = p.PersonID, FirstName = p.Name }) | |
112 .Select(p => new { PersonID = p.ID, Name = p.FirstName }) | |
113 .Select(p => new Person { ID = p.PersonID, FirstName = p.Name })); | |
114 } | |
115 | |
116 [Test] | |
117 public void MultipleSelect4() | |
118 { | |
119 TestJohn(db => db.Person | |
120 .Select(p1 => new { p1 }) | |
121 .Select(p2 => new { p2 }) | |
122 .Select(p3 => new Person { ID = p3.p2.p1.ID, FirstName = p3.p2.p1.FirstName })); | |
123 } | |
124 | |
125 [Test] | |
126 public void MultipleSelect5() | |
127 { | |
128 TestJohn(db => db.Person | |
129 .Select(p1 => new { p1 }) | |
130 .Select(p2 => new Person { ID = p2.p1.ID, FirstName = p2.p1.FirstName }) | |
131 .Select(p3 => new { p3 }) | |
132 .Select(p4 => new Person { ID = p4.p3.ID, FirstName = p4.p3.FirstName })); | |
133 } | |
134 | |
135 [Test] | |
136 public void MultipleSelect6() | |
137 { | |
138 TestJohn(db => db.Person | |
139 .Select(p1 => new { p1 }) | |
140 .Select(p2 => new Person { ID = p2.p1.ID, FirstName = p2.p1.FirstName }) | |
141 .Select(p3 => p3) | |
142 .Select(p4 => new Person { ID = p4.ID, FirstName = p4.FirstName })); | |
143 } | |
144 | |
145 [Test] | |
146 public void MultipleSelect7() | |
147 { | |
148 TestJohn(db => db.Person | |
149 .Select(p1 => new { ID = p1.ID + 1, p1.FirstName }) | |
150 .Select(p2 => new Person { ID = p2.ID - 1, FirstName = p2.FirstName })); | |
151 } | |
152 | |
153 [Test] | |
154 public void MultipleSelect8() | |
155 { | |
156 ForEachProvider(db => | |
157 { | |
158 var person = ( | |
159 | |
160 db.Person | |
161 .Select(p1 => new Person { ID = p1.ID * 2, FirstName = p1.FirstName }) | |
162 .Select(p2 => new { ID = p2.ID / "22".Length, p2.FirstName }) | |
163 | |
164 ).ToList().Where(p => p.ID == 1).First(); | |
165 Assert.AreEqual(1, person.ID); | |
166 Assert.AreEqual("John", person.FirstName); | |
167 }); | |
168 } | |
169 | |
170 [Test] | |
171 public void MultipleSelect9() | |
172 { | |
173 TestJohn(db => db.Person | |
174 .Select(p1 => new { ID = p1.ID - 1, p1.FirstName }) | |
175 .Select(p2 => new Person { ID = p2.ID + 1, FirstName = p2.FirstName }) | |
176 .Select(p3 => p3) | |
177 .Select(p4 => new { ID = p4.ID * "22".Length, p4.FirstName }) | |
178 .Select(p5 => new Person { ID = p5.ID / 2, FirstName = p5.FirstName })); | |
179 } | |
180 | |
181 [Test] | |
182 public void MultipleSelect10() | |
183 { | |
184 TestJohn(db => db.Person | |
185 .Select(p1 => new { p1.ID, p1 }) | |
186 .Select(p2 => new { p2.ID, p2.p1, p2 }) | |
187 .Select(p3 => new { p3.ID, p3.p1.FirstName, p11 = p3.p2.p1, p3 }) | |
188 .Select(p4 => new Person { ID = p4.p11.ID, FirstName = p4.p3.p1.FirstName })); | |
189 } | |
190 | |
191 [Test] | |
192 public void Coalesce() | |
193 { | |
194 ForEachProvider(db => | |
195 { | |
196 var q = ( | |
197 | |
198 from p in db.Person | |
199 where p.ID == 1 | |
200 select new | |
201 { | |
202 p.ID, | |
203 FirstName = p.FirstName ?? "None", | |
204 MiddleName = p.MiddleName ?? "None" | |
205 } | |
206 | |
207 ).ToList().First(); | |
208 | |
209 Assert.AreEqual(1, q.ID); | |
210 Assert.AreEqual("John", q.FirstName); | |
211 Assert.AreEqual("None", q.MiddleName); | |
212 }); | |
213 } | |
214 | |
215 [Test] | |
216 public void Coalesce2() | |
217 { | |
218 ForEachProvider(db => | |
219 { | |
220 var q = ( | |
221 | |
222 from p in db.Person | |
223 where p.ID == 1 | |
224 select new | |
225 { | |
226 p.ID, | |
227 FirstName = p.MiddleName ?? p.FirstName ?? "None", | |
228 LastName = p.LastName ?? p.FirstName ?? "None", | |
229 MiddleName = p.MiddleName ?? p.MiddleName ?? "None" | |
230 } | |
231 | |
232 ).ToList().First(); | |
233 | |
234 Assert.AreEqual(1, q.ID); | |
235 Assert.AreEqual("John", q.FirstName); | |
236 Assert.AreEqual("Pupkin", q.LastName); | |
237 Assert.AreEqual("None", q.MiddleName); | |
238 }); | |
239 } | |
240 | |
241 class MyMapSchema : MappingSchema | |
242 { | |
243 public override void InitNullValues() | |
244 { | |
245 base.InitNullValues(); | |
246 DefaultStringNullValue = null; | |
247 } | |
248 } | |
249 | |
250 static readonly MyMapSchema _myMapSchema = new MyMapSchema(); | |
251 | |
252 [Test] | |
253 public void Coalesce3() | |
254 { | |
255 ForEachProvider(db => | |
256 { | |
257 if (db is DbManager) | |
258 { | |
259 ((DbManager)db).MappingSchema = _myMapSchema; | |
260 | |
261 var q = ( | |
262 | |
263 from p in db.Person | |
264 where p.ID == 1 | |
265 select new | |
266 { | |
267 p.ID, | |
268 FirstName = p.MiddleName ?? p.FirstName ?? "None", | |
269 LastName = p.LastName ?? p.FirstName ?? "None", | |
270 MiddleName = p.MiddleName ?? p.MiddleName ?? "None" | |
271 } | |
272 | |
273 ).ToList().First(); | |
274 | |
275 Assert.AreEqual(1, q.ID); | |
276 Assert.AreEqual("John", q.FirstName); | |
277 Assert.AreEqual("Pupkin", q.LastName); | |
278 Assert.AreEqual("None", q.MiddleName); | |
279 } | |
280 }); | |
281 } | |
282 | |
283 [Test] | |
284 public void Coalesce4() | |
285 { | |
286 ForEachProvider(new[] { ProviderName.SqlCe }, db => AreEqual( | |
287 from c in Child | |
288 select Sql.AsSql((from ch in Child where ch.ChildID == c.ChildID select ch.Parent.Value1).FirstOrDefault() ?? c.ChildID), | |
289 from c in db.Child | |
290 select Sql.AsSql((from ch in db.Child where ch.ChildID == c.ChildID select ch.Parent.Value1).FirstOrDefault() ?? c.ChildID))); | |
291 } | |
292 | |
293 [Test] | |
294 public void Coalesce5() | |
295 { | |
296 ForEachProvider(new[] { ProviderName.SqlCe }, db => AreEqual( | |
297 from p in Parent select Sql.AsSql(p.Children.Max(c => (int?)c.ChildID) ?? p.Value1), | |
298 from p in db.Parent select Sql.AsSql(p.Children.Max(c => (int?)c.ChildID) ?? p.Value1))); | |
299 } | |
300 | |
301 [Test] | |
302 public void Concatenation() | |
303 { | |
304 ForEachProvider(db => | |
305 { | |
306 var q = from p in db.Person where p.ID == 1 select new { p.ID, FirstName = "123" + p.FirstName + "456" }; | |
307 var f = q.Where(p => p.FirstName == "123John456").ToList().First(); | |
308 Assert.AreEqual(1, f.ID); | |
309 }); | |
310 } | |
311 | |
312 IEnumerable<int> GetList(int i) | |
313 { | |
314 yield return i; | |
315 } | |
316 | |
317 [Test] | |
318 public void SelectEnumerable() | |
319 { | |
320 ForEachProvider(db => AreEqual( | |
321 from p in Parent select new { Max = GetList(p.ParentID).Max() }, | |
322 from p in db.Parent select new { Max = GetList(p.ParentID).Max() })); | |
323 } | |
324 | |
325 [Test] | |
326 public void ConstractClass() | |
327 { | |
328 ForEachProvider(db => | |
329 db.Parent.Select(f => | |
330 new ListViewItem(new[] { "", f.ParentID.ToString(), f.Value1.ToString() }) | |
331 { | |
332 Checked = true, | |
333 ImageIndex = 0, | |
334 Tag = f.ParentID | |
335 }).ToList()); | |
336 } | |
337 | |
338 static string ConvertString(string s, int? i, bool b, int n) | |
339 { | |
340 return s + "." + i + "." + b + "." + n; | |
341 } | |
342 | |
343 [Test] | |
344 public void Index() | |
345 { | |
346 ForEachProvider(db => | |
347 { | |
348 var q = | |
349 db.Child | |
350 .OrderByDescending(m => m.ChildID) | |
351 .Where(m => m.Parent != null && m.ParentID > 0); | |
352 | |
353 var lines = | |
354 q.Select( | |
355 (m, i) => | |
356 ConvertString(m.Parent.ParentID.ToString(), m.ChildID, i % 2 == 0, i)).ToArray(); | |
357 | |
358 Assert.AreEqual("7.77.True.0", lines[0]); | |
359 }); | |
360 } | |
361 | |
362 [Test] | |
363 public void InterfaceTest() | |
364 { | |
365 ForEachProvider(db => | |
366 { | |
367 var q = from p in db.Parent2 select new { p.ParentID, p.Value1 }; | |
368 q.ToList(); | |
369 }); | |
370 } | |
371 | |
372 [Test] | |
373 public void ProjectionTest1() | |
374 { | |
375 ForEachProvider(db => AreEqual( | |
376 from c in Child select new { c.ChildID, ID = 0, ID1 = c.ParentID2.ParentID2, c.ParentID2.Value1, ID2 = c.ParentID }, | |
377 from c in db.Child select new { c.ChildID, ID = 0, ID1 = c.ParentID2.ParentID2, c.ParentID2.Value1, ID2 = c.ParentID })); | |
378 } | |
379 | |
380 <<<<<<< HEAD | |
381 [TableName("Person")] | |
382 [ObjectFactory(typeof(TestPersonObject.Factory))] | |
383 public class TestPersonObject | |
384 { | |
385 public class Factory : IObjectFactory | |
386 { | |
387 #region IObjectFactory Members | |
388 | |
389 public object CreateInstance(TypeAccessor typeAccessor, InitContext context) | |
390 { | |
391 if (context == null) | |
392 throw new Exception("InitContext is null while mapping from DataReader!"); | |
393 | |
394 return typeAccessor.CreateInstance(); | |
395 } | |
396 | |
397 #endregion | |
398 } | |
399 public int PersonID; | |
400 public string FirstName; | |
401 } | |
402 | |
403 [Test] | |
404 public void ObjectFactoryTest() | |
405 { | |
406 ForEachProvider(db => | |
407 db.GetTable<TestPersonObject>().ToList()); | |
408 ======= | |
409 [Test] | |
410 public void ProjectionTest2() | |
411 { | |
412 ForEachProvider(db => AreEqual( | |
413 from p in Person select p.Patient, | |
414 from p in db.Person select p.Patient)); | |
415 } | |
416 | |
417 [Test] | |
418 public void EqualTest1() | |
419 { | |
420 ForEachProvider(db => | |
421 { | |
422 var q = (from p in db.Parent select new { p1 = p, p2 = p }).First(); | |
423 Assert.AreSame(q.p1, q.p2); | |
424 }); | |
425 >>>>>>> 5ec55e61b6c00e47619c1399483bd531f161f93a | |
426 } | |
427 } | |
428 } |