test: add async fetch coverage for TimeWindow data source#869
Conversation
Greptile SummaryAdds async coverage for
|
| Filename | Overview |
|---|---|
| test/data/test_time_window.py | Adds async def fetch to MockDataSource and one new test covering async variable ordering and time-coordinate alignment; uses asyncio.run() instead of the project's standard @pytest.mark.asyncio pattern, and only asserts the first of three offset calls. |
Reviews (1): Last reviewed commit: "test: add async fetch coverage for TimeW..." | Re-trigger Greptile
| def test_fetch_matches_sync_output_coordinates(self): | ||
| """Test async fetch applies offsets and preserves output coordinates.""" | ||
| ds = MockDataSource() | ||
| tw = TimeWindow( | ||
| datasource=ds, | ||
| offsets=[timedelta(hours=-6), timedelta(hours=0), timedelta(hours=6)], | ||
| suffixes=["_tm1", "_t", "_tp1"], | ||
| ) | ||
|
|
||
| base_time = datetime(2024, 1, 1, 12, 0) | ||
| result = asyncio.run(tw.fetch(base_time, ["t2m", "u10m"])) |
There was a problem hiding this comment.
The project is configured with
pytest-asyncio (asyncio_mode = "strict") and all other async tests in this repo use @pytest.mark.asyncio with async def (see test_data_utils.py, test_async_zarr.py, test_server_utils.py). Using asyncio.run() in a sync wrapper diverges from that established convention, and calling asyncio.run() from within a running event loop (e.g. if an async fixture or plugin attaches one) will raise RuntimeError: This event loop is already running.
| def test_fetch_matches_sync_output_coordinates(self): | |
| """Test async fetch applies offsets and preserves output coordinates.""" | |
| ds = MockDataSource() | |
| tw = TimeWindow( | |
| datasource=ds, | |
| offsets=[timedelta(hours=-6), timedelta(hours=0), timedelta(hours=6)], | |
| suffixes=["_tm1", "_t", "_tp1"], | |
| ) | |
| base_time = datetime(2024, 1, 1, 12, 0) | |
| result = asyncio.run(tw.fetch(base_time, ["t2m", "u10m"])) | |
| @pytest.mark.asyncio | |
| async def test_fetch_matches_sync_output_coordinates(self): | |
| """Test async fetch applies offsets and preserves output coordinates.""" | |
| ds = MockDataSource() | |
| tw = TimeWindow( | |
| datasource=ds, | |
| offsets=[timedelta(hours=-6), timedelta(hours=0), timedelta(hours=6)], | |
| suffixes=["_tm1", "_t", "_tp1"], | |
| ) | |
| base_time = datetime(2024, 1, 1, 12, 0) | |
| result = await tw.fetch(base_time, ["t2m", "u10m"]) |
| assert actual_vars == expected_vars | ||
| assert result.time.values[0] == np.datetime64(base_time) | ||
| assert ds.call_history[0]["time"] == [datetime(2024, 1, 1, 6, 0)] |
There was a problem hiding this comment.
Only the first offset call (-6 h) is verified. The corresponding sync test
test_offset_times_calculated_correctly checks all three call-history entries. Without checking indices 1 (0 h → 12:00) and 2 (+6 h → 18:00) the test could pass even if the offset arithmetic for the later calls was broken.
| assert actual_vars == expected_vars | |
| assert result.time.values[0] == np.datetime64(base_time) | |
| assert ds.call_history[0]["time"] == [datetime(2024, 1, 1, 6, 0)] | |
| assert actual_vars == expected_vars | |
| assert result.time.values[0] == np.datetime64(base_time) | |
| assert ds.call_history[0]["time"] == [datetime(2024, 1, 1, 6, 0)] # -6h | |
| assert ds.call_history[1]["time"] == [datetime(2024, 1, 1, 12, 0)] # 0h | |
| assert ds.call_history[2]["time"] == [datetime(2024, 1, 1, 18, 0)] # +6h |
Summary
Adds focused async coverage for
TimeWindow.fetch().This verifies that async fetch:
Closes #589.
Test plan
python -m pytest test/data/test_time_window.py -vResult: 28 passed, 1 skipped.