|
16 | 16 | import ast |
17 | 17 | import copy |
18 | 18 | import ctypes |
| 19 | +import importlib.util |
19 | 20 | import json |
20 | 21 | import math |
21 | 22 | import re |
|
30 | 31 | ROOT = Path(__file__).resolve().parents[2] |
31 | 32 | CHECKER = ROOT / "scripts/check_selected_window_ownership.py" |
32 | 33 | MANIFEST = ROOT / "tests/ownership/manifest.v1.json" |
| 34 | +SPEC = importlib.util.spec_from_file_location("selected_window_ownership", CHECKER) |
| 35 | +ownership = importlib.util.module_from_spec(SPEC) |
| 36 | +sys.modules[SPEC.name] = ownership |
| 37 | +SPEC.loader.exec_module(ownership) |
33 | 38 |
|
34 | 39 |
|
35 | 40 | def emitted_schema(root: Path) -> dict: |
@@ -460,6 +465,44 @@ def test_new_public_field_has_no_implicit_owner(self): |
460 | 465 | def test_d4_independent_reconstruction_matches_manifest(self): |
461 | 466 | self.assertEqual(manifest_schema(self.doc), emitted_schema(ROOT)) |
462 | 467 |
|
| 468 | + def scalar_fixture(self, source): |
| 469 | + assignment, = ast.parse(source).body |
| 470 | + self.assertIsInstance(assignment, ast.Assign) |
| 471 | + target, = assignment.targets |
| 472 | + self.assertIsInstance(target, ast.Name) |
| 473 | + self.assertEqual(target.id, "p") |
| 474 | + return ownership.scalar_type(ast.Name(id="p", ctx=ast.Load()), {"p": assignment.value}) |
| 475 | + |
| 476 | + def test_d4_report_array_scalar_index_is_accepted(self): |
| 477 | + for source in ("p = report.equity_curve[i]", "p = report.equity_curve[0]", |
| 478 | + "p = report.trades[i]", "p = report.trades[0]"): |
| 479 | + with self.subTest(source=source): |
| 480 | + self.assertEqual(self.scalar_fixture(source), {"kind": "number", "null_allowed": False}) |
| 481 | + |
| 482 | + def test_d4_unrecognized_report_array_index_is_rejected(self): |
| 483 | + self.assertEqual(self.scalar_fixture("p = report.equity_curve[i]"), |
| 484 | + {"kind": "number", "null_allowed": False}) |
| 485 | + for expression in ("report.unknown[i]", "other.equity_curve[i]", |
| 486 | + "report.other.equity_curve[i]", "report.equity_curve.values[i]"): |
| 487 | + with self.subTest(expression=expression): |
| 488 | + with self.assertRaises(ownership.InputError) as raised: |
| 489 | + self.scalar_fixture("p = " + expression) |
| 490 | + self.assertEqual(str(raised.exception), |
| 491 | + "unrecognized scalar producer expression: " + expression) |
| 492 | + |
| 493 | + def test_d4_report_array_slice_is_rejected(self): |
| 494 | + self.assertEqual(self.scalar_fixture("p = report.equity_curve[i]"), |
| 495 | + {"kind": "number", "null_allowed": False}) |
| 496 | + for expression in ("report.equity_curve[i:j]", "report.equity_curve[:]", |
| 497 | + "report.equity_curve[True]", "report.equity_curve[1.0]", |
| 498 | + "report.equity_curve[i + 1]", "report.equity_curve[index()]", |
| 499 | + "report.equity_curve[i, j]"): |
| 500 | + with self.subTest(expression=expression): |
| 501 | + with self.assertRaises(ownership.InputError) as raised: |
| 502 | + self.scalar_fixture("p = " + expression) |
| 503 | + self.assertEqual(str(raised.exception), |
| 504 | + "unrecognized scalar producer expression: " + expression) |
| 505 | + |
463 | 506 | def test_d4_manifest_leaf_removed_is_rejected(self): |
464 | 507 | # Killer for D4-comparison-disabled; checks this specific diagnostic. |
465 | 508 | rows = self.doc["canonical_schema"]["leaves"] |
|
0 commit comments