Skip to content

Commit

Permalink
Test coverage back to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell committed Sep 19, 2024
1 parent b39663b commit 86e9ff1
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 31 deletions.
3 changes: 3 additions & 0 deletions example/config/settings_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# Changing these settings will affect lots of tests!
BLOG_TITLE = "My Test DJ Press Blog"
BLOG_DESCRIPTION = "This is a test blog."
ARCHIVES_PATH = "test-url-archives"
ARCHIVES_PATH_ENABLED = True
AUTHOR_PATH_ENABLED = True
AUTHOR_PATH = "test-url-author"
CATEGORY_PATH_ENABLED = True
Expand All @@ -31,3 +33,4 @@
RECENT_PUBLISHED_POSTS_COUNT = 3
DATE_ARCHIVES_ENABLED = True
POST_READ_MORE_TEXT = "Test read more..."
RSS_PATH = "test-rss"
49 changes: 32 additions & 17 deletions src/djpress/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,36 @@
post_detail,
)

regex_path = r"^(?P<path>[0-9A-Za-z/_-]*)$"

# The following regex is used to match the archives path. It is used to match
# the following patterns:
# - 2024
# - 2024/01
# - 2024/01/01
# There will always be a year.
# If there is a month, there will always be a year.
# If there is a day, there will always be a month and a year.
regex_archives = r"(?P<year>\d{4})(?:/(?P<month>\d{2})(?:/(?P<day>\d{2}))?)?$"

if settings.APPEND_SLASH:
regex_path = regex_path[:-1] + "/$"
regex_archives = regex_archives[:-1] + "/$"

def regex_path() -> str:
"""Generate the regex path for the post detail view.
The following regex is used to match the path. It is used to match the
any path that contains letters, numbers, underscores, hyphens, and slashes.
"""
regex = r"^(?P<path>[0-9A-Za-z/_-]*)$"
if settings.APPEND_SLASH:
return regex[:-1] + "/$"
return regex


def regex_archives() -> str:
"""Generate the regex path for the archives view.
The following regex is used to match the archives path. It is used to match
the following patterns:
- 2024
- 2024/01
- 2024/01/01
There will always be a year.
If there is a month, there will always be a year.
If there is a day, there will always be a month and a year.
"""
regex = r"(?P<year>\d{4})(?:/(?P<month>\d{2})(?:/(?P<day>\d{2}))?)?$"
if settings.APPEND_SLASH:
return regex[:-1] + "/$"
return regex


app_name = "djpress"

