Skip to content

Commit 879ce7e

Browse files
Add specific tests for caching
1 parent 22acc18 commit 879ce7e

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

pixi.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_source_utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
from pathlib import Path
6+
from unittest import mock
67

78
import cftime
89
import pytest
@@ -13,6 +14,7 @@
1314
HashableIndexes,
1415
_guess_start_end_dates,
1516
get_timeinfo,
17+
open_dataset_cached,
1618
)
1719

1820

@@ -259,3 +261,39 @@ def test_hashable_indexes_cftime():
259261
assert h1 ^ h2 == set()
260262

261263
assert h1 & h2 == set(h1.keys())
264+
265+
266+
@mock.patch(
267+
"access_nri_intake.source.utils.xr.open_dataset",
268+
)
269+
@pytest.mark.parametrize(
270+
"paths, num_calls",
271+
[
272+
(["path1.nc", "path2.nc", "path1.nc"], 2),
273+
(["path1.nc", "path1.nc", "path1.nc"], 1),
274+
(["path1.nc", "path2.nc", "path3.nc"], 3),
275+
([f"path_{i}.nc" for i in range(6)] * 2, 6),
276+
([f"path_{i}.nc" for i in range(100)], 100),
277+
],
278+
)
279+
def test_open_dataset_caching(mock_open, paths, num_calls):
280+
mock_open.reset_mock()
281+
open_dataset_cached.cache_clear()
282+
mock_open.side_effect = lambda *args, **kwargs: xr.Dataset(
283+
{"data": ("x", [1, 2, 3])}
284+
)
285+
286+
for p in paths:
287+
open_dataset_cached(p)
288+
289+
assert mock_open.call_count == num_calls
290+
291+
path = "dummy_path.nc"
292+
ds1 = open_dataset_cached(path)
293+
ds2 = open_dataset_cached(path)
294+
295+
N_MAX = 11
296+
assert open_dataset_cached.cache_info().hits >= 1
297+
assert open_dataset_cached.cache_info().currsize < N_MAX
298+
299+
assert ds1 is ds2 # Should be the same object due to caching

0 commit comments

Comments
 (0)