|
| 1 | +"""assert_no_collection_name_collisions — the single choke point every route/router |
| 2 | +generator path goes through (called once, in run_gen, over the full entity set |
| 3 | +BEFORE any generator runs). See collection_name_collision.py's module docstring. |
| 4 | +""" |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import json |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from metaobjects import MetaDataLoader |
| 13 | +from metaobjects.meta.core.object.meta_object import MetaObject |
| 14 | +from metaobjects.shared.base_types import TYPE_OBJECT |
| 15 | +from metaobjects.codegen.collection_name_collision import ( |
| 16 | + ERR_COLLECTION_NAME_COLLISION, |
| 17 | + assert_no_collection_name_collisions, |
| 18 | +) |
| 19 | + |
| 20 | +_NAMING_CONFORMANCE_FIXTURE = ( |
| 21 | + Path(__file__).parents[4] / "fixtures" / "naming-conformance" / "already-plural-pluralize.json" |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def _entities(tmp_path: Path, *names: str) -> list: |
| 26 | + children = [ |
| 27 | + { |
| 28 | + "object.entity": { |
| 29 | + "name": name, |
| 30 | + "children": [ |
| 31 | + {"source.rdb": {"@table": name.lower()}}, |
| 32 | + {"field.long": {"name": "id"}}, |
| 33 | + {"identity.primary": {"@fields": "id"}}, |
| 34 | + ], |
| 35 | + } |
| 36 | + } |
| 37 | + for name in names |
| 38 | + ] |
| 39 | + meta_dir = tmp_path / "meta" |
| 40 | + meta_dir.mkdir(parents=True, exist_ok=True) |
| 41 | + (meta_dir / "meta.json").write_text( |
| 42 | + json.dumps({"metadata.root": {"package": "acme", "children": children}}) |
| 43 | + ) |
| 44 | + root = MetaDataLoader.from_directory(meta_dir).root |
| 45 | + return [c for c in root.own_children() if c.type == TYPE_OBJECT and isinstance(c, MetaObject)] |
| 46 | + |
| 47 | + |
| 48 | +def _entity_and_value(tmp_path: Path, entity_name: str, value_name: str) -> list: |
| 49 | + doc = { |
| 50 | + "metadata.root": { |
| 51 | + "package": "acme", |
| 52 | + "children": [ |
| 53 | + { |
| 54 | + "object.entity": { |
| 55 | + "name": entity_name, |
| 56 | + "children": [ |
| 57 | + {"source.rdb": {"@table": entity_name.lower()}}, |
| 58 | + {"field.long": {"name": "id"}}, |
| 59 | + {"identity.primary": {"@fields": "id"}}, |
| 60 | + ], |
| 61 | + } |
| 62 | + }, |
| 63 | + { |
| 64 | + "object.value": { |
| 65 | + "name": value_name, |
| 66 | + "children": [{"field.string": {"name": "text"}}], |
| 67 | + } |
| 68 | + }, |
| 69 | + ], |
| 70 | + } |
| 71 | + } |
| 72 | + meta_dir = tmp_path / "meta" |
| 73 | + meta_dir.mkdir(parents=True, exist_ok=True) |
| 74 | + (meta_dir / "meta.json").write_text(json.dumps(doc)) |
| 75 | + root = MetaDataLoader.from_directory(meta_dir).root |
| 76 | + return [c for c in root.own_children() if c.type == TYPE_OBJECT and isinstance(c, MetaObject)] |
| 77 | + |
| 78 | + |
| 79 | +def test_does_not_raise_for_distinct_plurals(tmp_path: Path) -> None: |
| 80 | + assert_no_collection_name_collisions(_entities(tmp_path, "Post", "Author", "Category")) |
| 81 | + |
| 82 | + |
| 83 | +def test_raises_for_address_and_addresses(tmp_path: Path) -> None: |
| 84 | + with pytest.raises(ValueError) as exc_info: |
| 85 | + assert_no_collection_name_collisions(_entities(tmp_path, "Address", "Addresses")) |
| 86 | + msg = str(exc_info.value) |
| 87 | + assert ERR_COLLECTION_NAME_COLLISION in msg |
| 88 | + assert "Address" in msg |
| 89 | + assert "Addresses" in msg |
| 90 | + |
| 91 | + |
| 92 | +def test_raises_for_order_and_orders(tmp_path: Path) -> None: |
| 93 | + with pytest.raises(ValueError): |
| 94 | + assert_no_collection_name_collisions(_entities(tmp_path, "Order", "Orders")) |
| 95 | + |
| 96 | + |
| 97 | +def test_excludes_value_objects(tmp_path: Path) -> None: |
| 98 | + # "Address" (entity) pluralizes to "Addresses"; an unrelated object.value named |
| 99 | + # "Addresses" never gets a route, so it must not trip this. |
| 100 | + assert_no_collection_name_collisions(_entity_and_value(tmp_path, "Address", "Addresses")) |
| 101 | + |
| 102 | + |
| 103 | +def test_naming_conformance_collision_cases_are_refused(tmp_path: Path) -> None: |
| 104 | + cases = json.loads(_NAMING_CONFORMANCE_FIXTURE.read_text())["collisionCases"] |
| 105 | + for i, case in enumerate(cases): |
| 106 | + with pytest.raises(ValueError): |
| 107 | + assert_no_collection_name_collisions( |
| 108 | + _entities(tmp_path / str(i), case["entityA"], case["entityB"]) |
| 109 | + ) |
0 commit comments