Skip to content
This repository
Browse code

Adding transaction as a method on database

  • Loading branch information...
commit 17cfb217b7b2718d0c50a7cb71604cdb47ff3595 1 parent d8d3ff0
Charles Leifer authored May 10, 2012

Showing 2 changed files with 19 additions and 1 deletion. Show diff stats Hide diff stats

  1. 3  peewee.py
  2. 17  tests.py
3  peewee.py
@@ -472,6 +472,9 @@ def get_tables(self):
472 472
     def sequence_exists(self, sequence):
473 473
         raise NotImplementedError
474 474
 
  475
+    def transaction(self):
  476
+        return transaction(self)
  477
+
475 478
 
476 479
 class SqliteDatabase(Database):
477 480
     def __init__(self, database, **connect_kwargs):
17  tests.py
@@ -3383,9 +3383,16 @@ def do_will_fail():
3383 3383
             with transaction(test_db):
3384 3384
                 will_fail()
3385 3385
 
  3386
+        def do_will_fail2():
  3387
+            with test_db.transaction():
  3388
+                will_fail()
  3389
+
3386 3390
         self.assertRaises(Exception, do_will_fail)
3387 3391
         self.assertEqual(Blog.select().count(), 0)
3388 3392
 
  3393
+        self.assertRaises(Exception, do_will_fail2)
  3394
+        self.assertEqual(Blog.select().count(), 0)
  3395
+
3389 3396
         def will_succeed():
3390 3397
             b = Blog.create(title='b1')
3391 3398
             e = Entry.create(title='e1', content='e1', blog=b)
@@ -3395,10 +3402,18 @@ def do_will_succeed():
3395 3402
             with transaction(test_db):
3396 3403
                 will_succeed()
3397 3404
 
3398  
-        b, e = will_succeed()
  3405
+        def do_will_succeed2():
  3406
+            with test_db.transaction():
  3407
+                will_succeed()
  3408
+
  3409
+        do_will_succeed()
3399 3410
         self.assertEqual(Blog.select().count(), 1)
3400 3411
         self.assertEqual(Entry.select().count(), 1)
3401 3412
 
  3413
+        do_will_succeed2()
  3414
+        self.assertEqual(Blog.select().count(), 2)
  3415
+        self.assertEqual(Entry.select().count(), 2)
  3416
+
3402 3417
 
3403 3418
 if test_db.adapter.for_update_support:
3404 3419
     class ForUpdateTestCase(BaseModelTestCase):

0 notes on commit 17cfb21

Please sign in to comment.
Something went wrong with that request. Please try again.