|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +if TYPE_CHECKING: |
| 6 | + import pytest |
| 7 | + |
| 8 | + |
| 9 | +def test_service_fixture(pytester: pytest.Pytester) -> None: |
| 10 | + pytester.makepyfile(""" |
| 11 | + import pytest |
| 12 | + import psycopg |
| 13 | + from pytest_databases.docker.postgres import _make_connection_string # noqa: PLC2701 |
| 14 | +
|
| 15 | + pytest_plugins = ["pytest_databases.docker.yugabyte"] |
| 16 | +
|
| 17 | + def test(yugabyte_service) -> None: |
| 18 | + opts = "&".join(f"{k}={v}" for k, v in yugabyte_service.driver_opts.items()) |
| 19 | + with psycopg.connect( |
| 20 | + f"postgresql://yugabyte:yugabyte@{yugabyte_service.host}:{yugabyte_service.port}/{yugabyte_service.database}?{opts}" |
| 21 | + ) as conn: |
| 22 | + db_open = conn.execute("SELECT 1").fetchone() |
| 23 | + assert db_open is not None and db_open[0] == 1 |
| 24 | + """) |
| 25 | + |
| 26 | + result = pytester.runpytest_subprocess("-p", "pytest_databases") |
| 27 | + result.assert_outcomes(passed=1) |
| 28 | + |
| 29 | + |
| 30 | +def test_startup_connection_fixture(pytester: pytest.Pytester) -> None: |
| 31 | + pytester.makepyfile(""" |
| 32 | + import pytest |
| 33 | + import psycopg |
| 34 | + from pytest_databases.docker.postgres import _make_connection_string # noqa: PLC2701 |
| 35 | +
|
| 36 | +
|
| 37 | + pytest_plugins = ["pytest_databases.docker.yugabyte"] |
| 38 | +
|
| 39 | + def test(yugabyte_connection) -> None: |
| 40 | + yugabyte_connection.execute("CREATE TABLE if not exists simple_table as SELECT 1") |
| 41 | + result = yugabyte_connection.execute("select * from simple_table").fetchone() |
| 42 | + assert result is not None and result[0] == 1 |
| 43 | + """) |
| 44 | + |
| 45 | + result = pytester.runpytest_subprocess("-p", "pytest_databases") |
| 46 | + result.assert_outcomes(passed=1) |
| 47 | + |
| 48 | + |
| 49 | +def test_xdist_isolate_database(pytester: pytest.Pytester) -> None: |
| 50 | + pytester.makepyfile(""" |
| 51 | + import pytest |
| 52 | + import psycopg |
| 53 | + from pytest_databases.docker.postgres import _make_connection_string |
| 54 | +
|
| 55 | + pytest_plugins = ["pytest_databases.docker.yugabyte"] |
| 56 | +
|
| 57 | + def test_one(yugabyte_service) -> None: |
| 58 | + opts = "&".join(f"{k}={v}" for k, v in yugabyte_service.driver_opts.items()) |
| 59 | + with psycopg.connect( |
| 60 | + f"postgresql://yugabyte:yugabyte@{yugabyte_service.host}:{yugabyte_service.port}/{yugabyte_service.database}?{opts}" |
| 61 | + ) as conn: |
| 62 | + conn.execute("CREATE TABLE foo AS SELECT 1") |
| 63 | +
|
| 64 | + def test_two(yugabyte_service) -> None: |
| 65 | + opts = "&".join(f"{k}={v}" for k, v in yugabyte_service.driver_opts.items()) |
| 66 | + with psycopg.connect( |
| 67 | + f"postgresql://yugabyte:yugabyte@{yugabyte_service.host}:{yugabyte_service.port}/{yugabyte_service.database}?{opts}" |
| 68 | + ) as conn: |
| 69 | + conn.execute("CREATE TABLE foo AS SELECT 1") |
| 70 | + """) |
| 71 | + |
| 72 | + result = pytester.runpytest_subprocess("-n", "2") |
| 73 | + result.assert_outcomes(passed=2) |
| 74 | + |
| 75 | + |
| 76 | +def test_xdist_isolate_server(pytester: pytest.Pytester) -> None: |
| 77 | + pytester.makepyfile(""" |
| 78 | + import pytest |
| 79 | + import psycopg |
| 80 | + from pytest_databases.docker.postgres import _make_connection_string |
| 81 | +
|
| 82 | + pytest_plugins = ["pytest_databases.docker.yugabyte"] |
| 83 | +
|
| 84 | + @pytest.fixture(scope="session") |
| 85 | + def xdist_yugabyte_isolation_level(): |
| 86 | + return "server" |
| 87 | +
|
| 88 | + def test_one(yugabyte_service) -> None: |
| 89 | + opts = "&".join(f"{k}={v}" for k, v in yugabyte_service.driver_opts.items()) |
| 90 | + with psycopg.connect( |
| 91 | + f"postgresql://yugabyte:yugabyte@{yugabyte_service.host}:{yugabyte_service.port}/{yugabyte_service.database}?{opts}", |
| 92 | + autocommit=True, |
| 93 | + ) as conn: |
| 94 | + conn.execute("CREATE DATABASE foo") |
| 95 | +
|
| 96 | + def test_two(yugabyte_service) -> None: |
| 97 | + opts = "&".join(f"{k}={v}" for k, v in yugabyte_service.driver_opts.items()) |
| 98 | + with psycopg.connect( |
| 99 | + f"postgresql://yugabyte:yugabyte@{yugabyte_service.host}:{yugabyte_service.port}/{yugabyte_service.database}?{opts}", |
| 100 | + autocommit=True, |
| 101 | + ) as conn: |
| 102 | + conn.execute("CREATE DATABASE foo") |
| 103 | + """) |
| 104 | + |
| 105 | + result = pytester.runpytest_subprocess("-n", "2") |
| 106 | + result.assert_outcomes(passed=2) |
0 commit comments