Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cubedash/testutils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quoting the database name is not done by the corresponding code in datacube-core.

Since multiple downstream applications seem to need to re-invent the stuff already in datacube-core, can you make a PR on datacube-core that alters the timezone for a database so we can get rid of this functionality in Explorer in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might not have bothered with the quoting, but I happened to name my test database odc-explorer-test, and Postgres requires that to be inside double quotes.

I'll think about it. It's kind of a weird niche testing thing, that I'm not sure is universally useful. On the other hand, datacube-core does take care of things like roles and permissions.

I'd want to do some more research and investigation into whether:

  • the database timezone should always be UTC, or if it might make sense to have in a local timezone in some cases.
  • if changing it would break things, or be unexpected otherwise
  • it might be better as a warning when running datacube database init or system check

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",
Expand Down
22 changes: 22 additions & 0 deletions integration_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import time
from contextlib import contextmanager
from pathlib import Path
from textwrap import indent
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion integration_tests/test_page_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/test_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down