Skip to content

Commit

Permalink
Improved Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Mar 25, 2024
1 parent 7cd5e97 commit 5f43212
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 41 deletions.
2 changes: 1 addition & 1 deletion tests/assets/example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="")


Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import shutil
import tempfile
from datetime import UTC, datetime
from pathlib import Path
from uuid import uuid4
Expand Down Expand Up @@ -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)
45 changes: 31 additions & 14 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
15 changes: 3 additions & 12 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 3 additions & 13 deletions tests/transformers/test_mermaid.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5f43212

Please sign in to comment.