Datei: IntegrationTests/IntegrationTests/TransactionScopeTests.cs
Last Commit (8bd8c9d)
1 | // |
2 | // Copyright (c) 2002-2016 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using NUnit.Framework; |
24 | using System; |
25 | using NDO; |
26 | using Reisekosten.Personal; |
27 | using System.Text.RegularExpressions; |
28 | using NDO.Query; |
29 | using Reisekosten; |
30 | using Formfakten.TestLogger; |
31 | |
32 | namespace NdoUnitTests |
33 | { |
34 | ····//[Ignore( "These tests must be adapted to the TransactionScope." )] |
35 | ····[TestFixture] |
36 | ····public class TransactionScopeTests : NDOTest |
37 | ····{ |
38 | public void Setup( ) { } |
39 | |
40 | ········public void TearDown() |
41 | ········{ |
42 | ············Logger.ClearTestLogs(); |
43 | ············var pm = PmFactory.NewPersistenceManager(); |
44 | ············NDOQuery<Mitarbeiter> q = new NDOQuery<Mitarbeiter>( pm ); |
45 | ············pm.Delete( q.Execute() ); |
46 | ········} |
47 | |
48 | |
49 | ········/* |
50 | ········forceCommit should be: |
51 | ········ |
52 | ····················Query····Save····Save(true) |
53 | ········Optimistic····1········1········0 |
54 | ········Pessimistic····0········1········0 |
55 | ············ |
56 | ········Deferred Mode············ |
57 | ····················Query····Save····Save(true) |
58 | ········Optimistic····0········1········0 |
59 | ········Pessimistic····0········1········0 |
60 | |
61 | ········ */ |
62 | |
63 | ········[Test] |
64 | ········public void TestIfQueryWithoutTransactionDoesNotStartAndNotCommit() |
65 | ········{ |
66 | ············using (var pm = PmFactory.NewPersistenceManager()) |
67 | ············{ |
68 | ················pm.TransactionMode = TransactionMode.None; |
69 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
70 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
71 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
72 | ················Assert.That( startCount == 0, "Transaction shouldn't be started" ); |
73 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
74 | ············} |
75 | ········} |
76 | |
77 | |
78 | ········[Test] |
79 | ········public void TestIfOptimisticQueryIsImmediatelyCommitted() |
80 | ········{ |
81 | ············using (var pm = PmFactory.NewPersistenceManager()) |
82 | ············{ |
83 | ················pm.TransactionMode = TransactionMode.Optimistic; |
84 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
85 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
86 | ················Assert.That( commitCount > 0, "Transaction should be committed" ); |
87 | ············} |
88 | ········} |
89 | |
90 | ········[Test] |
91 | ········public void TestIfPessimisticQueryIsNotCommitted() |
92 | ········{ |
93 | ············using (var pm = PmFactory.NewPersistenceManager()) |
94 | ············{ |
95 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
96 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
97 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
98 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
99 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
100 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
101 | ············} |
102 | ········} |
103 | |
104 | ········[Test] |
105 | ········public void TestIfOptimisticSaveIsCommitted() |
106 | ········{ |
107 | ············using (var pm = PmFactory.NewPersistenceManager()) |
108 | ············{ |
109 | ················pm.TransactionMode = TransactionMode.Optimistic; |
110 | ················Mitarbeiter m = new Mitarbeiter(); |
111 | ················pm.MakePersistent( m ); |
112 | ················pm.Save(); |
113 | |
114 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
115 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
116 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
117 | ················Assert.That( commitCount > 0, "Transaction should be committed" ); |
118 | ············} |
119 | ········} |
120 | |
121 | ········[Test] |
122 | ········public void TestIfPessimisticSaveIsCommitted() |
123 | ········{ |
124 | ············using (var pm = PmFactory.NewPersistenceManager()) |
125 | ············{ |
126 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
127 | ················Mitarbeiter m = new Mitarbeiter(); |
128 | ················pm.MakePersistent( m ); |
129 | ················pm.Save(); |
130 | |
131 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
132 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
133 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
134 | ················Assert.That( commitCount > 0, "Transaction should be committed" ); |
135 | ············} |
136 | ········} |
137 | |
138 | ········[Test] |
139 | ········public void OptimisticQueryAndSaveShouldHaveTwoTransactions() |
140 | ········{ |
141 | ············using (var pm = PmFactory.NewPersistenceManager()) |
142 | ············{ |
143 | ················Logger.ClearTestLogs(); |
144 | ················pm.TransactionMode = TransactionMode.Optimistic; |
145 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
146 | ················Mitarbeiter m = new Mitarbeiter(); |
147 | ················pm.MakePersistent( m ); |
148 | ················pm.Save(); |
149 | |
150 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
151 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
152 | ················Assert.That( startCount == 2, "Two Transactions should be started" ); |
153 | ················Assert.That( commitCount == 2, "Two Transactions should be committed" ); |
154 | ············} |
155 | ········} |
156 | |
157 | ········[Test] |
158 | ········public void PessimisticQueryAndSaveShouldHaveOneTransaction() |
159 | ········{ |
160 | ············using (var pm = PmFactory.NewPersistenceManager()) |
161 | ············{ |
162 | ················Logger.ClearTestLogs(); |
163 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
164 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
165 | ················Mitarbeiter m = new Mitarbeiter(); |
166 | ················pm.MakePersistent( m ); |
167 | ················pm.Save(); |
168 | |
169 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
170 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
171 | ················Assert.That( startCount == 1, "One Transaction should be started" ); |
172 | ················Assert.That( commitCount == 1, "One Transaction should be committed" ); |
173 | ············} |
174 | ········} |
175 | |
176 | ········[Test] |
177 | ········public void OptimisticDeferredSaveShouldNotCommit() |
178 | ········{ |
179 | ············using (var pm = PmFactory.NewPersistenceManager()) |
180 | ············{ |
181 | ················pm.TransactionMode = TransactionMode.Optimistic; |
182 | ················pm.LogAdapter = new TestLogAdapter(); |
183 | ················pm.VerboseMode = true; |
184 | ················Mitarbeiter m = new Mitarbeiter(); |
185 | ················pm.MakePersistent( m ); |
186 | ················pm.Save( true ); |
187 | string log = pm. LogAdapter. ToString( ) ; |
188 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
189 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ) ; |
190 | ············} |
191 | ········} |
192 | |
193 | ········[Test] |
194 | ········public void PessimisticDeferredSaveShouldNotCommit() |
195 | ········{ |
196 | ············using (var pm = PmFactory.NewPersistenceManager()) |
197 | ············{ |
198 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
199 | ················pm.LogAdapter = new TestLogAdapter(); |
200 | ················pm.VerboseMode = true; |
201 | ················Mitarbeiter m = new Mitarbeiter(); |
202 | ················pm.MakePersistent( m ); |
203 | ················pm.Save( true ); |
204 | string log = pm. LogAdapter. ToString( ) ; |
205 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
206 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ) ; |
207 | ············} |
208 | ········} |
209 | |
210 | ········[Test] |
211 | ········public void OptimisticDeferredSaveAndQueryShouldNotCommit() |
212 | ········{ |
213 | ············using (var pm = PmFactory.NewPersistenceManager()) |
214 | ············{ |
215 | ················pm.TransactionMode = TransactionMode.Optimistic; |
216 | ················pm.LogAdapter = new TestLogAdapter(); |
217 | ················pm.VerboseMode = true; |
218 | ················Mitarbeiter m = new Mitarbeiter(); |
219 | ················pm.MakePersistent( m ); |
220 | ················pm.Save( true ); |
221 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
222 | string log = pm. LogAdapter. ToString( ) ; |
223 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
224 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should not be committed" ) ; |
225 | ············} |
226 | ········} |
227 | |
228 | ········[Test] |
229 | ········public void PessimisticDeferredSaveAndQueryShouldNotCommit() |
230 | ········{ |
231 | ············using (var pm = PmFactory.NewPersistenceManager()) |
232 | ············{ |
233 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
234 | ················pm.LogAdapter = new TestLogAdapter(); |
235 | ················pm.VerboseMode = true; |
236 | ················Mitarbeiter m = new Mitarbeiter(); |
237 | ················pm.MakePersistent( m ); |
238 | ················pm.Save( true ); |
239 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
240 | string log = pm. LogAdapter. ToString( ) ; |
241 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
242 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should not be committed" ) ; |
243 | ············} |
244 | ········} |
245 | |
246 | ········[Test] |
247 | ········public void OptimisticSaveAfterDeferredSaveShouldCommit() |
248 | ········{ |
249 | ············using (var pm = PmFactory.NewPersistenceManager()) |
250 | ············{ |
251 | ················pm.TransactionMode = TransactionMode.Optimistic; |
252 | ················pm.LogAdapter = new TestLogAdapter(); |
253 | ················pm.VerboseMode = true; |
254 | ················Mitarbeiter m = new Mitarbeiter(); |
255 | ················pm.MakePersistent( m ); |
256 | ················pm.Save( true ); |
257 | ················m.Nachname = "Test"; |
258 | ················pm.Save(); |
259 | string log = pm. LogAdapter. ToString( ) ; |
260 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transaction should be started" ) ; |
261 | Assert. That( new Regex( "Committing transaction" ) . Matches( log ) . Count == 1, "One Transaction should be committed" ) ; |
262 | ············} |
263 | ········} |
264 | |
265 | ········[Test] |
266 | ········public void PessimisticSaveAfterDeferredSaveShouldCommit() |
267 | ········{ |
268 | ············using (var pm = PmFactory.NewPersistenceManager()) |
269 | ············{ |
270 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
271 | ················pm.LogAdapter = new TestLogAdapter(); |
272 | ················pm.VerboseMode = true; |
273 | ················Mitarbeiter m = new Mitarbeiter(); |
274 | ················pm.MakePersistent( m ); |
275 | ················pm.Save( true ); |
276 | ················m.Nachname = "Test"; |
277 | ················pm.Save(); |
278 | string log = pm. LogAdapter. ToString( ) ; |
279 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transaction should be started" ) ; |
280 | Assert. That( new Regex( "Committing transaction" ) . Matches( log ) . Count == 1, "One Transaction should be committed" ) ; |
281 | ············} |
282 | ········} |
283 | |
284 | |
285 | ········[Test] |
286 | ········public void DirectSqlPassThroughWithoutTransactionShouldNotCommit() |
287 | ········{ |
288 | ············using (var pm = PmFactory.NewPersistenceManager()) |
289 | ············{ |
290 | ················pm.TransactionMode = TransactionMode.None; |
291 | ················pm.LogAdapter = new TestLogAdapter(); |
292 | ················pm.VerboseMode = true; |
293 | ················using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
294 | ················{ |
295 | ····················var reader = sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
296 | ····················string log = pm.LogAdapter.ToString(); |
297 | ····················Assert.That( log.IndexOf( "Starting transaction" ) == -1, "Transaction should be committed" ); |
298 | ····················Assert.That( log.IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ); |
299 | ····················Assert.That( reader == null, "Reader should be null" ); |
300 | ················} |
301 | ············} |
302 | ········} |
303 | |
304 | ········int FlughafenCount(PersistenceManager pm) |
305 | ········{ |
306 | ············NDOQuery<Flughafen> q = new NDOQuery<Flughafen>( pm ); |
307 | ············return (int)(decimal)q.ExecuteAggregate( "oid", AggregateType.Count ); |
308 | ········} |
309 | |
310 | ········int LänderCount( PersistenceManager pm ) |
311 | ········{ |
312 | ············NDOQuery<Land> q = new NDOQuery<Land>( pm ); |
313 | ············return (int)(decimal)q.ExecuteAggregate( "oid", AggregateType.Count ); |
314 | ········} |
315 | |
316 | ········[Test] |
317 | ········public void AbortedDeferredMultiStepTransactionDoesNotCommit() |
318 | ········{ |
319 | ············using (var pm = PmFactory.NewPersistenceManager()) |
320 | ············{ |
321 | ················var landCount = LänderCount( pm ); |
322 | ················var fhCount = FlughafenCount( pm ); |
323 | |
324 | ················pm.LogAdapter = new TestLogAdapter(); |
325 | ················pm.VerboseMode = true; |
326 | ················pm.TransactionMode = TransactionMode.Optimistic; |
327 | |
328 | ················Land land = new Land(); |
329 | ················land.Name = "Germany"; |
330 | ················pm.MakePersistent( land ); |
331 | ················pm.Save( true ); |
332 | |
333 | ················var flughafen = new Flughafen(); |
334 | ················flughafen.Kürzel = "MUC"; |
335 | ················pm.MakePersistent( flughafen ); |
336 | ················land.AddFlughafen( flughafen ); |
337 | ················pm.Save( true ); |
338 | |
339 | ················pm.Abort(); |
340 | |
341 | ················string log = pm.LogAdapter.ToString(); |
342 | ················Assert.That( landCount == LänderCount( pm ) ); |
343 | ················Assert.That( fhCount == FlughafenCount( pm ) ); |
344 | ············} |
345 | ········} |
346 | |
347 | ········[Test] |
348 | ········public void DirectSqlPassThroughWithTransactionShouldCommit() |
349 | ········{ |
350 | ············using (var pm = PmFactory.NewPersistenceManager()) |
351 | ············{ |
352 | |
353 | ················pm.LogAdapter = new TestLogAdapter(); |
354 | ················pm.VerboseMode = true; |
355 | ················using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
356 | ················{ |
357 | ····················sqlHandler.BeginTransaction(); |
358 | ····················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
359 | ····················sqlHandler.Execute( "DELETE FROM Reise" ); |
360 | ····················sqlHandler.CommitTransaction(); |
361 | ················} |
362 | string log = pm. LogAdapter. ToString( ) ; |
363 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transaction should be started" ) ; |
364 | Assert. That( new Regex( "Committing transaction" ) . Matches( log ) . Count == 1, "One Transaction should be committed" ) ; |
365 | ············} |
366 | ········} |
367 | |
368 | ········[Test] |
369 | ········public void DirectSqlPassThroughWithCombinedStatementsDoesNotCommit() |
370 | ········{ |
371 | ············var pm = PmFactory.NewPersistenceManager(); |
372 | pm. LogAdapter = new TestLogAdapter( ) ; |
373 | ············pm.VerboseMode = true; |
374 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
375 | ············{ |
376 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
377 | ················sqlHandler.Execute( "DELETE FROM Reise" ); |
378 | ············} |
379 | string log = pm. LogAdapter. ToString( ) ; |
380 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should not be committed" ) ; |
381 | ········} |
382 | |
383 | ········[Test] |
384 | ········public void DirectSqlPassWithAbortWorks() |
385 | ········{ |
386 | ············var pm = PmFactory.NewPersistenceManager(); |
387 | ············var m = new Mitarbeiter(); |
388 | ············pm.MakePersistent( m ); |
389 | ············pm.Save(); |
390 | ············pm.UnloadCache(); |
391 | //pm. LogAdapter = new TestLogAdapter( ) ; |
392 | ············//pm.VerboseMode = true; |
393 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
394 | ············{ |
395 | ················sqlHandler.BeginTransaction(); |
396 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
397 | ················pm.Abort(); |
398 | ············} |
399 | //string log = pm. LogAdapter. ToString( ) ; |
400 | ············pm = PmFactory.NewPersistenceManager(); |
401 | ············bool hasRecords = new NDOQuery<Mitarbeiter>( pm ).Execute().Count > 0; |
402 | ············Assert.That( hasRecords, "At least one record should be present" ); |
403 | ········} |
404 | ····} |
405 | } |
406 |
New Commit (08b23aa)
1 | // |
2 | // Copyright (c) 2002-2016 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | /* |
23 | This Code should test the TransactionScopes. Actually the tests in this file are copy & paste from TransactionTests. |
24 | */ |
25 | |
26 | |
27 | #if nix |
28 | |
29 | using NUnit.Framework; |
30 | using System; |
31 | using NDO; |
32 | using Reisekosten.Personal; |
33 | using System.Text.RegularExpressions; |
34 | using NDO.Query; |
35 | using Reisekosten; |
36 | using Formfakten.TestLogger; |
37 | |
38 | namespace NdoUnitTests |
39 | { |
40 | ····//[Ignore( "These tests must be adapted to the TransactionScope." )] |
41 | ····[TestFixture] |
42 | ····public class TransactionScopeTests : NDOTest |
43 | ····{ |
44 | public void Setup( ) |
45 | ········{ |
46 | ············Logger.ClearTestLogs(); |
47 | ········} |
48 | |
49 | ········public void TearDown() |
50 | ········{ |
51 | ············Logger.ClearTestLogs(); |
52 | ············var pm = PmFactory.NewPersistenceManager(); |
53 | ············NDOQuery<Mitarbeiter> q = new NDOQuery<Mitarbeiter>( pm ); |
54 | ············pm.Delete( q.Execute() ); |
55 | ········} |
56 | |
57 | |
58 | ········/* |
59 | ········forceCommit should be: |
60 | ········ |
61 | ····················Query····Save····Save(true) |
62 | ········Optimistic····1········1········0 |
63 | ········Pessimistic····0········1········0 |
64 | ············ |
65 | ········Deferred Mode············ |
66 | ····················Query····Save····Save(true) |
67 | ········Optimistic····0········1········0 |
68 | ········Pessimistic····0········1········0 |
69 | |
70 | ········ */ |
71 | |
72 | ········[Test] |
73 | ········public void TestIfQueryWithoutTransactionDoesNotStartAndNotCommit() |
74 | ········{ |
75 | ············using (var pm = PmFactory.NewPersistenceManager()) |
76 | ············{ |
77 | ················pm.TransactionMode = TransactionMode.None; |
78 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
79 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
80 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
81 | ················Assert.That( startCount == 0, "Transaction shouldn't be started" ); |
82 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
83 | ············} |
84 | ········} |
85 | |
86 | |
87 | ········[Test] |
88 | ········public void TestIfOptimisticQueryIsImmediatelyCommitted() |
89 | ········{ |
90 | ············using (var pm = PmFactory.NewPersistenceManager()) |
91 | ············{ |
92 | ················pm.TransactionMode = TransactionMode.Optimistic; |
93 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
94 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
95 | ················Assert.That( commitCount > 0, "Transaction should be committed" ); |
96 | ············} |
97 | ········} |
98 | |
99 | ········[Test] |
100 | ········public void TestIfPessimisticQueryIsNotCommitted() |
101 | ········{ |
102 | ············using (var pm = PmFactory.NewPersistenceManager()) |
103 | ············{ |
104 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
105 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
106 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
107 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
108 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
109 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
110 | ············} |
111 | ········} |
112 | |
113 | ········[Test] |
114 | ········public void TestIfOptimisticSaveIsCommitted() |
115 | ········{ |
116 | ············using (var pm = PmFactory.NewPersistenceManager()) |
117 | ············{ |
118 | ················pm.TransactionMode = TransactionMode.Optimistic; |
119 | ················Mitarbeiter m = new Mitarbeiter(); |
120 | ················pm.MakePersistent( m ); |
121 | ················pm.Save(); |
122 | |
123 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
124 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
125 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
126 | ················Assert.That( commitCount > 0, "Transaction should be committed" ); |
127 | ············} |
128 | ········} |
129 | |
130 | ········[Test] |
131 | ········public void TestIfPessimisticSaveIsCommitted() |
132 | ········{ |
133 | ············using (var pm = PmFactory.NewPersistenceManager()) |
134 | ············{ |
135 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
136 | ················Mitarbeiter m = new Mitarbeiter(); |
137 | ················pm.MakePersistent( m ); |
138 | ················pm.Save(); |
139 | |
140 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
141 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
142 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
143 | ················Assert.That( commitCount > 0, "Transaction should be committed" ); |
144 | ············} |
145 | ········} |
146 | |
147 | ········[Test] |
148 | ········public void OptimisticQueryAndSaveShouldHaveTwoTransactions() |
149 | ········{ |
150 | ············using (var pm = PmFactory.NewPersistenceManager()) |
151 | ············{ |
152 | ················pm.TransactionMode = TransactionMode.Optimistic; |
153 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
154 | ················Mitarbeiter m = new Mitarbeiter(); |
155 | ················pm.MakePersistent( m ); |
156 | ················pm.Save(); |
157 | |
158 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
159 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
160 | ················Assert.That( startCount == 2, "Two Transactions should be started" ); |
161 | ················Assert.That( commitCount == 2, "Two Transactions should be committed" ); |
162 | ············} |
163 | ········} |
164 | |
165 | ········[Test] |
166 | ········public void PessimisticQueryAndSaveShouldHaveOneTransaction() |
167 | ········{ |
168 | ············using (var pm = PmFactory.NewPersistenceManager()) |
169 | ············{ |
170 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
171 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
172 | ················Mitarbeiter m = new Mitarbeiter(); |
173 | ················pm.MakePersistent( m ); |
174 | ················pm.Save(); |
175 | |
176 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
177 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
178 | ················Assert.That( startCount == 1, "One Transaction should be started" ); |
179 | ················Assert.That( commitCount == 1, "One Transaction should be committed" ); |
180 | ············} |
181 | ········} |
182 | |
183 | ········[Test] |
184 | ········public void OptimisticDeferredSaveShouldNotCommit() |
185 | ········{ |
186 | ············using (var pm = PmFactory.NewPersistenceManager()) |
187 | ············{ |
188 | ················pm.TransactionMode = TransactionMode.Optimistic; |
189 | ················Mitarbeiter m = new Mitarbeiter(); |
190 | ················pm.MakePersistent( m ); |
191 | ················pm.Save( true ); |
192 | |
193 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
194 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
195 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
196 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
197 | ············} |
198 | ········} |
199 | |
200 | ········[Test] |
201 | ········public void PessimisticDeferredSaveShouldNotCommit() |
202 | ········{ |
203 | ············using (var pm = PmFactory.NewPersistenceManager()) |
204 | ············{ |
205 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
206 | ················Mitarbeiter m = new Mitarbeiter(); |
207 | ················pm.MakePersistent( m ); |
208 | ················pm.Save( true ); |
209 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
210 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
211 | Assert. That( startCount > 0, "Transaction should be started" ) ; |
212 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
213 | ············} |
214 | ········} |
215 | |
216 | ········[Test] |
217 | ········public void OptimisticDeferredSaveAndQueryShouldNotCommit() |
218 | ········{ |
219 | ············using (var pm = PmFactory.NewPersistenceManager()) |
220 | ············{ |
221 | ················pm.TransactionMode = TransactionMode.Optimistic; |
222 | ················Mitarbeiter m = new Mitarbeiter(); |
223 | ················pm.MakePersistent( m ); |
224 | ················pm.Save( true ); |
225 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
226 | |
227 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
228 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
229 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
230 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
231 | ············} |
232 | ········} |
233 | |
234 | ········[Test] |
235 | ········public void PessimisticDeferredSaveAndQueryShouldNotCommit() |
236 | ········{ |
237 | ············using (var pm = PmFactory.NewPersistenceManager()) |
238 | ············{ |
239 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
240 | ················Mitarbeiter m = new Mitarbeiter(); |
241 | ················pm.MakePersistent( m ); |
242 | ················pm.Save( true ); |
243 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
244 | |
245 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
246 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
247 | ················Assert.That( startCount > 0, "Transaction should be started" ); |
248 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
249 | ············} |
250 | ········} |
251 | |
252 | ········[Test] |
253 | ········public void OptimisticSaveAfterDeferredSaveShouldCommit() |
254 | ········{ |
255 | ············using (var pm = PmFactory.NewPersistenceManager()) |
256 | ············{ |
257 | ················pm.TransactionMode = TransactionMode.Optimistic; |
258 | ················Mitarbeiter m = new Mitarbeiter(); |
259 | ················pm.MakePersistent( m ); |
260 | ················pm.Save( true ); |
261 | ················m.Nachname = "Test"; |
262 | ················pm.Save(); |
263 | |
264 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
265 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
266 | ················Assert.That( startCount == 1, "Transaction should be started" ); |
267 | ················Assert.That( commitCount == 1, "Transaction should be committed" );················ |
268 | ············} |
269 | ········} |
270 | |
271 | ········[Test] |
272 | ········public void PessimisticSaveAfterDeferredSaveShouldCommit() |
273 | ········{ |
274 | ············using (var pm = PmFactory.NewPersistenceManager()) |
275 | ············{ |
276 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
277 | ················Mitarbeiter m = new Mitarbeiter(); |
278 | ················pm.MakePersistent( m ); |
279 | ················pm.Save( true ); |
280 | ················m.Nachname = "Test"; |
281 | ················pm.Save(); |
282 | |
283 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
284 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
285 | ················Assert.That( startCount == 1, "Transaction should be started" ); |
286 | ················Assert.That( commitCount == 1, "Transaction should be committed" ); |
287 | ············} |
288 | ········} |
289 | |
290 | |
291 | ········[Test] |
292 | ········public void DirectSqlPassThroughWithoutTransactionShouldNotCommit() |
293 | ········{ |
294 | ············using (var pm = PmFactory.NewPersistenceManager()) |
295 | ············{ |
296 | ················pm.TransactionMode = TransactionMode.None; |
297 | ················using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
298 | ················{ |
299 | ····················var reader = sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
300 | ····················Assert.That( reader == null, "Reader should be null" ); |
301 | ················} |
302 | ············ |
303 | ················var startCount = Logger.FindLogsWith("Starting transaction").Count; |
304 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
305 | ················Assert.That( startCount == 0, "Transaction shouldn't be started" ); |
306 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
307 | ············} |
308 | ········} |
309 | |
310 | ········int FlughafenCount(PersistenceManager pm) |
311 | ········{ |
312 | ············NDOQuery<Flughafen> q = new NDOQuery<Flughafen>( pm ); |
313 | ············return (int)(decimal)q.ExecuteAggregate( "oid", AggregateType.Count ); |
314 | ········} |
315 | |
316 | ········int LänderCount( PersistenceManager pm ) |
317 | ········{ |
318 | ············NDOQuery<Land> q = new NDOQuery<Land>( pm ); |
319 | ············return (int)(decimal)q.ExecuteAggregate( "oid", AggregateType.Count ); |
320 | ········} |
321 | |
322 | ········[Test] |
323 | ········public void AbortedDeferredMultiStepTransactionDoesNotCommit() |
324 | ········{ |
325 | ············using (var pm = PmFactory.NewPersistenceManager()) |
326 | ············{ |
327 | ················var landCount = LänderCount( pm ); |
328 | ················var fhCount = FlughafenCount( pm ); |
329 | |
330 | ················pm.TransactionMode = TransactionMode.Optimistic; |
331 | |
332 | ················Land land = new Land(); |
333 | ················land.Name = "Germany"; |
334 | ················pm.MakePersistent( land ); |
335 | ················pm.Save( true ); |
336 | |
337 | ················var flughafen = new Flughafen(); |
338 | ················flughafen.Kürzel = "MUC"; |
339 | ················pm.MakePersistent( flughafen ); |
340 | ················land.AddFlughafen( flughafen ); |
341 | ················pm.Save( true ); |
342 | |
343 | ················pm.Abort(); |
344 | |
345 | ················Assert.That( landCount == LänderCount( pm ) ); |
346 | ················Assert.That( fhCount == FlughafenCount( pm ) ); |
347 | ············} |
348 | ········} |
349 | |
350 | ········[Test] |
351 | ········public void DirectSqlPassThroughWithTransactionShouldCommit() |
352 | ········{ |
353 | ············using (var pm = PmFactory.NewPersistenceManager()) |
354 | ············{ |
355 | ················using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
356 | ················{ |
357 | ····················sqlHandler.BeginTransaction(); |
358 | ····················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
359 | ····················sqlHandler.Execute( "DELETE FROM Reise" ); |
360 | ····················sqlHandler.CommitTransaction(); |
361 | ················} |
362 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
363 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
364 | Assert. That( startCount == 1, "1 Transaction should be started" ) ; |
365 | ················Assert.That( commitCount == 1, "1 Transaction should be committed" ); |
366 | ············} |
367 | ········} |
368 | |
369 | ········[Test] |
370 | ········public void DirectSqlPassThroughWithCombinedStatementsDoesNotCommit() |
371 | ········{ |
372 | ············var pm = PmFactory.NewPersistenceManager(); |
373 | |
374 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
375 | ············{ |
376 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
377 | ················sqlHandler.Execute( "DELETE FROM Reise" ); |
378 | ············} |
379 | |
380 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
381 | ············Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
382 | ········} |
383 | |
384 | ········[Test] |
385 | ········public void DirectSqlPassWithAbortWorks() |
386 | ········{ |
387 | ············var pm = PmFactory.NewPersistenceManager(); |
388 | ············var m = new Mitarbeiter(); |
389 | ············pm.MakePersistent( m ); |
390 | ············pm.Save(); |
391 | ············pm.UnloadCache(); |
392 | |
393 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
394 | ············{ |
395 | ················sqlHandler.BeginTransaction(); |
396 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
397 | ················pm.Abort(); |
398 | ············} |
399 | |
400 | ············pm = PmFactory.NewPersistenceManager(); |
401 | ············bool hasRecords = new NDOQuery<Mitarbeiter>( pm ).Execute().Count > 0; |
402 | ············Assert.That( hasRecords, "At least one record should be present" ); |
403 | ········} |
404 | ····} |
405 | } |
406 | #endif |