Skip to content

Commit d9df811

Browse files
authored
Merge pull request #26 from rabelmervin/new
Changes made for table iteration
2 parents 455e0fc + 5d572b2 commit d9df811

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

paracelsus/graph.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import os
33
import sys
44
from pathlib import Path
5+
import re
56
from typing import List, Set
67

7-
from sqlalchemy import MetaData
8-
8+
from sqlalchemy.schema import MetaData
99
from .transformers.dot import Dot
1010
from .transformers.mermaid import Mermaid
1111

@@ -83,15 +83,18 @@ def resolve_included_tables(
8383
case 0, 0:
8484
return all_tables
8585
case 0, int():
86-
return all_tables - exclude_tables
86+
excluded = {table for table in all_tables if any(re.match(pattern, table) for pattern in exclude_tables)}
87+
return all_tables - excluded
8788
case int(), 0:
88-
if not include_tables.issubset(all_tables):
89+
included = {table for table in all_tables if any(re.match(pattern, table) for pattern in include_tables)}
90+
91+
if not included:
8992
non_existent_tables = include_tables - all_tables
9093
raise ValueError(
9194
f"Some tables to include ({non_existent_tables}) don't exist"
9295
"withinthe found tables ({all_tables})."
9396
)
94-
return include_tables
97+
return included
9598
case _:
9699
raise ValueError(
97100
f"Only one or none of include_tables ({include_tables}) or exclude_tables"

tests/test_cli.py

+49-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
from typing import Literal
13
import pytest
24

35
from typer.testing import CliRunner
@@ -9,7 +11,7 @@
911
runner = CliRunner()
1012

1113

12-
def test_graph(package_path):
14+
def test_graph(package_path: Path):
1315
result = runner.invoke(
1416
app,
1517
["graph", "example.base:Base", "--import-module", "example.models", "--python-dir", str(package_path)],
@@ -20,7 +22,7 @@ def test_graph(package_path):
2022

2123

2224
@pytest.mark.parametrize("column_sort_arg", ["key-based", "preserve-order"])
23-
def test_graph_column_sort(package_path, column_sort_arg):
25+
def test_graph_column_sort(package_path: Path, column_sort_arg: Literal["key-based"] | Literal["preserve-order"]):
2426
result = runner.invoke(
2527
app,
2628
[
@@ -39,7 +41,7 @@ def test_graph_column_sort(package_path, column_sort_arg):
3941
mermaid_assert(result.stdout)
4042

4143

42-
def test_graph_with_exclusion(package_path):
44+
def test_graph_with_exclusion(package_path: Path):
4345
result = runner.invoke(
4446
app,
4547
[
@@ -58,7 +60,7 @@ def test_graph_with_exclusion(package_path):
5860
assert "comments {" not in result.stdout
5961

6062

61-
def test_graph_with_inclusion(package_path):
63+
def test_graph_with_inclusion(package_path: Path):
6264
result = runner.invoke(
6365
app,
6466
[
@@ -77,7 +79,7 @@ def test_graph_with_inclusion(package_path):
7779
assert "comments {" in result.stdout
7880

7981

80-
def test_inject_check(package_path):
82+
def test_inject_check(package_path: Path):
8183
result = runner.invoke(
8284
app,
8385
[
@@ -94,7 +96,7 @@ def test_inject_check(package_path):
9496
assert result.exit_code == 1
9597

9698

97-
def test_inject(package_path):
99+
def test_inject(package_path: Path):
98100
result = runner.invoke(
99101
app,
100102
[
@@ -115,7 +117,7 @@ def test_inject(package_path):
115117

116118

117119
@pytest.mark.parametrize("column_sort_arg", ["key-based", "preserve-order"])
118-
def test_inject_column_sort(package_path, column_sort_arg):
120+
def test_inject_column_sort(package_path: Path, column_sort_arg: Literal["key-based"] | Literal["preserve-order"]):
119121
result = runner.invoke(
120122
app,
121123
[
@@ -140,3 +142,43 @@ def test_inject_column_sort(package_path, column_sort_arg):
140142
def test_version():
141143
result = runner.invoke(app, ["version"])
142144
assert result.exit_code == 0
145+
146+
147+
def test_graph_with_inclusion_regex(package_path: Path):
148+
result = runner.invoke(
149+
app,
150+
[
151+
"graph",
152+
"example.base:Base",
153+
"--import-module",
154+
"example.models",
155+
"--python-dir",
156+
str(package_path),
157+
"--include-tables",
158+
"^com.*",
159+
],
160+
)
161+
assert result.exit_code == 0
162+
assert "comments {" in result.stdout
163+
assert "users {" not in result.stdout
164+
assert "post{" not in result.stdout
165+
166+
167+
def test_graph_with_exclusion_regex(package_path: Path):
168+
result = runner.invoke(
169+
app,
170+
[
171+
"graph",
172+
"example.base:Base",
173+
"--import-module",
174+
"example.models",
175+
"--python-dir",
176+
str(package_path),
177+
"--exclude-tables",
178+
"^pos*.",
179+
],
180+
)
181+
assert result.exit_code == 0
182+
assert "comments {" in result.stdout
183+
assert "users {" in result.stdout
184+
assert "post {" not in result.stdout

0 commit comments

Comments
 (0)