11import xarray as xr
2- from obspec_utils import ObstoreReader
2+ from obspec_utils import ObstoreMemCacheReader , ObstoreReader
33from obstore .store import LocalStore
44
55
@@ -8,3 +8,55 @@ def test_local_reader(local_netcdf4_file) -> None:
88 reader = ObstoreReader (store = LocalStore (), path = local_netcdf4_file )
99 ds_obstore = xr .open_dataset (reader , engine = "h5netcdf" )
1010 xr .testing .assert_allclose (ds_fsspec , ds_obstore )
11+
12+
13+ def test_memcache_reader (local_netcdf4_file ) -> None :
14+ """Test that ObstoreMemCacheReader works with xarray."""
15+ ds_fsspec = xr .open_dataset (local_netcdf4_file , engine = "h5netcdf" )
16+ reader = ObstoreMemCacheReader (store = LocalStore (), path = local_netcdf4_file )
17+ ds_obstore = xr .open_dataset (reader , engine = "h5netcdf" )
18+ xr .testing .assert_allclose (ds_fsspec , ds_obstore )
19+
20+
21+ def test_memcache_reader_interface (local_netcdf4_file ) -> None :
22+ """Test that ObstoreMemCacheReader implements the same interface as ObstoreReader."""
23+ store = LocalStore ()
24+ regular_reader = ObstoreReader (store = store , path = local_netcdf4_file )
25+ memcache_reader = ObstoreMemCacheReader (store = store , path = local_netcdf4_file )
26+
27+ # Test readall
28+ data_regular = regular_reader .readall ()
29+ data_memcache = memcache_reader .readall ()
30+ assert data_regular == data_memcache
31+ assert isinstance (data_memcache , bytes )
32+
33+
34+ def test_memcache_reader_multiple_reads (local_netcdf4_file ) -> None :
35+ """Test that ObstoreMemCacheReader can perform multiple reads."""
36+ store = LocalStore ()
37+ reader = ObstoreMemCacheReader (store = store , path = local_netcdf4_file )
38+
39+ # Read the first 100 bytes
40+ chunk1 = reader .read (100 )
41+ assert len (chunk1 ) == 100
42+ assert isinstance (chunk1 , bytes )
43+
44+ # Read the next 100 bytes
45+ chunk2 = reader .read (100 )
46+ assert len (chunk2 ) == 100
47+ assert isinstance (chunk2 , bytes )
48+
49+ # The two chunks should be different (different parts of the file)
50+ assert chunk1 != chunk2
51+
52+ # Test tell
53+ position = reader .tell ()
54+ assert position == 200
55+
56+ # Test seek
57+ reader .seek (0 )
58+ assert reader .tell () == 0
59+
60+ # Re-reading from the beginning should give us the same data
61+ chunk1_again = reader .read (100 )
62+ assert chunk1 == chunk1_again
0 commit comments