Revert "Revert "Refactor tests and add tag in default fixtures""

This reverts commit dd2f44b83f.
This commit is contained in:
Gabriel Augendre 2021-03-06 15:07:07 +01:00
parent dd2f44b83f
commit 70946c6bfd
2 changed files with 26 additions and 15 deletions

View file

@ -5,7 +5,7 @@ import pytest
from django.core.management import call_command from django.core.management import call_command
from django.utils import timezone from django.utils import timezone
from articles.models import Article, User from articles.models import Article, Tag, User
@pytest.fixture() @pytest.fixture()
@ -16,8 +16,14 @@ def author() -> User:
@pytest.fixture() @pytest.fixture()
@pytest.mark.django_db @pytest.mark.django_db
def published_article(author: User) -> Article: def tag() -> Tag:
return Article.objects.create( 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", title="Some interesting article title",
status=Article.PUBLISHED, status=Article.PUBLISHED,
author=author, author=author,
@ -31,6 +37,8 @@ def published_article(author: User) -> Article:
"[1]: https://example.com/image.png" "[1]: https://example.com/image.png"
), ),
) )
article.tags.set([tag])
return article
@pytest.fixture() @pytest.fixture()

View file

@ -18,10 +18,7 @@ def test_unauthenticated_render_redirects(published_article: Article, client: Cl
@pytest.mark.django_db @pytest.mark.django_db
def test_render_article_same_content(published_article: Article, client: Client): def test_render_article_same_content(published_article: Article, client: Client):
client.force_login(published_article.author) client.force_login(published_article.author)
api_res = client.post( api_res = post_article(client, published_article, published_article.content)
reverse("api-render-article", kwargs={"article_pk": published_article.pk}),
data={"content": published_article.content},
)
standard_res = client.get( standard_res = client.get(
reverse("article-detail", kwargs={"slug": published_article.slug}) 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): def test_render_article_change_content(published_article: Article, client: Client):
client.force_login(published_article.author) client.force_login(published_article.author)
preview_content = "This is a different content **with strong emphasis**" preview_content = "This is a different content **with strong emphasis**"
api_res = client.post( api_res = post_article(client, published_article, preview_content)
reverse("api-render-article", kwargs={"article_pk": published_article.pk}),
data={"content": preview_content},
)
assert api_res.status_code == 200 assert api_res.status_code == 200
api_content = api_res.content.decode("utf-8") # type: str api_content = api_res.content.decode("utf-8") # type: str
html_preview_content = format_article_content(preview_content) 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) client.force_login(published_article.author)
original_content = published_article.content original_content = published_article.content
preview_content = "This is a different content **with strong emphasis**" preview_content = "This is a different content **with strong emphasis**"
api_res = client.post( api_res = post_article(client, published_article, preview_content)
reverse("api-render-article", kwargs={"article_pk": published_article.pk}),
data={"content": preview_content},
)
assert api_res.status_code == 200 assert api_res.status_code == 200
published_article.refresh_from_db() published_article.refresh_from_db()
assert published_article.content == original_content 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))
),
},
)