Skip to content

Commit

Permalink
refactor: Pull out function for any epoch datetime TDE-1147 (#960)
Browse files Browse the repository at this point in the history
* refactor: Pull out function for any epoch datetime

* refactor: Pull out test fixture to default location

To be able to share between tests.

* fix: Add to rather than replacing dictionary key

To avoid clobbering existing data.
  • Loading branch information
l0b0 authored May 7, 2024
1 parent ea5c98b commit e71444f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
17 changes: 17 additions & 0 deletions scripts/files/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from shutil import rmtree
from tempfile import mkdtemp
from typing import Generator

import pytest


@pytest.fixture(name="setup", autouse=True)
def fixture_setup() -> Generator[str, None, None]:
"""
This function creates a temporary directory and deletes it after each test.
See following link for details:
https://docs.pytest.org/en/stable/fixture.html#yield-fixtures-recommended
"""
target = mkdtemp()
yield target
rmtree(target)
15 changes: 0 additions & 15 deletions scripts/files/tests/fs_local_test.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
import os
from shutil import rmtree
from tempfile import mkdtemp
from typing import Generator

import pytest

from scripts.files.fs_local import exists, read, write


@pytest.fixture(name="setup", autouse=True)
def fixture_setup() -> Generator[str, None, None]:
"""
This function creates a temporary directory and deletes it after each test.
See following link for details:
https://docs.pytest.org/en/stable/fixture.html#yield-fixtures-recommended
"""
target = mkdtemp()
yield target
rmtree(target)


@pytest.mark.dependency(name="write")
def test_write(setup: str) -> None:
target = setup
Expand Down
9 changes: 4 additions & 5 deletions scripts/stac/imagery/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ def update_datetime(self, start_datetime: str, end_datetime: str) -> None:
start_datetime: a start date in `YYYY-MM-DD` format
end_datetime: a end date in `YYYY-MM-DD` format
"""
self.stac["properties"] = {
"start_datetime": start_datetime,
"end_datetime": end_datetime,
"datetime": None,
}
self.stac.setdefault("properties", {})
self.stac["properties"]["start_datetime"] = start_datetime
self.stac["properties"]["end_datetime"] = end_datetime
self.stac["properties"]["datetime"] = None

# FIXME: redefine the 'Any'
def update_spatial(self, geometry: Dict[str, Any], bbox: Tuple[float, ...]) -> None:
Expand Down
13 changes: 10 additions & 3 deletions scripts/tests/datetimes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def test_should_raise_error_when_formatting_a_naive_datetime(subtests: SubTests)


def test_should_be_able_to_invert_conversion() -> None:
start = datetime(1800, 1, 1, tzinfo=timezone.utc)
end = datetime(2100, 1, 1, tzinfo=timezone.utc)
original_datetime = any_datetime_between(start, end)
original_datetime = any_epoch_datetime()
assert parse_rfc_3339_datetime(format_rfc_3339_datetime_string(original_datetime)) == original_datetime


Expand All @@ -51,6 +49,15 @@ def test_should_format_rfc_3339_nz_midnight_datetime_string() -> None:
assert format_rfc_3339_nz_midnight_datetime_string(datetime_object) == "2001-02-02T11:00:00Z"


def any_epoch_datetime() -> datetime:
"""
Get arbitrary datetime
"""
start = datetime(1970, 1, 1, tzinfo=timezone.utc)
end = datetime(2100, 1, 1, tzinfo=timezone.utc)
return any_datetime_between(start, end)


def any_datetime_between(start: datetime, end: datetime) -> datetime:
"""
Get arbitrary datetime between start (inclusive) and end (exclusive), with second precision.
Expand Down

0 comments on commit e71444f

Please sign in to comment.