-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_reference.py
More file actions
520 lines (404 loc) · 16.7 KB
/
test_reference.py
File metadata and controls
520 lines (404 loc) · 16.7 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
from typing import Union
import pytest
def test_access():
from jobflow import OutputReference
# test empty
ref = OutputReference("123")
assert ref.attributes == ()
# test bad init
with pytest.raises(
ValueError, match="Unrecognised attribute type 'x' for attribute '1'"
):
OutputReference("123", (("x", 1),))
new_ref = ref.a
assert new_ref.attributes == (("a", "a"),)
assert new_ref.uuid == "123"
assert isinstance(new_ref, OutputReference)
new_ref = ref["a"]
assert new_ref.attributes == (("i", "a"),)
assert new_ref.uuid == "123"
assert isinstance(new_ref, OutputReference)
new_ref = ref[1]
assert new_ref.attributes == (("i", 1),)
assert new_ref.uuid == "123"
assert isinstance(new_ref, OutputReference)
# test filled
ref = OutputReference("123", (("a", "b"),))
new_ref = ref.a
assert new_ref.attributes == (("a", "b"), ("a", "a"))
assert new_ref.uuid == "123"
assert isinstance(new_ref, OutputReference)
new_ref = ref["a"]
assert new_ref.attributes == (("a", "b"), ("i", "a"))
assert new_ref.uuid == "123"
assert isinstance(new_ref, OutputReference)
with pytest.raises(AttributeError):
_ = ref.args
with pytest.raises(AttributeError):
_ = ref.__fake_variable
def test_get_set_attr():
from jobflow import OutputReference
ref = OutputReference("123")
# these should fail
with pytest.raises(TypeError):
ref["a"] = 1
with pytest.raises(TypeError):
ref[1] = 1
with pytest.raises(TypeError):
ref.a = 1
ref.uuid = 1
assert ref.uuid == 1
def test_repr():
from jobflow import OutputReference
ref = OutputReference("123")
assert str(ref) == "OutputReference(123)"
ref = OutputReference("123", (("a", "a"),))
assert str(ref) == "OutputReference(123, .a)"
ref = OutputReference("123", (("a", "a"), ("i", 1)))
assert str(ref) == "OutputReference(123, .a, [1])"
def test_hash():
from jobflow import OutputReference
assert hash(OutputReference("123")) == hash(OutputReference("123"))
assert hash(OutputReference("123", (("i", 1), ("i", 2)))) == hash(
OutputReference("123", (("i", 1), ("i", 2)))
)
assert hash(OutputReference("123", (("a", "b"), ("i", 2)))) == hash(
OutputReference("123", (("a", "b"), ("i", 2)))
)
assert hash(OutputReference("123", (("a", "b"), ("i", "2")))) == hash(
OutputReference("123", (("a", "b"), ("i", "2")))
)
def test_eq():
from jobflow import OutputReference
assert OutputReference("123") == OutputReference("123")
assert OutputReference("123") != OutputReference("1234")
assert OutputReference("123", (("i", 1),)) == OutputReference("123", (("i", 1),))
assert OutputReference("123", (("i", "a"),)) == OutputReference(
"123", (("i", "a"),)
)
assert OutputReference("123", (("a", "a"),)) == OutputReference(
"123", (("a", "a"),)
)
assert OutputReference("123", (("a", "a"), ("i", "b"))) == OutputReference(
"123", (("a", "a"), ("i", "b"))
)
assert OutputReference("123", (("i", 1),)) != OutputReference("1234", (("i", 1),))
assert OutputReference("123", (("i", 1),)) != OutputReference("123", (("i", 2),))
assert OutputReference("123", (("i", 1),)) != OutputReference(
"123", (("i", 2), ("i", 3), ("i", 4))
)
assert OutputReference("123", (("i", 1),)) != "OutputReference(123, [1])"
def test_as_dict():
from jobflow import OutputReference
ref = OutputReference("123")
d = ref.as_dict()
assert d["@class"] == "OutputReference"
assert d["@module"] == "jobflow.core.reference"
assert d["uuid"] == "123"
ref = OutputReference("123", (("a", "a"), ("i", "b")))
d = ref.as_dict()
assert d["@class"] == "OutputReference"
assert d["@module"] == "jobflow.core.reference"
assert d["uuid"] == "123"
assert d["attributes"] == (("a", "a"), ("i", "b"))
def test_set_uuid():
from jobflow import OutputReference
ref = OutputReference("123")
new_ref = ref.set_uuid("321")
assert ref.uuid == "321"
assert new_ref.uuid == "321"
ref = OutputReference("123")
new_ref = ref.set_uuid("321", inplace=False)
assert ref.uuid == "123"
assert new_ref.uuid == "321"
def test_schema():
from pydantic import BaseModel
from jobflow import OutputReference
class InnerSchema(BaseModel):
n: float
class MediumSchema(BaseModel):
s: str
nested: InnerSchema
nested_opt: InnerSchema = None
nested_u: Union[InnerSchema, dict] # noqa: FA100
nested_l: list[InnerSchema]
nested_d: dict[str, InnerSchema]
class MySchema(BaseModel):
number: int
name: str
nested: MediumSchema
ref = OutputReference("123", output_schema=MySchema)
assert ref.attributes == ()
# check valid schema access works
new_ref = ref.number
assert new_ref.uuid == "123"
assert new_ref.output_schema is None
new_ref = ref["name"]
assert new_ref.uuid == "123"
assert new_ref.output_schema is None
with pytest.raises(AttributeError):
_ = ref.a.uuid
with pytest.raises(AttributeError):
_ = ref["a"].uuid
with pytest.raises(AttributeError):
_ = ref[1].uuid
# check valid nested schemas
assert ref.nested.s.uuid == "123"
with pytest.raises(AttributeError):
_ = ref.nested.m.uuid
assert ref.nested.nested.n.uuid == "123"
with pytest.raises(AttributeError):
_ = ref.nested.nested.m.uuid
assert ref.nested.nested_opt.n.uuid == "123"
with pytest.raises(AttributeError):
_ = ref.nested.nested_opt.m.uuid
# Union, List and Dict are currently not recognized by their inner type
# but check that there is no problem with them
assert ref.nested.nested_u.n.uuid == "123"
assert ref.nested.nested_l[0].n.uuid == "123"
assert ref.nested.nested_d["a"].n.uuid == "123"
def test_resolve(memory_jobstore):
from jobflow import OnMissing, OutputReference
ref = OutputReference("123")
# fail if cache or store not provided
with pytest.raises(TypeError):
ref.resolve()
# test on missing
assert ref.resolve(memory_jobstore, on_missing=OnMissing.NONE) is None
assert ref.resolve(memory_jobstore, on_missing=OnMissing.PASS) == ref
with pytest.raises(ValueError, match="Could not resolve reference"):
ref.resolve(memory_jobstore, on_missing=OnMissing.ERROR)
# resolve using store
memory_jobstore.update({"uuid": "123", "index": 1, "output": 101})
assert ref.resolve(store=memory_jobstore) == 101
# resolve using store and empty cache
cache = {}
assert ref.resolve(store=memory_jobstore, cache=cache) == 101
assert cache["123"][1] == 101
# check cache supersedes store
cache = {"123": {1: "xyz"}}
assert ref.resolve(store=memory_jobstore, cache=cache) == "xyz"
assert cache["123"][1] == "xyz"
# test indexing
ref = OutputReference("123", (("i", "a"), ("i", 1)))
memory_jobstore.update({"uuid": "123", "index": 1, "output": {"a": [5, 6, 7]}})
assert ref.resolve(memory_jobstore) == 6
# test attribute access
ref = OutputReference("123", (("a", "__module__"),))
memory_jobstore.update({"uuid": "123", "index": 1, "output": OutputReference})
assert ref.resolve(memory_jobstore) == "jobflow.core.reference"
# test missing attribute throws error
ref = OutputReference("123", (("a", "b"),))
memory_jobstore.update({"uuid": "123", "index": 1, "output": [1234]})
with pytest.raises(AttributeError):
ref.resolve(memory_jobstore)
# test missing index throws error
ref = OutputReference("123", (("i", "b"),))
with pytest.raises(TypeError):
ref.resolve(memory_jobstore)
def test_resolve_references(memory_jobstore):
from jobflow import OnMissing, OutputReference
from jobflow.core.reference import resolve_references
# resolve single using store
ref = OutputReference("123")
memory_jobstore.update({"uuid": "123", "index": 1, "output": "xyz"})
output = resolve_references([ref], memory_jobstore)
assert len(output) == 1
assert output[ref] == "xyz"
# resolve multiple using cache
ref1 = OutputReference("123")
ref2 = OutputReference("1234")
memory_jobstore.update({"uuid": "1234", "index": 1, "output": 101})
output = resolve_references([ref1, ref2], memory_jobstore)
assert len(output) == 2
assert output[ref1] == "xyz"
assert output[ref2] == 101
# resolve group using cache
ref1 = OutputReference("123", (("i", "a"),))
ref2 = OutputReference("123", (("i", "b"),))
ref3 = OutputReference("1234")
memory_jobstore.update(
{"uuid": "123", "index": 1, "output": {"a": "xyz", "b": "abc"}}
)
output = resolve_references([ref1, ref2, ref3], memory_jobstore)
assert len(output) == 3
assert output[ref1] == "xyz"
assert output[ref2] == "abc"
assert output[ref3] == 101
# test on missing
ref1 = OutputReference("123")
ref2 = OutputReference("12345")
memory_jobstore.update({"uuid": "123", "index": 1, "output": "xyz"})
output = resolve_references(
[ref1, ref2], memory_jobstore, on_missing=OnMissing.PASS
)
assert len(output) == 2
assert output[ref1] == "xyz"
assert output[ref2] == ref2
ref2 = OutputReference("12345")
with pytest.raises(ValueError, match="Could not resolve reference"):
resolve_references([ref1, ref2], memory_jobstore, on_missing=OnMissing.ERROR)
# resolve using store and empty cache
cache = {}
output = resolve_references([ref], memory_jobstore, cache=cache)
assert len(output) == 1
assert output[ref] == "xyz"
# check cache supersedes store
cache = {"123": {1: 101}}
output = resolve_references([ref], memory_jobstore, cache=cache)
assert len(output) == 1
assert output[ref] == 101
# test attributes
ref = OutputReference("123", (("i", "a"), ("i", 1)))
memory_jobstore.update({"uuid": "123", "index": 1, "output": {"a": [5, 6, 7]}})
output = resolve_references([ref], memory_jobstore)
assert output[ref] == 6
ref = OutputReference("123", (("a", "__module__"),))
memory_jobstore.update({"uuid": "123", "index": 1, "output": OutputReference})
output = resolve_references([ref], memory_jobstore)
assert output[ref] == "jobflow.core.reference"
def test_find_and_get_references():
from jobflow.core.reference import OutputReference, find_and_get_references
ref1 = OutputReference("123")
ref2 = OutputReference("1234", (("a", "a"),))
# test single reference
assert find_and_get_references(ref1) == (ref1,)
# test list and tuple of references
assert find_and_get_references([ref1]) == (ref1,)
assert set(find_and_get_references([ref1, ref2])) == {ref1, ref2}
assert set(find_and_get_references((ref1, ref2))) == {ref1, ref2}
# test dictionary dictionary values
assert find_and_get_references({"a": ref1}) == (ref1,)
assert set(find_and_get_references({"a": ref1, "b": ref2})) == {ref1, ref2}
# test nested
assert set(find_and_get_references({"a": [ref1, ref2]})) == {ref1, ref2}
assert set(find_and_get_references([{"a": ref1}, {"b": ref2}])) == {ref1, ref2}
def test_find_and_resolve_references(memory_jobstore):
from monty.json import MSONable
from jobflow.core.reference import (
OnMissing,
OutputReference,
find_and_resolve_references,
)
global WithProp
class WithProp(MSONable):
def __init__(self, x):
self.x = x
@property
def plus(self):
return self.x + 1
ref1 = OutputReference("123")
ref2 = OutputReference("1234", (("i", "a"),))
ref_attr = OutputReference("123456", (("a", "x"),))
ref_prop = OutputReference("123456", (("a", "plus"),))
memory_jobstore.update({"uuid": "123", "index": 1, "output": 101})
memory_jobstore.update({"uuid": "1234", "index": 1, "output": {"a": "xyz", "b": 5}})
memory_jobstore.update({"uuid": "123456", "index": 1, "output": WithProp(1)})
# test no reference
assert find_and_resolve_references(arg=True, store=memory_jobstore) is True
assert find_and_resolve_references("xyz", memory_jobstore) == "xyz"
assert (
find_and_resolve_references("xyz", memory_jobstore, deserialize=False) == "xyz"
)
assert find_and_resolve_references([101], memory_jobstore) == [101]
assert find_and_resolve_references([101], memory_jobstore, deserialize=False) == [
101
]
# test single reference
assert find_and_resolve_references(ref1, memory_jobstore) == 101
assert find_and_resolve_references(ref1, memory_jobstore, deserialize=False) == 101
# test single reference with object
assert find_and_resolve_references(ref_attr, memory_jobstore) == 1
assert find_and_resolve_references(ref_prop, memory_jobstore) == 2
assert (
find_and_resolve_references(ref_attr, memory_jobstore, deserialize=False) == 1
)
with pytest.raises(KeyError, match="plus"):
find_and_resolve_references(ref_prop, memory_jobstore, deserialize=False)
# test list and tuple of references
assert find_and_resolve_references([ref1], memory_jobstore) == [101]
assert find_and_resolve_references([ref1, ref2], memory_jobstore) == [101, "xyz"]
assert find_and_resolve_references(
[ref1, ref2], memory_jobstore, deserialize=False
) == [101, "xyz"]
# test dictionary values
output = find_and_resolve_references({"a": ref1}, memory_jobstore)
assert output == {"a": 101}
output = find_and_resolve_references({"a": ref1, "b": ref2}, memory_jobstore)
assert output == {
"a": 101,
"b": "xyz",
}
# test nested
output = find_and_resolve_references({"a": [ref1, ref2]}, memory_jobstore)
assert output == {"a": [101, "xyz"]}
output = find_and_resolve_references([{"a": ref1}, {"b": ref2}], memory_jobstore)
assert output == [
{"a": 101},
{"b": "xyz"},
]
# test store, blank cache
cache = {}
output = find_and_resolve_references(
{"a": [ref1, ref2]}, store=memory_jobstore, cache=cache
)
assert output == {"a": [101, "xyz"]}
assert cache["123"][1] == 101
# test cache overrides store
output = find_and_resolve_references(
{"a": [ref1, ref2]}, store=memory_jobstore, cache={"123": {1: 1}}
)
assert output == {"a": [1, "xyz"]}
# test on missing
ref3 = OutputReference("12345", (("i", "a"),))
output = find_and_resolve_references(
[ref1, ref3], memory_jobstore, on_missing=OnMissing.PASS
)
assert output == [101, ref3]
output = find_and_resolve_references(
[ref1, ref3], memory_jobstore, on_missing=OnMissing.NONE
)
assert output == [101, None]
with pytest.raises(ValueError, match="Could not resolve reference"):
find_and_resolve_references(
[ref1, ref3], memory_jobstore, on_missing=OnMissing.ERROR
)
with pytest.raises(ValueError, match="Could not resolve reference"):
find_and_resolve_references(
[ref1, ref3], memory_jobstore, on_missing=OnMissing.ERROR, deserialize=False
)
def test_circular_resolve(memory_jobstore):
from jobflow.core.reference import OutputReference
# test catching circular resolve failure
ref1 = OutputReference("12345")
task_data = {"uuid": ref1.uuid, "index": 1, "output": ref1}
memory_jobstore.update(task_data)
with pytest.raises(RuntimeError):
ref1.resolve(memory_jobstore)
def test_reference_in_output(memory_jobstore):
from jobflow.core.reference import OnMissing, OutputReference
# test resolvable reference in job output
ref1 = OutputReference("12345")
ref2 = OutputReference("56789")
task_data1 = {"uuid": ref1.uuid, "index": 1, "output": ref2}
task_data2 = {"uuid": ref2.uuid, "index": 1, "output": "xyz"}
memory_jobstore.update(task_data1)
memory_jobstore.update(task_data2)
assert ref1.resolve(memory_jobstore) == "xyz"
# test missing reference in output
ref1 = OutputReference("12345")
ref2 = OutputReference("999")
task_data = {"uuid": ref1.uuid, "index": 1, "output": ref2}
memory_jobstore.update(task_data)
assert ref1.resolve(memory_jobstore, on_missing=OnMissing.NONE) is None
assert ref1.resolve(memory_jobstore, on_missing=OnMissing.PASS) == ref2
with pytest.raises(ValueError, match="Could not resolve reference"):
ref1.resolve(memory_jobstore, on_missing=OnMissing.ERROR)
def test_not_iterable():
from jobflow.core.reference import OutputReference
ref = OutputReference("12345")
with pytest.raises(TypeError):
next(ref)
with pytest.raises(TypeError):
for _ in ref:
pass