Repository navigation
Expand file tree
/
Copy pathtest_authoring_v2.py
More file actions
372 lines (333 loc) · 14.8 KB
/
Copy pathtest_authoring_v2.py
File metadata and controls
372 lines (333 loc) · 14.8 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
"""Behavioral checks for signature inputs, inherited quantities and typed handles."""
import hashlib
import shutil
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from cso_python.bindings import generate
from cso_python.execution import Execution, execute
from cso_python.source import SourceError
def sources(*names):
def decorate(method):
method.sources = names
return method
return decorate
class AuthoringV2Test(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory(prefix="cso-v2-test-")
self.root = Path(self.temp.name)
fixture = Path(__file__).parent / "fixtures/authoring-api"
self.fixture = fixture
for name in ("metadata.py", "defaulted_step.cso.py"):
shutil.copy(fixture / name, self.root / name)
# Composition and hidden-observation cases are added only by tests that use them.
for name in getattr(getattr(type(self), self._testMethodName), "sources", ()):
shutil.copy(fixture / name, self.root / name)
def tearDown(self):
self.temp.cleanup()
def add_hidden_assignment(self):
self.edit(
"defaulted_step.cso.py",
" return",
' intermediate: Annotated[float, symbol(glyph="I", description="Hidden", unit="m")] = adjusted * 2\n return',
)
def run_case(self, function="forward", **inputs):
return execute(
self.root
/ (
"defaulted_step.cso.py"
if function == "adjust"
else "forwarded_output.cso.py"
),
function,
inputs,
)
def success(self, response):
self.assertTrue(response["ok"], response)
self.assertEqual(response["protocolVersion"], "2")
return response["execution"]
def edit(self, name, before, after):
path = self.root / name
self.assertIn(before, path.read_text())
path.write_text(path.read_text().replace(before, after))
@sources("hidden_intermediate.cso.py")
def test_standalone_records_hidden_intermediate_and_selected_outputs(self):
result = self.success(
execute(self.root / "hidden_intermediate.cso.py", "hidden", {})
)
self.assertEqual([o["value"] for o in result["observations"]], [2, 3, 6])
self.assertEqual(
[o["name"] for o in result["authoring"]["outputs"]], ["adjusted"]
)
self.assertEqual(len(result["authoring"]["uses"]), 1)
@sources("forwarded_output.cso.py")
def test_generation_bootstraps_without_importing_or_executing_calculations(self):
with patch.object(Execution, "run") as run:
self.assertTrue(generate(self.root)["ok"])
run.assert_not_called()
before = {
p: p.read_bytes()
for p in (self.root / "_cso_bindings").iterdir()
if p.is_file()
}
self.assertTrue(generate(self.root, check=True)["ok"])
self.assertEqual(before, {p: p.read_bytes() for p in before})
@sources("forwarded_output.cso.py")
def test_repeated_calls_share_inputs_but_keep_invocation_identity(self):
generate(self.root)
result = self.success(self.run_case())
self.assertEqual(len(result["observations"]), 5)
self.assertEqual(sum(o["kind"] == "input" for o in result["observations"]), 2)
self.assertEqual(
[i["id"] for i in result["invocations"]],
["root", "root/first", "root/second"],
)
second = next(
p
for p in result["authoring"]["parameters"]
if p["invocationId"] == "root/second" and p["parameterName"] == "amount"
)
self.assertEqual(
second["origin"],
{"kind": "output", "invocationId": "root/first", "outputName": "original"},
)
for item in result["sourceManifest"]:
self.assertEqual(
item["sha256"],
hashlib.sha256((self.root / item["moduleId"]).read_bytes()).hexdigest(),
)
@sources("forwarded_output.cso.py")
def test_stale_interface_rejected_but_formula_edit_needs_no_regeneration(self):
generate(self.root)
self.edit(
"defaulted_step.cso.py", "amount + increment", "amount + increment * 2"
)
self.assertTrue(generate(self.root, check=True)["ok"])
self.success(self.run_case())
self.edit(
"defaulted_step.cso.py",
"increment: Increment = 3",
"increment: Increment = 4",
)
self.assertFalse(generate(self.root, check=True)["ok"])
self.assertEqual(self.run_case()["diagnostics"][0]["code"], "STALE_BINDINGS")
@sources("forwarded_output.cso.py")
def test_private_output_and_unknown_arguments_fail_preflight(self):
self.add_hidden_assignment()
generate(self.root)
self.edit(
"forwarded_output.cso.py", 'first["adjusted"]', 'first["intermediate"]'
)
with patch.object(Execution, "run") as run:
self.assertFalse(self.run_case()["ok"])
run.assert_not_called()
@sources("forwarded_output.cso.py")
def test_literal_and_default_child_inputs_get_their_own_rows(self):
self.edit(
"forwarded_output.cso.py",
"adjust(amount=amount, increment=increment)",
"adjust(amount=2)",
)
generate(self.root)
result = self.success(self.run_case())
self.assertEqual(sum(o["kind"] == "input" for o in result["observations"]), 4)
@sources("forwarded_output.cso.py")
def test_unit_mismatch_rejected_before_execution(self):
self.edit(
"forwarded_output.cso.py",
"amount: Amount = 2",
'amount: Annotated[float, symbol(glyph="Q", description="Amount", unit="cm")] = 2',
)
generate(self.root)
with patch.object(Execution, "run") as run:
result = self.run_case()
self.assertEqual(result["diagnostics"][0]["code"], "INPUT_UNIT_MISMATCH")
run.assert_not_called()
def test_source_change_after_capture_does_not_change_execution(self):
engine = Execution(self.root / "defaulted_step.cso.py", "adjust", {"amount": 2})
self.edit(
"defaulted_step.cso.py", "amount + increment", "amount + increment * 100"
)
result = engine.response()["execution"]
self.assertEqual(result["authoring"]["outputs"][0]["value"], 5)
@sources("forwarded_output.cso.py")
def test_nested_forwarding_keeps_each_parameter_hop(self):
(
self.root / "wrapper.cso.py"
).write_text("""from cso_python import calculation, section, CalculationResults
from metadata import Amount, Increment
from _cso_bindings.defaulted_step import adjust
@calculation(id="wrapper", title="Wrapper")
@section(id="wrapper", title="Wrapper")
def wrapper(w: Amount, h: Increment) -> CalculationResults:
child = adjust(amount=w, increment=h)
return {"adjusted": child["adjusted"], "original": w}
""")
self.edit(
"forwarded_output.cso.py",
"from _cso_bindings.defaulted_step import adjust",
"from _cso_bindings.wrapper import wrapper",
)
self.edit("forwarded_output.cso.py", "adjust(amount=", "wrapper(w=")
self.edit("forwarded_output.cso.py", ", increment=increment)", ", h=increment)")
generate(self.root)
result = self.success(self.run_case())
child = next(
p
for p in result["authoring"]["parameters"]
if p["invocationId"] == "root/first/child"
and p["parameterName"] == "amount"
)
self.assertEqual(
child["origin"],
{"kind": "parameter", "invocationId": "root/first", "parameterName": "w"},
)
self.assertEqual(sum(o["kind"] == "input" for o in result["observations"]), 2)
symbols = self.symbols(result)
for parent in ("first", "second"):
self.assertEqual(
symbols[f'["symbol","root/{parent}/child","adjusted"]']["glyph"],
f"R_{{{parent},child}}",
)
def test_generated_output_cannot_overwrite_authored_files_or_escape(self):
output = self.root / "_cso_bindings"
output.mkdir()
authored = output / "__init__.py"
authored.write_text("authored = True\n")
with self.assertRaises(SourceError):
generate(self.root)
self.assertEqual(authored.read_text(), "authored = True\n")
authored.unlink()
authored.symlink_to(self.root / "metadata.py")
with self.assertRaises(SourceError):
generate(self.root)
def test_metadata_effects_and_cycles_fail_before_execution(self):
for content in ('print("effect")', "from metadata import Amount"):
with self.subTest(content=content):
(self.root / "metadata.py").write_text(content)
with patch.object(Execution, "run") as run:
result = self.run_case("adjust", amount=2)
self.assertFalse(result["ok"])
self.assertIn("location", result["diagnostics"][0])
run.assert_not_called()
@sources("forwarded_output.cso.py")
def test_unknown_missing_arguments_and_inherited_reassignment_fail(self):
generate(self.root)
for inputs in ({"wdith": 2}, {}):
result = self.run_case("adjust", **inputs)
self.assertFalse(result["ok"])
self.assertIn("location", result["diagnostics"][0])
self.edit(
"defaulted_step.cso.py",
" adjusted:",
' amount: Annotated[float, symbol(glyph="Q", description="Changed", unit="m")] = amount * 2\n adjusted:',
)
generate(self.root)
self.assertFalse(self.run_case()["ok"])
@sources("forwarded_output.cso.py")
def test_observation_hooks_do_not_repeat_invocations_or_calculations(self):
import math
self.edit(
"defaulted_step.cso.py",
"from typing import",
"from math import sqrt\nfrom typing import",
)
self.edit(
"defaulted_step.cso.py", "amount + increment", "sqrt(amount + increment)"
)
generate(self.root)
original = math.sqrt
with patch("math.sqrt", wraps=original) as sqrt:
result = self.success(self.run_case(amount=2, increment=2))
self.assertEqual(sqrt.call_count, 2)
inputs = [o for o in result["observations"] if o["kind"] == "input"]
self.assertEqual([o["value"] for o in inputs], [2, 2])
self.assertNotEqual(inputs[0]["symbolId"], inputs[1]["symbolId"])
def test_metadata_and_selected_output_changes_invalidate_bindings(self):
generate(self.root)
self.edit(
"metadata.py", 'description="Amount"', 'description="Different amount"'
)
self.assertFalse(generate(self.root, check=True)["ok"])
generate(self.root)
self.edit(
"defaulted_step.cso.py", '"original": amount', '"original": increment'
)
self.assertFalse(generate(self.root, check=True)["ok"])
@sources("forwarded_output.cso.py")
def test_generated_package_initializer_is_captured_and_effects_rejected(self):
generate(self.root)
result = self.success(self.run_case())
self.assertIn(
"_cso_bindings/__init__.py",
[m["moduleId"] for m in result["sourceManifest"]],
)
(self.root / "_cso_bindings/__init__.py").write_text(
'print("unexpected effect")'
)
with patch.object(Execution, "run") as run:
self.assertFalse(self.run_case()["ok"])
run.assert_not_called()
def symbols(self, result):
return {
item["symbol"]["id"]: item["symbol"]
for section in result["cso"]["sections"]
for item in section["items"]
if item["kind"] == "symbol"
}
@sources("forwarded_output.cso.py")
def test_repeated_calls_have_unique_glyphs_and_keep_inherited_inputs(self):
generate(self.root)
symbols = self.symbols(self.success(self.run_case()))
self.assertEqual(len({s["glyph"] for s in symbols.values()}), len(symbols))
self.assertEqual(
symbols['["symbol","root/first","adjusted"]']["glyph"], "R_{first}"
)
self.assertEqual(
symbols['["symbol","root/second","adjusted"]']["glyph"], "R_{second}"
)
self.assertEqual(symbols['["symbol","root","amount"]']["glyph"], "Q")
self.assertEqual(symbols['["symbol","root","total"]']["glyph"], "R")
standalone = self.symbols(self.success(self.run_case("adjust", amount=2)))
self.assertEqual(standalone['["symbol","root","adjusted"]']["glyph"], "R")
def test_duplicate_local_glyphs_fail_before_execution_with_both_locations(self):
self.add_hidden_assignment()
self.edit("defaulted_step.cso.py", 'glyph="I"', 'glyph="R"')
with patch.object(Execution, "run") as run:
result = self.run_case("adjust", amount=2)
self.assertFalse(result["ok"])
diagnostic = result["diagnostics"][0]
self.assertEqual(diagnostic["code"], "DUPLICATE_GLYPH")
self.assertEqual(diagnostic["location"]["start"]["line"], 13)
self.assertEqual(diagnostic["relatedLocations"][0]["start"]["line"], 10)
run.assert_not_called()
@sources("forwarded_output.cso.py")
def test_descriptive_call_names_use_compact_scopes_and_keep_full_provenance(self):
self.edit("forwarded_output.cso.py", "first", "first_panel")
self.edit("forwarded_output.cso.py", "second", "second_panel")
generate(self.root)
symbols = self.symbols(self.success(self.run_case()))
self.assertEqual(
symbols['["symbol","root/first_panel","adjusted"]']["glyph"], "R_{fp}"
)
self.assertEqual(
symbols['["symbol","root/second_panel","adjusted"]']["metadata"][
"glyphScope"
],
"second_panel",
)
@sources("forwarded_output.cso.py")
def test_abbreviation_collisions_fail_before_execution(self):
self.edit("forwarded_output.cso.py", "first", "first_panel")
self.edit("forwarded_output.cso.py", "second", "front_panel")
generate(self.root)
with patch.object(Execution, "run") as run:
result = self.run_case()
self.assertFalse(result["ok"])
self.assertEqual(result["diagnostics"][0]["code"], "DUPLICATE_GLYPH")
self.assertIn("location", result["diagnostics"][0])
self.assertIn("relatedLocations", result["diagnostics"][0])
run.assert_not_called()
if __name__ == "__main__":
unittest.main()