Files
omicron-ssg/tests/unit/test_site.py
T

193 lines
5.3 KiB
Python

from pathlib import Path
import pytest
from omicron.ssg.output import ContentError, Directory, OutputError
from omicron.ssg.site import ConfigError, Site
def test_discover_raises_on_duplicate_destination(make_site):
site = make_site("duplicate_slug")
with pytest.raises(ContentError):
site.discover()
def test_discover_creates_content_directories(make_site):
site = make_site("main")
site.discover()
assert isinstance(site.by_path[Path("posts")], Directory)
assert isinstance(site.by_path[Path("posts/first-post")], Directory)
def test_read_config_errors(site_dir):
path = site_dir("thin_site")
config_path = path / "config.yml"
config_path.unlink()
with pytest.raises(ConfigError, match="config file missing"):
Site(path)
config_path.write_text("- a\n- b\n")
with pytest.raises(ConfigError, match="YAML mapping with string keys"):
Site(path)
config_path.write_text("123: value\n")
with pytest.raises(ConfigError, match="YAML mapping with string keys"):
Site(path)
config_path.write_text("template: plain\n")
with pytest.raises(ConfigError, match="must contain a name property"):
Site(path)
config_path.write_text("name: test\n")
with pytest.raises(ConfigError, match="must contain a template property"):
Site(path)
def test_site_relative_base_url(site_dir):
path = site_dir("thin_site")
(path / "config.yml").write_text("name: test\ntemplate: plain\nbase_url: blog\n")
with pytest.raises(ConfigError, match="relative path"):
site = Site(path)
def test_discover_skips_drafts_by_default(make_site):
site = make_site("draft_content")
site.discover()
assert len(site.content) == 1
assert site.content[0].title == "Published"
def test_discover_includes_drafts_when_enabled(make_site):
site = make_site("draft_content", drafts=True)
site.discover()
assert len(site.content) == 2
def test_clean_removes_output_directory(make_site):
site = make_site("main")
site.build()
assert site.output_path.exists()
site.clean()
assert not site.output_path.exists()
site.clean()
assert not site.output_path.exists()
def test_resolve_template_path_raises_for_missing_template():
with pytest.raises(ConfigError, match="not found"):
Site.resolve_template_path("does-not-exist")
def test_home_url(site_dir):
path = site_dir("thin_site")
(path / "config.yml").write_text("name: test\ntemplate: plain\n")
assert Site(path).home_url() == "/"
(path / "config.yml").write_text(
"name: test\ntemplate: plain\nbase_url: http://example.com/blog\n"
)
assert Site(path).home_url() == "/blog"
def test_url_for(site_dir):
path = site_dir("thin_site")
(path / "config.yml").write_text(
"name: test\ntemplate: plain\nbase_url: http://example.com/blog\n"
)
site = Site(path)
assert site.url_for("assets/style.css") == "/blog/assets/style.css"
with pytest.raises(OutputError, match="must be relative"):
site.url_for("/assets/style.css")
def test_section_url(site_dir):
path = site_dir("sections")
site = Site(path)
site.discover()
assert site.section_url("posts") == "/posts"
assert site.section_url("notes") == "/notes"
(path / "config.yml").write_text("name: test\ntemplate: plain\nbase_url: /blog\n")
site = Site(path)
site.discover()
assert site.section_url("posts") == "/blog/posts"
assert site.section_url("notes") == "/blog/notes"
def test_add_by_section(make_site):
site = make_site("sections")
site.discover()
assert set(site.by_section) == {"posts", "notes"}
assert {c.title for c in site.by_section["posts"]} == {"First Post", "Second Post"}
assert {c.title for c in site.by_section["notes"]} == {"First Note"}
def test_feed_url(site_dir):
path = site_dir("tags")
(path / "config.yml").write_text(
"name: test\ntemplate: plain\nbase_url: http://example.com\n"
)
site = Site(path)
site.discover()
assert site.feed_url("root", None) == "/atom.xml"
(path / "config.yml").write_text("name: test\ntemplate: plain\n")
site = Site(path)
site.discover()
assert site.feed_url("root", None) is None
def test_tag_url(site_dir):
path = site_dir("tags")
site = Site(path)
site.discover()
assert site.tag_url("python") == "/tags/python"
(path / "config.yml").write_text("name: test\ntemplate: plain\nbase_url: /blog\n")
site = Site(path)
site.discover()
assert site.tag_url("python") == "/blog/tags/python"
def test_tags_url(make_site):
site = make_site("tags")
site.discover()
assert site.tags_url() == "/tags"
def test_add_by_source(make_site):
site = make_site("tags")
site.discover()
assert site.by_source
for content in site.content:
assert site.by_source[content.source] is content
def test_tags_by_count(make_site):
site = make_site("tags")
site.discover()
assert site.tags_by_count == [("python", 2), ("testing", 1)]
def test_add_by_tag(make_site):
site = make_site("tags")
site.discover()
assert set(site.by_tag) == {"python", "testing"}
assert {c.title for c in site.by_tag["python"]} == {"One Tag", "Two Tags"}
assert {c.title for c in site.by_tag["testing"]} == {"Two Tags"}