diff --git a/cubedash/testutils/database.py b/cubedash/testutils/database.py index 16e9d7c9c..337686e6e 100644 --- a/cubedash/testutils/database.py +++ b/cubedash/testutils/database.py @@ -147,6 +147,12 @@ def odc_test_db(cfg_env): # during testing, and need any performance gains we can get. with index._db._engine.begin() as conn: # type: ignore[attr-defined] + # Some tests are sensitive to the timezone of the database, and expect it to be UTC + quoted_db_name = conn.dialect.identifier_preparer.quote( + index._db._engine.url.database # type: ignore[attr-defined] + ) + conn.execute(text(f"ALTER DATABASE {quoted_db_name} SET timezone TO 'UTC'")) + if index.name == "pg_index": for table in [ "agdc.dataset_location", diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index f01662c03..eb9e90a52 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -1,3 +1,5 @@ +import os +import time from contextlib import contextmanager from pathlib import Path from textwrap import indent @@ -142,6 +144,26 @@ def client(unpopulated_client: FlaskClient) -> FlaskClient: return unpopulated_client +@pytest.fixture() +def fix_utc_timezone(): + """Set the timezone to UTC.""" + original_tz = os.environ.get("TZ") + os.environ["TZ"] = "UTC" + + # tzset isn't available in Windows + if hasattr(time, "tzset"): + time.tzset() + + yield + + if original_tz is None: + os.environ.pop("TZ", None) + else: + os.environ["TZ"] = original_tz + if hasattr(time, "tzset"): + time.tzset() + + def pytest_assertrepr_compare( config: pytest.Config, op: str, left: object, right: object ) -> list[str] | None: diff --git a/integration_tests/test_page_loads.py b/integration_tests/test_page_loads.py index 067c6f1ca..a37f97bcb 100644 --- a/integration_tests/test_page_loads.py +++ b/integration_tests/test_page_loads.py @@ -793,6 +793,8 @@ def test_invalid_product_returns_not_found(client: FlaskClient) -> None: def test_show_summary_cli(clirunner, client: FlaskClient) -> None: """ You should be able to view a product with cubedash-view command-line program. + + This test expects the database timezone to be UTC """ # ls7_nbar_scene, 2017, May res = clirunner(show.cli, ["ls7_nbar_scene", "2017", "5"]) @@ -810,7 +812,8 @@ def test_show_summary_cli(clirunner, client: FlaskClient) -> None: f" to {expected_to.isoformat()} ", ) ) - assert res.output.startswith(expected_header) + result_header = "\n".join(res.output.splitlines()[:5]) + assert expected_header == result_header expected_metadata = "\n".join( # noqa: FLY002 ( "Metadata", diff --git a/integration_tests/test_stac.py b/integration_tests/test_stac.py index ba895a1c0..ff261405d 100644 --- a/integration_tests/test_stac.py +++ b/integration_tests/test_stac.py @@ -636,6 +636,8 @@ def test_stac_collection(stac_client: FlaskClient): """ Follow the links to the "high_tide_comp_20p" collection and ensure it includes all of our tests data. + + This test expects the database timezone to be UTC """ collections = get_json(stac_client, "/stac") diff --git a/integration_tests/test_stores.py b/integration_tests/test_stores.py index 6dfd3f5c0..4884dacc3 100644 --- a/integration_tests/test_stores.py +++ b/integration_tests/test_stores.py @@ -120,7 +120,7 @@ def test_add_no_periods(summary_store: SummaryStore) -> None: assert summary_store.get("ga_ls8c_level1_3", 2015, 7, None) is None -def test_month_iteration() -> None: +def test_month_iteration(fix_utc_timezone) -> None: def assert_month_iteration( start: datetime, end: datetime, expected_months: list[date] ) -> None: