|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from textwrap import dedent |
| 4 | + |
| 5 | +from pytest import FixtureRequest |
1 | 6 | from _pytest.pytester import Pytester |
| 7 | +from sqlalchemy_utils import database_exists, create_database |
2 | 8 |
|
3 | 9 |
|
4 | | -def test_plugin(pytester: Pytester, db_url: str) -> None: |
| 10 | +def test_minimal(pytester: Pytester, db_url: str) -> None: |
5 | 11 | pytester.copy_example("tests/examples/test_minimal.py") |
6 | 12 | result = pytester.runpytest('--sqlalchemy-connect-url', db_url) |
7 | 13 | result.assert_outcomes(passed=1) |
8 | 14 |
|
| 15 | + |
| 16 | +def test_transaction_fixture(pytester: Pytester, db_url: str) -> None: |
| 17 | + pytester.copy_example("tests/examples/test_transaction.py") |
| 18 | + result = pytester.runpytest('--sqlalchemy-connect-url', db_url) |
| 19 | + result.assert_outcomes(passed=1) |
| 20 | + |
| 21 | + |
| 22 | +def test_dbsession_fixture(pytester: Pytester, db_url: str) -> None: |
| 23 | + pytester.copy_example("tests/examples/test_dbsession.py") |
| 24 | + result = pytester.runpytest('--sqlalchemy-connect-url', db_url) |
| 25 | + result.assert_outcomes(passed=1) |
| 26 | + |
| 27 | + |
| 28 | +def test_db_schema(pytester: Pytester, new_db_url: str) -> None: |
| 29 | + assert not database_exists(new_db_url) |
| 30 | + pytester.copy_example("tests/examples/test_db_schema.py") |
| 31 | + result = pytester.runpytest( |
| 32 | + '--sqlalchemy-connect-url', new_db_url, '--sqlalchemy-manage-db' |
| 33 | + ) |
| 34 | + result.assert_outcomes(passed=1) |
| 35 | + assert not database_exists(new_db_url) |
| 36 | + |
| 37 | + |
| 38 | +def test_db_exists_no_keep_db(pytester: Pytester, db_url: str) -> None: |
| 39 | + if not database_exists(db_url): |
| 40 | + create_database(db_url) |
| 41 | + pytester.copy_example("tests/examples/test_db_schema.py") |
| 42 | + result = pytester.runpytest( |
| 43 | + '--sqlalchemy-connect-url', db_url, '--sqlalchemy-manage-db' |
| 44 | + ) |
| 45 | + result.assert_outcomes(errors=1) |
| 46 | + assert "DB exists, remove it before proceeding" in result.stdout.str() |
| 47 | + assert database_exists(db_url) |
| 48 | + |
| 49 | + |
| 50 | +def test_db_exists_keep_db(pytester: Pytester, db_url: str) -> None: |
| 51 | + if not database_exists(db_url): |
| 52 | + create_database(db_url) |
| 53 | + pytester.copy_example("tests/examples/test_db_schema.py") |
| 54 | + result = pytester.runpytest( |
| 55 | + '--sqlalchemy-connect-url', |
| 56 | + db_url, |
| 57 | + '--sqlalchemy-manage-db', |
| 58 | + '--sqlalchemy-keep-db', |
| 59 | + ) |
| 60 | + result.assert_outcomes(passed=1) |
| 61 | + assert database_exists(db_url) |
| 62 | + |
| 63 | + |
| 64 | +def test_connect_uri_alias(pytester: Pytester, db_url: str) -> None: |
| 65 | + pytester.copy_example("tests/examples/test_connect_uri.py") |
| 66 | + result = pytester.runpytest('--sqlalchemy-connect-url', db_url) |
| 67 | + result.assert_outcomes(passed=1) |
| 68 | + |
| 69 | + |
| 70 | +def test_engine_with_config_file(pytester: Pytester, tmp_path: Path) -> None: |
| 71 | + config_path = tmp_path / "config.ini" |
| 72 | + config_path.write_text( |
| 73 | + dedent( |
| 74 | + """ |
| 75 | + [DEFAULT] |
| 76 | + sqlalchemy.url = sqlite:///:memory: |
| 77 | + """ |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + pytester.copy_example("tests/examples/test_minimal.py") |
| 82 | + result = pytester.runpytest('--sqlalchemy-config-file', config_path) |
| 83 | + result.assert_outcomes(passed=1) |
| 84 | + |
| 85 | + |
| 86 | +def test_engine_no_config_source(pytester: Pytester) -> None: |
| 87 | + pytester.copy_example("tests/examples/test_minimal.py") |
| 88 | + result = pytester.runpytest() |
| 89 | + result.assert_outcomes(errors=1) |
| 90 | + assert "Can not establish a connection to the database" in result.stdout.str() |
| 91 | + |
| 92 | + |
| 93 | +def test_xdist_naming(pytester: Pytester, db_url: str, request: FixtureRequest) -> None: |
| 94 | + running_under_coverage = bool(os.environ.get("COVERAGE_RUN")) |
| 95 | + # Measuring coverage when running under xdist is pretty tricky: |
| 96 | + if running_under_coverage: |
| 97 | + pyproject_toml = Path(__file__).parent.parent / "pyproject.toml" |
| 98 | + pytester.makeconftest(dedent(f""" |
| 99 | + import coverage, os, sys |
| 100 | + def pytest_configure(config): |
| 101 | + worker_id = os.environ.get("PYTEST_XDIST_WORKER") |
| 102 | + if worker_id is not None: |
| 103 | + os.environ["COVERAGE_PROCESS_START"] = "{pyproject_toml}" |
| 104 | + coverage.process_startup() |
| 105 | + """)) |
| 106 | + pytester.copy_example("tests/examples/test_xdist.py") |
| 107 | + result = pytester.runpytest('-n', '2', '--sqlalchemy-connect-url', db_url) |
| 108 | + result.assert_outcomes(passed=2) |
| 109 | + if running_under_coverage: |
| 110 | + for item in pytester.path.glob('.coverage.*'): |
| 111 | + item.rename(request.config.rootdir / item.name) |
0 commit comments