|
| 1 | +""" |
| 2 | +Guards the diagram edge-weight (cardinality) rule (#1532). |
| 3 | +
|
| 4 | +Line weight encodes cardinality only, and it is binary: |
| 5 | +- **thick** (penwidth 2): the foreign key constitutes the child's *entire* |
| 6 | + primary key -> a 1:1 dependency. |
| 7 | +- **thin** (penwidth 0.75): the child has primary-key attributes beyond those |
| 8 | + the foreign key contributes (newly declared, or inherited from another foreign |
| 9 | + key) -> a one-to-many dependency. |
| 10 | +
|
| 11 | +Master-part is NOT a weight: a part almost always adds a key attribute, so its |
| 12 | +edge is thin under this same rule. This test pins that, since the historical |
| 13 | +documentation inverted it ("thick = master-part"). |
| 14 | +""" |
| 15 | + |
| 16 | +import time |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +import datajoint as dj |
| 21 | + |
| 22 | +THICK = 2.0 |
| 23 | +THIN = 0.75 |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="function") |
| 27 | +def schema_by_backend(connection_by_backend, db_creds_by_backend): |
| 28 | + backend = db_creds_by_backend["backend"] |
| 29 | + test_id = str(int(time.time() * 1000))[-8:] |
| 30 | + schema_name = f"djtest_edgewt_{backend}_{test_id}"[:64] |
| 31 | + if connection_by_backend.is_connected: |
| 32 | + try: |
| 33 | + connection_by_backend.query( |
| 34 | + f"DROP DATABASE IF EXISTS {connection_by_backend.adapter.quote_identifier(schema_name)}" |
| 35 | + ) |
| 36 | + except Exception: |
| 37 | + pass |
| 38 | + schema = dj.Schema(schema_name, connection=connection_by_backend) |
| 39 | + yield schema |
| 40 | + if connection_by_backend.is_connected: |
| 41 | + try: |
| 42 | + connection_by_backend.query( |
| 43 | + f"DROP DATABASE IF EXISTS {connection_by_backend.adapter.quote_identifier(schema_name)}" |
| 44 | + ) |
| 45 | + except Exception: |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +def _penwidth_by_dest(dot): |
| 50 | + """Map each edge's destination-node tail -> penwidth (float).""" |
| 51 | + out = {} |
| 52 | + for edge in dot.get_edges(): |
| 53 | + dest = edge.get_destination().strip('"').lower() |
| 54 | + try: |
| 55 | + pw = float(edge.get_penwidth()) |
| 56 | + except (TypeError, ValueError): |
| 57 | + pw = None |
| 58 | + out.setdefault(dest, []).append((edge.get_source().strip('"').lower(), pw)) |
| 59 | + return out |
| 60 | + |
| 61 | + |
| 62 | +def _penwidth_for(edges_by_dest, dest_name): |
| 63 | + matches = edges_by_dest.get(dest_name, []) |
| 64 | + assert matches, f"no edge found into node {dest_name!r}; nodes: {list(edges_by_dest)}" |
| 65 | + return matches |
| 66 | + |
| 67 | + |
| 68 | +def test_edge_weight_encodes_cardinality(schema_by_backend): |
| 69 | + if not dj.diagram.diagram_active: |
| 70 | + pytest.skip("networkx/pydot not available") |
| 71 | + |
| 72 | + @schema_by_backend |
| 73 | + class Parent(dj.Manual): |
| 74 | + definition = """ |
| 75 | + parent_id : int32 |
| 76 | + """ |
| 77 | + |
| 78 | + class Part(dj.Part): |
| 79 | + definition = """ |
| 80 | + -> master |
| 81 | + part_id : int32 |
| 82 | + """ |
| 83 | + |
| 84 | + @schema_by_backend |
| 85 | + class OneToOne(dj.Manual): |
| 86 | + definition = """ |
| 87 | + -> Parent |
| 88 | + """ |
| 89 | + |
| 90 | + @schema_by_backend |
| 91 | + class OneToMany(dj.Manual): |
| 92 | + definition = """ |
| 93 | + -> Parent |
| 94 | + sub_id : int32 |
| 95 | + """ |
| 96 | + |
| 97 | + @schema_by_backend |
| 98 | + class RenamedOneToOne(dj.Manual): |
| 99 | + # A renamed foreign key can still be 1:1: the renamed column is |
| 100 | + # RenamedOneToOne's entire primary key, so the dependency is 1:1 -> thick. |
| 101 | + # The rule must compare child columns to the child PK, not parent-PK |
| 102 | + # names to child-PK names (which renaming would break). |
| 103 | + definition = """ |
| 104 | + -> Parent.proj(alt_parent_id='parent_id') |
| 105 | + """ |
| 106 | + |
| 107 | + dot = dj.Diagram(schema_by_backend).make_dot() |
| 108 | + edges = _penwidth_by_dest(dot) |
| 109 | + |
| 110 | + # 1:1 — the FK is OneToOne's entire primary key -> thick. |
| 111 | + assert all( |
| 112 | + pw == THICK for _, pw in _penwidth_for(edges, "onetoone") |
| 113 | + ), f"1:1 dependency must be thick ({THICK}); edges={edges}" |
| 114 | + # multi-valued — OneToMany adds `sub_id` -> thin. |
| 115 | + assert all( |
| 116 | + pw == THIN for _, pw in _penwidth_for(edges, "onetomany") |
| 117 | + ), f"multi-valued dependency must be thin ({THIN}); edges={edges}" |
| 118 | + # master -> part — the part adds `part_id` -> thin (NOT thick). |
| 119 | + assert all( |
| 120 | + pw == THIN for _, pw in _penwidth_for(edges, "parent.part") |
| 121 | + ), f"master-part edge must be thin ({THIN}); it is not a 1:1 dependency; edges={edges}" |
| 122 | + # renamed FK that is the child's whole primary key — still 1:1 -> thick. |
| 123 | + assert all( |
| 124 | + pw == THICK for _, pw in _penwidth_for(edges, "renamedonetoone") |
| 125 | + ), f"a renamed 1:1 foreign key must be thick ({THICK}); the rule must be rename-safe; edges={edges}" |
0 commit comments