Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfraney committed Apr 11, 2024
1 parent 1eed3ca commit 13923f5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
5 changes: 4 additions & 1 deletion blurry/schema_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
from collections.abc import MutableMapping
from pathlib import Path

from pydantic.v1 import ValidationError
Expand All @@ -12,7 +13,9 @@ class Config:
extra = "forbid"


def validate_front_matter_as_schema(path: Path, front_matter: dict, console: Console):
def validate_front_matter_as_schema(
path: Path, front_matter: MutableMapping, console: Console
):
"""
Validates schema data using pydantic_schemaorg, disallowing extra fields
"""
Expand Down
73 changes: 73 additions & 0 deletions tests/test_schema_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from pathlib import Path
from unittest.mock import MagicMock

from rich.console import Console

from blurry.markdown.front_matter import get_data
from blurry.schema_validation import validate_front_matter_as_schema

MARKDOWN_WITH_VALID_TOML_FRONT_MATTER = """
+++
"@type" = "WebPage"
name = "Introduction"
abstract = "A Python-powered static site generator with a focus on page speed and SEO."
datePublished = 2023-04-09
+++
# Blurry: A Python-powered static site generator
""".strip()


def test_validate_front_matter_as_schema_with_valid_front_matter():
_, front_matter = get_data(MARKDOWN_WITH_VALID_TOML_FRONT_MATTER)
path = Path("pages/intro.md")
test_console = Console()
test_console.print = MagicMock()
validate_front_matter_as_schema(path, front_matter, test_console)
assert not test_console.print.called


MARKDOWN_WITH_EXTRA_VALUE_IN_TOML_FRONT_MATTER = """
+++
"@type" = "WebPage"
name = "Introduction"
abstract = "A Python-powered static site generator with a focus on page speed and SEO."
datePublished = 2023-04-09
extra_value = true
+++
# Blurry: A Python-powered static site generator
""".strip()


def test_validate_front_matter_as_schema_with_extra_value():
_, front_matter = get_data(MARKDOWN_WITH_EXTRA_VALUE_IN_TOML_FRONT_MATTER)
path = Path("pages/intro.md")
test_console = Console()
test_console.print = MagicMock()
validate_front_matter_as_schema(path, front_matter, test_console)
test_console.print.assert_called_with(
"pages/intro.md: WebPage schema validation error: extra fields not permitted: ('extra_value',)"
)


MARKDOWN_WITH_NON_SCHEMA_VALUE_IN_TOML_FRONT_MATTER = """
+++
"@type" = "WebPage"
name = "Introduction"
abstract = "A Python-powered static site generator with a focus on page speed and SEO."
datePublished = 2023-04-09
~valid_extra_value = true
+++
# Blurry: A Python-powered static site generator
""".strip()


def test_validate_front_matter_as_schema_with_non_schema_value():
_, front_matter = get_data(MARKDOWN_WITH_NON_SCHEMA_VALUE_IN_TOML_FRONT_MATTER)
path = Path("pages/intro.md")
test_console = Console()
test_console.print = MagicMock()
validate_front_matter_as_schema(path, front_matter, test_console)
assert not test_console.print.called

0 comments on commit 13923f5

Please sign in to comment.