Mercurial > pub > bltoolkit
comparison UnitTests/Linq/TakeSkipTest.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 | |
4 using NUnit.Framework; | |
5 | |
6 using BLToolkit.Data.DataProvider; | |
7 using BLToolkit.Data.Linq; | |
8 | |
9 namespace Data.Linq | |
10 { | |
11 using Model; | |
12 | |
13 [TestFixture] | |
14 public class TakeSkipTest : TestBase | |
15 { | |
16 [Test] | |
17 public void Take1() | |
18 { | |
19 ForEachProvider(db => | |
20 { | |
21 for (var i = 2; i <= 3; i++) | |
22 Assert.AreEqual(i, (from ch in db.Child select ch).Take(i).ToList().Count); | |
23 }); | |
24 } | |
25 | |
26 static void TakeParam(ITestDataContext db, int n) | |
27 { | |
28 Assert.AreEqual(n, (from ch in db.Child select ch).Take(n).ToList().Count); | |
29 } | |
30 | |
31 [Test] | |
32 public void Take2() | |
33 { | |
34 ForEachProvider(db => TakeParam(db, 1)); | |
35 } | |
36 | |
37 [Test] | |
38 public void Take3() | |
39 { | |
40 ForEachProvider(db => | |
41 Assert.AreEqual(3, (from ch in db.Child where ch.ChildID > 3 || ch.ChildID < 4 select ch).Take(3).ToList().Count)); | |
42 } | |
43 | |
44 [Test] | |
45 public void Take4() | |
46 { | |
47 ForEachProvider(db => | |
48 Assert.AreEqual(3, (from ch in db.Child where ch.ChildID >= 0 && ch.ChildID <= 100 select ch).Take(3).ToList().Count)); | |
49 } | |
50 | |
51 [Test] | |
52 public void Take5() | |
53 { | |
54 ForEachProvider(db => Assert.AreEqual(3, db.Child.Take(3).ToList().Count)); | |
55 } | |
56 | |
57 [Test] | |
58 public void Take6() | |
59 { | |
60 var expected = Child.OrderBy(c => c.ChildID).Take(3); | |
61 | |
62 ForEachProvider(db => | |
63 { | |
64 var result = db.Child.OrderBy(c => c.ChildID).Take(3); | |
65 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
66 }); | |
67 } | |
68 | |
69 [Test] | |
70 public void Take7() | |
71 { | |
72 ForEachProvider(db => Assert.AreEqual(3, db.Child.Take(() => 3).ToList().Count)); | |
73 } | |
74 | |
75 [Test] | |
76 public void Take8() | |
77 { | |
78 var n = 3; | |
79 ForEachProvider(db => Assert.AreEqual(3, db.Child.Take(() => n).ToList().Count)); | |
80 } | |
81 | |
82 [Test] | |
83 public void TakeCount() | |
84 { | |
85 ForEachProvider(new[] { ProviderName.Sybase }, db => Assert.AreEqual( | |
86 Child.Take(5).Count(), | |
87 db.Child.Take(5).Count())); | |
88 } | |
89 | |
90 [Test] | |
91 public void Skip1() | |
92 { | |
93 ForEachProvider(db => AreEqual(Child.Skip(3), db.Child.Skip(3))); | |
94 } | |
95 | |
96 [Test] | |
97 public void Skip2() | |
98 { | |
99 var expected = (from ch in Child where ch.ChildID > 3 || ch.ChildID < 4 select ch).Skip(3); | |
100 ForEachProvider(db => AreEqual(expected, (from ch in db.Child where ch.ChildID > 3 || ch.ChildID < 4 select ch).Skip(3))); | |
101 } | |
102 | |
103 [Test] | |
104 public void Skip3() | |
105 { | |
106 var expected = (from ch in Child where ch.ChildID >= 0 && ch.ChildID <= 100 select ch).Skip(3); | |
107 ForEachProvider(db => AreEqual(expected, (from ch in db.Child where ch.ChildID >= 0 && ch.ChildID <= 100 select ch).Skip(3))); | |
108 } | |
109 | |
110 [Test] | |
111 public void Skip4() | |
112 { | |
113 var expected = Child.OrderByDescending(c => c.ChildID).Skip(3); | |
114 | |
115 ForEachProvider(db => | |
116 { | |
117 var result = db.Child.OrderByDescending(c => c.ChildID).Skip(3); | |
118 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
119 }); | |
120 } | |
121 | |
122 [Test] | |
123 public void Skip5() | |
124 { | |
125 var expected = Child.OrderByDescending(c => c.ChildID).ThenBy(c => c.ParentID + 1).Skip(3); | |
126 ForEachProvider(db => AreEqual(expected, db.Child.OrderByDescending(c => c.ChildID).ThenBy(c => c.ParentID + 1).Skip(3))); | |
127 } | |
128 | |
129 [Test] | |
130 public void Skip6() | |
131 { | |
132 ForEachProvider(db => AreEqual(Child.Skip(3), db.Child.Skip(() => 3))); | |
133 } | |
134 | |
135 [Test] | |
136 public void Skip7() | |
137 { | |
138 var n = 3; | |
139 ForEachProvider(db => AreEqual(Child.Skip(n), db.Child.Skip(() => n))); | |
140 } | |
141 | |
142 [Test] | |
143 public void SkipCount() | |
144 { | |
145 ForEachProvider(new[] { "Sql2000", ProviderName.Sybase, ProviderName.SQLite, ProviderName.Access }, db => Assert.AreEqual( | |
146 Child.Skip(2).Count(), | |
147 db.Child.Skip(2).Count())); | |
148 } | |
149 | |
150 [Test] | |
151 public void SkipTake1() | |
152 { | |
153 var expected = Child.OrderByDescending(c => c.ChildID).Skip(2).Take(5); | |
154 ForEachProvider(db => | |
155 { | |
156 var result = db.Child.OrderByDescending(c => c.ChildID).Skip(2).Take(5); | |
157 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
158 }); | |
159 } | |
160 | |
161 [Test] | |
162 public void SkipTake2() | |
163 { | |
164 var expected = Child.OrderByDescending(c => c.ChildID).Take(7).Skip(2); | |
165 ForEachProvider(db => | |
166 { | |
167 var result = db.Child.OrderByDescending(c => c.ChildID).Take(7).Skip(2); | |
168 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
169 }); | |
170 } | |
171 | |
172 [Test] | |
173 public void SkipTake3() | |
174 { | |
175 var expected = Child.OrderBy(c => c.ChildID).Skip(1).Take(7).Skip(2); | |
176 ForEachProvider(db => | |
177 { | |
178 var result = db.Child.OrderBy(c => c.ChildID).Skip(1).Take(7).Skip(2); | |
179 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
180 }); | |
181 } | |
182 | |
183 [Test] | |
184 public void SkipTake4() | |
185 { | |
186 var expected = Child.OrderByDescending(c => c.ChildID).Skip(1).Take(7).OrderBy(c => c.ChildID).Skip(2); | |
187 ForEachProvider(new[] { "Sql2000", ProviderName.SQLite, ProviderName.Sybase, ProviderName.Access }, db => | |
188 { | |
189 var result = db.Child.OrderByDescending(c => c.ChildID).Skip(1).Take(7).OrderBy(c => c.ChildID).Skip(2); | |
190 Assert.IsTrue(result.ToList().SequenceEqual(expected)); | |
191 }); | |
192 } | |
193 | |
194 [Test] | |
195 public void SkipTake5() | |
196 { | |
197 ForEachProvider(db => | |
198 { | |
199 var list = db.Child.Skip(2).Take(5).ToList(); | |
200 Assert.AreEqual(5, list.Count); | |
201 }); | |
202 } | |
203 | |
204 void SkipTake6(ITestDataContext db, bool doSkip) | |
205 { | |
206 var q1 = from g in db.GrandChild select g; | |
207 | |
208 if (doSkip) | |
209 q1 = q1.Skip(12); | |
210 q1 = q1.Take(3); | |
211 | |
212 var q2 = | |
213 from c in db.Child | |
214 from p in q1 | |
215 where c.ParentID == p.ParentID | |
216 select c; | |
217 | |
218 var q3 = from g in GrandChild select g; | |
219 | |
220 if (doSkip) | |
221 q3 = q3.Skip(12); | |
222 q3 = q3.Take(3); | |
223 | |
224 var q4 = | |
225 from c in Child | |
226 from p in q3 | |
227 where c.ParentID == p.ParentID | |
228 select c; | |
229 | |
230 AreEqual(q4, q2); | |
231 } | |
232 | |
233 [Test] | |
234 public void SkipTake6() | |
235 { | |
236 ForEachProvider( | |
237 new[] { "Sql2000", ProviderName.SqlCe, ProviderName.SQLite, ProviderName.Access, ProviderName.Sybase }, | |
238 db => | |
239 { | |
240 SkipTake6(db, false); | |
241 SkipTake6(db, true); | |
242 }); | |
243 } | |
244 | |
245 [Test] | |
246 public void SkipTakeCount() | |
247 { | |
248 ForEachProvider(new[] { "Sql2000", ProviderName.SqlCe, ProviderName.Sybase, ProviderName.SQLite, ProviderName.Access }, db => Assert.AreEqual( | |
249 Child.Skip(2).Take(5).Count(), | |
250 db.Child.Skip(2).Take(5).Count())); | |
251 } | |
252 | |
253 [Test] | |
254 public void SkipFirst() | |
255 { | |
256 var expected = (from p in Parent where p.ParentID > 1 select p).Skip(1).First(); | |
257 | |
258 ForEachProvider(db => | |
259 { | |
260 var result = from p in db.GetTable<Parent>() select p; | |
261 result = from p in result where p.ParentID > 1 select p; | |
262 var b = result.Skip(1).First(); | |
263 | |
264 Assert.AreEqual(expected, b); | |
265 }); | |
266 } | |
267 | |
268 [Test] | |
269 public void ElementAt1([DataContexts] string context) | |
270 { | |
271 using (var db = GetDataContext(context)) | |
272 Assert.AreEqual( | |
273 (from p in Parent where p.ParentID > 1 select p).ElementAt(3), | |
274 (from p in db.Parent where p.ParentID > 1 select p).ElementAt(3)); | |
275 } | |
276 | |
277 [Test] | |
278 public void ElementAt2() | |
279 { | |
280 var n = 3; | |
281 ForEachProvider(db => Assert.AreEqual( | |
282 (from p in Parent where p.ParentID > 1 select p).ElementAt(n), | |
283 (from p in db.Parent where p.ParentID > 1 select p).ElementAt(() => n))); | |
284 } | |
285 | |
286 [Test] | |
287 public void ElementAtDefault1() | |
288 { | |
289 ForEachProvider(db => Assert.AreEqual( | |
290 (from p in Parent where p.ParentID > 1 select p).ElementAtOrDefault(3), | |
291 (from p in db.Parent where p.ParentID > 1 select p).ElementAtOrDefault(3))); | |
292 } | |
293 | |
294 [Test] | |
295 public void ElementAtDefault2() | |
296 { | |
297 ForEachProvider(db => Assert.IsNull((from p in db.Parent where p.ParentID > 1 select p).ElementAtOrDefault(300000))); | |
298 } | |
299 | |
300 [Test] | |
301 public void ElementAtDefault3() | |
302 { | |
303 var n = 3; | |
304 ForEachProvider(db => Assert.AreEqual( | |
305 (from p in Parent where p.ParentID > 1 select p).ElementAtOrDefault(n), | |
306 (from p in db.Parent where p.ParentID > 1 select p).ElementAtOrDefault(() => n))); | |
307 } | |
308 | |
309 [Test] | |
310 public void ElementAtDefault4() | |
311 { | |
312 var n = 300000; | |
313 ForEachProvider(db => Assert.IsNull((from p in db.Parent where p.ParentID > 1 select p).ElementAtOrDefault(() => n))); | |
314 } | |
315 | |
316 [Test] | |
317 public void ElementAtDefault5() | |
318 { | |
319 ForEachProvider(db => Assert.AreEqual( | |
320 Person.ElementAtOrDefault(3), | |
321 db.Person.ElementAtOrDefault(3))); | |
322 } | |
323 } | |
324 } |