1+ import datetime
12import io
23
34import ase
89from ase .io import read , write
910import numpy as np
1011
11- from abcd .model import AbstractModel
12+ from abcd .model import AbstractModel , Hasher
1213from ase .calculators .lj import LennardJones
1314
1415
@@ -238,7 +239,6 @@ def test_write_and_read(store_calc):
238239 "hash" ,
239240 "modified" ,
240241 "uploaded" ,
241- "hash_structure" , # see issue #118
242242 }:
243243 assert (
244244 abcd_data [key ] == abcd_data_after_read [key ]
@@ -247,10 +247,75 @@ def test_write_and_read(store_calc):
247247 # expected differences - n.b. order of calls above
248248 assert abcd_data_after_read ["modified" ] > abcd_data ["modified" ]
249249 assert abcd_data_after_read ["uploaded" ] > abcd_data ["uploaded" ]
250- assert abcd_data_after_read ["hash" ] != abcd_data ["hash" ]
251250
252251 # expect results to match within fp precision
253252 for key in set (abcd_data .results_keys ):
254253 assert abcd_data [key ] == approx (
255254 np .array (abcd_data_after_read [key ])
256255 ), f"{ key } 's value does not match"
256+
257+
258+ def test_hash_update ():
259+ """Test hash can be updated after initialisation."""
260+ hasher_1 = Hasher ()
261+
262+ init_hash = hasher_1 ()
263+ hasher_1 .update ("Test value" )
264+ assert hasher_1 () != init_hash
265+
266+
267+ @pytest .mark .parametrize (
268+ "data" ,
269+ [
270+ 1296 ,
271+ 3.14 ,
272+ [1 , 2 , 3 ],
273+ (4 , 5 , 6 ),
274+ {"a" : "value" },
275+ datetime .datetime .now (datetime .timezone .utc ),
276+ b"test" ,
277+ ],
278+ )
279+ def test_hash_data_types (data ):
280+ """Test updating hash for different data types."""
281+ hasher_1 = Hasher ()
282+ hasher_1 .update ("Test value" )
283+ updated_hash = hasher_1 ()
284+
285+ hasher_1 .update (data )
286+ assert updated_hash != hasher_1 ()
287+
288+
289+ def test_second_hash_init ():
290+ """Test second hash is initialised correctly."""
291+ hasher_1 = Hasher ()
292+
293+ init_hash = hasher_1 ()
294+ hasher_1 .update ("Test value" )
295+
296+ hasher_2 = Hasher ()
297+ assert hasher_2 () == init_hash
298+
299+
300+ @pytest .mark .parametrize (
301+ "data" ,
302+ [
303+ 1296 ,
304+ 3.14 ,
305+ [1 , 2 , 3 ],
306+ (4 , 5 , 6 ),
307+ {"a" : "value" },
308+ datetime .datetime .now (datetime .timezone .utc ),
309+ b"test" ,
310+ ],
311+ )
312+ def test_consistent_hash (data ):
313+ """Test two hashers agree with same data."""
314+ hasher_1 = Hasher ()
315+ hasher_1 .update ("Test value" )
316+ hasher_1 .update (data )
317+
318+ hasher_2 = Hasher ()
319+ hasher_2 .update ("Test value" )
320+ hasher_2 .update (data )
321+ assert hasher_1 () == hasher_2 ()
0 commit comments