From 1013429d436706453869c1846fccc299c6daa75b Mon Sep 17 00:00:00 2001 From: Fridolin Glatter Date: Fri, 9 Feb 2024 13:24:07 +0100 Subject: [PATCH] Skip pgsql tests if necessary --- tests/conftest.py | 26 +++++++++++------------- tests/utils.py | 50 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 85160d2f..358bacc1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,6 +24,14 @@ except FileNotFoundError: TEST_DATA_BIG = None # skip benchmark tests +SKIP_PGSQL_TESTS = False +try: + mp = Platform(_backend=PostgresTestBackend()) + # TODO: Do we need the following? + # mp.backend.close() +except OperationalError: + SKIP_PGSQL_TESTS = True + @pytest.fixture(scope="function") def test_data_big(): @@ -104,13 +112,7 @@ def test_sqlite_mp(): @pytest.fixture def test_pgsql_mp(): - try: - mp = Platform(_backend=PostgresTestBackend()) - except OperationalError as e: - pytest.skip( - f"Cannot connect to PostgreSQL database service, skipping test: {e}" - ) - + mp = Platform(_backend=PostgresTestBackend()) yield mp mp.backend.close() @@ -133,14 +135,8 @@ def test_sqlite_mp_generated(): @pytest.fixture(scope="module") -def test_pgsql_mp_generated(): - try: - mp = Platform(_backend=PostgresTestBackend()) - except OperationalError as e: - pytest.skip( - f"Cannot connect to PostgreSQL database service, skipping test: {e}" - ) - +def test_pgsql_mp_generated(request): + mp = Platform(_backend=PostgresTestBackend()) generate_mock_data(mp) yield mp mp.backend.close() diff --git a/tests/utils.py b/tests/utils.py index 02dd9ff8..0f086949 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -4,6 +4,8 @@ from ixmp4 import DataPoint +from .conftest import SKIP_PGSQL_TESTS + def add_regions(mp, regions): for region in regions: @@ -27,9 +29,21 @@ def assert_unordered_equality(df1, df2, **kwargs): "test_mp", [ "test_sqlite_mp", - "test_pgsql_mp", + pytest.param( + "test_pgsql_mp", + marks=pytest.mark.skipif( + SKIP_PGSQL_TESTS, + reason="Cannot connect to PostgreSQL database service, skipping test", + ), + ), "test_api_sqlite_mp", - "test_api_pgsql_mp", + pytest.param( + "test_api_pgsql_mp", + marks=pytest.mark.skipif( + SKIP_PGSQL_TESTS, + reason="Cannot connect to PostgreSQL database service, skipping test", + ), + ), ], ) @@ -37,9 +51,21 @@ def assert_unordered_equality(df1, df2, **kwargs): "generated_mp", [ "test_sqlite_mp_generated", - "test_pgsql_mp_generated", + pytest.param( + "test_pgsql_mp_generated", + marks=pytest.mark.skipif( + SKIP_PGSQL_TESTS, + reason="Cannot connect to PostgreSQL database service, skipping test", + ), + ), "test_api_sqlite_mp_generated", - "test_api_pgsql_mp_generated", + pytest.param( + "test_api_pgsql_mp_generated", + marks=pytest.mark.skipif( + SKIP_PGSQL_TESTS, + reason="Cannot connect to PostgreSQL database service, skipping test", + ), + ), ], ) @@ -48,7 +74,13 @@ def assert_unordered_equality(df1, df2, **kwargs): "test_mp", [ "test_api_sqlite_mp", - "test_api_pgsql_mp", + pytest.param( + "test_api_pgsql_mp", + marks=pytest.mark.skipif( + SKIP_PGSQL_TESTS, + reason="Cannot connect to PostgreSQL database service, skipping test", + ), + ), ], ) @@ -56,7 +88,13 @@ def assert_unordered_equality(df1, df2, **kwargs): "test_mp", [ "test_sqlite_mp", - "test_pgsql_mp", + pytest.param( + "test_pgsql_mp", + marks=pytest.mark.skipif( + SKIP_PGSQL_TESTS, + reason="Cannot connect to PostgreSQL database service, skipping test", + ), + ), ], )