Mercurial > pub > bltoolkit
comparison UnitTests/Linq/DeleteTest.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 BLToolkit.Data.DataProvider; | |
5 using BLToolkit.Data.Linq; | |
6 | |
7 using NUnit.Framework; | |
8 | |
9 using Data.Linq; | |
10 using Data.Linq.Model; | |
11 | |
12 #region ReSharper disable | |
13 // ReSharper disable ConvertToConstant.Local | |
14 // ReSharper disable CheckNamespace | |
15 #endregion | |
16 | |
17 namespace Update | |
18 { | |
19 [TestFixture] | |
20 public class DeleteTest : TestBase | |
21 { | |
22 [Test] | |
23 public void Delete1() | |
24 { | |
25 ForEachProvider(db => | |
26 { | |
27 var parent = new Parent1 { ParentID = 1001, Value1 = 1001 }; | |
28 | |
29 db.Delete(parent); | |
30 | |
31 try | |
32 { | |
33 db.Insert(parent); | |
34 | |
35 Assert.AreEqual(1, db.Parent.Count (p => p.ParentID == parent.ParentID)); | |
36 Assert.AreEqual(1, db.Parent.Delete(p => p.ParentID == parent.ParentID)); | |
37 Assert.AreEqual(0, db.Parent.Count (p => p.ParentID == parent.ParentID)); | |
38 } | |
39 finally | |
40 { | |
41 db.Delete(parent); | |
42 } | |
43 }); | |
44 } | |
45 | |
46 [Test] | |
47 public void Delete2() | |
48 { | |
49 ForEachProvider(db => | |
50 { | |
51 var parent = new Parent1 { ParentID = 1001, Value1 = 1001 }; | |
52 | |
53 db.Delete(parent); | |
54 | |
55 try | |
56 { | |
57 db.Insert(parent); | |
58 | |
59 Assert.AreEqual(1, db.Parent.Count(p => p.ParentID == parent.ParentID)); | |
60 Assert.AreEqual(1, db.Parent.Where(p => p.ParentID == parent.ParentID).Delete()); | |
61 Assert.AreEqual(0, db.Parent.Count(p => p.ParentID == parent.ParentID)); | |
62 } | |
63 finally | |
64 { | |
65 db.Delete(parent); | |
66 } | |
67 }); | |
68 } | |
69 | |
70 [Test] | |
71 public void Delete3() | |
72 { | |
73 ForEachProvider(new[] { ProviderName.Informix }, db => | |
74 { | |
75 db.Child.Delete(c => new[] { 1001, 1002 }.Contains(c.ChildID)); | |
76 | |
77 try | |
78 { | |
79 db.Child.Insert(() => new Child { ParentID = 1, ChildID = 1001 }); | |
80 db.Child.Insert(() => new Child { ParentID = 1, ChildID = 1002 }); | |
81 | |
82 Assert.AreEqual(3, db.Child.Count(c => c.ParentID == 1)); | |
83 Assert.AreEqual(2, db.Child.Where(c => c.Parent.ParentID == 1 && new[] { 1001, 1002 }.Contains(c.ChildID)).Delete()); | |
84 Assert.AreEqual(1, db.Child.Count(c => c.ParentID == 1)); | |
85 } | |
86 finally | |
87 { | |
88 db.Child.Delete(c => new[] { 1001, 1002 }.Contains(c.ChildID)); | |
89 } | |
90 }); | |
91 } | |
92 | |
93 [Test] | |
94 public void Delete4() | |
95 { | |
96 ForEachProvider(new[] { ProviderName.Informix }, db => | |
97 { | |
98 db.GrandChild1.Delete(gc => new[] { 1001, 1002 }.Contains(gc.GrandChildID.Value)); | |
99 | |
100 try | |
101 { | |
102 db.GrandChild.Insert(() => new GrandChild { ParentID = 1, ChildID = 1, GrandChildID = 1001 }); | |
103 db.GrandChild.Insert(() => new GrandChild { ParentID = 1, ChildID = 2, GrandChildID = 1002 }); | |
104 | |
105 Assert.AreEqual(3, db.GrandChild1.Count(gc => gc.ParentID == 1)); | |
106 Assert.AreEqual(2, db.GrandChild1.Where(gc => gc.Parent.ParentID == 1 && new[] { 1001, 1002 }.Contains(gc.GrandChildID.Value)).Delete()); | |
107 Assert.AreEqual(1, db.GrandChild1.Count(gc => gc.ParentID == 1)); | |
108 } | |
109 finally | |
110 { | |
111 db.GrandChild1.Delete(gc => new[] { 1001, 1002 }.Contains(gc.GrandChildID.Value)); | |
112 } | |
113 }); | |
114 } | |
115 | |
116 [Test] | |
117 public void Delete5() | |
118 { | |
119 ForEachProvider(db => | |
120 { | |
121 var values = new[] { 1001, 1002 }; | |
122 | |
123 db.Parent.Delete(_ => _.ParentID > 1000); | |
124 | |
125 try | |
126 { | |
127 db.Parent.Insert(() => new Parent { ParentID = values[0], Value1 = 1 }); | |
128 db.Parent.Insert(() => new Parent { ParentID = values[1], Value1 = 1 }); | |
129 | |
130 Assert.AreEqual(2, db.Parent.Count(_ => _.ParentID > 1000)); | |
131 Assert.AreEqual(2, db.Parent.Delete(_ => values.Contains(_.ParentID))); | |
132 Assert.AreEqual(0, db.Parent.Count(_ => _.ParentID > 1000)); | |
133 } | |
134 finally | |
135 { | |
136 db.Parent.Delete(_ => _.ParentID > 1000); | |
137 } | |
138 }); | |
139 } | |
140 | |
141 [Test] | |
142 public void AlterDelete([DataContexts(ProviderName.Informix, ExcludeLinqService=true)] string context) | |
143 { | |
144 using (var db = GetDataContext(context)) | |
145 { | |
146 var q = | |
147 from p in db.Parent | |
148 join ch in db.Child on p.ParentID equals ch.ParentID into lj1 | |
149 from ch in lj1.DefaultIfEmpty() | |
150 where ch != null && ch.ParentID == -1 || ch == null && p.ParentID == -1 | |
151 select p; | |
152 | |
153 q.Delete(); | |
154 | |
155 var sql = ((TestDbManager)db).LastQuery; | |
156 | |
157 if (sql.Contains("EXISTS")) | |
158 Assert.That(sql.IndexOf("(("), Is.GreaterThan(0)); | |
159 } | |
160 } | |
161 | |
162 [Test] | |
163 public void DeleteMany1([DataContexts( | |
164 ProviderName.Access, ProviderName.DB2, ProviderName.Informix, "Oracle", | |
165 ProviderName.PostgreSQL, ProviderName.SqlCe, ProviderName.SQLite, ProviderName.Firebird | |
166 )] string context) | |
167 { | |
168 using (var db = GetDataContext(context)) | |
169 { | |
170 db.Parent.Insert(() => new Parent { ParentID = 1001 }); | |
171 db.Child. Insert(() => new Child { ParentID = 1001, ChildID = 1 }); | |
172 db.Child. Insert(() => new Child { ParentID = 1001, ChildID = 2 }); | |
173 | |
174 try | |
175 { | |
176 var q = | |
177 from p in db.Parent | |
178 where p.ParentID >= 1000 | |
179 select p; | |
180 | |
181 var n = q.SelectMany(p => p.Children).Delete(); | |
182 | |
183 Assert.That(n, Is.GreaterThanOrEqualTo(2)); | |
184 } | |
185 finally | |
186 { | |
187 db.Child. Delete(c => c.ParentID >= 1000); | |
188 db.Parent.Delete(c => c.ParentID >= 1000); | |
189 } | |
190 } | |
191 } | |
192 | |
193 [Test] | |
194 public void DeleteMany2([DataContexts( | |
195 ProviderName.Access, ProviderName.DB2, ProviderName.Informix, "Oracle", | |
196 ProviderName.PostgreSQL, ProviderName.SqlCe, ProviderName.SQLite, ProviderName.Firebird | |
197 )] string context) | |
198 { | |
199 using (var db = GetDataContext(context)) | |
200 { | |
201 db.Parent. Insert(() => new Parent { ParentID = 1001 }); | |
202 db.Child. Insert(() => new Child { ParentID = 1001, ChildID = 1 }); | |
203 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 1, GrandChildID = 1}); | |
204 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 1, GrandChildID = 2}); | |
205 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 1, GrandChildID = 3}); | |
206 db.Child. Insert(() => new Child { ParentID = 1001, ChildID = 2 }); | |
207 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 2, GrandChildID = 1}); | |
208 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 2, GrandChildID = 2}); | |
209 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 2, GrandChildID = 3}); | |
210 | |
211 try | |
212 { | |
213 var q = | |
214 from p in db.Parent | |
215 where p.ParentID >= 1000 | |
216 select p; | |
217 | |
218 var n1 = q.SelectMany(p => p.Children.SelectMany(c => c.GrandChildren)).Delete(); | |
219 var n2 = q.SelectMany(p => p.Children). Delete(); | |
220 | |
221 Assert.That(n1, Is.EqualTo(6)); | |
222 Assert.That(n2, Is.EqualTo(2)); | |
223 } | |
224 finally | |
225 { | |
226 db.GrandChild.Delete(c => c.ParentID >= 1000); | |
227 db.Child. Delete(c => c.ParentID >= 1000); | |
228 db.Parent. Delete(c => c.ParentID >= 1000); | |
229 } | |
230 } | |
231 } | |
232 | |
233 [Test] | |
234 public void DeleteMany3([DataContexts( | |
235 ProviderName.Access, ProviderName.DB2, ProviderName.Informix, "Oracle", | |
236 ProviderName.PostgreSQL, ProviderName.SqlCe, ProviderName.SQLite, ProviderName.Firebird | |
237 )] string context) | |
238 { | |
239 var ids = new[] { 1001 }; | |
240 | |
241 using (var db = GetDataContext(context)) | |
242 { | |
243 db.GrandChild.Delete(c => c.ParentID >= 1000); | |
244 db.Child. Delete(c => c.ParentID >= 1000); | |
245 db.Parent. Delete(c => c.ParentID >= 1000); | |
246 | |
247 db.Parent. Insert(() => new Parent { ParentID = 1001 }); | |
248 db.Child. Insert(() => new Child { ParentID = 1001, ChildID = 1 }); | |
249 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 1, GrandChildID = 1}); | |
250 db.GrandChild.Insert(() => new GrandChild { ParentID = 1001, ChildID = 1, GrandChildID = 2}); | |
251 | |
252 try | |
253 { | |
254 var q = | |
255 from p in db.Parent | |
256 where ids.Contains(p.ParentID) | |
257 select p; | |
258 | |
259 var n1 = q.SelectMany(p => p.Children).SelectMany(gc => gc.GrandChildren).Delete(); | |
260 | |
261 Assert.That(n1, Is.EqualTo(2)); | |
262 } | |
263 finally | |
264 { | |
265 db.GrandChild.Delete(c => c.ParentID >= 1000); | |
266 db.Child. Delete(c => c.ParentID >= 1000); | |
267 db.Parent. Delete(c => c.ParentID >= 1000); | |
268 } | |
269 } | |
270 } | |
271 } | |
272 } |