Skip to content
Open
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
32 changes: 32 additions & 0 deletions test/data/test_time_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def __call__(self, time, variable):
},
)

async def fetch(self, time, variable):
"""Mock async data fetch."""
return self(time, variable)


class TestTimeWindowInitialization:
"""Test TimeWindow initialization and validation."""
Expand Down Expand Up @@ -357,6 +361,34 @@ def test_time_coordinate_alignment(self):
assert len(result.time) == 1
assert result.time.values[0] == np.datetime64(base_time)

@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"])

expected_vars = [
"t2m_tm1",
"t2m_t",
"t2m_tp1",
"u10m_tm1",
"u10m_t",
"u10m_tp1",
]
actual_vars = [str(v) for v in result.coords["variable"].values]
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


class TestTimeWindowErrorHandling:
"""Test error handling in TimeWindow."""
Expand Down