Skip to content

Commit

Permalink
Merge pull request #121 from stuartmaxwell:update-djpress
Browse files Browse the repository at this point in the history
Update-djpress
  • Loading branch information
stuartmaxwell authored Oct 10, 2024
2 parents 0b7a191 + 8cfd34e commit 415111b
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 26 deletions.
20 changes: 5 additions & 15 deletions config/settings_testing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Settings for running tests."""
"""Django settings for running tests."""

from .settings import * # noqa: F403, F401, RUF100

Expand All @@ -10,18 +10,8 @@
},
}

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
PASSWORD_HASHERS: list[str] = [
"django.contrib.auth.hashers.MD5PasswordHasher",
]

CACHES = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}
149 changes: 149 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import pytest

from django.contrib.auth.models import User

from djpress.models import Category, Post


@pytest.fixture
def user():
return User.objects.create_user(
username="testuser",
password="testpass",
first_name="Test",
last_name="User",
)


@pytest.fixture
def category1():
return Category.objects.create(title="Test Category1", slug="test-category1")


@pytest.fixture
def category2():
return Category.objects.create(title="Test Category2", slug="test-category2")


@pytest.fixture
def category3():
category = Category.objects.create(title="Development", slug="dev")
return category


@pytest.fixture
def test_post1(user, category1):
post = Post.objects.create(
title="Test Post1",
slug="test-post1",
content="This is test post 1.",
author=user,
status="published",
post_type="post",
)
post.categories.set([category1])

return post


@pytest.fixture
def test_post2(user, category2):
post = Post.objects.create(
title="Test Post2",
slug="test-post2",
content="This is test post 2.",
author=user,
status="published",
post_type="post",
)
post.categories.set([category2])
return post


@pytest.fixture
def test_post3(user, category1):
post = Post.objects.create(
title="Test Post3",
slug="test-post3",
content="This is test post 3.",
author=user,
status="published",
post_type="post",
)

return post


@pytest.fixture
def test_long_post1(user, settings, category1):
truncate_tag = settings.DJPRESS_SETTINGS["TRUNCATE_TAG"]
post = Post.post_objects.create(
title="Test Long Post1",
slug="test-long-post1",
content=f"This is the truncated content.\n\n{truncate_tag}\n\nThis is the rest of the post.",
author=user,
status="published",
post_type="post",
)
post.categories.set([category1])
return post


@pytest.fixture
def test_page1(user):
return Post.objects.create(
title="Test Page1",
slug="test-page1",
content="This is test page 1.",
author=user,
status="published",
post_type="page",
)


@pytest.fixture
def test_page2(user):
return Post.objects.create(
title="Test Page2",
slug="test-page2",
content="This is test page 2.",
author=user,
status="published",
post_type="page",
)


@pytest.fixture
def test_page3(user):
return Post.objects.create(
title="Test Page3",
slug="test-page3",
content="This is test page 3.",
author=user,
status="published",
post_type="page",
)


@pytest.fixture
def test_page4(user):
return Post.objects.create(
title="Test Page4",
slug="test-page4",
content="This is test page 4.",
author=user,
status="published",
post_type="page",
)


@pytest.fixture
def test_page5(user):
return Post.objects.create(
title="Test Page5",
slug="test-page5",
content="This is test page 5.",
author=user,
status="published",
post_type="page",
)
62 changes: 60 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
import pytest

from django.http import HttpResponse
from django.test import Client
import pytest
from django.urls import reverse
from django.utils import timezone


@pytest.mark.django_db
def test_index(client: Client) -> None:
def test_index(client, test_post1) -> None:
"""Test index view."""
url = "/"
response: HttpResponse = client.get(url)
assert response.status_code == 200


@pytest.mark.django_db
def test_archives(client, test_post1) -> None:
"""Test archives views."""
test_post1.date = timezone.make_aware(timezone.datetime(2024, 6, 1))
test_post1.save()

url = "/2024/"
response: HttpResponse = client.get(url)
assert response.status_code == 200
assert "Test Post1" in str(response.content)

url = "/2024/06/"
response: HttpResponse = client.get(url)
assert response.status_code == 200
assert "Test Post1" in str(response.content)


@pytest.mark.django_db
def test_single_post(client, test_post1) -> None:
"""Test single post view."""
test_post1.date = timezone.make_aware(timezone.datetime(2024, 6, 1))
test_post1.save()

url = "/2024/06/test-post1/"
response: HttpResponse = client.get(url)
assert response.status_code == 200
assert "Test Post1" in str(response.content)


@pytest.mark.django_db
def test_author_view(client, test_post1):
"""Test author view."""
url = "/author/testuser/"
response: HttpResponse = client.get(url)
assert response.status_code == 200
assert "Test Post1" in str(response.content)


@pytest.mark.django_db
def test_categories_vew(client, test_post1):
"""Test categories view."""
url = "/category/test-category1/"
response: HttpResponse = client.get(url)
assert response.status_code == 200
assert "Test Post1" in str(response.content)


@pytest.mark.django_db
def test_single_page(client, test_page1):
"""Test single page view."""
url = "/test-page1/"
response: HttpResponse = client.get(url)
assert response.status_code == 200
assert "Test Page1" in str(response.content)
18 changes: 9 additions & 9 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 415111b

Please sign in to comment.