-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new tests for get_author_display_name in utils.py
- Loading branch information
1 parent
b05a4a6
commit 8cb7f74
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
from djpress.utils import get_author_display_name | ||
from django.contrib.auth.models import User | ||
|
||
|
||
# create a parameterized fixture for a test user with first name, last name, and username | ||
@pytest.fixture( | ||
params=[ | ||
("Sam", "Brown", "sambrown"), | ||
("Sam", "", "sambrown"), | ||
("", "Brown", "sambrown"), | ||
("", "", "sambrown"), | ||
] | ||
) | ||
def test_user(request): | ||
first_name, last_name, username = request.param | ||
user = User.objects.create_user( | ||
username=username, | ||
first_name=first_name, | ||
last_name=last_name, | ||
password="testpass", | ||
) | ||
|
||
return user | ||
|
||
|
||
# Test the get_author_display_name function with the test_user fixture | ||
@pytest.mark.django_db | ||
def test_get_author_display_name(test_user): | ||
display_name = get_author_display_name(test_user) | ||
first_name = test_user.first_name | ||
last_name = test_user.last_name | ||
user_name = test_user.username | ||
|
||
# Assert that the display name is the first name and last name separated by a space | ||
if first_name and last_name: | ||
assert display_name == f"{first_name} {last_name}" | ||
|
||
if first_name and not last_name: | ||
assert display_name == first_name | ||
|
||
if not first_name and last_name: | ||
assert display_name == user_name | ||
|
||
if not first_name and not last_name: | ||
assert display_name == user_name |