-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_hdf.py
More file actions
156 lines (142 loc) · 5.38 KB
/
Copy pathtest_hdf.py
File metadata and controls
156 lines (142 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import shutil
import unittest
try:
from executorlib.standalone.hdf import (
dump,
load,
get_output,
get_runtime,
get_queue_id,
)
skip_h5py_test = False
except ImportError:
skip_h5py_test = True
def my_funct(a, b):
return a + b
@unittest.skipIf(
skip_h5py_test, "h5py is not installed, so the h5io tests are skipped."
)
class TestSharedFunctions(unittest.TestCase):
def test_hdf_mixed(self):
cache_directory = os.path.abspath("executorlib_cache")
os.makedirs(cache_directory, exist_ok=True)
file_name = os.path.join(cache_directory, "test_mixed.h5")
a = 1
b = 2
dump(
file_name=file_name,
data_dict={"fn": my_funct, "args": [a], "kwargs": {"b": b}},
)
data_dict = load(file_name=file_name)
self.assertTrue("fn" in data_dict.keys())
self.assertEqual(data_dict["args"], [a])
self.assertEqual(data_dict["kwargs"], {"b": b})
flag, no_error, output = get_output(file_name=file_name)
self.assertTrue(get_runtime(file_name=file_name) == 0.0)
self.assertFalse(no_error)
self.assertFalse(flag)
self.assertIsNone(output)
def test_hdf_args(self):
cache_directory = os.path.abspath("executorlib_cache")
os.makedirs(cache_directory, exist_ok=True)
file_name = os.path.join(cache_directory, "test_args.h5")
a = 1
b = 2
dump(file_name=file_name, data_dict={"fn": my_funct, "args": [a, b]})
data_dict = load(file_name=file_name)
self.assertTrue("fn" in data_dict.keys())
self.assertEqual(data_dict["args"], [a, b])
self.assertEqual(data_dict["kwargs"], {})
flag, no_error, output = get_output(file_name=file_name)
self.assertTrue(get_runtime(file_name=file_name) == 0.0)
self.assertFalse(flag)
self.assertFalse(no_error)
self.assertIsNone(output)
def test_hdf_kwargs(self):
cache_directory = os.path.abspath("executorlib_cache")
os.makedirs(cache_directory, exist_ok=True)
file_name = os.path.join(cache_directory, "test_kwargs.h5")
a = 1
b = 2
dump(
file_name=file_name,
data_dict={
"fn": my_funct,
"args": (),
"kwargs": {"a": a, "b": b},
"queue_id": 123,
"error_log_file": "error.out",
},
)
data_dict = load(file_name=file_name)
self.assertTrue("fn" in data_dict.keys())
self.assertEqual(data_dict["args"], ())
self.assertEqual(data_dict["kwargs"], {"a": a, "b": b})
self.assertEqual(get_queue_id(file_name=file_name), 123)
flag, no_error, output = get_output(file_name=file_name)
self.assertTrue(get_runtime(file_name=file_name) == 0.0)
self.assertFalse(flag)
self.assertFalse(no_error)
self.assertIsNone(output)
def test_hdf_missing_funct(self):
cache_directory = os.path.abspath("executorlib_cache")
os.makedirs(cache_directory, exist_ok=True)
file_name = os.path.join(cache_directory, "test_missing_funct.h5")
dump(
file_name=file_name,
data_dict={
"queue_id": 123,
},
)
with self.assertRaises(TypeError):
load(file_name=file_name)
def test_hdf_missing_args(self):
cache_directory = os.path.abspath("executorlib_cache")
os.makedirs(cache_directory, exist_ok=True)
file_name = os.path.join(cache_directory, "test_missing_args.h5")
dump(
file_name=file_name,
data_dict={
"fn": my_funct,
},
)
data_dict = load(file_name=file_name)
self.assertTrue("fn" in data_dict.keys())
self.assertEqual(data_dict["args"], ())
flag, no_error, output = get_output(file_name=file_name)
self.assertTrue(get_runtime(file_name=file_name) == 0.0)
self.assertFalse(flag)
self.assertFalse(no_error)
self.assertIsNone(output)
def test_hdf_queue_id(self):
cache_directory = os.path.abspath("executorlib_cache")
os.makedirs(cache_directory, exist_ok=True)
file_name = os.path.join(cache_directory, "test_queue.h5")
queue_id = 123
dump(
file_name=file_name,
data_dict={"queue_id": queue_id},
)
self.assertEqual(get_queue_id(file_name=file_name), 123)
flag, no_error, output = get_output(file_name=file_name)
self.assertTrue(get_runtime(file_name=file_name) == 0.0)
self.assertFalse(flag)
self.assertFalse(no_error)
self.assertIsNone(output)
def test_hdf_error(self):
cache_directory = os.path.abspath("executorlib_cache")
os.makedirs(cache_directory, exist_ok=True)
file_name = os.path.join(cache_directory, "test_error.h5")
error = ValueError()
dump(
file_name=file_name,
data_dict={"error": error},
)
flag, no_error, output = get_output(file_name=file_name)
self.assertTrue(get_runtime(file_name=file_name) == 0.0)
self.assertTrue(flag)
self.assertFalse(no_error)
self.assertTrue(isinstance(output, error.__class__))
def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)