From 546f8b40788571d7a9c8233d8b38f230aab636f5 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 18 Aug 2020 08:36:45 +0200 Subject: [PATCH] Extract some fixtures and use reversed urls --- articles/tests/conftest.py | 29 ++++++++++++++++++++++++++++- articles/tests/test_articles.py | 19 ++++++++++--------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/articles/tests/conftest.py b/articles/tests/conftest.py index b57f153..8c4aa03 100644 --- a/articles/tests/conftest.py +++ b/articles/tests/conftest.py @@ -1,9 +1,36 @@ import pytest +from django.utils import timezone -from articles.models import User +from articles.models import Article, Page, User @pytest.fixture() @pytest.mark.django_db def author(): return User.objects.create_user("gaugendre") + + +@pytest.fixture() +@pytest.mark.django_db +def published_article(author): + return Article.objects.create( + title="Some interesting title", + status=Article.PUBLISHED, + author=author, + published_at=timezone.now(), + slug="some-slug", + content="# some markdown\n\n[a link](https://example.com)", + ) + + +@pytest.fixture() +@pytest.mark.django_db +def published_page(author): + return Page.objects.create( + title="Some interesting title", + status=Article.PUBLISHED, + author=author, + published_at=timezone.now(), + slug="some-slug", + content="# some markdown\n\n[a link](https://example.com)", + ) diff --git a/articles/tests/test_articles.py b/articles/tests/test_articles.py index eea0152..26c6331 100644 --- a/articles/tests/test_articles.py +++ b/articles/tests/test_articles.py @@ -1,21 +1,22 @@ import pytest from django.test import Client +from django.urls import reverse from model_bakery import baker from articles.models import Article, Page, User @pytest.mark.django_db -def test_can_access_list(client: Client, author: User): - article = baker.make(Article, author=author, status=Article.PUBLISHED) - page = baker.make(Page, author=author, status=Article.PUBLISHED) - res = client.get("/") +def test_can_access_list( + client: Client, published_article: Article, published_page: Page +): + res = client.get(reverse("articles-list")) assert res.status_code == 200 content = res.content.decode("utf-8") - for art in [article, page]: + for art in [published_article, published_page]: assert art.title in content - assert article.content in content - assert page.content not in content + assert published_article.get_abstract() in content + assert published_page.get_formatted_content() not in content @pytest.mark.django_db @@ -27,8 +28,8 @@ def test_abstract_shown_on_list(client: Client, author: User): status=Article.PUBLISHED, author=author, content=f"{abstract}\n\n{after}", - ) - res = client.get("/") + ) # type: Article + res = client.get(reverse("articles-list")) content = res.content.decode("utf-8") assert abstract in content assert after not in content