diff --git a/articles/tests.py b/articles/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/articles/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/articles/tests/__init__.py b/articles/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/articles/tests/conftest.py b/articles/tests/conftest.py new file mode 100644 index 0000000..b57f153 --- /dev/null +++ b/articles/tests/conftest.py @@ -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") diff --git a/articles/tests/test_articles.py b/articles/tests/test_articles.py new file mode 100644 index 0000000..e666925 --- /dev/null +++ b/articles/tests/test_articles.py @@ -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\n{after}", + ) + res = client.get("/") + content = res.content.decode("utf-8") + assert abstract in content + assert after not in content diff --git a/pyproject.toml b/pyproject.toml index e131e50..14521c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,9 @@ [tool.black] target-version = ['py38'] + +[tool.pytest.ini_options] +minversion = "6.0" +DJANGO_SETTINGS_MODULE = "blog.settings" +testpaths = [ + "articles", +] diff --git a/requirements-dev.txt b/requirements-dev.txt index e69b5a0..4180e96 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1 +1,4 @@ pre-commit==2.6.0 +pytest==6.0.1 +pytest-django==3.9.0 +model-bakery==1.1.1