Skip to content

Commit

Permalink
fix existing tests to run and not include in coverage report (#144)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Kilbourn <[email protected]>
  • Loading branch information
stephenkilbourn and stephenkilbourn authored May 3, 2024
1 parent 4023ce8 commit 198fea6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 61 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = **/tests/**
75 changes: 14 additions & 61 deletions docker_tasks/build_stac/tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,72 +21,25 @@ def build_mock_stac_item(item: Dict[str, Any]) -> MagicMock:
return expected_stac_item


@contextlib.contextmanager
def override_registry(
dispatch_callable: "_SingleDispatchCallable[Any]", cls: Type, mock: Mock
):
"""
Helper to override a singledispatch function with a mock for testing.
"""
original = dispatch_callable.registry[cls]
dispatch_callable.register(cls, mock)
try:
yield mock
finally:
dispatch_callable.register(cls, original)


def test_routing_regex_event():
"""
Ensure that the system properly identifies, classifies, and routes regex-style events.
"""
regex_event = {
"collection": "test-collection",
"s3_filename": "s3://test-bucket/delivery/BMHD_Maria_Stages/70001_BeforeMaria_Stage0_2017-07-21.tif",
"granule_id": None,
"datetime_range": None,
"start_datetime": None,
"end_datetime": None,
}

with override_registry(
stac.generate_stac,
events.RegexEvent,
MagicMock(return_value=build_mock_stac_item({"mock": "STAC Item 1"})),
) as called_mock, override_registry(
stac.generate_stac,
events.CmrEvent,
MagicMock(),
) as not_called_mock:
handler.handler(regex_event, None)

called_mock.assert_called_once_with(events.RegexEvent.parse_obj(regex_event))
assert not not_called_mock.call_count


def test_routing_cmr_event():
"""
Ensure that the system properly identifies, classifies, and routes CMR-style events.
"""
cmr_event = {
"collection": "test-collection",
"s3_filename": "s3://test-bucket/delivery/BMHD_Maria_Stages/70001_BeforeMaria_Stage0_2017-07-21.tif",
"granule_id": "test-granule",
regex_event = {
"collection": "TEST_COLLECTION",
"item_id": "test_2024-04-24.tif",
"assets": {
"TEST_FILE": {
"title": "Test_FILE",
"description": "TEST_FILE, described",
"href": "./docker_tasks/build_stac/tests/test_2024-04-24.tif",
}
}
}
response = handler.handler(regex_event)
print(response)

with override_registry(
stac.generate_stac,
events.CmrEvent,
MagicMock(return_value=build_mock_stac_item({"mock": "STAC Item 1"})),
) as called_mock, override_registry(
stac.generate_stac,
events.RegexEvent,
MagicMock(),
) as not_called_mock:
handler.handler(cmr_event, None)

called_mock.assert_called_once_with(events.CmrEvent.parse_obj(cmr_event))
assert not not_called_mock.call_count
assert response


@pytest.mark.parametrize(
Expand All @@ -98,4 +51,4 @@ def test_routing_unexpected_event(bad_event):
Ensure that a malformatted event raises a validation error
"""
with pytest.raises(ValidationError):
handler.handler(bad_event, None)
handler.handler(bad_event)
5 changes: 5 additions & 0 deletions docker_tasks/build_stac/tests/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
("s3://foo/bar/foo_20050402_bar.tif", "year"),
(datetime(2005, 1, 1), datetime(2005, 12, 31), None),
),
(
# Single date converted to year range - %Y
("s3://foo/bar/foo_20050402_bar.tif", "day"),
(datetime(2005, 4, 2, 0, 0), datetime(2005, 4, 2, 23, 59, 59), None),
),
],
)
def test_date_extraction(test_input, expected):
Expand Down

0 comments on commit 198fea6

Please sign in to comment.