|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import typing |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from psqlpy_piccolo import PSQLPyEngine |
| 8 | +from tests.test_apps.music.tables import Band, Manager |
| 9 | + |
| 10 | + |
| 11 | +def test_atomic_error_statement() -> None: |
| 12 | + """Make sure queries in a transaction aren't committed if a query fails.""" |
| 13 | + atomic = Band._meta.db.atomic() |
| 14 | + atomic.add( |
| 15 | + Band.raw("MALFORMED QUERY ... SHOULD ERROR"), |
| 16 | + ) |
| 17 | + with pytest.raises(Exception): # noqa: B017, PT011 |
| 18 | + atomic.run_sync() |
| 19 | + |
| 20 | + |
| 21 | +def test_atomic_succeeds_statement() -> None: |
| 22 | + """Make sure that when atomic is run successfully the database is modified accordingly.""" |
| 23 | + atomic = Band._meta.db.atomic() |
| 24 | + atomic.add(Manager.insert(Manager(name="test-manager-name"))) |
| 25 | + atomic.run_sync() |
| 26 | + assert Manager.count().run_sync() == 1 |
| 27 | + |
| 28 | + |
| 29 | +async def test_atomic_pool() -> None: |
| 30 | + """Make sure atomic works correctly when a connection pool is active.""" |
| 31 | + engine = Manager._meta.db |
| 32 | + await engine.start_connection_pool() |
| 33 | + |
| 34 | + atomic = engine.atomic() |
| 35 | + atomic.add(Manager.insert(Manager(name="test-manager-name"))) |
| 36 | + |
| 37 | + await atomic.run() |
| 38 | + await engine.close_connection_pool() |
| 39 | + |
| 40 | + assert Manager.count().run_sync() == 1 |
| 41 | + |
| 42 | + |
| 43 | +async def test_transaction_error() -> None: |
| 44 | + """Make sure queries in a transaction aren't committed if a query fails.""" |
| 45 | + with pytest.raises(Exception): # noqa: B017, PT011, PT012 |
| 46 | + async with Manager._meta.db.transaction(): |
| 47 | + await Manager.insert(Manager(name="test-manager-name")) |
| 48 | + await Manager.raw("MALFORMED QUERY ... SHOULD ERROR") |
| 49 | + |
| 50 | + assert Manager.count().run_sync() == 0 |
| 51 | + |
| 52 | + |
| 53 | +async def test_transaction_succeeds() -> None: |
| 54 | + async with Manager._meta.db.transaction(): |
| 55 | + await Manager.insert(Manager(name="test-manager-name")) |
| 56 | + |
| 57 | + assert Manager.count().run_sync() == 1 |
| 58 | + |
| 59 | + |
| 60 | +async def test_transaction_manual_commit() -> None: |
| 61 | + async with Band._meta.db.transaction() as transaction: |
| 62 | + await Manager.insert(Manager(name="test-manager-name")) |
| 63 | + await transaction.commit() |
| 64 | + |
| 65 | + assert Manager.count().run_sync() == 1 |
| 66 | + |
| 67 | + |
| 68 | +async def test_transaction_manual_rollback() -> None: |
| 69 | + async with Band._meta.db.transaction() as transaction: |
| 70 | + await Manager.insert(Manager(name="test-manager-name")) |
| 71 | + await transaction.rollback() |
| 72 | + |
| 73 | + assert Manager.count().run_sync() == 0 |
| 74 | + |
| 75 | + |
| 76 | +async def test_transaction_id() -> None: |
| 77 | + """An extra sanity check, that the transaction id is the same for each query inside the transaction block.""" |
| 78 | + |
| 79 | + async def get_transaction_ids() -> list[str]: |
| 80 | + responses = [] |
| 81 | + async with Band._meta.db.transaction(): |
| 82 | + responses.append(await Manager.raw("SELECT txid_current()").run()) |
| 83 | + responses.append(await Manager.raw("SELECT txid_current()").run()) |
| 84 | + |
| 85 | + return [response[0]["txid_current"] for response in responses] |
| 86 | + |
| 87 | + transaction_ids: typing.Final = await get_transaction_ids() |
| 88 | + assert len(set(transaction_ids)) == 1 |
| 89 | + |
| 90 | + next_transaction_ids: typing.Final = await get_transaction_ids() |
| 91 | + assert len(set(next_transaction_ids)) == 1 |
| 92 | + assert next_transaction_ids[0] != transaction_ids[0] |
| 93 | + |
| 94 | + |
| 95 | +async def test_transaction_exists() -> None: |
| 96 | + """Make sure we can detect when code is within a transaction.""" |
| 97 | + engine: typing.Final = typing.cast(PSQLPyEngine, Manager._meta.db) |
| 98 | + |
| 99 | + async with engine.transaction(): |
| 100 | + assert engine.transaction_exists() |
| 101 | + |
| 102 | + assert not engine.transaction_exists() |
| 103 | + |
| 104 | + |
| 105 | +async def test_transaction_savepoint() -> None: |
| 106 | + async with Manager._meta.db.transaction() as transaction: |
| 107 | + await Manager.insert(Manager(name="Manager 1")) |
| 108 | + savepoint = await transaction.savepoint() |
| 109 | + await Manager.insert(Manager(name="Manager 2")) |
| 110 | + await savepoint.rollback_to() |
| 111 | + |
| 112 | + assert await Manager.select(Manager.name).run() == [{"name": "Manager 1"}] |
| 113 | + |
| 114 | + |
| 115 | +async def test_transaction_named_savepoint() -> None: |
| 116 | + async with Manager._meta.db.transaction() as transaction: |
| 117 | + await Manager.insert(Manager(name="Manager 1")) |
| 118 | + await transaction.savepoint("my_savepoint1") |
| 119 | + await Manager.insert(Manager(name="Manager 2")) |
| 120 | + await transaction.savepoint("my_savepoint2") |
| 121 | + await Manager.insert(Manager(name="Manager 3")) |
| 122 | + await transaction.rollback_to("my_savepoint1") |
| 123 | + |
| 124 | + assert await Manager.select(Manager.name).run() == [{"name": "Manager 1"}] |
| 125 | + |
| 126 | + |
| 127 | +async def test_savepoint_sqli_checks() -> None: |
| 128 | + with pytest.raises(ValueError): # noqa: PT011 |
| 129 | + async with Manager._meta.db.transaction() as transaction: |
| 130 | + await transaction.savepoint( |
| 131 | + "my_savepoint; SELECT * FROM Manager", |
| 132 | + ) |
0 commit comments