diff --git a/.github/workflows/ruff.yaml b/.github/workflows/ruff.yaml index caedbfd..e19a5d1 100644 --- a/.github/workflows/ruff.yaml +++ b/.github/workflows/ruff.yaml @@ -5,7 +5,7 @@ on: pull_request: jobs: - black: + ruff: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/tests/assets/example/models.py b/tests/assets/example/models.py index 47c17d1..7b879c6 100644 --- a/tests/assets/example/models.py +++ b/tests/assets/example/models.py @@ -21,7 +21,7 @@ class Post(Base): id = mapped_column(Uuid, primary_key=True, default=uuid4()) author = mapped_column(ForeignKey(User.id), nullable=False) created = mapped_column(DateTime, nullable=False, default=datetime.now(UTC)) - live = mapped_column(Boolean, default=False) + live = mapped_column(Boolean, default=False, comment="True if post is published") content = mapped_column(Text, default="") diff --git a/tests/conftest.py b/tests/conftest.py index 216493e..aa19311 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,6 @@ import os +import shutil +import tempfile from datetime import UTC, datetime from pathlib import Path from uuid import uuid4 @@ -43,4 +45,7 @@ class Comment(Base): @pytest.fixture def package_path(): - return Path(os.path.dirname(os.path.realpath(__file__))) / "assets" + template_path = Path(os.path.dirname(os.path.realpath(__file__))) / "assets" + with tempfile.TemporaryDirectory() as package_path: + shutil.copytree(template_path, package_path, dirs_exist_ok=True) + yield Path(package_path) diff --git a/tests/test_cli.py b/tests/test_cli.py index f51ef0a..450c05c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,27 +2,43 @@ from paracelsus.cli import app +from .utils import mermaid_assert + runner = CliRunner() def test_graph(package_path): result = runner.invoke( - app, ["graph", "example.base:Base", "--import-module", "example.models", "--python-dir", str(package_path)] + app, + [ + "graph", + "example.base:Base", + "--import-module", + "example.models", + "--python-dir", + str(package_path), + ], ) assert result.exit_code == 0 + mermaid_assert(result.stdout) - assert "users {" in result.stdout - assert "posts {" in result.stdout - assert "comments {" in result.stdout - assert "users ||--o{ posts : author" in result.stdout - assert "posts ||--o{ comments : post" in result.stdout - assert "users ||--o{ comments : author" in result.stdout - - assert "CHAR(32) author FK" in result.stdout - assert 'CHAR(32) post FK "nullable"' in result.stdout - assert "DATETIME created" in result.stdout +def test_inject_check(package_path): + result = runner.invoke( + app, + [ + "inject", + str(package_path / "README.md"), + "example.base:Base", + "--import-module", + "example.models", + "--python-dir", + str(package_path), + "--check", + ], + ) + assert result.exit_code == 1 def test_inject(package_path): @@ -36,14 +52,15 @@ def test_inject(package_path): "example.models", "--python-dir", str(package_path), - "--check", ], ) + assert result.exit_code == 0 - assert result.exit_code == 1 + with open(package_path / "README.md") as fp: + readme = fp.read() + mermaid_assert(readme) def test_version(): result = runner.invoke(app, ["version"]) - assert result.exit_code == 0 diff --git a/tests/test_graph.py b/tests/test_graph.py index f588ad0..8977e58 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -1,17 +1,8 @@ from paracelsus.graph import get_graph_string +from .utils import mermaid_assert + def test_get_graph_string(package_path): graph_string = get_graph_string("example.base:Base", ["example.models"], [package_path], "mermaid") - - assert "users {" in graph_string - assert "posts {" in graph_string - assert "comments {" in graph_string - - assert "users ||--o{ posts : author" in graph_string - assert "posts ||--o{ comments : post" in graph_string - assert "users ||--o{ comments : author" in graph_string - - assert "CHAR(32) author FK" in graph_string - assert 'CHAR(32) post FK "nullable"' in graph_string - assert "DATETIME created" in graph_string + mermaid_assert(graph_string) diff --git a/tests/transformers/test_mermaid.py b/tests/transformers/test_mermaid.py index cd176e1..4a5be2a 100644 --- a/tests/transformers/test_mermaid.py +++ b/tests/transformers/test_mermaid.py @@ -1,19 +1,9 @@ from paracelsus.transformers.mermaid import Mermaid +from ..utils import mermaid_assert + def test_mermaid(metaclass): mermaid = Mermaid(metaclass=metaclass) graph_string = str(mermaid) - - assert "users {" in graph_string - assert "posts {" in graph_string - assert "comments {" in graph_string - - assert "users ||--o{ posts : author" in graph_string - assert "posts ||--o{ comments : post" in graph_string - assert "users ||--o{ comments : author" in graph_string - - assert "CHAR(32) author FK" in graph_string - assert 'CHAR(32) post FK "nullable"' in graph_string - assert 'BOOLEAN live "True if post is published,nullable"' in graph_string - assert "DATETIME created" in graph_string + mermaid_assert(graph_string) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..4aa3782 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,13 @@ +def mermaid_assert(output: str) -> None: + assert "users {" in output + assert "posts {" in output + assert "comments {" in output + + assert "users ||--o{ posts : author" in output + assert "posts ||--o{ comments : post" in output + assert "users ||--o{ comments : author" in output + + assert "CHAR(32) author FK" in output + assert 'CHAR(32) post FK "nullable"' in output + assert 'BOOLEAN live "True if post is published,nullable"' in output + assert "DATETIME created" in output