From f991d3b110fcb22a42bb3408ea466f437797f629 Mon Sep 17 00:00:00 2001 From: omicron Date: Fri, 4 Sep 2026 11:06:39 +0200 Subject: [PATCH] Expand test Site tests --- tests/data/sites/sections/config.yml | 2 + .../data/sites/sections/content/first-note.md | 8 ++ .../data/sites/sections/content/first-post.md | 8 ++ .../sites/sections/content/second-post.md | 8 ++ tests/data/sites/tags/config.yml | 2 + tests/data/sites/tags/content/no-tags.md | 8 ++ tests/data/sites/tags/content/one-tag.md | 9 ++ tests/data/sites/tags/content/two-tags.md | 9 ++ tests/unit/test_site.py | 111 +++++++++++++++++- 9 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 tests/data/sites/sections/config.yml create mode 100644 tests/data/sites/sections/content/first-note.md create mode 100644 tests/data/sites/sections/content/first-post.md create mode 100644 tests/data/sites/sections/content/second-post.md create mode 100644 tests/data/sites/tags/config.yml create mode 100644 tests/data/sites/tags/content/no-tags.md create mode 100644 tests/data/sites/tags/content/one-tag.md create mode 100644 tests/data/sites/tags/content/two-tags.md diff --git a/tests/data/sites/sections/config.yml b/tests/data/sites/sections/config.yml new file mode 100644 index 0000000..b3f8026 --- /dev/null +++ b/tests/data/sites/sections/config.yml @@ -0,0 +1,2 @@ +name: Sections Test +template: plain diff --git a/tests/data/sites/sections/content/first-note.md b/tests/data/sites/sections/content/first-note.md new file mode 100644 index 0000000..ba6fc01 --- /dev/null +++ b/tests/data/sites/sections/content/first-note.md @@ -0,0 +1,8 @@ +--- +type: article +section: notes +slug: first-note +title: First Note +date: 2026-08-28 +--- +First note. diff --git a/tests/data/sites/sections/content/first-post.md b/tests/data/sites/sections/content/first-post.md new file mode 100644 index 0000000..8c61d3f --- /dev/null +++ b/tests/data/sites/sections/content/first-post.md @@ -0,0 +1,8 @@ +--- +type: article +section: posts +slug: first-post +title: First Post +date: 2026-08-28 +--- +First post. diff --git a/tests/data/sites/sections/content/second-post.md b/tests/data/sites/sections/content/second-post.md new file mode 100644 index 0000000..278fcb3 --- /dev/null +++ b/tests/data/sites/sections/content/second-post.md @@ -0,0 +1,8 @@ +--- +type: article +section: posts +slug: second-post +title: Second Post +date: 2026-08-28 +--- +Second post. diff --git a/tests/data/sites/tags/config.yml b/tests/data/sites/tags/config.yml new file mode 100644 index 0000000..15884ff --- /dev/null +++ b/tests/data/sites/tags/config.yml @@ -0,0 +1,2 @@ +name: Tags Test +template: plain diff --git a/tests/data/sites/tags/content/no-tags.md b/tests/data/sites/tags/content/no-tags.md new file mode 100644 index 0000000..2d50b31 --- /dev/null +++ b/tests/data/sites/tags/content/no-tags.md @@ -0,0 +1,8 @@ +--- +type: article +section: posts +slug: no-tags +title: No Tags +date: 2026-08-28 +--- +No tags. diff --git a/tests/data/sites/tags/content/one-tag.md b/tests/data/sites/tags/content/one-tag.md new file mode 100644 index 0000000..e7067e4 --- /dev/null +++ b/tests/data/sites/tags/content/one-tag.md @@ -0,0 +1,9 @@ +--- +type: article +section: posts +slug: one-tag +title: One Tag +date: 2026-08-28 +tags: [python] +--- +One tag. diff --git a/tests/data/sites/tags/content/two-tags.md b/tests/data/sites/tags/content/two-tags.md new file mode 100644 index 0000000..499eae9 --- /dev/null +++ b/tests/data/sites/tags/content/two-tags.md @@ -0,0 +1,9 @@ +--- +type: article +section: posts +slug: two-tags +title: Two Tags +date: 2026-08-28 +tags: [python, testing] +--- +Two tags. diff --git a/tests/unit/test_site.py b/tests/unit/test_site.py index bfcd305..8d7a503 100644 --- a/tests/unit/test_site.py +++ b/tests/unit/test_site.py @@ -1,6 +1,6 @@ from pathlib import Path import pytest -from omicron.ssg.output import ContentError, Directory +from omicron.ssg.output import ContentError, Directory, OutputError from omicron.ssg.site import ConfigError, Site @@ -98,3 +98,112 @@ def test_clean_removes_output_directory(make_site): 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"}