comparison UnitTests/Linq/Functions.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 NUnit.Framework;
7
8 using BLToolkit.Data.DataProvider;
9 using BLToolkit.Data.Linq;
10
11 namespace Data.Linq
12 {
13 using Model;
14
15 [TestFixture]
16 public class Functions : TestBase
17 {
18 [Test]
19 public void Contains1()
20 {
21 ForEachProvider(db => AreEqual(
22 from p in Parent where new[] { 1, 2 }.Contains(p.ParentID) select p,
23 from p in db.Parent where new[] { 1, 2 }.Contains(p.ParentID) select p));
24 }
25
26 [Test]
27 public void Contains2()
28 {
29 var arr = new[] { 1, 2 };
30
31 ForEachProvider(db => AreEqual(
32 from p in Parent where arr.Contains(p.ParentID) select p,
33 from p in db.Parent where arr.Contains(p.ParentID) select p));
34 }
35
36 [Test]
37 public void Contains3()
38 {
39 var n = 2;
40
41 var expected =
42 from p in Parent
43 where new[] { 1, n }.Contains(p.ParentID)
44 select p;
45
46 ForEachProvider(data => AreEqual(expected, CompiledQuery.Compile<ITestDataContext,IQueryable<Parent>>(db =>
47 from p in db.Parent
48 where new[] { 1, n }.Contains(p.ParentID)
49 select p)(data)));
50 }
51
52 [Test]
53 public void Contains4()
54 {
55 var arr = new[] { 1, 2 };
56
57 var expected =
58 from p in Parent
59 where arr.Contains(p.ParentID)
60 select p;
61
62 ForEachProvider(data => AreEqual(expected, CompiledQuery.Compile<ITestDataContext,IQueryable<Parent>>(db =>
63 from p in db.Parent
64 where arr.Contains(p.ParentID)
65 select p)(data)));
66 }
67
68 [Test]
69 public void Contains5()
70 {
71 var arr1 = new[] { 1, 2 };
72 var arr2 = new[] { 1, 2, 4 };
73
74 var expected1 = from p in Parent where arr1.Contains(p.ParentID) select p;
75 var expected2 = from p in Parent where arr2.Contains(p.ParentID) select p;
76
77 ForEachProvider(data =>
78 {
79 var cq = CompiledQuery.Compile<ITestDataContext,int[],IQueryable<Parent>>((db,a) =>
80 from p in db.Parent
81 where a.Contains(p.ParentID)
82 select p);
83
84 AreEqual(expected1, cq(data, arr1));
85 AreEqual(expected2, cq(data, arr2));
86 });
87 }
88
89 [Test]
90 public void Contains6()
91 {
92 var arr = new List<int> { 1, 2 };
93
94 ForEachProvider(db => AreEqual(
95 from p in Parent where arr.Contains(p.ParentID) select p,
96 from p in db.Parent where arr.Contains(p.ParentID) select p));
97 }
98
99 [Test]
100 public void Contains7()
101 {
102 IEnumerable<int> arr = new[] { 1, 2 };
103
104 ForEachProvider(db => AreEqual(
105 from p in Parent where arr.Contains(p.ParentID) select p,
106 from p in db.Parent where arr.Contains(p.ParentID) select p));
107 }
108
109 [Test]
110 public void ContainsKey1()
111 {
112 var arr = new Dictionary<int,int>
113 {
114 { 1, 1 },
115 { 2, 2 },
116 };
117
118 ForEachProvider(db => AreEqual(
119 from p in Parent where arr.Keys.Contains(p.ParentID) select p,
120 from p in db.Parent where arr.Keys.Contains(p.ParentID) select p));
121 }
122
123 [Test]
124 public void ContainsKey2()
125 {
126 var arr = new Dictionary<int,int>
127 {
128 { 1, 1 },
129 { 2, 2 },
130 };
131
132 ForEachProvider(db => AreEqual(
133 from p in Parent where arr.ContainsKey(p.ParentID) select p,
134 from p in db.Parent where arr.ContainsKey(p.ParentID) select p));
135 }
136
137 [Test]
138 public void ContainsValue1()
139 {
140 var arr = new Dictionary<int,int>
141 {
142 { 1, 1 },
143 { 2, 2 },
144 };
145
146 ForEachProvider(db => AreEqual(
147 from p in Parent where arr.Values.Contains(p.ParentID) select p,
148 from p in db.Parent where arr.Values.Contains(p.ParentID) select p));
149 }
150
151 [Test]
152 public void ContainsValue2()
153 {
154 var arr = new Dictionary<int,int>
155 {
156 { 1, 1 },
157 { 2, 2 },
158 };
159
160 ForEachProvider(db => AreEqual(
161 from p in Parent where arr.ContainsValue(p.ParentID) select p,
162 from p in db.Parent where arr.ContainsValue(p.ParentID) select p));
163 }
164
165 [Test]
166 public void ContainsHashSet1()
167 {
168 var arr = new HashSet<int> { 1, 2 };
169
170 ForEachProvider(db => AreEqual(
171 from p in Parent where arr.Contains(p.ParentID) select p,
172 from p in db.Parent where arr.Contains(p.ParentID) select p));
173 }
174
175 [Test]
176 public void EmptyContains1()
177 {
178 var expected =
179 from p in Parent
180 where new int[0].Contains(p.ParentID) || p.ParentID == 2
181 select p;
182
183 ForEachProvider(db => AreEqual(expected,
184 from p in db.Parent
185 where new int[0].Contains(p.ParentID) || p.ParentID == 2
186 select p));
187 }
188
189 [Test]
190 public void ContainsString11()
191 {
192 var arr = new List<string> { "John" };
193
194 ForEachProvider(db => AreEqual(
195 from p in Person where arr.Contains(p.FirstName) select p,
196 from p in db.Person where arr.Contains(p.FirstName) select p));
197 }
198
199 [Test]
200 public void ContainsString12()
201 {
202 var nm = "John";
203
204 ForEachProvider(db => AreEqual(
205 from p in Person where new List<string> { nm }.Contains(p.FirstName) select p,
206 from p in db.Person where new List<string> { nm }.Contains(p.FirstName) select p));
207 }
208
209 [Test]
210 public void ContainsString13()
211 {
212 ForEachProvider(db => AreEqual(
213 from p in Person where new List<string> { "John" }.Contains(p.FirstName) select p,
214 from p in db.Person where new List<string> { "John" }.Contains(p.FirstName) select p));
215 }
216
217 [Test]
218 public void ContainsString21()
219 {
220 var arr = new[] { "John" };
221
222 ForEachProvider(db => AreEqual(
223 from p in Person where arr.Contains(p.FirstName) select p,
224 from p in db.Person where arr.Contains(p.FirstName) select p));
225 }
226
227 [Test]
228 public void ContainsString22()
229 {
230 var nm = "John";
231
232 ForEachProvider(db => AreEqual(
233 from p in Person where new[] { nm }.Contains(p.FirstName) select p,
234 from p in db.Person where new[] { nm }.Contains(p.FirstName) select p));
235 }
236
237 [Test]
238 public void ContainsString23()
239 {
240 ForEachProvider(db => AreEqual(
241 from p in Person where new[] { "John" }.Contains(p.FirstName) select p,
242 from p in db.Person where new[] { "John" }.Contains(p.FirstName) select p));
243 }
244
245 [Test]
246 public void Equals1()
247 {
248 ForEachProvider(db => AreEqual(
249 from p in Parent where p.ParentID.Equals(2) select p,
250 from p in db.Parent where p.ParentID.Equals(2) select p));
251 }
252
253 [Test]
254 public void Equals2()
255 {
256 var child = (from ch in Child where ch.ParentID == 2 select ch).First();
257 var expected = from ch in Child where !ch.Equals(child) select ch;
258
259 ForEachProvider(db => AreEqual(expected, from ch in db.Child where !ch.Equals(child) select ch));
260 }
261
262 [Test]
263 public void Equals3()
264 {
265 ForEachProvider(db => AreEqual(
266 from p in Parent where p.Value1.Equals(null) select p,
267 from p in db.Parent where p.Value1.Equals(null) select p));
268 }
269
270 [Test]
271 public void Equals4([IncludeDataContexts("Northwind")] string context)
272 {
273 using (var db = new NorthwindDB())
274 AreEqual(
275 Customer.Where(c => !c.Address.Equals(null)),
276 db.Customer.Where(c => !c.Address.Equals(null)));
277 }
278
279 [Test]
280 public void NewGuid1()
281 {
282 ForEachProvider(new[] { ProviderName.DB2, ProviderName.Informix, ProviderName.PostgreSQL, ProviderName.SQLite, ProviderName.Access }, db => AreEqual(
283 from p in Types where p.GuidValue != Sql.NewGuid() select p.GuidValue,
284 from p in db.Types where p.GuidValue != Sql.NewGuid() select p.GuidValue));
285 }
286
287 [Test]
288 public void NewGuid2()
289 {
290 ForEachProvider(new[] { ProviderName.DB2, ProviderName.Informix, ProviderName.PostgreSQL, ProviderName.SQLite, ProviderName.Access }, db =>
291 Assert.AreNotEqual(Guid.Empty, (from p in db.Types select Sql.NewGuid()).First()));
292 }
293
294 [Test]
295 public void CustomFunc()
296 {
297 Expressions.MapMember<Person>(p => p.FullName(), (Expression<Func<Person,string>>)(p => p.LastName + ", " + p.FirstName));
298
299 ForEachProvider(db => AreEqual(
300 from p in Person where p.FullName() == "Pupkin, John" select p.FullName(),
301 from p in db.Person where p.FullName() == "Pupkin, John" select p.FullName()));
302 }
303
304 [Test]
305 public void Count1()
306 {
307 ForEachProvider(db => Assert.AreEqual(
308 Child.Count(c => c.ParentID == 1),
309 db.Child.Count(c => c.ParentID == 1)));
310 }
311
312 [Test]
313 public void Sum1()
314 {
315 ForEachProvider(db => Assert.AreEqual(
316 Child.Sum(c => c.ParentID),
317 db.Child.Sum(c => c.ParentID)));
318 }
319
320 [MethodExpression("ChildCountExpression")]
321 public static int ChildCount(Parent parent)
322 {
323 throw new NotSupportedException();
324 }
325
326 static Expression ChildCountExpression()
327 {
328 return
329 (Expression<Func<Parent, int>>)
330 (p => p.Children.Where(c => c.ParentID > 2).Sum(c => c.ParentID * c.ChildID));
331 }
332
333 [Test]
334 public void Sum2()
335 {
336 ForEachProvider(db => AreEqual(
337 Parent.Select(p => p.Children.Where(c => c.ParentID > 2).Sum(c => c.ParentID * c.ChildID)),
338 db.Parent.Select(p => ChildCount(p))));
339 }
340 }
341
342 public static class PersonExtension
343 {
344 static public string FullName(this Person person)
345 {
346 return person.LastName + ", " + person.FirstName;
347 }
348 }
349 }