From 70946c6bfda99c2aabf4e0f1c6972a9de67467bc Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 6 Mar 2021 15:07:07 +0100 Subject: [PATCH] Revert "Revert "Refactor tests and add tag in default fixtures"" This reverts commit dd2f44b83f6573446bc76bd64f083103db9bac25. --- articles/tests/conftest.py | 14 +++++++++++--- articles/tests/test_api_views.py | 27 +++++++++++++++------------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/articles/tests/conftest.py b/articles/tests/conftest.py index c995265..a99e497 100644 --- a/articles/tests/conftest.py +++ b/articles/tests/conftest.py @@ -5,7 +5,7 @@ import pytest from django.core.management import call_command from django.utils import timezone -from articles.models import Article, User +from articles.models import Article, Tag, User @pytest.fixture() @@ -16,8 +16,14 @@ def author() -> User: @pytest.fixture() @pytest.mark.django_db -def published_article(author: User) -> Article: - return Article.objects.create( +def tag() -> Tag: + return Tag.objects.create(name="This is a new tag", slug="this-new-tag") + + +@pytest.fixture() +@pytest.mark.django_db +def published_article(author: User, tag: Tag) -> Article: + article = Article.objects.create( title="Some interesting article title", status=Article.PUBLISHED, author=author, @@ -31,6 +37,8 @@ def published_article(author: User) -> Article: "[1]: https://example.com/image.png" ), ) + article.tags.set([tag]) + return article @pytest.fixture() diff --git a/articles/tests/test_api_views.py b/articles/tests/test_api_views.py index 9311000..6bb9239 100644 --- a/articles/tests/test_api_views.py +++ b/articles/tests/test_api_views.py @@ -18,10 +18,7 @@ def test_unauthenticated_render_redirects(published_article: Article, client: Cl @pytest.mark.django_db def test_render_article_same_content(published_article: Article, client: Client): client.force_login(published_article.author) - api_res = client.post( - reverse("api-render-article", kwargs={"article_pk": published_article.pk}), - data={"content": published_article.content}, - ) + api_res = post_article(client, published_article, published_article.content) standard_res = client.get( reverse("article-detail", kwargs={"slug": published_article.slug}) ) @@ -42,10 +39,7 @@ def test_render_article_same_content(published_article: Article, client: Client) def test_render_article_change_content(published_article: Article, client: Client): client.force_login(published_article.author) preview_content = "This is a different content **with strong emphasis**" - api_res = client.post( - reverse("api-render-article", kwargs={"article_pk": published_article.pk}), - data={"content": preview_content}, - ) + api_res = post_article(client, published_article, preview_content) assert api_res.status_code == 200 api_content = api_res.content.decode("utf-8") # type: str html_preview_content = format_article_content(preview_content) @@ -57,10 +51,19 @@ def test_render_article_doesnt_save(published_article, client: Client): client.force_login(published_article.author) original_content = published_article.content preview_content = "This is a different content **with strong emphasis**" - api_res = client.post( - reverse("api-render-article", kwargs={"article_pk": published_article.pk}), - data={"content": preview_content}, - ) + api_res = post_article(client, published_article, preview_content) assert api_res.status_code == 200 published_article.refresh_from_db() assert published_article.content == original_content + + +def post_article(client: Client, article: Article, content: str): + return client.post( + reverse("api-render-article", kwargs={"article_pk": article.pk}), + data={ + "content": content, + "tag_ids": ",".join( + map(str, article.tags.all().values_list("pk", flat=True)) + ), + }, + )