~crocmagnon/blog

617c34e5ba1ad449235f6a78747b92e78460d866 — Gabriel Augendre 8 months ago b893e8b
Add unit tests
6 files changed, 69 insertions(+), 3 deletions(-)

D articles/tests.py
A articles/tests/__init__.py
A articles/tests/conftest.py
A articles/tests/test_articles.py
M pyproject.toml
M requirements-dev.txt
D articles/tests.py => articles/tests.py +0 -3
@@ 1,3 0,0 @@
from django.test import TestCase

# Create your tests here.

A articles/tests/__init__.py => articles/tests/__init__.py +0 -0
A articles/tests/conftest.py => articles/tests/conftest.py +9 -0
@@ 0,0 1,9 @@
import pytest

from articles.models import User


@pytest.fixture()
@pytest.mark.django_db
def author():
    return User.objects.create_user("gaugendre")

A articles/tests/test_articles.py => articles/tests/test_articles.py +50 -0
@@ 0,0 1,50 @@
import pytest
from django.test import Client
from django.utils import timezone

from articles.models import Article, Page, User


@pytest.mark.django_db
def test_can_access_list(client: Client, author: User):
    article = Article.objects.create(
        author=author,
        title="Sample published",
        status=Article.PUBLISHED,
        published_at=timezone.now(),
        slug="sample-published",
        content="Some content lorem ipsum",
    )
    page = Page.objects.create(
        author=author,
        title="Sample page published",
        status=Article.PUBLISHED,
        published_at=timezone.now(),
        slug="sample-page-published",
        content="Some page content lorem ipsum",
    )
    res = client.get("/")
    assert res.status_code == 200
    content = res.content.decode("utf-8")
    for art in [article, page]:
        assert art.title in content
    assert article.content in content
    assert page.content not in content


@pytest.mark.django_db
def test_abstract_shown_on_list(client: Client, author: User):
    abstract = "Some abstract"
    after = "Some content after abstract"
    article = Article.objects.create(
        author=author,
        title="Sample published",
        status=Article.PUBLISHED,
        published_at=timezone.now(),
        slug="sample-published",
        content=f"{abstract}\n<!--more-->\n{after}",
    )
    res = client.get("/")
    content = res.content.decode("utf-8")
    assert abstract in content
    assert after not in content

M pyproject.toml => pyproject.toml +7 -0
@@ 1,2 1,9 @@
[tool.black]
target-version = ['py38']

[tool.pytest.ini_options]
minversion = "6.0"
DJANGO_SETTINGS_MODULE = "blog.settings"
testpaths = [
    "articles",
]

M requirements-dev.txt => requirements-dev.txt +3 -0
@@ 1,1 1,4 @@
pre-commit==2.6.0
pytest==6.0.1
pytest-django==3.9.0
model-bakery==1.1.1