You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ossie.apache.org by jb...@apache.org on 2026/10/06 07:50:03 UTC

(ossie) branch main updated: test(validation): cover every name kind in the duplicate check (#525)

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ossie.git


The following commit(s) were added to refs/heads/main by this push:
     new 1d29b869 test(validation): cover every name kind in the duplicate check (#525)
1d29b869 is described below

commit 1d29b86961388e9fd5a114e69a33d9ce1ec07171
Author: Ayush Saxena <ay...@apache.org>
AuthorDate: Tue Oct 6 13:19:57 2026 +0530

    test(validation): cover every name kind in the duplicate check (#525)
    
    #517 fixed find_duplicates but its test covered dataset names only, even
    though the same path reports duplicate field, metric and relationship
    names. Parametrize over all four so the behaviour is pinned for each;
    each case would report twice under the pre-#517 logic.
    
    Also take the review's suggestion and express find_duplicates with
    Counter, which keeps first-appearance ordering.
---
 validation/tests/test_validate.py | 60 ++++++++++++++++++++++++++++++++++++---
 validation/validate.py            |  6 ++--
 2 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/validation/tests/test_validate.py b/validation/tests/test_validate.py
index e9cc6dac..c34f8b5f 100644
--- a/validation/tests/test_validate.py
+++ b/validation/tests/test_validate.py
@@ -213,11 +213,63 @@ def test_find_duplicates_reports_each_name_once(items: list[str], expected: list
     assert _VALIDATE.find_duplicates(items) == expected
 
 
-def test_a_name_repeated_three_times_is_one_error() -> None:
-    """Three copies of a name are one problem, not two identical messages."""
-    errors = _VALIDATE.validate_unique_names(_document([_ORDERS, _ORDERS, _ORDERS], []))
+def _expression(expr: str = "x") -> dict:
+    return {"dialects": [{"dialect": "ANSI_SQL", "expression": expr}]}
+
+
+def _with_duplicate_fields(name: str) -> dict:
+    dataset = {
+        "name": "orders",
+        "source": "db.s.orders",
+        "fields": [
+            {"name": name, "expression": _expression()},
+            {"name": name, "expression": _expression()},
+            {"name": name, "expression": _expression()},
+        ],
+    }
+    return _document([dataset], [])
 
-    assert errors == ["[Unique] Duplicate dataset name 'orders' in model 'm'"]
+
+def _with_duplicate_metrics(name: str) -> dict:
+    document = _document([_ORDERS], [])
+    document["metrics"] = [
+        {"name": name, "expression": _expression("SUM(orders.amount)")},
+        {"name": name, "expression": _expression("SUM(orders.amount)")},
+        {"name": name, "expression": _expression("SUM(orders.amount)")},
+    ]
+    return document
+
+
+def _with_duplicate_relationships(name: str) -> dict:
+    rel = dict(_relationship(to_columns=["id"]), name=name)
+    return _document([_ORDERS, _CUSTOMERS], [dict(rel), dict(rel), dict(rel)])
+
+
+@pytest.mark.parametrize(
+    ("build", "expected"),
+    [
+        (
+            lambda: _document([_ORDERS, _ORDERS, _ORDERS], []),
+            "[Unique] Duplicate dataset name 'orders' in model 'm'",
+        ),
+        (
+            lambda: _with_duplicate_fields("amount"),
+            "[Unique] Duplicate field name 'amount' in dataset 'orders'",
+        ),
+        (
+            lambda: _with_duplicate_metrics("revenue"),
+            "[Unique] Duplicate metric name 'revenue' in model 'm'",
+        ),
+        (
+            lambda: _with_duplicate_relationships("orders_to_customers"),
+            "[Unique] Duplicate relationship name 'orders_to_customers' in model 'm'",
+        ),
+    ],
+    ids=["dataset", "field", "metric", "relationship"],
+)
+def test_a_name_repeated_three_times_is_one_error(build, expected: str) -> None:
+    """Three copies of a name are one problem, for every kind of name checked."""
+    assert _VALIDATE.validate_unique_names(build()) == [expected]
 
 
 def test_unique_names_are_checked_in_the_root_model() -> None:
diff --git a/validation/validate.py b/validation/validate.py
index 29de910b..293403e0 100644
--- a/validation/validate.py
+++ b/validation/validate.py
@@ -44,6 +44,7 @@ Usage:
 
 import json
 import sys
+from collections import Counter
 from collections.abc import Hashable
 from pathlib import Path
 
@@ -194,10 +195,7 @@ def find_duplicates(items: list[str]) -> list[str]:
     occurrence would emit the same message twice and inflate the error count.
     Order follows first appearance, so a document's diagnostics are stable.
     """
-    counts: dict[str, int] = {}
-    for item in items:
-        counts[item] = counts.get(item, 0) + 1
-    return [item for item, count in counts.items() if count > 1]
+    return [item for item, count in Counter(items).items() if count > 1]
 
 
 def validate_unique_names(data: dict) -> list[str]: