Skip to content

expose method is_in_transaction #297

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 5 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asyncpg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
__all__ = ('connect', 'create_pool', 'Record', 'Connection') + \
exceptions.__all__ # NOQA

__version__ = '0.16.0.dev0'
__version__ = '0.16.0.dev1'
8 changes: 8 additions & 0 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def transaction(self, *, isolation='read_committed', readonly=False,
self._check_open()
return transaction.Transaction(self, isolation, readonly, deferrable)

def is_in_transaction(self):
"""Return True if Connection is currently inside a transaction.

:return bool: True if inside transaction, False otherwise.
.. versionadded:: 0.16.0
"""
return self._protocol.is_in_transaction()

async def execute(self, query: str, *args, timeout: float=None) -> str:
"""Execute an SQL command (or commands).

Expand Down
22 changes: 22 additions & 0 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ class TestTransaction(tb.ConnectedTestCase):

async def test_transaction_regular(self):
self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())
tr = self.con.transaction()
self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

with self.assertRaises(ZeroDivisionError):
async with tr as with_tr:
self.assertIs(self.con._top_xact, tr)
self.assertTrue(self.con.is_in_transaction())

# We don't return the transaction object from __aenter__,
# to make it harder for people to use '.rollback()' and
Expand All @@ -33,6 +36,7 @@ async def test_transaction_regular(self):
1 / 0

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

with self.assertRaisesRegex(asyncpg.PostgresError,
'"mytab" does not exist'):
Expand All @@ -42,31 +46,39 @@ async def test_transaction_regular(self):

async def test_transaction_nested(self):
self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

tr = self.con.transaction()

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

with self.assertRaises(ZeroDivisionError):
async with tr:
self.assertIs(self.con._top_xact, tr)
self.assertTrue(self.con.is_in_transaction())

await self.con.execute('''
CREATE TABLE mytab (a int);
''')

async with self.con.transaction():
self.assertIs(self.con._top_xact, tr)
self.assertTrue(self.con.is_in_transaction())

await self.con.execute('''
INSERT INTO mytab (a) VALUES (1), (2);
''')

self.assertIs(self.con._top_xact, tr)
self.assertTrue(self.con.is_in_transaction())

with self.assertRaises(ZeroDivisionError):
in_tr = self.con.transaction()
async with in_tr:

self.assertIs(self.con._top_xact, tr)
self.assertTrue(self.con.is_in_transaction())

await self.con.execute('''
INSERT INTO mytab (a) VALUES (3), (4);
Expand All @@ -85,10 +97,12 @@ async def test_transaction_nested(self):
self.assertEqual(recs[1][0], 2)

self.assertIs(self.con._top_xact, tr)
self.assertTrue(self.con.is_in_transaction())

1 / 0

self.assertIs(self.con._top_xact, None)
self.assertFalse(self.con.is_in_transaction())

with self.assertRaisesRegex(asyncpg.PostgresError,
'"mytab" does not exist'):
Expand All @@ -98,6 +112,7 @@ async def test_transaction_nested(self):

async def test_transaction_interface_errors(self):
self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

tr = self.con.transaction(readonly=True, isolation='serializable')
with self.assertRaisesRegex(asyncpg.InterfaceError,
Expand All @@ -109,13 +124,15 @@ async def test_transaction_interface_errors(self):
'<asyncpg.Transaction state:rolledback serializable readonly'))

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

with self.assertRaisesRegex(asyncpg.InterfaceError,
'cannot start; .* already rolled back'):
async with tr:
pass

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

tr = self.con.transaction()
with self.assertRaisesRegex(asyncpg.InterfaceError,
Expand All @@ -124,6 +141,7 @@ async def test_transaction_interface_errors(self):
await tr.commit()

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

tr = self.con.transaction()
with self.assertRaisesRegex(asyncpg.InterfaceError,
Expand All @@ -132,6 +150,7 @@ async def test_transaction_interface_errors(self):
await tr.rollback()

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

tr = self.con.transaction()
with self.assertRaisesRegex(asyncpg.InterfaceError,
Expand All @@ -142,11 +161,13 @@ async def test_transaction_interface_errors(self):

async def test_transaction_within_manual_transaction(self):
self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

await self.con.execute('BEGIN')

tr = self.con.transaction()
self.assertIsNone(self.con._top_xact)
self.assertTrue(self.con.is_in_transaction())

with self.assertRaisesRegex(asyncpg.InterfaceError,
'cannot use Connection.transaction'):
Expand All @@ -157,3 +178,4 @@ async def test_transaction_within_manual_transaction(self):
await self.con.reset()

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())