44import cloudpickle as cp
55
66from .utils import multiply
7- from .. import helpers
7+ from ..helpers import hash_value , hash_function , save , create_pyscript
88from .. import helpers_file
9+ from ..specs import File
910
1011
1112def test_save (tmpdir ):
1213 outdir = Path (tmpdir )
1314 with pytest .raises (ValueError ):
14- helpers . save (tmpdir )
15+ save (tmpdir )
1516 foo = multiply (name = "mult" , x = 1 , y = 2 )
1617 # save task
17- helpers . save (outdir , task = foo )
18+ save (outdir , task = foo )
1819 del foo
1920 # load saved task
2021 task_pkl = outdir / "_task.pklz"
@@ -24,7 +25,7 @@ def test_save(tmpdir):
2425 # execute task and save result
2526 res = foo ()
2627 assert res .output .out == 2
27- helpers . save (outdir , result = res )
28+ save (outdir , result = res )
2829 del res
2930 # load saved result
3031 res_pkl = outdir / "_result.pklz"
@@ -35,10 +36,10 @@ def test_save(tmpdir):
3536def test_create_pyscript (tmpdir ):
3637 outdir = Path (tmpdir )
3738 with pytest .raises (Exception ):
38- helpers . create_pyscript (outdir , "checksum" )
39+ create_pyscript (outdir , "checksum" )
3940 foo = multiply (name = "mult" , x = 1 , y = 2 )
40- helpers . save (outdir , task = foo )
41- pyscript = helpers . create_pyscript (outdir , foo .checksum )
41+ save (outdir , task = foo )
42+ pyscript = create_pyscript (outdir , foo .checksum )
4243 assert pyscript .exists ()
4344
4445
@@ -64,11 +65,62 @@ def test_hashfun_float():
6465 == pi_50 .as_integer_ratio ()
6566 == pi_15 .as_integer_ratio ()
6667 )
67- assert (
68- helpers .hash_function (math .pi )
69- == helpers .hash_function (pi_15 )
70- == helpers .hash_function (pi_50 )
71- )
68+ assert hash_function (math .pi ) == hash_function (pi_15 ) == hash_function (pi_50 )
7269 # comparing for x that have different x.as_integer_ratio()
7370 assert math .pi .as_integer_ratio () != pi_10 .as_integer_ratio ()
74- assert helpers .hash_function (math .pi ) != helpers .hash_function (pi_10 )
71+ assert hash_function (math .pi ) != hash_function (pi_10 )
72+
73+
74+ def test_hash_value_dict ():
75+ dict1 = {"a" : 10 , "b" : 5 }
76+ dict2 = {"b" : 5 , "a" : 10 }
77+ assert (
78+ hash_value (dict1 )
79+ == hash_value (dict2 )
80+ == [["a" , hash_value (10 )], ["b" , hash_value (5 )]]
81+ == [["a" , 10 ], ["b" , 5 ]]
82+ )
83+
84+
85+ def test_hash_value_list_tpl ():
86+ lst = [2 , 5.6 , "ala" ]
87+ tpl = (2 , 5.6 , "ala" )
88+ assert hash_value (lst ) == [hash_value (2 ), hash_value (5.6 ), hash_value ("ala" )] == lst
89+ assert hash_value (lst ) == hash_value (tpl )
90+
91+
92+ def test_hash_value_list_dict ():
93+ lst = [2 , {"a" : "ala" , "b" : 1 }]
94+ hash_value (lst )
95+ assert (
96+ hash_value (lst )
97+ == [hash_value (2 ), hash_value ([["a" , "ala" ], ["b" , 1 ]])]
98+ == [2 , [["a" , "ala" ], ["b" , 1 ]]]
99+ )
100+
101+
102+ def test_hash_value_files (tmpdir ):
103+ file_1 = tmpdir .join ("file_1.txt" )
104+ file_2 = tmpdir .join ("file_2.txt" )
105+ with open (file_1 , "w" ) as f :
106+ f .write ("hello" )
107+ with open (file_2 , "w" ) as f :
108+ f .write ("hello" )
109+
110+ assert hash_value (file_1 , tp = File ) == hash_value (file_2 , tp = File )
111+ assert hash_value (file_1 , tp = str ) != hash_value (file_2 , tp = str )
112+ assert hash_value (file_1 ) != hash_value (file_2 )
113+
114+
115+ def test_hash_value_files_list (tmpdir ):
116+ file_1 = tmpdir .join ("file_1.txt" )
117+ file_2 = tmpdir .join ("file_2.txt" )
118+ with open (file_1 , "w" ) as f :
119+ f .write ("hello" )
120+ with open (file_2 , "w" ) as f :
121+ f .write ("hi" )
122+
123+ assert hash_value ([file_1 , file_2 ], tp = File ) == [
124+ hash_value (file_1 , tp = File ),
125+ hash_value (file_2 , tp = File ),
126+ ]
0 commit comments