File size: 5,860 Bytes
29fc4be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
diff --git a/tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py b/tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py
new file mode 100644
index 00000000..25eb4532
--- /dev/null
+++ b/tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py
@@ -0,0 +1,191 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+import tempfile
+
+import pytest
+
+import covalent as ct
+from covalent._data_store.datastore import DataStore
+from covalent._data_store.models import Lattice
+from covalent._results_manager import result as result_module
+from covalent._results_manager.result import Result, initialize_result_object
+from covalent._results_manager import write_result_to_db
+from covalent._workflow.lattice import Lattice as LatticeClass
+
+
+@pytest.fixture
+def test_db():
+
+
+    return DataStore(
+        db_URL="sqlite+pysqlite:///:memory:",
+        initialize_db=True,
+    )
+
+
+@pytest.fixture(autouse=True)
+def patch_db(test_db, monkeypatch):
+    monkeypatch.setattr(result_module, "workflow_db", test_db)
+    monkeypatch.setattr(write_result_to_db, "workflow_db", test_db)
+
+
+
+    monkeypatch.setattr(result_module, "insert_electron_dependency_data", lambda *a, **k: None)
+    monkeypatch.setattr(
+        write_result_to_db, "insert_electron_dependency_data", lambda *a, **k: None
+    )
+    return test_db
+
+
+def _make_json_lattice(name="workflow"):
+    results_dir = tempfile.mkdtemp()
+
+    @ct.electron
+    def task(x):
+        return x
+
+    @ct.lattice(results_dir=results_dir)
+    def workflow(x):
+        return task(x)
+
+    workflow.__name__ = name
+    workflow.build_graph(2)
+    return workflow.serialize_to_json()
+
+
+def _make_result_directly(dispatch_id="dispatch_x"):
+    json_lattice = _make_json_lattice()
+    lattice = LatticeClass.deserialize_from_json(json_lattice)
+    result = Result(
+        lattice=lattice,
+        results_dir=lattice.metadata["results_dir"],
+        dispatch_id=dispatch_id,
+    )
+    result._initialize_nodes()
+    return result
+
+
+
+
+
+def test_root_dispatch_id_defaults_to_own_dispatch_id():
+    result = _make_result_directly(dispatch_id="dispatch_abc")
+    assert result.root_dispatch_id == "dispatch_abc"
+
+
+def test_root_dispatch_id_is_read_only():
+    result = _make_result_directly(dispatch_id="dispatch_abc")
+    with pytest.raises(AttributeError):
+        result.root_dispatch_id = "something_else"
+
+
+
+
+
+def test_persist_without_electron_id_keeps_null(test_db):
+    result = _make_result_directly(dispatch_id="dispatch_top")
+    result.persist()
+
+    with test_db.session() as session:
+        lattice_row = session.query(Lattice).filter_by(dispatch_id="dispatch_top").first()
+        assert lattice_row is not None
+        assert lattice_row.electron_id is None
+
+
+def test_persist_with_electron_id_writes_it(test_db):
+    result = _make_result_directly(dispatch_id="dispatch_sub")
+    result.persist(electron_id=42)
+
+    with test_db.session() as session:
+        lattice_row = session.query(Lattice).filter_by(dispatch_id="dispatch_sub").first()
+        assert lattice_row is not None
+        assert lattice_row.electron_id == 42
+
+
+
+
+
+def test_initialize_result_object_toplevel(test_db):
+    json_lattice = _make_json_lattice()
+    result = initialize_result_object(json_lattice)
+
+    assert result.dispatch_id is not None
+    assert result.root_dispatch_id == result.dispatch_id
+
+    with test_db.session() as session:
+        lattice_row = (
+            session.query(Lattice).filter_by(dispatch_id=result.dispatch_id).first()
+        )
+        assert lattice_row is not None
+        assert lattice_row.electron_id is None
+
+
+def test_initialize_result_object_unique_dispatch_ids():
+    json_lattice = _make_json_lattice()
+    r1 = initialize_result_object(json_lattice)
+    r2 = initialize_result_object(json_lattice)
+    assert r1.dispatch_id != r2.dispatch_id
+
+
+def test_initialize_result_object_sublattice_inherits_root(test_db):
+    parent = initialize_result_object(_make_json_lattice(name="parent"))
+
+    sub = initialize_result_object(
+        _make_json_lattice(name="sub"),
+        parent_result_object=parent,
+        parent_electron_id=7,
+    )
+
+
+    assert sub.dispatch_id != parent.dispatch_id
+
+    assert sub.root_dispatch_id == parent.root_dispatch_id
+    assert sub.root_dispatch_id == parent.dispatch_id
+
+
+    with test_db.session() as session:
+        lattice_row = (
+            session.query(Lattice).filter_by(dispatch_id=sub.dispatch_id).first()
+        )
+        assert lattice_row is not None
+        assert lattice_row.electron_id == 7
+
+
+def test_initialize_result_object_nested_sublattice_inherits_root(test_db):
+    root = initialize_result_object(_make_json_lattice(name="root"))
+    mid = initialize_result_object(
+        _make_json_lattice(name="mid"),
+        parent_result_object=root,
+        parent_electron_id=1,
+    )
+    leaf = initialize_result_object(
+        _make_json_lattice(name="leaf"),
+        parent_result_object=mid,
+        parent_electron_id=2,
+    )
+
+    assert mid.root_dispatch_id == root.dispatch_id
+    assert leaf.root_dispatch_id == root.dispatch_id
+    assert leaf.dispatch_id != root.dispatch_id
+    assert leaf.dispatch_id != mid.dispatch_id
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+
+
+
+
+set -euo pipefail
+HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+cd /testbed
+
+
+
+
+
+
+
+
+python -m pytest -q \
+    tests/covalent_tests/data_store/data_store_test.py \
+    tests/covalent_tests/workflow/lattice_serialization_test.py
+
+
+python -m pytest -q \
+    tests/covalent_tests/results_manager_tests/root_dispatch_id_test.py