import pytest from taggit.models import Tag from apps.blog.models import ArticleIndexPage, ArticlePage, HomePage, TagMetadata from apps.blog.tests.factories import AuthorFactory def _build_article_tree(home_page: HomePage, count: int = 12): index = ArticleIndexPage(title="Articles", slug="articles") home_page.add_child(instance=index) author = AuthorFactory() tag = Tag.objects.create(name="Bench", slug="bench") TagMetadata.objects.create(tag=tag, colour="cyan") for n in range(count): article = ArticlePage( title=f"Article {n}", slug=f"article-{n}", author=author, summary="summary", body=[("rich_text", "

body words

")], ) index.add_child(instance=article) article.tags.add(tag) article.save_revision().publish() return index @pytest.mark.django_db def test_homepage_query_budget(client, home_page, django_assert_num_queries): _build_article_tree(home_page, count=8) with django_assert_num_queries(20, exact=False): response = client.get("/") assert response.status_code == 200 @pytest.mark.django_db def test_article_index_query_budget(client, home_page, django_assert_num_queries): _build_article_tree(home_page, count=12) with django_assert_num_queries(20, exact=False): response = client.get("/articles/") assert response.status_code == 200 @pytest.mark.django_db def test_article_read_query_budget(client, home_page, django_assert_num_queries): index = _build_article_tree(home_page, count=4) article = ArticlePage.objects.child_of(index).live().first() assert article is not None with django_assert_num_queries(20, exact=False): response = client.get(article.url) assert response.status_code == 200 def test_read_time_benchmark(benchmark): author = AuthorFactory.build() body = [("rich_text", "

" + "word " * 1000 + "

")] article = ArticlePage(title="Bench", slug="bench", author=author, summary="summary", body=body) result = benchmark(article._compute_read_time) assert result >= 1