4
4
import cloudpickle as cp
5
5
6
6
from .utils import multiply
7
- from .. import helpers
7
+ from ..helpers import hash_value , hash_function , save , create_pyscript
8
8
from .. import helpers_file
9
+ from ..specs import File
9
10
10
11
11
12
def test_save (tmpdir ):
12
13
outdir = Path (tmpdir )
13
14
with pytest .raises (ValueError ):
14
- helpers . save (tmpdir )
15
+ save (tmpdir )
15
16
foo = multiply (name = "mult" , x = 1 , y = 2 )
16
17
# save task
17
- helpers . save (outdir , task = foo )
18
+ save (outdir , task = foo )
18
19
del foo
19
20
# load saved task
20
21
task_pkl = outdir / "_task.pklz"
@@ -24,7 +25,7 @@ def test_save(tmpdir):
24
25
# execute task and save result
25
26
res = foo ()
26
27
assert res .output .out == 2
27
- helpers . save (outdir , result = res )
28
+ save (outdir , result = res )
28
29
del res
29
30
# load saved result
30
31
res_pkl = outdir / "_result.pklz"
@@ -35,10 +36,10 @@ def test_save(tmpdir):
35
36
def test_create_pyscript (tmpdir ):
36
37
outdir = Path (tmpdir )
37
38
with pytest .raises (Exception ):
38
- helpers . create_pyscript (outdir , "checksum" )
39
+ create_pyscript (outdir , "checksum" )
39
40
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 )
42
43
assert pyscript .exists ()
43
44
44
45
@@ -50,3 +51,76 @@ def test_hash_file(tmpdir):
50
51
helpers_file .hash_file (outdir / "test.file" )
51
52
== "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
52
53
)
54
+
55
+
56
+ def test_hashfun_float ():
57
+ import math
58
+
59
+ pi_50 = 3.14159265358979323846264338327950288419716939937510
60
+ pi_15 = 3.141592653589793
61
+ pi_10 = 3.1415926536
62
+ # comparing for x that have the same x.as_integer_ratio()
63
+ assert (
64
+ math .pi .as_integer_ratio ()
65
+ == pi_50 .as_integer_ratio ()
66
+ == pi_15 .as_integer_ratio ()
67
+ )
68
+ assert hash_function (math .pi ) == hash_function (pi_15 ) == hash_function (pi_50 )
69
+ # comparing for x that have different x.as_integer_ratio()
70
+ assert math .pi .as_integer_ratio () != pi_10 .as_integer_ratio ()
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