11import asyncio
22
3+ import pytest
4+ from peewee import IntegrityError
5+
36from peewee_async import Transaction
47from tests .conftest import dbs_all
58from tests .models import TestModel
69
710
8- @ dbs_all
9- async def test_savepoint_success ( db ):
11+ class FakeConnectionError ( Exception ):
12+ pass
1013
11- async with db .aio_atomic ():
12- await TestModel .aio_create (text = 'FOO' )
1314
15+ @dbs_all
16+ async def test_transaction_error_on_begin (db , mocker ):
17+ mocker .patch .object (Transaction , "begin" , side_effect = FakeConnectionError )
18+ with pytest .raises (FakeConnectionError ):
1419 async with db .aio_atomic ():
15- await TestModel .update (text = "BAR" ).aio_execute ()
20+ await TestModel .aio_create (text = 'FOO' )
21+ assert db .aio_pool .has_acquired_connections () is False
1622
17- assert await TestModel .aio_get_or_none (text = "BAR" ) is not None
23+ @dbs_all
24+ async def test_transaction_error_on_commit (db , mocker ):
25+ mocker .patch .object (Transaction , "commit" , side_effect = FakeConnectionError )
26+ with pytest .raises (FakeConnectionError ):
27+ async with db .aio_atomic ():
28+ await TestModel .aio_create (text = 'FOO' )
1829 assert db .aio_pool .has_acquired_connections () is False
1930
2031
2132@dbs_all
22- async def test_transaction_success (db ):
23- async with db .aio_atomic ():
24- await TestModel .aio_create (text = 'FOO' )
33+ async def test_transaction_error_on_rollback (db , mocker ):
34+ await TestModel .aio_create (text = 'FOO' , data = "" )
35+ mocker .patch .object (Transaction , "rollback" , side_effect = FakeConnectionError )
36+ with pytest .raises (FakeConnectionError ):
37+ async with db .aio_atomic ():
38+ await TestModel .update (data = "BAR" ).aio_execute ()
39+ assert await TestModel .aio_get_or_none (data = "BAR" ) is not None
40+ await TestModel .aio_create (text = 'FOO' )
2541
26- assert await TestModel .aio_get_or_none (text = "FOO" ) is not None
2742 assert db .aio_pool .has_acquired_connections () is False
2843
2944
3045@dbs_all
31- async def test_savepoint_rollback (db ):
32- await TestModel .aio_create (text = 'FOO' , data = "" )
33-
46+ async def test_transaction_success (db ):
3447 async with db .aio_atomic ():
35- await TestModel .update (data = "BAR" ).aio_execute ()
36-
37- try :
38- async with db .aio_atomic ():
39- await TestModel .aio_create (text = 'FOO' )
40- except :
41- pass
48+ await TestModel .aio_create (text = 'FOO' )
4249
43- assert await TestModel .aio_get_or_none (data = "BAR " ) is not None
50+ assert await TestModel .aio_get_or_none (text = "FOO " ) is not None
4451 assert db .aio_pool .has_acquired_connections () is False
4552
4653
4754@dbs_all
4855async def test_transaction_rollback (db ):
4956 await TestModel .aio_create (text = 'FOO' , data = "" )
5057
51- try :
58+ with pytest . raises ( IntegrityError ) :
5259 async with db .aio_atomic ():
5360 await TestModel .update (data = "BAR" ).aio_execute ()
5461 assert await TestModel .aio_get_or_none (data = "BAR" ) is not None
5562 await TestModel .aio_create (text = 'FOO' )
56- except :
57- pass
5863
5964 assert await TestModel .aio_get_or_none (data = "BAR" ) is None
6065 assert db .aio_pool .has_acquired_connections () is False
@@ -72,11 +77,9 @@ async def t1():
7277 async def t2 ():
7378 async with db .aio_atomic ():
7479 await TestModel .aio_create (text = 'FOO2' , data = "" )
75- try :
80+ with pytest . raises ( IntegrityError ) :
7681 async with db .aio_atomic ():
7782 await TestModel .aio_create (text = 'FOO2' , data = "not_created" )
78- except :
79- pass
8083
8184 async def t3 ():
8285 async with db .aio_atomic ():
@@ -110,6 +113,33 @@ async def test_transaction_manual_work(db):
110113 assert db .aio_pool .has_acquired_connections () is False
111114
112115
116+ @dbs_all
117+ async def test_savepoint_success (db ):
118+ async with db .aio_atomic ():
119+ await TestModel .aio_create (text = 'FOO' )
120+
121+ async with db .aio_atomic ():
122+ await TestModel .update (text = "BAR" ).aio_execute ()
123+
124+ assert await TestModel .aio_get_or_none (text = "BAR" ) is not None
125+ assert db .aio_pool .has_acquired_connections () is False
126+
127+
128+ @dbs_all
129+ async def test_savepoint_rollback (db ):
130+ await TestModel .aio_create (text = 'FOO' , data = "" )
131+
132+ async with db .aio_atomic ():
133+ await TestModel .update (data = "BAR" ).aio_execute ()
134+
135+ with pytest .raises (IntegrityError ):
136+ async with db .aio_atomic ():
137+ await TestModel .aio_create (text = 'FOO' )
138+
139+ assert await TestModel .aio_get_or_none (data = "BAR" ) is not None
140+ assert db .aio_pool .has_acquired_connections () is False
141+
142+
113143@dbs_all
114144async def test_savepoint_manual_work (db ):
115145 async with db .aio_connection () as connection :
@@ -178,3 +208,4 @@ async def insert_records(event_for_wait: asyncio.Event):
178208 # The transaction has not been committed
179209 assert len (list (await TestModel .select ().aio_execute ())) == 0
180210 assert db .aio_pool .has_acquired_connections () is False
211+
0 commit comments