-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(profiles): add serializer tests for profiles app
- Loading branch information
1 parent
a9a5de0
commit 4b6a6e9
Showing
3 changed files
with
88 additions
and
9 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
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
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,82 @@ | ||
"""Serializer Tests for Profiles App.""" | ||
|
||
import pytest | ||
|
||
from ..serializers import ( | ||
ProfileReadSerializer, | ||
ProfileWriteSerializer, | ||
ProfileMinimalSerializer, | ||
ProfileAboutSerializer, | ||
) | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestProfileSerializers: | ||
"""Tests for Profile serializers.""" | ||
|
||
def test_profile_read_serializer(self, profile): | ||
serializer = ProfileReadSerializer(profile) | ||
expected_data = { | ||
"id": str(profile.id), | ||
"user_id": { | ||
"id": str(profile.user_id.id), | ||
"username": profile.user_id.username, | ||
"role": profile.user_id.get_role_display(), | ||
"is_online": profile.user_id.is_online, | ||
}, | ||
"first_name": profile.first_name, | ||
"last_name": profile.last_name, | ||
"birth_date": str(profile.birth_date), | ||
"bio": profile.bio, | ||
"image": profile.image.url, | ||
"cover": serializer.data["cover"], # TODO: Fix | ||
} | ||
|
||
assert serializer.data == expected_data | ||
|
||
def test_profile_write_serializer_valid_data(self, profile): | ||
data = { | ||
"first_name": "First Name", | ||
"last_name": "Last Name", | ||
"birth_date": profile.birth_date, | ||
"bio": profile.bio, | ||
"image": profile.image, | ||
"cover": profile.cover, | ||
} | ||
serializer = ProfileWriteSerializer(data=data) | ||
|
||
assert serializer.is_valid(), serializer.errors | ||
assert serializer.validated_data["first_name"] == "First Name" | ||
assert serializer.validated_data["last_name"] == "Last Name" | ||
|
||
def test_profile_write_serializer_invalid_data(self): | ||
data = {} | ||
serializer = ProfileWriteSerializer(data=data) | ||
|
||
assert not serializer.is_valid() | ||
assert "image" in serializer.errors | ||
|
||
def test_profile_minimal_serializer(self, profile): | ||
serializer = ProfileMinimalSerializer(profile) | ||
expected_data = { | ||
"id": str(profile.id), | ||
"user_id": { | ||
"id": str(profile.user_id.id), | ||
"username": profile.user_id.username, | ||
"role": profile.user_id.get_role_display(), | ||
"is_online": profile.user_id.is_online, | ||
}, | ||
"first_name": profile.first_name, | ||
"last_name": profile.last_name, | ||
"image": profile.image.url, | ||
} | ||
|
||
assert serializer.data == expected_data | ||
|
||
def test_profile_about_serializer(self, profile): | ||
serializer = ProfileAboutSerializer(profile) | ||
expected_data = { | ||
"bio": profile.bio, | ||
} | ||
|
||
assert serializer.data == expected_data |