-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
tests/examples/test_postgres_template_database_cleanup/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import os | ||
|
||
from pytest_mock_resources import create_postgres_fixture | ||
import pytest | ||
|
||
from pytest_mock_resources import create_postgres_fixture, PostgresConfig | ||
|
||
should_clean = bool(os.environ.get("CLEAN", False)) | ||
port = int(os.environ["PMR_POSTGRES_PORT"]) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def pmr_postgres_config(): | ||
return PostgresConfig(port=port, ci_port=port) | ||
|
||
|
||
pg = create_postgres_fixture(session=True, cleanup_databases=should_clean) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from sqlalchemy import text | ||
|
||
from pytest_mock_resources import create_postgres_fixture, PostgresConfig | ||
|
||
pg = create_postgres_fixture(session=True) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def pmr_postgres_config(): | ||
return PostgresConfig(port=None) | ||
|
||
|
||
@pytest.mark.postgres | ||
def test_postgres_template_database_cleanup(pytester, pg, pmr_postgres_config): | ||
"""Execute two tests in a subprocess and assert they clean up their databases. | ||
Normally, two tests sharing a fixture would share a template database and leave | ||
both their individual test databases and their corresponding template databases | ||
behind as artifacts. | ||
This test asserts that the template database and test database are **both** cleaned up. | ||
""" | ||
pytester.copy_example() | ||
|
||
args = ["-vv", "test_cleanup.py"] | ||
|
||
databases_before = get_databases(pg) | ||
|
||
port = pmr_postgres_config.port | ||
assert port | ||
|
||
with patch("os.environ", new={"PMR_POSTGRES_PORT": port, "CLEAN": "1"}): | ||
result = pytester.inline_run(*args) | ||
result.assertoutcome(passed=2, skipped=0, failed=0) | ||
|
||
databases_after = get_databases(pg) | ||
assert databases_before == databases_after | ||
|
||
# Re-run it after, without cleaning turned on to make sure this isn't a false positive | ||
with patch("os.environ", new={"PMR_POSTGRES_PORT": port}): | ||
result = pytester.inline_run(*args) | ||
|
||
result.assertoutcome(passed=2, skipped=0, failed=0) | ||
|
||
databases_after = get_databases(pg) | ||
assert databases_before != databases_after | ||
|
||
|
||
def get_databases(pg): | ||
return sorted([n for (n,) in pg.execute(text("select datname from pg_database")).all()]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters