|
| 1 | +"""Unit tests for relationship @onDelete / @onUpdate (referential actions). |
| 2 | +
|
| 3 | +Tier-1 cross-language contract: |
| 4 | + - Attr names: 'onDelete', 'onUpdate' |
| 5 | + - Action value-set: ('cascade', 'set-null', 'restrict', 'no-action') — kebab-case |
| 6 | + - Per-subtype @onDelete defaults: composition→cascade, aggregation→set-null, |
| 7 | + association→restrict (defaults applied at consumption time; the loader does |
| 8 | + not synthesize them, only validates explicit values). |
| 9 | + - @onUpdate default: cascade (subtype-independent). |
| 10 | +
|
| 11 | +The loader rejects any value outside the action set with ERR_BAD_ATTR_VALUE |
| 12 | +(implemented via the allowed_values schema in core_types.py — single source of |
| 13 | +truth). |
| 14 | +""" |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +import tempfile |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +from metaobjects.core_types import core_provider |
| 23 | +from metaobjects.loader.meta_data_loader import load_directory |
| 24 | +from metaobjects.meta.core.relationship.meta_relationship import MetaRelationship |
| 25 | +from metaobjects.meta.core.relationship.relationship_constants import ( |
| 26 | + ON_DELETE_DEFAULT_BY_SUBTYPE, |
| 27 | + ON_UPDATE_DEFAULT, |
| 28 | + REFERENTIAL_ACTIONS, |
| 29 | + RELATIONSHIP_ATTR_ON_DELETE, |
| 30 | + RELATIONSHIP_ATTR_ON_UPDATE, |
| 31 | + RELATIONSHIP_SUBTYPE_AGGREGATION, |
| 32 | + RELATIONSHIP_SUBTYPE_ASSOCIATION, |
| 33 | + RELATIONSHIP_SUBTYPE_COMPOSITION, |
| 34 | +) |
| 35 | +from metaobjects.shared.base_types import TYPE_RELATIONSHIP |
| 36 | + |
| 37 | + |
| 38 | +# --------------------------------------------------------------------------- |
| 39 | +# Constants — Tier-1 cross-language contract |
| 40 | +# --------------------------------------------------------------------------- |
| 41 | + |
| 42 | + |
| 43 | +def test_referential_attr_name_constants() -> None: |
| 44 | + """Attr names must equal TS + Java byte-for-byte.""" |
| 45 | + assert RELATIONSHIP_ATTR_ON_DELETE == "onDelete" |
| 46 | + assert RELATIONSHIP_ATTR_ON_UPDATE == "onUpdate" |
| 47 | + |
| 48 | + |
| 49 | +def test_referential_actions_value_set() -> None: |
| 50 | + """Action set must be kebab-case (no 'setDefault'); order pins migrate-ts FkAction.""" |
| 51 | + assert REFERENTIAL_ACTIONS == ("cascade", "set-null", "restrict", "no-action") |
| 52 | + |
| 53 | + |
| 54 | +def test_on_delete_defaults_by_subtype() -> None: |
| 55 | + """Per-subtype @onDelete defaults (consumption-time, not validation-time).""" |
| 56 | + assert ON_DELETE_DEFAULT_BY_SUBTYPE[RELATIONSHIP_SUBTYPE_COMPOSITION] == "cascade" |
| 57 | + assert ON_DELETE_DEFAULT_BY_SUBTYPE[RELATIONSHIP_SUBTYPE_AGGREGATION] == "set-null" |
| 58 | + assert ON_DELETE_DEFAULT_BY_SUBTYPE[RELATIONSHIP_SUBTYPE_ASSOCIATION] == "restrict" |
| 59 | + |
| 60 | + |
| 61 | +def test_on_update_default_is_cascade() -> None: |
| 62 | + """@onUpdate default is subtype-independent: cascade.""" |
| 63 | + assert ON_UPDATE_DEFAULT == "cascade" |
| 64 | + |
| 65 | + |
| 66 | +# --------------------------------------------------------------------------- |
| 67 | +# Accessors |
| 68 | +# --------------------------------------------------------------------------- |
| 69 | + |
| 70 | + |
| 71 | +def test_on_delete_accessor_none_when_unset() -> None: |
| 72 | + """meta-relationship.on_delete() returns None when not authored (default applied later).""" |
| 73 | + r = MetaRelationship(TYPE_RELATIONSHIP, RELATIONSHIP_SUBTYPE_COMPOSITION, "items") |
| 74 | + assert r.on_delete() is None |
| 75 | + assert r.on_update() is None |
| 76 | + |
| 77 | + |
| 78 | +def test_on_delete_accessor_returns_authored_value() -> None: |
| 79 | + r = MetaRelationship(TYPE_RELATIONSHIP, RELATIONSHIP_SUBTYPE_ASSOCIATION, "owner") |
| 80 | + r.set_attr(RELATIONSHIP_ATTR_ON_DELETE, "cascade", sub_type="string") |
| 81 | + r.set_attr(RELATIONSHIP_ATTR_ON_UPDATE, "no-action", sub_type="string") |
| 82 | + assert r.on_delete() == "cascade" |
| 83 | + assert r.on_update() == "no-action" |
| 84 | + |
| 85 | + |
| 86 | +# --------------------------------------------------------------------------- |
| 87 | +# Loader integration — allowed-values gate |
| 88 | +# --------------------------------------------------------------------------- |
| 89 | + |
| 90 | + |
| 91 | +def _load(doc: dict) -> list[str]: |
| 92 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 93 | + path = os.path.join(tmpdir, "meta.test.json") |
| 94 | + Path(path).write_text(json.dumps(doc)) |
| 95 | + result = load_directory(tmpdir, providers=[core_provider]) |
| 96 | + return [e.code.name for e in result.errors] |
| 97 | + |
| 98 | + |
| 99 | +def _two_entity_doc(child_attrs: dict[str, str]) -> dict: |
| 100 | + """A two-entity doc: Program -> Week composition with overridable referential actions.""" |
| 101 | + return { |
| 102 | + "metadata.root": { |
| 103 | + "package": "acme", |
| 104 | + "children": [ |
| 105 | + { |
| 106 | + "object.entity": { |
| 107 | + "name": "Program", |
| 108 | + "children": [ |
| 109 | + {"source.rdb": {"@table": "programs"}}, |
| 110 | + {"field.long": {"name": "id"}}, |
| 111 | + {"identity.primary": {"@fields": "id"}}, |
| 112 | + { |
| 113 | + "relationship.composition": { |
| 114 | + "name": "weeks", |
| 115 | + "@objectRef": "Week", |
| 116 | + "@cardinality": "one-to-many", |
| 117 | + **{f"@{k}": v for k, v in child_attrs.items()}, |
| 118 | + } |
| 119 | + }, |
| 120 | + ], |
| 121 | + } |
| 122 | + }, |
| 123 | + { |
| 124 | + "object.entity": { |
| 125 | + "name": "Week", |
| 126 | + "children": [ |
| 127 | + {"source.rdb": {"@table": "weeks"}}, |
| 128 | + {"field.long": {"name": "id"}}, |
| 129 | + {"field.long": {"name": "programId"}}, |
| 130 | + {"identity.primary": {"@fields": "id"}}, |
| 131 | + ], |
| 132 | + } |
| 133 | + }, |
| 134 | + ], |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | +def test_valid_on_delete_action_loads_ok() -> None: |
| 140 | + """Every member of REFERENTIAL_ACTIONS is accepted as @onDelete.""" |
| 141 | + for action in REFERENTIAL_ACTIONS: |
| 142 | + codes = _load(_two_entity_doc({"onDelete": action})) |
| 143 | + assert "ERR_BAD_ATTR_VALUE" not in codes, f"{action!r} should be valid; got {codes}" |
| 144 | + |
| 145 | + |
| 146 | +def test_valid_on_update_action_loads_ok() -> None: |
| 147 | + """Every member of REFERENTIAL_ACTIONS is accepted as @onUpdate.""" |
| 148 | + for action in REFERENTIAL_ACTIONS: |
| 149 | + codes = _load(_two_entity_doc({"onUpdate": action})) |
| 150 | + assert "ERR_BAD_ATTR_VALUE" not in codes, f"{action!r} should be valid; got {codes}" |
| 151 | + |
| 152 | + |
| 153 | +def test_invalid_on_delete_rejected() -> None: |
| 154 | + """Out-of-set @onDelete value → ERR_BAD_ATTR_VALUE.""" |
| 155 | + codes = _load(_two_entity_doc({"onDelete": "setDefault"})) |
| 156 | + assert "ERR_BAD_ATTR_VALUE" in codes, f"Expected ERR_BAD_ATTR_VALUE; got {codes}" |
| 157 | + |
| 158 | + |
| 159 | +def test_invalid_on_update_rejected() -> None: |
| 160 | + """Out-of-set @onUpdate value → ERR_BAD_ATTR_VALUE.""" |
| 161 | + codes = _load(_two_entity_doc({"onUpdate": "RESTRICT"})) # wrong case — should reject |
| 162 | + assert "ERR_BAD_ATTR_VALUE" in codes, f"Expected ERR_BAD_ATTR_VALUE; got {codes}" |
0 commit comments