Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding transaction as a method on database

  • Loading branch information...
commit 17cfb217b7b2718d0c50a7cb71604cdb47ff3595 1 parent d8d3ff0
Charles Leifer authored
Showing with 19 additions and 1 deletion.
  1. +3 −0  peewee.py
  2. +16 −1 tests.py
3  peewee.py
View
@@ -472,6 +472,9 @@ def get_tables(self):
def sequence_exists(self, sequence):
raise NotImplementedError
+ def transaction(self):
+ return transaction(self)
+
class SqliteDatabase(Database):
def __init__(self, database, **connect_kwargs):
17 tests.py
View
@@ -3383,9 +3383,16 @@ def do_will_fail():
with transaction(test_db):
will_fail()
+ def do_will_fail2():
+ with test_db.transaction():
+ will_fail()
+
self.assertRaises(Exception, do_will_fail)
self.assertEqual(Blog.select().count(), 0)
+ self.assertRaises(Exception, do_will_fail2)
+ self.assertEqual(Blog.select().count(), 0)
+
def will_succeed():
b = Blog.create(title='b1')
e = Entry.create(title='e1', content='e1', blog=b)
@@ -3395,10 +3402,18 @@ def do_will_succeed():
with transaction(test_db):
will_succeed()
- b, e = will_succeed()
+ def do_will_succeed2():
+ with test_db.transaction():
+ will_succeed()
+
+ do_will_succeed()
self.assertEqual(Blog.select().count(), 1)
self.assertEqual(Entry.select().count(), 1)
+ do_will_succeed2()
+ self.assertEqual(Blog.select().count(), 2)
+ self.assertEqual(Entry.select().count(), 2)
+
if test_db.adapter.for_update_support:
class ForUpdateTestCase(BaseModelTestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.