From 1855328bcbe5dc4e8c52a08fb360f85e9ca99448 Mon Sep 17 00:00:00 2001 From: Coba Date: Wed, 17 Jul 2024 02:20:29 +0200 Subject: [PATCH] fix: mermaid complex types (#119) * fix: match outermost parent type with deeper nested types Simplifying the regex expression. Previously from `a>` it would ignore `a` and capture `b`, `c d` and `, e f`. Now it captures `a` and `b`, which is disregarded as far as I can see. * feat: add test_mermaid_column_types * Revert "fix: match outermost parent type with deeper nested types" This reverts commit 3b9d1eecb7403f555082a28465dd530967b6002a. * Revert "Revert "fix: match outermost parent type with deeper nested types"" This reverts commit 78d28910f22b010450e17e27e80b93791b7e7505. * chore: formatting --- dbterd/adapters/targets/mermaid.py | 2 +- .../mermaid/test_mermaid_column_types.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/unit/adapters/targets/mermaid/test_mermaid_column_types.py diff --git a/dbterd/adapters/targets/mermaid.py b/dbterd/adapters/targets/mermaid.py index 5f3ef51..443e140 100644 --- a/dbterd/adapters/targets/mermaid.py +++ b/dbterd/adapters/targets/mermaid.py @@ -42,7 +42,7 @@ def match_complex_column_type(column_type: str) -> Optional[str]: Returns: Optional[str]: Returns root type if input type is nested complex type, otherwise returns `None` for primitive types """ - pattern = r"(\w+)<(\w+\s+\w+(\s*,\s*\w+\s+\w+)*)>" + pattern = r"(\w+)<.*>" match = re.match(pattern, column_type) if match: return match.group(1) diff --git a/tests/unit/adapters/targets/mermaid/test_mermaid_column_types.py b/tests/unit/adapters/targets/mermaid/test_mermaid_column_types.py new file mode 100644 index 0000000..4fcd089 --- /dev/null +++ b/tests/unit/adapters/targets/mermaid/test_mermaid_column_types.py @@ -0,0 +1,26 @@ +import pytest + +from dbterd.adapters.targets import mermaid + +complex_column_types = [ + ("string", None), + ("struct", "struct"), + ("array>", "array"), + ("array>", "array"), +] +column_types = [ + ("string", "string"), + ("struct", "struct[OMITTED]"), + ("array>", "array[OMITTED]"), + ("array>", "array[OMITTED]"), +] + + +@pytest.mark.parametrize("input,expected", complex_column_types) +def test_match_complex_column_type(input, expected): + assert mermaid.match_complex_column_type(input) == expected + + +@pytest.mark.parametrize("input,expected", column_types) +def test_replace_column_type(input, expected): + assert mermaid.replace_column_type(input) == expected