|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import asyncio |
| 6 | + |
5 | 7 | import pytest |
6 | 8 | from simple_module_test import database |
7 | | -from sqlalchemy import Column, Integer, MetaData, String, Table, func, select, text |
| 9 | +from sqlalchemy import ( |
| 10 | + Column, |
| 11 | + ForeignKey, |
| 12 | + Identity, |
| 13 | + Integer, |
| 14 | + MetaData, |
| 15 | + String, |
| 16 | + Table, |
| 17 | + func, |
| 18 | + select, |
| 19 | + text, |
| 20 | +) |
| 21 | +from sqlalchemy.exc import DBAPIError |
8 | 22 | from sqlalchemy.ext.asyncio import create_async_engine |
9 | 23 |
|
10 | 24 | pytestmark = pytest.mark.postgres |
|
13 | 27 | _item = Table( |
14 | 28 | "reuse_probe_item", |
15 | 29 | _metadata, |
16 | | - Column("id", Integer, primary_key=True), |
| 30 | + Column("id", Integer, Identity(), primary_key=True), |
17 | 31 | Column("name", String(20)), |
18 | 32 | ) |
19 | 33 |
|
@@ -73,3 +87,99 @@ async def test_a_test_that_changes_the_schema_forces_a_rebuild(engine): |
73 | 87 | assert builds == [1, 1] |
74 | 88 | async with engine.connect() as conn: |
75 | 89 | assert await conn.scalar(text("SELECT to_regclass('reuse_probe_extra')")) is None |
| 90 | + |
| 91 | + |
| 92 | +async def test_reset_bypasses_delete_row_triggers_and_restarts_identity(engine): |
| 93 | + await _next_test(engine, []) |
| 94 | + child = Table( |
| 95 | + "reset probe child", |
| 96 | + MetaData(), |
| 97 | + Column("item_id", Integer, ForeignKey(_item.c.id), primary_key=True), |
| 98 | + schema="public", |
| 99 | + ) |
| 100 | + async with engine.begin() as conn: |
| 101 | + await conn.run_sync(child.create) |
| 102 | + await conn.execute( |
| 103 | + text(""" |
| 104 | + CREATE FUNCTION refuse_probe_delete() RETURNS trigger |
| 105 | + LANGUAGE plpgsql AS $$ |
| 106 | + BEGIN |
| 107 | + RAISE EXCEPTION 'probe rows are immutable'; |
| 108 | + END; |
| 109 | + $$ |
| 110 | + """) |
| 111 | + ) |
| 112 | + await conn.execute( |
| 113 | + text(""" |
| 114 | + CREATE TRIGGER refuse_probe_delete BEFORE DELETE ON reuse_probe_item |
| 115 | + FOR EACH ROW EXECUTE FUNCTION refuse_probe_delete() |
| 116 | + """) |
| 117 | + ) |
| 118 | + assert [await _insert(engine), await _insert(engine)] == [1, 2] |
| 119 | + async with engine.begin() as conn: |
| 120 | + await conn.execute(child.insert().values(item_id=1)) |
| 121 | + |
| 122 | + # A quoted, schema-qualified child and parent-first input need no DELETE ordering. |
| 123 | + await database._empty_tables(engine, [_item, child]) |
| 124 | + |
| 125 | + async with engine.connect() as conn: |
| 126 | + assert await conn.scalar(select(func.count()).select_from(_item)) == 0 |
| 127 | + assert await conn.scalar(select(func.count()).select_from(child)) == 0 |
| 128 | + assert await _insert(engine) == 1 |
| 129 | + |
| 130 | + |
| 131 | +async def test_reset_restarts_sequences_without_a_table_owner(engine): |
| 132 | + await _next_test(engine, []) |
| 133 | + async with engine.begin() as conn: |
| 134 | + await conn.execute(text("CREATE SEQUENCE reuse_probe_unowned")) |
| 135 | + assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 1 |
| 136 | + assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 2 |
| 137 | + |
| 138 | + await database._empty_tables(engine, [_item]) |
| 139 | + |
| 140 | + async with engine.connect() as conn: |
| 141 | + assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 1 |
| 142 | + |
| 143 | + |
| 144 | +async def test_reset_refuses_to_empty_tables_outside_the_passed_list(engine): |
| 145 | + await _next_test(engine, []) |
| 146 | + outside = Table( |
| 147 | + "outside_probe", |
| 148 | + MetaData(), |
| 149 | + Column("id", Integer, primary_key=True), |
| 150 | + Column("item_id", Integer, ForeignKey(_item.c.id)), |
| 151 | + ) |
| 152 | + async with engine.begin() as conn: |
| 153 | + await conn.run_sync(outside.create) |
| 154 | + # DELETE on the empty parent would leave this unrelated row untouched. |
| 155 | + await conn.execute(outside.insert().values(id=1, item_id=None)) |
| 156 | + |
| 157 | + with pytest.raises(DBAPIError, match="foreign key"): |
| 158 | + await database._empty_tables(engine, [_item]) |
| 159 | + |
| 160 | + async with engine.connect() as conn: |
| 161 | + assert await conn.scalar(select(outside.c.id)) == 1 |
| 162 | + |
| 163 | + |
| 164 | +async def test_reset_times_out_when_a_previous_reader_holds_a_lock(engine): |
| 165 | + await _next_test(engine, []) |
| 166 | + async with engine.connect() as reader: |
| 167 | + await reader.execute(select(_item)) |
| 168 | + try: |
| 169 | + with pytest.raises(DBAPIError, match="lock timeout"): |
| 170 | + await asyncio.wait_for(database._empty_tables(engine, [_item]), timeout=10) |
| 171 | + finally: |
| 172 | + await reader.rollback() |
| 173 | + |
| 174 | + |
| 175 | +async def test_reset_with_no_tables_preserves_the_migration_stamp(engine): |
| 176 | + await _next_test(engine, []) |
| 177 | + async with engine.begin() as conn: |
| 178 | + await conn.execute(text("CREATE TABLE alembic_version (version_num text)")) |
| 179 | + await conn.execute(text("INSERT INTO alembic_version VALUES ('probe')")) |
| 180 | + |
| 181 | + await database._empty_tables(engine, []) |
| 182 | + await database._empty_tables(engine, [_item]) |
| 183 | + |
| 184 | + async with engine.connect() as conn: |
| 185 | + assert await conn.scalar(text("SELECT version_num FROM alembic_version")) == "probe" |
0 commit comments