Mercurial > pub > bltoolkit
comparison UnitTests/Linq/UpdateTest.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 | |
5 using BLToolkit.Data; | |
6 using BLToolkit.Data.DataProvider; | |
7 using BLToolkit.Data.Linq; | |
8 using BLToolkit.DataAccess; | |
9 using BLToolkit.Mapping; | |
10 | |
11 using NUnit.Framework; | |
12 | |
13 using Data.Linq; | |
14 using Data.Linq.Model; | |
15 | |
16 #region ReSharper disable | |
17 // ReSharper disable ConvertToConstant.Local | |
18 #endregion | |
19 | |
20 namespace Update | |
21 { | |
22 [TestFixture] | |
23 public class UpdateTest : TestBase | |
24 { | |
25 [Test] | |
26 public void Update1() | |
27 { | |
28 ForEachProvider(db => | |
29 { | |
30 try | |
31 { | |
32 var parent = new Parent1 { ParentID = 1001, Value1 = 1001 }; | |
33 | |
34 db.Parent.Delete(p => p.ParentID > 1000); | |
35 db.Insert(parent); | |
36 | |
37 Assert.AreEqual(1, db.Parent.Count (p => p.ParentID == parent.ParentID)); | |
38 Assert.AreEqual(1, db.Parent.Update(p => p.ParentID == parent.ParentID, p => new Parent { ParentID = p.ParentID + 1 })); | |
39 Assert.AreEqual(1, db.Parent.Count (p => p.ParentID == parent.ParentID + 1)); | |
40 } | |
41 finally | |
42 { | |
43 db.Child.Delete(c => c.ChildID > 1000); | |
44 } | |
45 }); | |
46 } | |
47 | |
48 [Test] | |
49 public void Update2() | |
50 { | |
51 ForEachProvider(db => | |
52 { | |
53 try | |
54 { | |
55 var parent = new Parent1 { ParentID = 1001, Value1 = 1001 }; | |
56 | |
57 db.Parent.Delete(p => p.ParentID > 1000); | |
58 db.Insert(parent); | |
59 | |
60 Assert.AreEqual(1, db.Parent.Count(p => p.ParentID == parent.ParentID)); | |
61 Assert.AreEqual(1, db.Parent.Where(p => p.ParentID == parent.ParentID).Update(p => new Parent { ParentID = p.ParentID + 1 })); | |
62 Assert.AreEqual(1, db.Parent.Count(p => p.ParentID == parent.ParentID + 1)); | |
63 } | |
64 finally | |
65 { | |
66 db.Child.Delete(c => c.ChildID > 1000); | |
67 } | |
68 }); | |
69 } | |
70 | |
71 [Test] | |
72 public void Update3() | |
73 { | |
74 ForEachProvider(new[] { ProviderName.Informix }, db => | |
75 { | |
76 try | |
77 { | |
78 var id = 1001; | |
79 | |
80 db.Child.Delete(c => c.ChildID > 1000); | |
81 db.Child.Insert(() => new Child { ParentID = 1, ChildID = id}); | |
82 | |
83 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id)); | |
84 Assert.AreEqual(1, db.Child.Where(c => c.ChildID == id && c.Parent.Value1 == 1).Update(c => new Child { ChildID = c.ChildID + 1 })); | |
85 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id + 1)); | |
86 } | |
87 finally | |
88 { | |
89 db.Child.Delete(c => c.ChildID > 1000); | |
90 } | |
91 }); | |
92 } | |
93 | |
94 [Test] | |
95 public void Update4() | |
96 { | |
97 ForEachProvider(new[] { ProviderName.Informix }, db => | |
98 { | |
99 try | |
100 { | |
101 var id = 1001; | |
102 | |
103 db.Child.Delete(c => c.ChildID > 1000); | |
104 db.Child.Insert(() => new Child { ParentID = 1, ChildID = id}); | |
105 | |
106 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id)); | |
107 Assert.AreEqual(1, | |
108 db.Child | |
109 .Where(c => c.ChildID == id && c.Parent.Value1 == 1) | |
110 .Set(c => c.ChildID, c => c.ChildID + 1) | |
111 .Update()); | |
112 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id + 1)); | |
113 } | |
114 finally | |
115 { | |
116 db.Child.Delete(c => c.ChildID > 1000); | |
117 } | |
118 }); | |
119 } | |
120 | |
121 [Test] | |
122 public void Update5() | |
123 { | |
124 ForEachProvider(new[] { ProviderName.Informix }, db => | |
125 { | |
126 try | |
127 { | |
128 var id = 1001; | |
129 | |
130 db.Child.Delete(c => c.ChildID > 1000); | |
131 db.Child.Insert(() => new Child { ParentID = 1, ChildID = id}); | |
132 | |
133 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id)); | |
134 Assert.AreEqual(1, | |
135 db.Child | |
136 .Where(c => c.ChildID == id && c.Parent.Value1 == 1) | |
137 .Set(c => c.ChildID, () => id + 1) | |
138 .Update()); | |
139 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id + 1)); | |
140 } | |
141 finally | |
142 { | |
143 db.Child.Delete(c => c.ChildID > 1000); | |
144 } | |
145 }); | |
146 } | |
147 | |
148 [Test] | |
149 public void Update6() | |
150 { | |
151 ForEachProvider(new[] { ProviderName.Informix }, db => | |
152 { | |
153 try | |
154 { | |
155 var id = 1001; | |
156 | |
157 db.Parent4.Delete(p => p.ParentID > 1000); | |
158 db.Insert(new Parent4 { ParentID = id, Value1 = TypeValue.Value1 }); | |
159 | |
160 Assert.AreEqual(1, db.Parent4.Count(p => p.ParentID == id && p.Value1 == TypeValue.Value1)); | |
161 Assert.AreEqual(1, | |
162 db.Parent4 | |
163 .Where(p => p.ParentID == id) | |
164 .Set(p => p.Value1, () => TypeValue.Value2) | |
165 .Update()); | |
166 Assert.AreEqual(1, db.Parent4.Count(p => p.ParentID == id && p.Value1 == TypeValue.Value2)); | |
167 } | |
168 finally | |
169 { | |
170 db.Parent4.Delete(p => p.ParentID > 1000); | |
171 } | |
172 }); | |
173 } | |
174 | |
175 [Test] | |
176 public void Update7() | |
177 { | |
178 ForEachProvider(new[] { ProviderName.Informix }, db => | |
179 { | |
180 try | |
181 { | |
182 var id = 1001; | |
183 | |
184 db.Parent4.Delete(p => p.ParentID > 1000); | |
185 db.Insert(new Parent4 { ParentID = id, Value1 = TypeValue.Value1 }); | |
186 | |
187 Assert.AreEqual(1, db.Parent4.Count(p => p.ParentID == id && p.Value1 == TypeValue.Value1)); | |
188 Assert.AreEqual(1, | |
189 db.Parent4 | |
190 .Where(p => p.ParentID == id) | |
191 .Set(p => p.Value1, TypeValue.Value2) | |
192 .Update()); | |
193 Assert.AreEqual(1, db.Parent4.Count(p => p.ParentID == id && p.Value1 == TypeValue.Value2)); | |
194 | |
195 Assert.AreEqual(1, | |
196 db.Parent4 | |
197 .Where(p => p.ParentID == id) | |
198 .Set(p => p.Value1, TypeValue.Value3) | |
199 .Update()); | |
200 Assert.AreEqual(1, db.Parent4.Count(p => p.ParentID == id && p.Value1 == TypeValue.Value3)); | |
201 } | |
202 finally | |
203 { | |
204 db.Parent4.Delete(p => p.ParentID > 1000); | |
205 } | |
206 }); | |
207 } | |
208 | |
209 [Test] | |
210 public void Update8() | |
211 { | |
212 ForEachProvider(db => | |
213 { | |
214 try | |
215 { | |
216 var parent = new Parent1 { ParentID = 1001, Value1 = 1001 }; | |
217 | |
218 db.Parent.Delete(p => p.ParentID > 1000); | |
219 db.Insert(parent); | |
220 | |
221 parent.Value1++; | |
222 | |
223 db.Update(parent); | |
224 | |
225 Assert.AreEqual(1002, db.Parent.Single(p => p.ParentID == parent.ParentID).Value1); | |
226 } | |
227 finally | |
228 { | |
229 db.Child.Delete(c => c.ChildID > 1000); | |
230 } | |
231 }); | |
232 } | |
233 | |
234 [Test] | |
235 public void Update9() | |
236 { | |
237 ForEachProvider(new[] { ProviderName.Informix, ProviderName.SqlCe, ProviderName.DB2, ProviderName.Firebird, "Oracle", "DevartOracle", ProviderName.PostgreSQL, ProviderName.MySql, ProviderName.SQLite, ProviderName.Access }, db => | |
238 { | |
239 try | |
240 { | |
241 var id = 1001; | |
242 | |
243 db.Child.Delete(c => c.ChildID > 1000); | |
244 db.Child.Insert(() => new Child { ParentID = 1, ChildID = id}); | |
245 | |
246 var q = | |
247 from c in db.Child | |
248 join p in db.Parent on c.ParentID equals p.ParentID | |
249 where c.ChildID == id && c.Parent.Value1 == 1 | |
250 select new { c, p }; | |
251 | |
252 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id)); | |
253 Assert.AreEqual(1, q.Update(db.Child, _ => new Child { ChildID = _.c.ChildID + 1, ParentID = _.p.ParentID })); | |
254 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id + 1)); | |
255 } | |
256 finally | |
257 { | |
258 db.Child.Delete(c => c.ChildID > 1000); | |
259 } | |
260 }); | |
261 } | |
262 | |
263 [Test] | |
264 public void Update10() | |
265 { | |
266 ForEachProvider(new[] { ProviderName.Informix, ProviderName.SqlCe, ProviderName.DB2, ProviderName.Firebird, "Oracle", "DevartOracle", ProviderName.PostgreSQL, ProviderName.MySql, ProviderName.SQLite, ProviderName.Access }, db => | |
267 { | |
268 try | |
269 { | |
270 var id = 1001; | |
271 | |
272 db.Child.Delete(c => c.ChildID > 1000); | |
273 db.Child.Insert(() => new Child { ParentID = 1, ChildID = id}); | |
274 | |
275 var q = | |
276 from p in db.Parent | |
277 join c in db.Child on p.ParentID equals c.ParentID | |
278 where c.ChildID == id && c.Parent.Value1 == 1 | |
279 select new { c, p }; | |
280 | |
281 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id)); | |
282 Assert.AreEqual(1, q.Update(db.Child, _ => new Child { ChildID = _.c.ChildID + 1, ParentID = _.p.ParentID })); | |
283 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id + 1)); | |
284 } | |
285 finally | |
286 { | |
287 db.Child.Delete(c => c.ChildID > 1000); | |
288 } | |
289 }); | |
290 } | |
291 | |
292 //[Test] | |
293 public void Update11() | |
294 { | |
295 ForEachProvider(db => | |
296 { | |
297 var q = db.GetTable<LinqDataTypes2>().Union(db.GetTable<LinqDataTypes2>()); | |
298 | |
299 //db.GetTable<LinqDataTypes2>().Update(_ => q.Contains(_), _ => new LinqDataTypes2 { GuidValue = _.GuidValue }); | |
300 | |
301 q.Update(_ => new LinqDataTypes2 { GuidValue = _.GuidValue }); | |
302 }); | |
303 } | |
304 | |
305 [Test] | |
306 public void UpdateAssociation1([DataContexts(ProviderName.Sybase, ProviderName.Informix)] string context) | |
307 { | |
308 using (var db = GetDataContext(context)) | |
309 { | |
310 const int childId = 10000; | |
311 const int parentId = 20000; | |
312 | |
313 try | |
314 { | |
315 db.Child. Delete(x => x.ChildID == childId); | |
316 db.Parent.Delete(x => x.ParentID == parentId); | |
317 | |
318 db.Parent.Insert(() => new Parent { ParentID = parentId, Value1 = parentId }); | |
319 db.Child. Insert(() => new Child { ChildID = childId, ParentID = parentId }); | |
320 | |
321 var parents = | |
322 from child in db.Child | |
323 where child.ChildID == childId | |
324 select child.Parent; | |
325 | |
326 Assert.AreEqual(1, parents.Update(db.Parent, x => new Parent { Value1 = 5 })); | |
327 } | |
328 finally | |
329 { | |
330 db.Child. Delete(x => x.ChildID == childId); | |
331 db.Parent.Delete(x => x.ParentID == parentId); | |
332 } | |
333 } | |
334 } | |
335 | |
336 [Test] | |
337 public void UpdateAssociation2([DataContexts(ProviderName.Sybase, ProviderName.Informix)] string context) | |
338 { | |
339 using (var db = GetDataContext(context)) | |
340 { | |
341 const int childId = 10000; | |
342 const int parentId = 20000; | |
343 | |
344 try | |
345 { | |
346 db.Child. Delete(x => x.ChildID == childId); | |
347 db.Parent.Delete(x => x.ParentID == parentId); | |
348 | |
349 db.Parent.Insert(() => new Parent { ParentID = parentId, Value1 = parentId }); | |
350 db.Child. Insert(() => new Child { ChildID = childId, ParentID = parentId }); | |
351 | |
352 var parents = | |
353 from child in db.Child | |
354 where child.ChildID == childId | |
355 select child.Parent; | |
356 | |
357 Assert.AreEqual(1, parents.Update(x => new Parent { Value1 = 5 })); | |
358 } | |
359 finally | |
360 { | |
361 db.Child. Delete(x => x.ChildID == childId); | |
362 db.Parent.Delete(x => x.ParentID == parentId); | |
363 } | |
364 } | |
365 } | |
366 | |
367 [Test] | |
368 public void UpdateAssociation3([DataContexts(ProviderName.Sybase, ProviderName.Informix)] string context) | |
369 { | |
370 using (var db = GetDataContext(context)) | |
371 { | |
372 const int childId = 10000; | |
373 const int parentId = 20000; | |
374 | |
375 try | |
376 { | |
377 db.Child. Delete(x => x.ChildID == childId); | |
378 db.Parent.Delete(x => x.ParentID == parentId); | |
379 | |
380 db.Parent.Insert(() => new Parent { ParentID = parentId, Value1 = parentId }); | |
381 db.Child. Insert(() => new Child { ChildID = childId, ParentID = parentId }); | |
382 | |
383 var parents = | |
384 from child in db.Child | |
385 where child.ChildID == childId | |
386 select child.Parent; | |
387 | |
388 Assert.AreEqual(1, parents.Update(x => x.ParentID > 0, x => new Parent { Value1 = 5 })); | |
389 } | |
390 finally | |
391 { | |
392 db.Child. Delete(x => x.ChildID == childId); | |
393 db.Parent.Delete(x => x.ParentID == parentId); | |
394 } | |
395 } | |
396 } | |
397 | |
398 [Test] | |
399 public void UpdateAssociation4([DataContexts(ProviderName.Sybase, ProviderName.Informix)] string context) | |
400 { | |
401 using (var db = GetDataContext(context)) | |
402 { | |
403 const int childId = 10000; | |
404 const int parentId = 20000; | |
405 | |
406 try | |
407 { | |
408 db.Child. Delete(x => x.ChildID == childId); | |
409 db.Parent.Delete(x => x.ParentID == parentId); | |
410 | |
411 db.Parent.Insert(() => new Parent { ParentID = parentId, Value1 = parentId }); | |
412 db.Child. Insert(() => new Child { ChildID = childId, ParentID = parentId }); | |
413 | |
414 var parents = | |
415 from child in db.Child | |
416 where child.ChildID == childId | |
417 select child.Parent; | |
418 | |
419 Assert.AreEqual(1, parents.Set(x => x.Value1, 5).Update()); | |
420 } | |
421 finally | |
422 { | |
423 db.Child. Delete(x => x.ChildID == childId); | |
424 db.Parent.Delete(x => x.ParentID == parentId); | |
425 } | |
426 } | |
427 } | |
428 | |
429 static readonly Func<TestDbManager,int,string,int> _updateQuery = | |
430 CompiledQuery.Compile <TestDbManager,int,string,int>((ctx,key,value) => | |
431 ctx.Person | |
432 .Where(_ => _.ID == key) | |
433 .Set(_ => _.FirstName, value) | |
434 .Update()); | |
435 | |
436 [Test] | |
437 public void CompiledUpdate() | |
438 { | |
439 using (var ctx = new TestDbManager()) | |
440 { | |
441 _updateQuery(ctx, 12345, "54321"); | |
442 } | |
443 } | |
444 | |
445 [TableName("LinqDataTypes")] | |
446 class Table1 | |
447 { | |
448 public int ID; | |
449 public bool BoolValue; | |
450 | |
451 [Association(ThisKey = "ID", OtherKey = "ParentID", CanBeNull = false)] | |
452 public List<Table2> Tables2; | |
453 } | |
454 | |
455 [TableName("Parent")] | |
456 class Table2 | |
457 { | |
458 public int ParentID; | |
459 public bool Value1; | |
460 | |
461 [Association(ThisKey = "ParentID", OtherKey = "ID", CanBeNull = false)] | |
462 public Table1 Table1; | |
463 } | |
464 | |
465 [Test] | |
466 public void UpdateAssociation5([DataContexts( | |
467 ProviderName.Access, ProviderName.DB2, ProviderName.Firebird, ProviderName.Informix, "Oracle", ProviderName.PostgreSQL, ProviderName.SqlCe, ProviderName.SQLite, | |
468 ExcludeLinqService=true)] string context) | |
469 { | |
470 using (var db = new DbManager(context)) | |
471 { | |
472 var ids = new[] { 10000, 20000 }; | |
473 | |
474 db.GetTable<Table2>() | |
475 .Where (x => ids.Contains(x.ParentID)) | |
476 .Select(x => x.Table1) | |
477 .Distinct() | |
478 .Set(y => y.BoolValue, y => y.Tables2.All(x => x.Value1)) | |
479 .Update(); | |
480 | |
481 var idx = db.LastQuery.IndexOf("INNER JOIN"); | |
482 | |
483 Assert.That(idx, Is.Not.EqualTo(-1)); | |
484 | |
485 idx = db.LastQuery.IndexOf("INNER JOIN", idx + 1); | |
486 | |
487 Assert.That(idx, Is.EqualTo(-1)); | |
488 } | |
489 } | |
490 | |
491 [Test] | |
492 public void AsUpdatableTest([DataContexts(ProviderName.Informix)] string context) | |
493 { | |
494 using (var db = GetDataContext(context)) | |
495 { | |
496 try | |
497 { | |
498 var id = 1001; | |
499 | |
500 db.Child.Delete(c => c.ChildID > 1000); | |
501 db.Child.Insert(() => new Child { ParentID = 1, ChildID = id}); | |
502 | |
503 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id)); | |
504 | |
505 var q = db.Child.Where(c => c.ChildID == id && c.Parent.Value1 == 1); | |
506 var uq = q.AsUpdatable(); | |
507 | |
508 uq = uq.Set(c => c.ChildID, c => c.ChildID + 1); | |
509 | |
510 Assert.AreEqual(1, uq.Update()); | |
511 Assert.AreEqual(1, db.Child.Count(c => c.ChildID == id + 1)); | |
512 } | |
513 finally | |
514 { | |
515 db.Child.Delete(c => c.ChildID > 1000); | |
516 } | |
517 } | |
518 } | |
519 | |
520 [TableName("GrandChild")] | |
521 class Table3 | |
522 { | |
523 [PrimaryKey(1)] public int? ParentID; | |
524 [PrimaryKey(2)] public int? ChildID; | |
525 public int? GrandChildID; | |
526 } | |
527 | |
528 [Test] | |
529 public void UpdateNullablePrimaryKey([DataContexts] string context) | |
530 { | |
531 using (var db = GetDataContext(context)) | |
532 { | |
533 db.Update(new Table3 { ParentID = 10000, ChildID = null, GrandChildID = 1000 }); | |
534 | |
535 if (db is DbManager) | |
536 Assert.IsTrue(((DbManager)db).LastQuery.Contains("IS NULL")); | |
537 | |
538 db.Update(new Table3 { ParentID = 10000, ChildID = 111, GrandChildID = 1000 }); | |
539 | |
540 if (db is DbManager) | |
541 Assert.IsFalse(((DbManager)db).LastQuery.Contains("IS NULL")); | |
542 } | |
543 } | |
544 } | |
545 } |