From 198fea683efca4e63746ce02efc7c2385767e18b Mon Sep 17 00:00:00 2001 From: Stephen Kilbourn Date: Fri, 3 May 2024 09:52:21 -0600 Subject: [PATCH] fix existing tests to run and not include in coverage report (#144) Co-authored-by: Stephen Kilbourn --- .coveragerc | 2 + docker_tasks/build_stac/tests/test_handler.py | 75 ++++--------------- docker_tasks/build_stac/tests/test_regex.py | 5 ++ 3 files changed, 21 insertions(+), 61 deletions(-) create mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 00000000..488aff11 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[run] +omit = **/tests/** \ No newline at end of file diff --git a/docker_tasks/build_stac/tests/test_handler.py b/docker_tasks/build_stac/tests/test_handler.py index bc48a491..d4147e63 100644 --- a/docker_tasks/build_stac/tests/test_handler.py +++ b/docker_tasks/build_stac/tests/test_handler.py @@ -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( @@ -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) \ No newline at end of file diff --git a/docker_tasks/build_stac/tests/test_regex.py b/docker_tasks/build_stac/tests/test_regex.py index 98dc462a..406ac810 100644 --- a/docker_tasks/build_stac/tests/test_regex.py +++ b/docker_tasks/build_stac/tests/test_regex.py @@ -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):