|
3 | 3 |
|
4 | 4 |
|
5 | 5 | from pathlib import Path |
| 6 | +from unittest import mock |
6 | 7 |
|
7 | 8 | import cftime |
8 | 9 | import pytest |
|
13 | 14 | HashableIndexes, |
14 | 15 | _guess_start_end_dates, |
15 | 16 | get_timeinfo, |
| 17 | + open_dataset_cached, |
16 | 18 | ) |
17 | 19 |
|
18 | 20 |
|
@@ -259,3 +261,39 @@ def test_hashable_indexes_cftime(): |
259 | 261 | assert h1 ^ h2 == set() |
260 | 262 |
|
261 | 263 | 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