Add tests for frontmatter
This commit is contained in:
1 parent
346e3d7e0c
commit
b97487eb4b
1 file changed
+93
@@ -0,0 +1,93 @@
|
||||
import datetime
|
||||
import pytest
|
||||
import yaml
|
||||
from omicron.ssg.output.article import ArticleMeta
|
||||
from omicron.ssg.output.content import ContentError
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def valid_data():
|
||||
return yaml.safe_load("""
|
||||
section: posts
|
||||
slug: hello
|
||||
title: Hello
|
||||
date: 2024-01-01
|
||||
""")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def full_data():
|
||||
return yaml.safe_load("""
|
||||
section: posts
|
||||
slug: hello
|
||||
title: Hello
|
||||
date: 2024-01-01
|
||||
updated: 2024-02-01
|
||||
tags: [python, testing]
|
||||
summary: A short summary.
|
||||
draft: true
|
||||
""")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", ["section", "slug", "title", "date"])
|
||||
def test_from_dict_missing_required_key(valid_data, key):
|
||||
del valid_data[key]
|
||||
|
||||
with pytest.raises(ContentError, match=f"must contain a {key} property"):
|
||||
ArticleMeta.from_dict(valid_data)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"key, value, expected_type",
|
||||
[
|
||||
("section", 1, "string"),
|
||||
("slug", 1, "string"),
|
||||
("title", 1, "string"),
|
||||
("date", "2024-01-01", "date"),
|
||||
("updated", "2024-01-01", "date"),
|
||||
("tags", "python", "list of strings"),
|
||||
("tags", [1, "python"], "list of strings"),
|
||||
("summary", 1, "string"),
|
||||
("draft", "yes", "boolean"),
|
||||
],
|
||||
)
|
||||
def test_from_dict_wrong_type(valid_data, key, value, expected_type):
|
||||
valid_data[key] = value
|
||||
|
||||
with pytest.raises(
|
||||
ContentError, match=f"{key} frontmatter property must be a {expected_type}"
|
||||
):
|
||||
ArticleMeta.from_dict(valid_data)
|
||||
|
||||
|
||||
def test_from_dict_unexpected_field(valid_data):
|
||||
valid_data["bogus"] = "value"
|
||||
|
||||
with pytest.raises(ContentError, match="unexpected frontmatter field.*bogus"):
|
||||
ArticleMeta.from_dict(valid_data)
|
||||
|
||||
|
||||
def test_from_dict_minimal(valid_data):
|
||||
meta = ArticleMeta.from_dict(valid_data)
|
||||
|
||||
assert meta.section == "posts"
|
||||
assert meta.slug == "hello"
|
||||
assert meta.title == "Hello"
|
||||
assert meta.date == datetime.date(2024, 1, 1)
|
||||
assert meta.updated is None
|
||||
assert meta.tags == set()
|
||||
assert meta.summary is None
|
||||
assert meta.draft is False
|
||||
|
||||
|
||||
def test_from_dict_full(full_data):
|
||||
meta = ArticleMeta.from_dict(full_data)
|
||||
|
||||
assert meta.section == "posts"
|
||||
assert meta.slug == "hello"
|
||||
assert meta.title == "Hello"
|
||||
assert meta.date == datetime.date(2024, 1, 1)
|
||||
assert meta.updated == datetime.date(2024, 2, 1)
|
||||
assert meta.tags == {"python", "testing"}
|
||||
assert meta.summary == "A short summary."
|
||||
assert meta.draft is True
|
||||
Reference in new issue
Block a user