|
| 1 | +"""Chaining noprocess fixtures tests for pytest-postgresql.""" |
| 2 | + |
| 3 | +import psycopg |
| 4 | + |
| 5 | +from pytest_postgresql import factories |
| 6 | +from pytest_postgresql.executor import PostgreSQLExecutor |
| 7 | +from pytest_postgresql.executor_noop import NoopExecutor |
| 8 | + |
| 9 | + |
| 10 | +def load_schema(host: str, port: int, user: str, dbname: str, password: str | None) -> None: |
| 11 | + """Load schema into the database.""" |
| 12 | + with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: |
| 13 | + with conn.cursor() as cur: |
| 14 | + cur.execute("CREATE TABLE schema_table (id serial PRIMARY KEY, name varchar);") |
| 15 | + conn.commit() |
| 16 | + |
| 17 | + |
| 18 | +def load_data(host: str, port: int, user: str, dbname: str, password: str | None) -> None: |
| 19 | + """Load the first layer of data into the database.""" |
| 20 | + with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: |
| 21 | + with conn.cursor() as cur: |
| 22 | + cur.execute("INSERT INTO schema_table (name) VALUES ('data_layer');") |
| 23 | + cur.execute("CREATE TABLE data_table (id serial PRIMARY KEY, val varchar);") |
| 24 | + conn.commit() |
| 25 | + |
| 26 | + |
| 27 | +def load_more_data(host: str, port: int, user: str, dbname: str, password: str | None) -> None: |
| 28 | + """Load the second layer of data into the database.""" |
| 29 | + with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: |
| 30 | + with conn.cursor() as cur: |
| 31 | + cur.execute("INSERT INTO schema_table (name) VALUES ('more_data_layer');") |
| 32 | + cur.execute("CREATE TABLE more_data_table (id serial PRIMARY KEY, extra varchar);") |
| 33 | + conn.commit() |
| 34 | + |
| 35 | + |
| 36 | +# Chaining: proc -> noproc -> client |
| 37 | +base_proc = factories.postgresql_proc(load=[load_schema]) |
| 38 | +seeded_noproc = factories.postgresql_noproc(depends_on="base_proc", load=[load_data]) |
| 39 | +client_layered = factories.postgresql("seeded_noproc") |
| 40 | + |
| 41 | +# Deeper chaining: proc -> noproc -> noproc -> client |
| 42 | +more_seeded_noproc = factories.postgresql_noproc(depends_on="seeded_noproc", load=[load_more_data]) |
| 43 | +client_deep_layered = factories.postgresql("more_seeded_noproc") |
| 44 | + |
| 45 | + |
| 46 | +def test_chaining_two_layers(client_layered: psycopg.Connection) -> None: |
| 47 | + """Test that data from both proc and noproc layers is present.""" |
| 48 | + with client_layered.cursor() as cur: |
| 49 | + # From base_proc (load_schema) |
| 50 | + cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'schema_table';") |
| 51 | + res = cur.fetchone() |
| 52 | + assert res |
| 53 | + assert res[0] == 1 |
| 54 | + |
| 55 | + # From seeded_noproc (load_data) |
| 56 | + cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'data_table';") |
| 57 | + res = cur.fetchone() |
| 58 | + assert res |
| 59 | + assert res[0] == 1 |
| 60 | + |
| 61 | + # Data inserted in seeded_noproc |
| 62 | + cur.execute("SELECT name FROM schema_table;") |
| 63 | + res = cur.fetchone() |
| 64 | + assert res |
| 65 | + assert res[0] == "data_layer" |
| 66 | + |
| 67 | + |
| 68 | +def test_chaining_three_layers(client_deep_layered: psycopg.Connection) -> None: |
| 69 | + """Test that data from all three layers is present.""" |
| 70 | + with client_deep_layered.cursor() as cur: |
| 71 | + # From base_proc |
| 72 | + cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'schema_table';") |
| 73 | + res = cur.fetchone() |
| 74 | + assert res |
| 75 | + assert res[0] == 1 |
| 76 | + |
| 77 | + # From seeded_noproc |
| 78 | + cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'data_table';") |
| 79 | + res = cur.fetchone() |
| 80 | + assert res |
| 81 | + assert res[0] == 1 |
| 82 | + |
| 83 | + # From more_seeded_noproc |
| 84 | + cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'more_data_table';") |
| 85 | + res = cur.fetchone() |
| 86 | + assert res |
| 87 | + assert res[0] == 1 |
| 88 | + |
| 89 | + # Data from multiple layers |
| 90 | + cur.execute("SELECT name FROM schema_table ORDER BY id;") |
| 91 | + results = cur.fetchall() |
| 92 | + assert results[0][0] == "data_layer" |
| 93 | + assert results[1][0] == "more_data_layer" |
| 94 | + |
| 95 | + |
| 96 | +def test_inheritance(base_proc: PostgreSQLExecutor, seeded_noproc: NoopExecutor) -> None: |
| 97 | + """Verify that connection parameters are inherited from the base fixture.""" |
| 98 | + assert seeded_noproc.host == base_proc.host |
| 99 | + assert seeded_noproc.port == base_proc.port |
| 100 | + assert seeded_noproc.user == base_proc.user |
| 101 | + assert seeded_noproc.password == base_proc.password |
0 commit comments