Datei: IntegrationTests/IntegrationTests/TransactionTests.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 System.Collections.Generic; |
26 | using System.Linq; |
27 | using System.Text; |
28 | using NDO; |
29 | using Reisekosten.Personal; |
30 | using System.Text.RegularExpressions; |
31 | using NDO.Query; |
32 | using Reisekosten; |
33 | using Microsoft. Extensions. Hosting; |
34 | using Microsoft.Extensions.DependencyInjection; |
35 | using Microsoft.Extensions.Logging; |
36 | |
37 | namespace NdoUnitTests |
38 | { |
39 | ····[TestFixture] |
40 | public class TransactionTests : NDOTest |
41 | ····{ |
42 | public void Setup( ) { } |
43 | |
44 | ········public void TearDown() |
45 | ········{ |
46 | ············var pm = PmFactory.NewPersistenceManager(); |
47 | ············NDOQuery<Mitarbeiter> q = new NDOQuery<Mitarbeiter>( pm ); |
48 | ············pm.Delete( q.Execute() ); |
49 | ········} |
50 | |
51 | |
52 | ········/* |
53 | ········forceCommit should be: |
54 | ········ |
55 | ····················Query····Save····Save(true) |
56 | ········Optimistic····1········1········0 |
57 | ········Pessimistic····0········1········0 |
58 | ············ |
59 | ········Deferred Mode············ |
60 | ····················Query····Save····Save(true) |
61 | ········Optimistic····0········1········0 |
62 | ········Pessimistic····0········1········0 |
63 | |
64 | ········ */ |
65 | |
66 | ········[Test] |
67 | ········public void TestIfQueryWithoutTransactionDoesNotStartAndNotCommit() |
68 | ········{ |
69 | ············var pm = PmFactory.NewPersistenceManager(); |
70 | ············pm.TransactionMode = TransactionMode.None; |
71 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
72 | ············logger.Clear(); |
73 | ············new NDOQuery<Mitarbeiter>( pm ).Execute(); |
74 | string log = logger. Text; |
75 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ) ; |
76 | Assert. That( log. IndexOf( "Starting transaction" ) == -1, "Transaction should be committed" ) ; |
77 | ········} |
78 | |
79 | |
80 | ········[Test] |
81 | ········public void TestIfOptimisticQueryIsImmediatelyCommitted() |
82 | ········{ |
83 | var pm = PmFactory. NewPersistenceManager( ) ; |
84 | ············pm.TransactionMode = TransactionMode.Optimistic; |
85 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
86 | ············logger.Clear(); |
87 | ············new NDOQuery<Mitarbeiter>( pm ).Execute(); |
88 | Assert. That( logger. Text. IndexOf( "Committing transaction" ) > -1, "Transaction should be committed" ) ; |
89 | ········} |
90 | |
91 | ········[Test] |
92 | ········public void TestIfPessimisticQueryIsNotCommitted() |
93 | ········{ |
94 | ············var pm = PmFactory.NewPersistenceManager(); |
95 | ············pm.TransactionMode = TransactionMode.Pessimistic; |
96 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
97 | ············logger.Clear(); |
98 | ············new NDOQuery<Mitarbeiter>( pm ).Execute(); |
99 | string log = logger. Text; |
100 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
101 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction shouldn't be committed" ) ; |
102 | pm. Abort( ) ; |
103 | ········} |
104 | |
105 | ········[Test] |
106 | ········public void TestIfOptimisticSaveIsCommitted() |
107 | ········{ |
108 | ············var pm = PmFactory.NewPersistenceManager(); |
109 | ············pm.TransactionMode = TransactionMode.Optimistic; |
110 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
111 | ············logger.Clear(); |
112 | ············Mitarbeiter m = new Mitarbeiter(); |
113 | ············pm.MakePersistent( m ); |
114 | ············pm.Save(); |
115 | string log = logger. Text; |
116 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
117 | Assert. That( log. IndexOf( "Committing transaction" ) > -1, "Transaction should be committed" ) ; |
118 | ········} |
119 | |
120 | ········[Test] |
121 | ········public void TestIfPessimisticSaveIsCommitted() |
122 | ········{ |
123 | var pm = PmFactory. NewPersistenceManager( ) ; |
124 | ············pm.TransactionMode = TransactionMode.Pessimistic; |
125 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
126 | ············logger.Clear(); |
127 | ············Mitarbeiter m = new Mitarbeiter(); |
128 | ············pm.MakePersistent( m ); |
129 | ············pm.Save(); |
130 | string log = logger. Text; |
131 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
132 | Assert. That( log. IndexOf( "Committing transaction" ) > -1, "Transaction should be committed" ) ; |
133 | ········} |
134 | |
135 | ········[Test] |
136 | ········public void OptimisticQueryAndSaveShouldHaveTwoTransactions() |
137 | ········{ |
138 | ············var pm = PmFactory.NewPersistenceManager(); |
139 | ············pm.TransactionMode = TransactionMode.Optimistic; |
140 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
141 | ············logger.Clear(); |
142 | ············new NDOQuery<Mitarbeiter>( pm ).Execute(); |
143 | ············Mitarbeiter m = new Mitarbeiter(); |
144 | ············pm.MakePersistent( m ); |
145 | ············pm.Save(); |
146 | string log = logger. Text; |
147 | Assert. That( new Regex( "Starting transaction" ) . Matches( log) . Count == 2, "Two Transactions should be started" ) ; |
148 | Assert. That( new Regex( "Committing transaction" ) . Matches( log) . Count == 2, "Two Transactions should be committed" ) ; |
149 | ········} |
150 | |
151 | ········[Test] |
152 | ········public void PessimisticQueryAndSaveShouldHaveOneTransaction() |
153 | ········{ |
154 | ············var pm = PmFactory.NewPersistenceManager(); |
155 | ············pm.TransactionMode = TransactionMode.Pessimistic; |
156 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
157 | ············logger.Clear(); |
158 | ············new NDOQuery<Mitarbeiter>( pm ).Execute(); |
159 | ············Mitarbeiter m = new Mitarbeiter(); |
160 | ············pm.MakePersistent( m ); |
161 | ············pm.Save(); |
162 | string log = logger. Text; |
163 | Console. WriteLine( log ) ; |
164 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transactions should be started" ) ; |
165 | Assert. That( new Regex( "Committing transaction" ) . Matches( log ) . Count == 1, "One Transactions should be committed" ) ; |
166 | ········} |
167 | |
168 | ········[Test] |
169 | ········public void OptimisticDeferredSaveShouldNotCommit() |
170 | ········{ |
171 | ············var pm = PmFactory.NewPersistenceManager(); |
172 | ············pm.TransactionMode = TransactionMode.Optimistic; |
173 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
174 | ············logger.Clear(); |
175 | ············Mitarbeiter m = new Mitarbeiter(); |
176 | ············pm.MakePersistent( m ); |
177 | pm. Save( true) ; |
178 | string log = logger. Text; |
179 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
180 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ) ; |
181 | pm. Abort( ) ; |
182 | ········} |
183 | |
184 | ········[Test] |
185 | ········public void PessimisticDeferredSaveShouldNotCommit() |
186 | ········{ |
187 | ············var pm = PmFactory.NewPersistenceManager(); |
188 | ············pm.TransactionMode = TransactionMode.Pessimistic; |
189 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
190 | ············logger.Clear(); |
191 | ············Mitarbeiter m = new Mitarbeiter(); |
192 | ············pm.MakePersistent( m ); |
193 | pm. Save( true) ; |
194 | string log = logger. Text; |
195 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
196 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ) ; |
197 | pm. Abort( ) ; |
198 | ········} |
199 | |
200 | ········[Test] |
201 | ········public void OptimisticDeferredSaveAndQueryShouldNotCommit() |
202 | ········{ |
203 | var pm = PmFactory. NewPersistenceManager( ) ; |
204 | ············pm.TransactionMode = TransactionMode.Optimistic; |
205 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
206 | ············logger.Clear(); |
207 | ············Mitarbeiter m = new Mitarbeiter(); |
208 | ············pm.MakePersistent( m ); |
209 | ············pm.Save( true ); |
210 | ············new NDOQuery<Mitarbeiter>( pm ).Execute(); |
211 | string log = logger. Text; |
212 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
213 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should not be committed" ) ; |
214 | pm. Abort( ) ; |
215 | ········} |
216 | |
217 | ········[Test] |
218 | ········public void PessimisticDeferredSaveAndQueryShouldNotCommit() |
219 | ········{ |
220 | ············var pm = PmFactory.NewPersistenceManager(); |
221 | ············pm.TransactionMode = TransactionMode.Pessimistic; |
222 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
223 | ············logger.Clear(); |
224 | ············Mitarbeiter m = new Mitarbeiter(); |
225 | ············pm.MakePersistent( m ); |
226 | ············pm.Save( true ); |
227 | ············new NDOQuery<Mitarbeiter>( pm ).Execute(); |
228 | string log = logger. Text; |
229 | Assert. That( log. IndexOf( "Starting transaction" ) > -1, "Transaction should be started" ) ; |
230 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should not be committed" ) ; |
231 | pm. Abort( ) ; |
232 | ········} |
233 | |
234 | ········[Test] |
235 | ········public void OptimisticSaveAfterDeferredSaveShouldCommit() |
236 | ········{ |
237 | var pm = PmFactory. NewPersistenceManager( ) ; |
238 | ············pm.TransactionMode = TransactionMode.Optimistic; |
239 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
240 | ············logger.Clear(); |
241 | ············Mitarbeiter m = new Mitarbeiter(); |
242 | ············pm.MakePersistent( m ); |
243 | ············pm.Save( true ); |
244 | ············m.Nachname = "Test"; |
245 | ············pm.Save(); |
246 | string log = logger. Text; |
247 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transactions should be started" ) ; |
248 | Assert. That( new Regex( "Committing transaction" ) . Matches( log ) . Count == 1, "One Transactions should be committed" ) ; |
249 | pm. Abort( ) ; |
250 | ········} |
251 | |
252 | ········[Test] |
253 | ········public void PessimisticSaveAfterDeferredSaveShouldCommit() |
254 | ········{ |
255 | var pm = PmFactory. NewPersistenceManager( ) ; |
256 | ············pm.TransactionMode = TransactionMode.Pessimistic; |
257 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
258 | ············logger.Clear(); |
259 | ············Mitarbeiter m = new Mitarbeiter(); |
260 | ············pm.MakePersistent( m ); |
261 | ············pm.Save( true ); |
262 | ············m.Nachname = "Test"; |
263 | ············pm.Save(); |
264 | string log = logger. Text; |
265 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transactions should be started" ) ; |
266 | Assert. That( new Regex( "Committing transaction" ) . Matches( log ) . Count == 1, "One Transactions should be committed" ) ; |
267 | pm. Abort( ) ; |
268 | ········} |
269 | |
270 | |
271 | ········[Test] |
272 | ········public void DirectSqlPassThroughWithoutTransactionShouldNotCommit() |
273 | ········{ |
274 | var pm = PmFactory. NewPersistenceManager( ) ; |
275 | ············pm.TransactionMode = TransactionMode.None; |
276 | var logger = ( TestLogger) Host. Services. GetRequiredService<ILoggerFactory>( ) . CreateLogger( "Test") ; |
277 | logger. Clear( ) ; |
278 | ············ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler(); |
279 | ············var reader = sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
280 | string log = logger. Text; |
281 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ) ; |
282 | Assert. That( log. IndexOf( "Starting transaction" ) == -1, "Transaction should be committed" ) ; |
283 | Assert. That( reader == null, "Reader should be null" ) ; |
284 | ········} |
285 | |
286 | int FlughafenCount( PersistenceManager pm) |
287 | ········{ |
288 | ············NDOQuery<Flughafen> q = new NDOQuery<Flughafen>( pm ); |
289 | return ( int) ( decimal) q. ExecuteAggregate( "oid", AggregateType. Count ) ; |
290 | ········} |
291 | |
292 | ········int LänderCount( PersistenceManager pm ) |
293 | ········{ |
294 | ············NDOQuery<Land> q = new NDOQuery<Land>( pm ); |
295 | return ( int) ( decimal) q. ExecuteAggregate( "oid", AggregateType. Count ) ; |
296 | ········} |
297 | |
298 | ········[Test] |
299 | ········public void AbortedDeferredMultiStepTransactionDoesNotCommit() |
300 | ········{ |
301 | ············PersistenceManager pm = new PersistenceManager(); |
302 | ············var landCount = LänderCount( pm ); |
303 | ············var fhCount = FlughafenCount( pm ); |
304 | |
305 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
306 | ············logger.Clear(); |
307 | ············pm.TransactionMode = TransactionMode.Optimistic; |
308 | |
309 | ············Land land = new Land(); |
310 | ············land.Name = "Germany"; |
311 | ············pm.MakePersistent( land ); |
312 | ············pm.Save( true ); |
313 | |
314 | ············var flughafen = new Flughafen(); |
315 | ············flughafen.Kürzel = "MUC"; |
316 | ············pm.MakePersistent( flughafen ); |
317 | ············land.AddFlughafen( flughafen ); |
318 | ············pm.Save( true ); |
319 | |
320 | ············pm.Abort(); |
321 | |
322 | string log = logger. Text; |
323 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transactions should be started" ) ; |
324 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should be committed" ) ; |
325 | ············Assert.That( new Regex( "Rollback transaction" ).Matches( log ).Count == 1, "One Transactions should be rolled back" ); |
326 | |
327 | ············pm.TransactionMode = TransactionMode.None; |
328 | ············Assert.That(landCount ==··LänderCount( pm ) ); |
329 | ············Assert.That(fhCount ==··FlughafenCount( pm ) ); |
330 | ········} |
331 | |
332 | ········[Test] |
333 | ········public void DirectSqlPassThroughWithTransactionShouldCommit() |
334 | ········{ |
335 | var pm = PmFactory. NewPersistenceManager( ) ; |
336 | var logger = ( TestLogger) Host. Services. GetRequiredService<ILoggerFactory>( ) . CreateLogger( "Test") ; |
337 | ············logger.Clear(); |
338 | ············ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler(); |
339 | ············sqlHandler.BeginTransaction(); |
340 | ············sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
341 | ············sqlHandler.Execute( "DELETE FROM Reise" ); |
342 | ············sqlHandler.CommitTransaction(); |
343 | string log = logger. Text; |
344 | Assert. That( new Regex( "Starting transaction" ) . Matches( log ) . Count == 1, "One Transactions should be started" ) ; |
345 | Assert. That( new Regex( "Committing transaction" ) . Matches( log ) . Count == 1, "One Transactions should be committed" ) ; |
346 | ········} |
347 | |
348 | ········[Test] |
349 | ········public void DirectSqlPassThroughWithCombinedStatementsDoesNotCommit() |
350 | ········{ |
351 | ············var pm = PmFactory.NewPersistenceManager(); |
352 | pm. TransactionMode = TransactionMode. None; |
353 | ············var logger = (TestLogger)Host.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Test"); |
354 | ············logger.Clear(); |
355 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
356 | ············{ |
357 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
358 | ················sqlHandler.Execute( "DELETE FROM Reise" ); |
359 | ············} |
360 | string log = logger. Text; |
361 | Assert. That( log. IndexOf( "Committing transaction" ) == -1, "Transaction should not be started" ) ; |
362 | Assert. That( log. IndexOf( "Starting transaction" ) == -1, "Transaction should not be committed" ) ; |
363 | ········} |
364 | |
365 | ········[Test] |
366 | ········public void DirectSqlPassWithAbortWorks() |
367 | ········{ |
368 | ············var pm = PmFactory.NewPersistenceManager(); |
369 | ············var m = new Mitarbeiter(); |
370 | ············pm.MakePersistent( m ); |
371 | ············pm.Save(); |
372 | ············pm.UnloadCache(); |
373 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
374 | ············{ |
375 | ················sqlHandler.BeginTransaction(); |
376 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
377 | ················pm.Abort(); |
378 | ············} |
379 | ············bool hasRecords = new NDOQuery<Mitarbeiter>( pm ).Execute().Count > 0; |
380 | ············Assert.That( hasRecords, "At least one record should be present" ); |
381 | ········} |
382 | ····} |
383 | } |
384 |
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 | using NUnit.Framework; |
24 | using NDO; |
25 | using Reisekosten.Personal; |
26 | using NDO.Query; |
27 | using Reisekosten; |
28 | using Formfakten. TestLogger; |
29 | |
30 | namespace NdoUnitTests |
31 | { |
32 | ····[TestFixture] |
33 | public class TransactionScopeTests : NDOTest |
34 | ····{ |
35 | ········public void Setup() |
36 | ········{ |
37 | Logger. ClearTestLogs( ) ; |
38 | ········} |
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 | ················pm.TransactionMode = TransactionMode.Optimistic; |
144 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
145 | ················Mitarbeiter m = new Mitarbeiter(); |
146 | ················pm.MakePersistent( m ); |
147 | ················pm.Save(); |
148 | |
149 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
150 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
151 | ················Assert.That( startCount == 2, "Two Transactions should be started" ); |
152 | ················Assert.That( commitCount == 2, "Two Transactions should be committed" ); |
153 | ············} |
154 | ········} |
155 | |
156 | ········[Test] |
157 | ········public void PessimisticQueryAndSaveShouldHaveOneTransaction() |
158 | ········{ |
159 | ············using (var pm = PmFactory.NewPersistenceManager()) |
160 | ············{ |
161 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
162 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
163 | ················Mitarbeiter m = new Mitarbeiter(); |
164 | ················pm.MakePersistent( m ); |
165 | ················pm.Save(); |
166 | |
167 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
168 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
169 | Assert. That( startCount == 1, "One Transaction should be started" ) ; |
170 | ················Assert.That( commitCount == 1, "One Transaction should be committed" ); |
171 | ············} |
172 | ········} |
173 | |
174 | ········[Test] |
175 | ········public void OptimisticDeferredSaveShouldNotCommit() |
176 | ········{ |
177 | ············using (var pm = PmFactory.NewPersistenceManager()) |
178 | ············{ |
179 | ················pm.TransactionMode = TransactionMode.Optimistic; |
180 | ················Mitarbeiter m = new Mitarbeiter(); |
181 | ················pm.MakePersistent( m ); |
182 | pm. Save( true ) ; |
183 | |
184 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
185 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
186 | Assert. That( startCount > 0, "Transaction should be started" ) ; |
187 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
188 | ············} |
189 | ········} |
190 | |
191 | ········[Test] |
192 | ········public void PessimisticDeferredSaveShouldNotCommit() |
193 | ········{ |
194 | ············using (var pm = PmFactory.NewPersistenceManager()) |
195 | ············{ |
196 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
197 | ················Mitarbeiter m = new Mitarbeiter(); |
198 | ················pm.MakePersistent( m ); |
199 | pm. Save( true ) ; |
200 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
201 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
202 | Assert. That( startCount > 0, "Transaction should be started" ) ; |
203 | Assert. That( commitCount == 0, "Transaction shouldn't be committed" ) ; |
204 | ············} |
205 | ········} |
206 | |
207 | ········[Test] |
208 | ········public void OptimisticDeferredSaveAndQueryShouldNotCommit() |
209 | ········{ |
210 | using ( var pm = PmFactory. NewPersistenceManager( ) ) |
211 | ············{ |
212 | ················pm.TransactionMode = TransactionMode.Optimistic; |
213 | ················Mitarbeiter m = new Mitarbeiter(); |
214 | ················pm.MakePersistent( m ); |
215 | ················pm.Save( true ); |
216 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
217 | |
218 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
219 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
220 | Assert. That( startCount > 0, "Transaction should be started" ) ; |
221 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
222 | ············} |
223 | ········} |
224 | |
225 | ········[Test] |
226 | ········public void PessimisticDeferredSaveAndQueryShouldNotCommit() |
227 | ········{ |
228 | ············using (var pm = PmFactory.NewPersistenceManager()) |
229 | ············{ |
230 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
231 | ················Mitarbeiter m = new Mitarbeiter(); |
232 | ················pm.MakePersistent( m ); |
233 | ················pm.Save( true ); |
234 | ················new NDOQuery<Mitarbeiter>( pm ).Execute(); |
235 | |
236 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
237 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
238 | Assert. That( startCount > 0, "Transaction should be started" ) ; |
239 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
240 | ············} |
241 | ········} |
242 | |
243 | ········[Test] |
244 | ········public void OptimisticSaveAfterDeferredSaveShouldCommit() |
245 | ········{ |
246 | using ( var pm = PmFactory. NewPersistenceManager( ) ) |
247 | ············{ |
248 | ················pm.TransactionMode = TransactionMode.Optimistic; |
249 | ················Mitarbeiter m = new Mitarbeiter(); |
250 | ················pm.MakePersistent( m ); |
251 | ················pm.Save( true ); |
252 | ················m.Nachname = "Test"; |
253 | ················pm.Save(); |
254 | |
255 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
256 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
257 | Assert. That( startCount == 1, "Transaction should be started" ) ; |
258 | ················Assert.That( commitCount == 1, "Transaction should be committed" ); |
259 | ············} |
260 | ········} |
261 | |
262 | ········[Test] |
263 | ········public void PessimisticSaveAfterDeferredSaveShouldCommit() |
264 | ········{ |
265 | using ( var pm = PmFactory. NewPersistenceManager( ) ) |
266 | ············{ |
267 | ················pm.TransactionMode = TransactionMode.Pessimistic; |
268 | ················Mitarbeiter m = new Mitarbeiter(); |
269 | ················pm.MakePersistent( m ); |
270 | ················pm.Save( true ); |
271 | ················m.Nachname = "Test"; |
272 | ················pm.Save(); |
273 | |
274 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
275 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
276 | Assert. That( startCount == 1, "Transaction should be started" ) ; |
277 | ················Assert.That( commitCount == 1, "Transaction should be committed" ); |
278 | ············} |
279 | ········} |
280 | |
281 | |
282 | ········[Test] |
283 | ········public void DirectSqlPassThroughWithoutTransactionShouldNotCommit() |
284 | ········{ |
285 | using ( var pm = PmFactory. NewPersistenceManager( ) ) |
286 | ············{ |
287 | ················pm.TransactionMode = TransactionMode.None; |
288 | using ( ISqlPassThroughHandler sqlHandler = pm. GetSqlPassThroughHandler( ) ) |
289 | { |
290 | ····················var reader = sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
291 | Assert. That( reader == null, "Reader should be null" ) ; |
292 | } |
293 | |
294 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
295 | ················var commitCount = Logger.FindLogsWith("Committing transaction").Count; |
296 | ················Assert.That( startCount == 0, "Transaction shouldn't be started" ); |
297 | ················Assert.That( commitCount == 0, "Transaction shouldn't be committed" ); |
298 | ············} |
299 | ········} |
300 | |
301 | int FlughafenCount( PersistenceManager pm ) |
302 | ········{ |
303 | ············NDOQuery<Flughafen> q = new NDOQuery<Flughafen>( pm ); |
304 | return ( int) ( decimal) q. ExecuteAggregate( "oid", AggregateType. Count ) ; |
305 | ········} |
306 | |
307 | ········int LänderCount( PersistenceManager pm ) |
308 | ········{ |
309 | ············NDOQuery<Land> q = new NDOQuery<Land>( pm ); |
310 | return ( int) ( decimal) q. ExecuteAggregate( "oid", AggregateType. Count ) ; |
311 | ········} |
312 | |
313 | ········[Test] |
314 | ········public void AbortedDeferredMultiStepTransactionDoesNotCommit() |
315 | ········{ |
316 | ············using (var pm = PmFactory.NewPersistenceManager()) |
317 | ············{ |
318 | ················var landCount = LänderCount( pm ); |
319 | ················var fhCount = FlughafenCount( pm ); |
320 | |
321 | ················pm.TransactionMode = TransactionMode.Optimistic; |
322 | |
323 | ················Land land = new Land(); |
324 | ················land.Name = "Germany"; |
325 | ················pm.MakePersistent( land ); |
326 | ················pm.Save( true ); |
327 | |
328 | ················var flughafen = new Flughafen(); |
329 | ················flughafen.Kürzel = "MUC"; |
330 | ················pm.MakePersistent( flughafen ); |
331 | ················land.AddFlughafen( flughafen ); |
332 | ················pm.Save( true ); |
333 | |
334 | ················pm.Abort(); |
335 | |
336 | Assert. That( landCount == LänderCount( pm ) ) ; |
337 | Assert. That( fhCount == FlughafenCount( pm ) ) ; |
338 | } |
339 | ········} |
340 | |
341 | ········[Test] |
342 | ········public void DirectSqlPassThroughWithTransactionShouldCommit() |
343 | ········{ |
344 | ············using (var pm = PmFactory.NewPersistenceManager()) |
345 | ············{ |
346 | using ( ISqlPassThroughHandler sqlHandler = pm. GetSqlPassThroughHandler( ) ) |
347 | { |
348 | ····················sqlHandler.BeginTransaction(); |
349 | ····················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
350 | ····················sqlHandler.Execute( "DELETE FROM Reise" ); |
351 | ····················sqlHandler.CommitTransaction(); |
352 | } |
353 | var startCount = Logger. FindLogsWith( "Starting transaction") . Count; |
354 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
355 | ················Assert.That( startCount == 1, "1 Transaction should be started" ); |
356 | ················Assert.That( commitCount == 1, "1 Transaction should be committed" ); |
357 | ············} |
358 | ········} |
359 | |
360 | ········[Test] |
361 | ········public void DirectSqlPassThroughWithCombinedStatementsDoesNotCommit() |
362 | ········{ |
363 | ············var pm = PmFactory.NewPersistenceManager(); |
364 | |
365 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
366 | ············{ |
367 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
368 | ················sqlHandler.Execute( "DELETE FROM Reise" ); |
369 | ············} |
370 | |
371 | var commitCount = Logger. FindLogsWith( "Committing transaction") . Count; |
372 | Assert. That( commitCount == 0, "Transaction shouldn't be committed" ) ; |
373 | ········} |
374 | |
375 | ········[Test] |
376 | ········public void DirectSqlPassWithAbortWorks() |
377 | ········{ |
378 | ············var pm = PmFactory.NewPersistenceManager(); |
379 | ············var m = new Mitarbeiter(); |
380 | ············pm.MakePersistent( m ); |
381 | ············pm.Save(); |
382 | ············pm.UnloadCache(); |
383 | |
384 | ············using (ISqlPassThroughHandler sqlHandler = pm.GetSqlPassThroughHandler()) |
385 | ············{ |
386 | ················sqlHandler.BeginTransaction(); |
387 | ················sqlHandler.Execute( "DELETE FROM Mitarbeiter" ); |
388 | ················pm.Abort(); |
389 | ············} |
390 | |
391 | ············pm = PmFactory.NewPersistenceManager(); |
392 | ············bool hasRecords = new NDOQuery<Mitarbeiter>( pm ).Execute().Count > 0; |
393 | ············Assert.That( hasRecords, "At least one record should be present" ); |
394 | ········} |
395 | ····} |
396 | } |
397 |