|
6 | 6 | from pytest import Pytester |
7 | 7 |
|
8 | 8 | import pytest_postgresql |
| 9 | +from pytest_postgresql.executor import PostgreSQLExecutor |
| 10 | +from pytest_postgresql.factories import postgresql_proc |
| 11 | +from pytest_postgresql.factories.noprocess import xdistify_dbname |
| 12 | +from pytest_postgresql.janitor import DatabaseJanitor |
| 13 | +from tests.loader import load_database |
9 | 14 |
|
10 | 15 |
|
11 | 16 | @pytest.fixture |
@@ -41,3 +46,78 @@ def test_postgres_loader_in_cli(pointed_pytester: Pytester) -> None: |
41 | 46 | test_sql_path = pointed_pytester.copy_example("test.sql") |
42 | 47 | ret = pointed_pytester.runpytest(f"--postgresql-load={test_sql_path}", "test_load.py") |
43 | 48 | ret.assert_outcomes(passed=1) |
| 49 | + |
| 50 | + |
| 51 | +postgresql_proc_to_override = postgresql_proc() |
| 52 | + |
| 53 | + |
| 54 | +def test_postgres_drop_test_database( |
| 55 | + postgresql_proc_to_override: PostgreSQLExecutor, |
| 56 | + pointed_pytester: Pytester, |
| 57 | +) -> None: |
| 58 | + """Check that the database is dropped on both process and client level if argument is passed. |
| 59 | +
|
| 60 | + Given: |
| 61 | + Preexisting tables override_tmpl and override (created with two DatabaseJanitor instances) |
| 62 | + When: |
| 63 | + Run test that connects to the process from current process with flag to drop database |
| 64 | + specified |
| 65 | + Then: |
| 66 | + The internal pytest run will delete the database on start (and after). |
| 67 | + It Would fail if it did not. Checks are performed by trying to connect to both override_tmpl |
| 68 | + and override databases and checking resulting exceptions. |
| 69 | + """ |
| 70 | + dbname = xdistify_dbname("override") |
| 71 | + template_dbname = dbname + "_tmpl" |
| 72 | + template_janitor = DatabaseJanitor( |
| 73 | + user=postgresql_proc_to_override.user, |
| 74 | + host=postgresql_proc_to_override.host, |
| 75 | + port=postgresql_proc_to_override.port, |
| 76 | + template_dbname=template_dbname, |
| 77 | + version=postgresql_proc_to_override.version, |
| 78 | + password=postgresql_proc_to_override.password, |
| 79 | + connection_timeout=5, |
| 80 | + ) |
| 81 | + template_janitor.init() |
| 82 | + template_janitor.load(load_database) |
| 83 | + assert template_janitor.template_dbname |
| 84 | + janitor = DatabaseJanitor( |
| 85 | + user=postgresql_proc_to_override.user, |
| 86 | + host=postgresql_proc_to_override.host, |
| 87 | + port=postgresql_proc_to_override.port, |
| 88 | + dbname=dbname, |
| 89 | + template_dbname=template_janitor.template_dbname, |
| 90 | + version=postgresql_proc_to_override.version, |
| 91 | + password=postgresql_proc_to_override.password, |
| 92 | + connection_timeout=5, |
| 93 | + ) |
| 94 | + janitor.init() |
| 95 | + assert janitor.dbname |
| 96 | + with janitor.cursor(janitor.dbname) as cur: |
| 97 | + cur.execute("SELECT * FROM stories") |
| 98 | + res = cur.fetchall() |
| 99 | + assert len(res) == 4 |
| 100 | + # Actual test happens now |
| 101 | + pointed_pytester.copy_example("test_drop_test_database.py") |
| 102 | + test_sql_path = pointed_pytester.copy_example("test.sql") |
| 103 | + ret = pointed_pytester.runpytest( |
| 104 | + f"--postgresql-load={test_sql_path}", |
| 105 | + f"--postgresql-port={postgresql_proc_to_override.port}", |
| 106 | + "--postgresql-dbname=override", |
| 107 | + "--postgresql-drop-test-database", |
| 108 | + "test_drop_test_database.py", |
| 109 | + ) |
| 110 | + ret.assert_outcomes(passed=1) |
| 111 | + |
| 112 | + with pytest.raises(TimeoutError) as excinfo: |
| 113 | + with janitor.cursor(janitor.dbname): |
| 114 | + pass |
| 115 | + assert hasattr(excinfo.value, "__cause__") |
| 116 | + assert f'FATAL: database "{janitor.dbname}" does not exist' in str(excinfo.value.__cause__) |
| 117 | + with pytest.raises(TimeoutError) as excinfo: |
| 118 | + with template_janitor.cursor(template_janitor.template_dbname): |
| 119 | + pass |
| 120 | + assert hasattr(excinfo.value, "__cause__") |
| 121 | + assert f'FATAL: database "{template_janitor.template_dbname}" does not exist' in str( |
| 122 | + excinfo.value.__cause__ |
| 123 | + ) |
0 commit comments