@@ -242,4 +242,57 @@ describe Driver do
242
242
File .delete(DB_FILENAME )
243
243
end
244
244
end
245
+
246
+ describe " transactions" do
247
+ it " can read inside transaction and rollback after" do
248
+ with_db do |db |
249
+ db.exec " create table person (name varchar(25))"
250
+ db.transaction do |tx |
251
+ tx.connection.scalar(" select count(*) from person" ).should eq(0 )
252
+ tx.connection.exec " insert into person (name) values (?)" , " John Doe"
253
+ tx.connection.scalar(" select count(*) from person" ).should eq(1 )
254
+ tx.rollback
255
+ end
256
+ db.scalar(" select count(*) from person" ).should eq(0 )
257
+ end
258
+ end
259
+
260
+ it " can read inside transaction or after commit" do
261
+ with_db do |db |
262
+ db.exec " create table person (name varchar(25))"
263
+ db.transaction do |tx |
264
+ tx.connection.scalar(" select count(*) from person" ).should eq(0 )
265
+ tx.connection.exec " insert into person (name) values (?)" , " John Doe"
266
+ tx.connection.scalar(" select count(*) from person" ).should eq(1 )
267
+ # using other connection
268
+ db.scalar(" select count(*) from person" ).should eq(0 )
269
+ end
270
+ db.scalar(" select count(*) from person" ).should eq(1 )
271
+ end
272
+ end
273
+ end
274
+
275
+ describe " nested transactions" do
276
+ it " can read inside transaction and rollback after" do
277
+ with_db do |db |
278
+ db.exec " create table person (name varchar(25))"
279
+ db.transaction do |tx_0 |
280
+ tx_0.connection.scalar(" select count(*) from person" ).should eq(0 )
281
+ tx_0.connection.exec " insert into person (name) values (?)" , " John Doe"
282
+ tx_0.transaction do |tx_1 |
283
+ tx_1.connection.exec " insert into person (name) values (?)" , " Sarah"
284
+ tx_1.connection.scalar(" select count(*) from person" ).should eq(2 )
285
+ tx_1.transaction do |tx_2 |
286
+ tx_2.connection.exec " insert into person (name) values (?)" , " Jimmy"
287
+ tx_2.connection.scalar(" select count(*) from person" ).should eq(3 )
288
+ tx_2.rollback
289
+ end
290
+ end
291
+ tx_0.connection.scalar(" select count(*) from person" ).should eq(2 )
292
+ tx_0.rollback
293
+ end
294
+ db.scalar(" select count(*) from person" ).should eq(0 )
295
+ end
296
+ end
297
+ end
245
298
end
0 commit comments