Skip to content

Commit a1be9b9

Browse files
committed
Add tests for serializing ModelEvaluationContext
Signed-off-by: Nijat Khanbabayev <[email protected]>
1 parent b80fafe commit a1be9b9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import json
2+
from datetime import date
3+
4+
from ccflow import DateContext
5+
from ccflow.callable import ModelEvaluationContext
6+
from ccflow.evaluators import GraphEvaluator, LoggingEvaluator, MultiEvaluator
7+
from ccflow.tests.evaluators.util import NodeModel
8+
9+
# NOTE: for these tests, round-tripping via JSON does not work
10+
# because the ModelEvaluationContext just has an InstanceOf validation check
11+
# and so we do not actually construct a full MEC on load.
12+
13+
14+
def _make_nested_mec(model):
15+
ctx = DateContext(date=date(2022, 1, 1))
16+
mec = model.__call__.get_evaluation_context(model, ctx)
17+
assert isinstance(mec, ModelEvaluationContext)
18+
# ensure nested: outer model is an evaluator, inner is a ModelEvaluationContext
19+
assert isinstance(mec.context, ModelEvaluationContext)
20+
return mec
21+
22+
23+
def test_mec_model_dump_basic():
24+
m = NodeModel()
25+
mec = _make_nested_mec(m)
26+
27+
d = mec.model_dump()
28+
assert isinstance(d, dict)
29+
assert "fn" in d and "model" in d and "context" in d and "options" in d
30+
31+
s = mec.model_dump_json()
32+
parsed = json.loads(s)
33+
assert parsed["fn"] == d["fn"]
34+
# Also verify mode-specific dumps
35+
d_py = mec.model_dump(mode="python")
36+
assert isinstance(d_py, dict)
37+
d_json = mec.model_dump(mode="json")
38+
assert isinstance(d_json, dict)
39+
json.dumps(d_json)
40+
41+
42+
def test_mec_model_dump_diamond_graph():
43+
n0 = NodeModel()
44+
n1 = NodeModel(deps_model=[n0])
45+
n2 = NodeModel(deps_model=[n0])
46+
root = NodeModel(deps_model=[n1, n2])
47+
48+
mec = _make_nested_mec(root)
49+
50+
d = mec.model_dump()
51+
assert isinstance(d, dict)
52+
assert set(["fn", "model", "context", "options"]).issubset(d.keys())
53+
54+
s = mec.model_dump_json()
55+
json.loads(s)
56+
# verify mode dumps
57+
d_py = mec.model_dump(mode="python")
58+
assert isinstance(d_py, dict)
59+
d_json = mec.model_dump(mode="json")
60+
assert isinstance(d_json, dict)
61+
json.dumps(d_json)
62+
63+
64+
def test_mec_model_dump_with_multi_evaluator():
65+
m = NodeModel()
66+
_ = LoggingEvaluator() # ensure import/validation
67+
evaluator = MultiEvaluator(evaluators=[LoggingEvaluator(), GraphEvaluator()])
68+
69+
# Simulate how Flow builds evaluation context with a custom evaluator
70+
ctx = DateContext(date=date(2022, 1, 1))
71+
mec = ModelEvaluationContext(model=evaluator, context=m.__call__.get_evaluation_context(m, ctx))
72+
73+
d = mec.model_dump()
74+
assert isinstance(d, dict)
75+
assert "fn" in d and "model" in d and "context" in d
76+
s = mec.model_dump_json()
77+
json.loads(s)
78+
# verify mode dumps
79+
d_py = mec.model_dump(mode="python")
80+
assert isinstance(d_py, dict)
81+
d_json = mec.model_dump(mode="json")
82+
assert isinstance(d_json, dict)
83+
json.dumps(d_json)

0 commit comments

Comments
 (0)