99import emsarray .operations .cache
1010
1111# Sha1
12- empty_sha1_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
1312attr_hash_lon = "4151c4ac3e0e913dd2c5520c0b2c33e6f20eb004"
1413str_hash = '67a0425c3bcb8a47ccc39615e59ab489d0c4b6a1'
1514int_hash = '7b08e025e311c3dfcf5179b67c0fdc08e73de261'
2423cache_key_hash_ugrid_mesh2d_one_indexed = "045be69d5ef70b20f26dc9a14e6cea8a11fe3cc9a3c4c4b303aed6f22206ac7a"
2524
2625
27- @pytest .fixture
28- def emsarray_version ():
29- return '1.0.0'
26+ @pytest .fixture (autouse = True )
27+ def override_version (monkeypatch : pytest .MonkeyPatch ) -> None :
28+ """
29+ The cache key depends on the emsarray version.
30+ This fixture overrides the version with a constant value
31+ so we don't have to regenerate the test cache keys every time we release a new version.
32+ """
33+ monkeypatch .setattr (emsarray , '__version__' , '1.0.0' )
3034
3135
3236def test_hash_attributes (datasets : pathlib .Path ):
3337 dataset = emsarray .open_dataset (datasets / 'cfgrid1d.nc' )
3438
3539 m_lon = hashlib .sha1 ()
3640
37- assert m_lon .hexdigest () == empty_sha1_hash
38-
3941 emsarray .operations .cache .hash_attributes (m_lon , dataset .variables ['lon' ].attrs )
4042
4143 result_hash_lon = m_lon .hexdigest ()
4244
4345 assert result_hash_lon is not None
4446
45- assert result_hash_lon != empty_sha1_hash
46-
4747 assert result_hash_lon == attr_hash_lon
4848
4949
5050def test_hash_string ():
5151 hash = hashlib .sha1 ()
5252
53- assert hash .hexdigest () == empty_sha1_hash
54-
5553 emsarray .operations .cache .hash_string (hash , "1234" )
5654
5755 result_hash = hash .hexdigest ()
5856
5957 assert result_hash is not None
6058
61- assert result_hash != empty_sha1_hash
62-
6359 assert result_hash == str_hash
6460
6561
6662def test_hash_int ():
6763 hash = hashlib .sha1 ()
6864
69- assert hash .hexdigest () == empty_sha1_hash
70-
7165 emsarray .operations .cache .hash_int (hash , 1234 )
7266
7367 result_hash = hash .hexdigest ()
7468
7569 assert result_hash is not None
7670
77- assert result_hash != empty_sha1_hash
78-
7971 assert result_hash == int_hash
8072
8173
82- def test_cache_key_cfgrid1d (datasets : pathlib .Path , monkeypatch , emsarray_version ):
83- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
84-
74+ def test_cache_key_cfgrid1d (datasets : pathlib .Path ):
8575 dataset_cf = emsarray .open_dataset (datasets / 'cfgrid1d.nc' )
8676
8777 result_cache_key_cf = emsarray .operations .cache .make_cache_key (dataset_cf )
@@ -91,9 +81,7 @@ def test_cache_key_cfgrid1d(datasets: pathlib.Path, monkeypatch, emsarray_versio
9181 assert result_cache_key_cf == cache_key_hash_cf1d
9282
9383
94- def test_cache_key_cfgrid2d (datasets : pathlib .Path , monkeypatch , emsarray_version ):
95- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
96-
84+ def test_cache_key_cfgrid2d (datasets : pathlib .Path ):
9785 dataset_cf = emsarray .open_dataset (datasets / 'cfgrid2d.nc' )
9886
9987 result_cache_key_cf = emsarray .operations .cache .make_cache_key (dataset_cf )
@@ -103,9 +91,7 @@ def test_cache_key_cfgrid2d(datasets: pathlib.Path, monkeypatch, emsarray_versio
10391 assert result_cache_key_cf == cache_key_hash_cf2d
10492
10593
106- def test_cache_key_shoc_standard (datasets : pathlib .Path , monkeypatch , emsarray_version ):
107- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
108-
94+ def test_cache_key_shoc_standard (datasets : pathlib .Path ):
10995 dataset_cf = emsarray .open_dataset (datasets / 'shoc_standard.nc' )
11096
11197 result_cache_key_cf = emsarray .operations .cache .make_cache_key (dataset_cf )
@@ -115,9 +101,7 @@ def test_cache_key_shoc_standard(datasets: pathlib.Path, monkeypatch, emsarray_v
115101 assert result_cache_key_cf == cache_key_hash_shoc_standard
116102
117103
118- def test_cache_key_ugrid_mesh2d (datasets : pathlib .Path , monkeypatch , emsarray_version ):
119- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
120-
104+ def test_cache_key_ugrid_mesh2d (datasets : pathlib .Path ):
121105 dataset_cf = emsarray .open_dataset (datasets / 'ugrid_mesh2d.nc' )
122106
123107 result_cache_key_cf = emsarray .operations .cache .make_cache_key (dataset_cf )
@@ -127,9 +111,7 @@ def test_cache_key_ugrid_mesh2d(datasets: pathlib.Path, monkeypatch, emsarray_ve
127111 assert result_cache_key_cf == cache_key_hash_ugrid_mesh2d
128112
129113
130- def test_cache_key_ugrid_mesh2d_one_indexed (datasets : pathlib .Path , monkeypatch , emsarray_version ):
131- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
132-
114+ def test_cache_key_ugrid_mesh2d_one_indexed (datasets : pathlib .Path ):
133115 dataset_cf = emsarray .open_dataset (datasets / 'ugrid_mesh2d_one_indexed.nc' )
134116
135117 result_cache_key_cf = emsarray .operations .cache .make_cache_key (dataset_cf )
@@ -139,9 +121,7 @@ def test_cache_key_ugrid_mesh2d_one_indexed(datasets: pathlib.Path, monkeypatch,
139121 assert result_cache_key_cf == cache_key_hash_ugrid_mesh2d_one_indexed
140122
141123
142- def test_cache_key_is_deterministic (datasets : pathlib .Path , monkeypatch , emsarray_version ):
143- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
144-
124+ def test_cache_key_is_deterministic (datasets : pathlib .Path ):
145125 dataset_cf = emsarray .open_dataset (datasets / 'cfgrid1d.nc' )
146126
147127 result_cache_key_cf_1 = emsarray .operations .cache .make_cache_key (dataset_cf )
@@ -152,9 +132,7 @@ def test_cache_key_is_deterministic(datasets: pathlib.Path, monkeypatch, emsarra
152132 assert result_cache_key_cf_1 == cache_key_hash_cf1d
153133
154134
155- def test_hash_attributes_is_deterministic (datasets : pathlib .Path , monkeypatch , emsarray_version ):
156- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
157-
135+ def test_hash_attributes_is_deterministic (datasets : pathlib .Path ):
158136 dataset = emsarray .open_dataset (datasets / 'cfgrid1d.nc' )
159137
160138 m_lon_1 = hashlib .sha1 ()
@@ -171,9 +149,7 @@ def test_hash_attributes_is_deterministic(datasets: pathlib.Path, monkeypatch, e
171149 assert result_hash_lon_1 == attr_hash_lon
172150
173151
174- def test_cache_key_gives_unique_keys (datasets : pathlib .Path , monkeypatch , emsarray_version ):
175- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
176-
152+ def test_cache_key_gives_unique_keys (datasets : pathlib .Path ):
177153 dataset_cf = emsarray .open_dataset (datasets / 'cfgrid1d.nc' )
178154 dataset_ugrid = emsarray .open_dataset (datasets / 'ugrid_mesh2d.nc' )
179155
@@ -189,17 +165,12 @@ def test_cache_key_gives_unique_keys(datasets: pathlib.Path, monkeypatch, emsarr
189165 assert result_cache_key_ugrid == cache_key_hash_ugrid_mesh2d
190166
191167
192- def test_hash_attributes_gives_unique_keys (datasets : pathlib .Path , monkeypatch , emsarray_version ):
193- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
194-
168+ def test_hash_attributes_gives_unique_keys (datasets : pathlib .Path ):
195169 dataset = emsarray .open_dataset (datasets / 'cfgrid1d.nc' )
196170
197171 m_lon = hashlib .sha1 ()
198172 m_lat = hashlib .sha1 ()
199173
200- assert m_lon .hexdigest () == empty_sha1_hash
201- assert m_lat .hexdigest () == empty_sha1_hash
202-
203174 emsarray .operations .cache .hash_attributes (m_lon , dataset .variables ['lon' ].attrs )
204175 emsarray .operations .cache .hash_attributes (m_lat , dataset .variables ['lat' ].attrs )
205176
@@ -215,9 +186,7 @@ def test_hash_attributes_gives_unique_keys(datasets: pathlib.Path, monkeypatch,
215186 assert result_hash_lat == attr_hash_lat
216187
217188
218- def test_cache_key_cfgrid1d_sha1 (datasets : pathlib .Path , monkeypatch , emsarray_version ):
219- monkeypatch .setattr (emsarray , "__version__" , emsarray_version )
220-
189+ def test_cache_key_cfgrid1d_sha1 (datasets : pathlib .Path ):
221190 dataset_cf = emsarray .open_dataset (datasets / 'cfgrid1d.nc' )
222191
223192 result_cache_key_cf = emsarray .operations .cache .make_cache_key (dataset_cf , hashlib .sha1 ())
0 commit comments