95 lines
3.0 KiB
Python
95 lines
3.0 KiB
Python
import pytest
|
|
from django.db import IntegrityError
|
|
from taggit.models import Tag
|
|
|
|
from apps.blog.models import ArticleIndexPage, ArticlePage, Category, HomePage, TagMetadata
|
|
from apps.blog.tests.factories import AuthorFactory
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_home_page_creation(home_page):
|
|
assert HomePage.objects.count() == 1
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_index_parent_restriction():
|
|
assert ArticleIndexPage.parent_page_types == ["blog.HomePage"]
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_compute_read_time_excludes_code(home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
article = ArticlePage(
|
|
title="A",
|
|
slug="a",
|
|
author=author,
|
|
summary="s",
|
|
body=[("rich_text", "<p>one two three</p>"), ("code", {"language": "python", "raw_code": "x y z"})],
|
|
)
|
|
index.add_child(instance=article)
|
|
article.save()
|
|
assert article.read_time_mins == 1
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_tag_metadata_css_and_uniqueness():
|
|
tag = Tag.objects.create(name="llms", slug="llms")
|
|
meta = TagMetadata.objects.create(tag=tag, colour="cyan")
|
|
assert meta.get_css_classes()["bg"] == "bg-brand-cyan/10"
|
|
with pytest.raises(IntegrityError):
|
|
TagMetadata.objects.create(tag=tag, colour="pink")
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_default_category_is_assigned(home_page):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
article = ArticlePage(
|
|
title="Categorised",
|
|
slug="categorised",
|
|
author=author,
|
|
summary="s",
|
|
body=[("rich_text", "<p>body</p>")],
|
|
)
|
|
index.add_child(instance=article)
|
|
article.save()
|
|
assert article.category.slug == "general"
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_article_read_time_is_not_recomputed_when_body_text_is_unchanged(home_page, monkeypatch):
|
|
index = ArticleIndexPage(title="Articles", slug="articles")
|
|
home_page.add_child(instance=index)
|
|
author = AuthorFactory()
|
|
article = ArticlePage(
|
|
title="Stable read time",
|
|
slug="stable-read-time",
|
|
author=author,
|
|
summary="s",
|
|
body=[("rich_text", "<p>body words</p>")],
|
|
)
|
|
index.add_child(instance=article)
|
|
article.save()
|
|
|
|
def fail_compute():
|
|
raise AssertionError("read time should not be recomputed when body text is unchanged")
|
|
|
|
monkeypatch.setattr(article, "_compute_read_time", fail_compute)
|
|
article.title = "Retitled"
|
|
article.save()
|
|
article.refresh_from_db()
|
|
|
|
assert article.read_time_mins == 1
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_category_ordering():
|
|
Category.objects.get_or_create(name="General", slug="general")
|
|
Category.objects.create(name="Z", slug="z", sort_order=2)
|
|
Category.objects.create(name="A", slug="a", sort_order=1)
|
|
names = list(Category.objects.values_list("name", flat=True))
|
|
assert names == ["General", "A", "Z"]
|