Skip to content

Commit 315fa48

Browse files
committed
Add a test for find_json_files_in_date_range
1 parent d4c0d69 commit 315fa48

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

tests/test_config_ocean_model.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import particle_tracking_manager
1515

1616
from particle_tracking_manager.config_ocean_model import ocean_model_simulation_mapper
17+
from particle_tracking_manager.models.opendrift.utils import (
18+
find_json_files_in_date_range,
19+
)
1720
from particle_tracking_manager.ocean_model_registry import ocean_model_registry
1821

1922

@@ -211,6 +214,92 @@ def test_start_end_times():
211214
)
212215

213216

217+
class MockFileSystem:
218+
"""Mock file system to simulate globbing for kerchunk JSON files."""
219+
220+
def __init__(self, files):
221+
self.files = files
222+
223+
def glob(self, year):
224+
# Mock glob method takes a year string instead of a pattern
225+
return [file for file in self.files if year in file]
226+
227+
228+
MOCK_FILES_YEARLY_DATES = [
229+
(1, 1),
230+
(1, 2),
231+
(1, 31),
232+
(3, 15),
233+
(5, 31),
234+
(6, 1),
235+
(6, 15),
236+
(6, 30),
237+
(7, 1),
238+
(9, 15),
239+
(12, 1),
240+
(12, 30),
241+
(12, 31),
242+
]
243+
MOCK_FILES_DATES = [
244+
datetime(year, month, day)
245+
for month, day in MOCK_FILES_YEARLY_DATES
246+
for year in [2019, 2020, 2021]
247+
]
248+
TEST_DATE_RANGES = [
249+
((2020, 6, 5), (2020, 6, 25), 1),
250+
((2020, 1, 1), (2020, 1, 31), 3),
251+
((2020, 1, 31), (2020, 1, 1), 3),
252+
((2019, 12, 1), (2020, 1, 31), 6),
253+
((2019, 12, 31), (2020, 1, 1), 2),
254+
((2020, 1, 1), (2020, 12, 31), 13),
255+
((2020, 1, 3), (2020, 6, 29), 5),
256+
((2020, 6, 29), (2020, 1, 3), 5),
257+
((2019, 12, 31), (2021, 1, 1), None),
258+
((2019, 12, 31), (2021, 12, 31), None),
259+
((2020, 1, 1), (2021, 12, 31), 26),
260+
]
261+
262+
263+
@pytest.mark.parametrize(
264+
"start_tuple, end_tuple, expected",
265+
TEST_DATE_RANGES,
266+
ids=[f"{start}->{end}" for start, end, _ in TEST_DATE_RANGES],
267+
)
268+
@pytest.mark.parametrize(
269+
"filename_format",
270+
["%Y_0%j", "ciofs_%Y-%m-%d", "nwgoa_%Y-%m-%d"],
271+
ids=["CIOFS", "CIOFSOP", "NWGOA"],
272+
)
273+
def test_krchunk_json_filtering(filename_format, start_tuple, end_tuple, expected):
274+
"""Check that kerchunk JSON files are correctly filtered based on date range"""
275+
start = datetime(*start_tuple)
276+
end = datetime(*end_tuple)
277+
278+
all_files = [f"{d.strftime(filename_format)}.json" for d in MOCK_FILES_DATES]
279+
mock_fs = MockFileSystem(all_files)
280+
281+
def make_glob_with_check(year: str) -> str:
282+
"""Check that the input string is a year and return it instead of any pattern.
283+
284+
The return value will be used by `MockFileSystem.glob` which accounts for this.
285+
"""
286+
assert year == f"{start.year:04}" or year == f"{end.year:04}"
287+
return year
288+
289+
try:
290+
jsons = find_json_files_in_date_range(
291+
mock_fs,
292+
make_glob_with_check,
293+
start,
294+
end,
295+
filename_format,
296+
)
297+
298+
assert len(jsons) == expected
299+
except ValueError:
300+
assert expected is None, f"Expected {expected} JSON files but got ValueError"
301+
302+
214303
def test_user_registry():
215304

216305
# Create a temporary directory

0 commit comments

Comments
 (0)