Expand All @@ -53,7 +68,7 @@
if settings.ARCHIVES_PATH_ENABLED and settings.ARCHIVES_PATH:
urlpatterns += [
re_path(
settings.ARCHIVES_PATH + "/" + regex_archives,
settings.ARCHIVES_PATH + "/" + regex_archives(),
archives_posts,
name="archives_posts",
),
Expand All @@ -71,7 +86,7 @@
urlpatterns += [
path("", index, name="index"),
re_path(
regex_path,
regex_path(),
post_detail,
name="post_detail",
),
Expand Down
3 changes: 3 additions & 0 deletions tests/test_models_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,6 @@ def test_get_cached_recent_published_posts(user, mock_timezone_now, monkeypatch)
kwargs.get("timeout") or args[2]
) # timeout might be a kwarg or the third positional arg
assert abs(actual_timeout - expected_timeout) < 5 # Allow a small margin of error

settings.set("CACHE_RECENT_PUBLISHED_POSTS", False)
assert settings.CACHE_RECENT_PUBLISHED_POSTS is False
18 changes: 9 additions & 9 deletions tests/test_templatetags_djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,9 @@ def test_post_date_link_with_date_archives_enabled(test_post1):
post_time = post_date.strftime("%-I:%M %p")

expected_output = (
f'<a href="/archives/{post_year}/{post_month}/" title="View all posts in {post_month_name} {post_year}">{post_month_name}</a> '
f'<a href="/archives/{post_year}/{post_month}/{post_day}/" title="View all posts on {post_day_name} {post_month_name} {post_year}">{post_day_name}</a>, '
f'<a href="/archives/{post_year}/" title="View all posts in {post_year}">{post_year}</a>, '
f'<a href="/test-url-archives/{post_year}/{post_month}/" title="View all posts in {post_month_name} {post_year}">{post_month_name}</a> '
f'<a href="/test-url-archives/{post_year}/{post_month}/{post_day}/" title="View all posts on {post_day_name} {post_month_name} {post_year}">{post_day_name}</a>, '
f'<a href="/test-url-archives/{post_year}/" title="View all posts in {post_year}">{post_year}</a>, '
f"{post_time}."
)

Expand All @@ -508,9 +508,9 @@ def test_post_date_link_with_date_archives_enabled_with_one_link_class(
post_time = post_date.strftime("%-I:%M %p")

expected_output = (
f'<a href="/archives/{post_year}/{post_month}/" title="View all posts in {post_month_name} {post_year}" class="class1">{post_month_name}</a> '
f'<a href="/archives/{post_year}/{post_month}/{post_day}/" title="View all posts on {post_day_name} {post_month_name} {post_year}" class="class1">{post_day_name}</a>, '
f'<a href="/archives/{post_year}/" title="View all posts in {post_year}" class="class1">{post_year}</a>, '
f'<a href="/test-url-archives/{post_year}/{post_month}/" title="View all posts in {post_month_name} {post_year}" class="class1">{post_month_name}</a> '
f'<a href="/test-url-archives/{post_year}/{post_month}/{post_day}/" title="View all posts on {post_day_name} {post_month_name} {post_year}" class="class1">{post_day_name}</a>, '
f'<a href="/test-url-archives/{post_year}/" title="View all posts in {post_year}" class="class1">{post_year}</a>, '
f"{post_time}."
)

Expand All @@ -535,9 +535,9 @@ def test_post_date_link_with_date_archives_enabled_with_two_link_classes(
post_time = post_date.strftime("%-I:%M %p")

expected_output = (
f'<a href="/archives/{post_year}/{post_month}/" title="View all posts in {post_month_name} {post_year}" class="class1 class2">{post_month_name}</a> '
f'<a href="/archives/{post_year}/{post_month}/{post_day}/" title="View all posts on {post_day_name} {post_month_name} {post_year}" class="class1 class2">{post_day_name}</a>, '
f'<a href="/archives/{post_year}/" title="View all posts in {post_year}" class="class1 class2">{post_year}</a>, '
f'<a href="/test-url-archives/{post_year}/{post_month}/" title="View all posts in {post_month_name} {post_year}" class="class1 class2">{post_month_name}</a> '
f'<a href="/test-url-archives/{post_year}/{post_month}/{post_day}/" title="View all posts on {post_day_name} {post_month_name} {post_year}" class="class1 class2">{post_day_name}</a>, '
f'<a href="/test-url-archives/{post_year}/" title="View all posts in {post_year}" class="class1 class2">{post_year}</a>, '
f"{post_time}."
)

Expand Down
192 changes: 192 additions & 0 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import pytest
import importlib

from django.urls import reverse, resolve, NoReverseMatch, clear_url_caches

from djpress import urls as djpress_urls
from djpress.conf import settings
from djpress.urls import regex_path, regex_archives


def test_regex_path():
"""Test that the URL is correctly set when APPEND_SLASH is True."""
# Default value is True
assert settings.APPEND_SLASH is True

# Test that the URL is correctly set
assert regex_path() == r"^(?P<path>[0-9A-Za-z/_-]*)/$"

settings.set("APPEND_SLASH", False)
assert settings.APPEND_SLASH is False

# Test that the URL is correctly set
assert regex_path() == r"^(?P<path>[0-9A-Za-z/_-]*)$"

# Set back to default value
settings.set("APPEND_SLASH", True)
assert settings.APPEND_SLASH is True


def test_regex_archives():
"""Test that the URL is correctly set when APPEND_SLASH is True."""
# Default value is True
assert settings.APPEND_SLASH is True

# Test that the URL is correctly set
assert (
regex_archives()
== r"(?P<year>\d{4})(?:/(?P<month>\d{2})(?:/(?P<day>\d{2}))?)?/$"
)

settings.set("APPEND_SLASH", False)
assert settings.APPEND_SLASH is False

# Test that the URL is correctly set
assert (
regex_archives()
== r"(?P<year>\d{4})(?:/(?P<month>\d{2})(?:/(?P<day>\d{2}))?)?$"
)

# Set back to default value
settings.set("APPEND_SLASH", True)
assert settings.APPEND_SLASH is True


def test_category_posts_url():
"""Test that the URL is correctly set when CATEGORY_PATH_ENABLED is True."""
# Check default settings
assert settings.CATEGORY_PATH_ENABLED is True
assert settings.CATEGORY_PATH == "test-url-category"

url = reverse("djpress:category_posts", kwargs={"slug": "test-slug"})
assert url == "/test-url-category/test-slug/"


@pytest.mark.urls("djpress.urls")
def test_category_posts_url_no_CATEGORY_PATH_ENABLED():
"""Test that the URL is correctly set when CATEGORY_PATH_ENABLED is True."""
# Check default settings
assert settings.CATEGORY_PATH_ENABLED is True
assert settings.CATEGORY_PATH == "test-url-category"

settings.set("CATEGORY_PATH_ENABLED", False)
assert settings.CATEGORY_PATH_ENABLED is False

# Clear the URL caches
clear_url_caches()

# Reload the URL module to reflect the changed settings
importlib.reload(djpress_urls)

# Try to reverse the URL and check if it's not registered
with pytest.raises(NoReverseMatch):
reverse("djpress:category_posts", kwargs={"slug": "test-slug"})

# Set back to default value
settings.set("CATEGORY_PATH_ENABLED", True)
assert settings.CATEGORY_PATH_ENABLED is True


def test_author_posts_url():
"""Test that the URL is correctly set when AUTHOR_PATH_ENABLED is True."""
# Check default settings
assert settings.AUTHOR_PATH_ENABLED is True
assert settings.AUTHOR_PATH == "test-url-author"

url = reverse("djpress:author_posts", kwargs={"author": "test-author"})
assert url == "/test-url-author/test-author/"


@pytest.mark.urls("djpress.urls")
def test_author_posts_url_no_AUTHOR_PATH_ENABLED():
"""Test that the URL is correctly set when AUTHOR_PATH_ENABLED is True."""
# Check default settings
assert settings.AUTHOR_PATH_ENABLED is True
assert settings.AUTHOR_PATH == "test-url-author"

settings.set("AUTHOR_PATH_ENABLED", False)
assert settings.AUTHOR_PATH_ENABLED is False

# Clear the URL caches
clear_url_caches()

# Reload the URL module to reflect the changed settings
importlib.reload(djpress_urls)

# Try to reverse the URL and check if it's not registered
with pytest.raises(NoReverseMatch):
reverse("djpress:author_posts", kwargs={"author": "test-author"})

# Set back to default value
settings.set("AUTHOR_PATH_ENABLED", True)
assert settings.AUTHOR_PATH_ENABLED is True


def test_archives_posts_url():
"""Test that the URL is correctly set when ARCHIVES_PATH_ENABLED is True."""
# Check default settings
assert settings.ARCHIVES_PATH_ENABLED is True
assert settings.ARCHIVES_PATH == "test-url-archives"

url = reverse("djpress:archives_posts", kwargs={"year": "2024"})
assert url == "/test-url-archives/2024/"


@pytest.mark.urls("djpress.urls")
def test_archives_posts_url_no_ARCHIVES_PATH_ENABLED():
"""Test that the URL is correctly set when ARCHIVES_PATH_ENABLED is True."""
# Check default settings
assert settings.ARCHIVES_PATH_ENABLED is True
assert settings.ARCHIVES_PATH == "test-url-archives"

settings.set("ARCHIVES_PATH_ENABLED", False)
assert settings.ARCHIVES_PATH_ENABLED is False

# Clear the URL caches
clear_url_caches()

# Reload the URL module to reflect the changed settings
importlib.reload(djpress_urls)

# Try to reverse the URL and check if it's not registered
with pytest.raises(NoReverseMatch):
reverse("djpress:archives_posts", kwargs={"year": "2024"})

# Set back to default value
settings.set("ARCHIVES_PATH_ENABLED", True)
assert settings.ARCHIVES_PATH_ENABLED is True


def test_rss_feed_url():
"""Test that the URL is correctly set when RSS_ENABLED is True."""
# Check default settings
assert settings.RSS_ENABLED is True
assert settings.RSS_PATH == "test-rss"

url = reverse("djpress:rss_feed")
assert url == "/test-rss/"


@pytest.mark.urls("djpress.urls")
def test_rss_feed_url_no_RSS_ENABLED():
"""Test that the URL is correctly set when RSS_ENABLED is True."""
# Check default settings
assert settings.RSS_ENABLED is True
assert settings.RSS_PATH == "test-rss"

settings.set("RSS_ENABLED", False)
assert settings.RSS_ENABLED is False

# Clear the URL caches
clear_url_caches()

# Reload the URL module to reflect the changed settings
importlib.reload(djpress_urls)

# Try to reverse the URL and check if it's not registered
with pytest.raises(NoReverseMatch):
reverse("djpress:rss_feed")

# Set back to default value
settings.set("RSS_ENABLED", True)
assert settings.RSS_ENABLED is True
10 changes: 5 additions & 5 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_date_archives_year(client, test_post1):

@pytest.mark.django_db
def test_date_archives_year_invalid_year(client):
response = client.get("/archives/0000/")
response = client.get("/test-url-archives/0000/")
assert response.status_code == 400


Expand All @@ -209,9 +209,9 @@ def test_date_archives_month(client, test_post1):

@pytest.mark.django_db
def test_date_archives_month_invalid_month(client):
response1 = client.get("/archives/2024/00/")
response1 = client.get("/test-url-archives/2024/00/")
assert response1.status_code == 400
response2 = client.get("/archives/2024/13/")
response2 = client.get("/test-url-archives/2024/13/")
assert response2.status_code == 400


Expand Down Expand Up @@ -239,9 +239,9 @@ def test_date_archives_day(client, test_post1):

@pytest.mark.django_db
def test_date_archives_day_invalid_day(client):
response1 = client.get("/archives/2024/01/00/")
response1 = client.get("/test-url-archives/2024/01/00/")
assert response1.status_code == 400
response2 = client.get("/archives/2024/01/32/")
response2 = client.get("/test-url-archives/2024/01/32/")
assert response2.status_code == 400


Expand Down

0 comments on commit 86e9ff1

Please sign in to comment.