-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_transaction.py
44 lines (32 loc) · 1.05 KB
/
test_transaction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import unittest
from reobject import Transaction, transactional
from reobject.models import Model, Field
class Number(Model):
value = Field()
@transactional
def kaboom(self):
self.value = '1111' # invalid value
self.value += 1 # should rollback
class TestTransaction(unittest.TestCase):
def setUp(self):
self.num = Number(value=-1)
def tearDown(self):
Number.objects.all().delete()
def test_transaction_decorator(self):
self.assertEqual(self.num.value, -1)
try:
self.num.kaboom()
except TypeError as e:
self.assertEqual(self.num.value, -1)
def test_transaction_ctx_manager(self):
try:
with Transaction(self.num):
self.num.value = 7
self.num.value += 1
self.assertEqual(self.num.value, 8)
self.num.value = '7'
self.num.value += 1
except TypeError:
self.assertEqual(self.num.value, -1)
if __name__ == '__main__':
unittest.main()