-
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.
Completed unit test for post category
- Loading branch information
1 parent
6743b6b
commit 3cdf4e7
Showing
5 changed files
with
118 additions
and
1 deletion.
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
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,44 @@ | ||
import factories | ||
import pytest | ||
from django.core.exceptions import ValidationError | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from django.db import IntegrityError | ||
|
||
from abstract.constants import GendersPet, ImageExtension | ||
from apps.pet.models import Pet | ||
from infrastructure.test_data_clear import TruncateTestData | ||
|
||
# *If you don't declare pytestmark our test class model don't accsess to database table | ||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
# !TestPostsCategory | ||
class TestPostsCategory: | ||
def test_str_method(self, category_factory): | ||
obj = category_factory() | ||
assert obj.__str__() == obj.name | ||
|
||
def test_slug(self, category_factory): | ||
obj = category_factory(name="category_1") | ||
assert obj.slug == obj.name.lower() | ||
|
||
def test_unique_name(self, category_factory): | ||
category_factory(name="category_1") | ||
with pytest.raises(IntegrityError): | ||
category_factory(name="category_1") | ||
|
||
def test_unique_slug(self, category_factory): | ||
obj = category_factory(name="category_1") | ||
with pytest.raises(IntegrityError): | ||
category_factory(name=obj.slug) | ||
|
||
def test_name_max_length(self, category_factory): | ||
name = "x" * 220 | ||
slug = "y" * 240 | ||
obj = category_factory(name=name, slug=slug) | ||
if len(name) > 100 or len(slug) > 100: | ||
with pytest.raises(ValidationError): | ||
obj.full_clean() | ||
else: | ||
assert len(name) <= 100 | ||
assert len(slug) <= 100 |
63 changes: 63 additions & 0 deletions
63
backend/config/tests/posts/category/test_post_category_endpoint.py
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,63 @@ | ||
import json | ||
|
||
import pytest | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import status | ||
|
||
# * If you don't declare pytestmark our test class model don't accsess to database table | ||
pytestmark = pytest.mark.django_db | ||
|
||
# User | ||
User = get_user_model() | ||
|
||
|
||
# !TestPostsCategoryEndpoints | ||
class TestPostsCategoryEndpoints: | ||
endpoint = "/posts/categories/" | ||
|
||
def get_register_payload(self): | ||
return { | ||
"username": "User", | ||
"email": "user@mail.ru", | ||
"password": "complexpassword123", | ||
"wallet_address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", | ||
} | ||
|
||
@pytest.fixture | ||
def category_response(self, api_client): | ||
payload = {"name": "devops"} | ||
response = api_client().post(f"{self.endpoint}", payload, format="json") | ||
yield payload, response | ||
|
||
@pytest.fixture | ||
def register_response(self, api_client): | ||
payload = self.get_register_payload() | ||
response = api_client().post("/users/register/", payload, format="json") | ||
yield payload, response | ||
|
||
@pytest.fixture | ||
def get_user(self, register_response, api_client): | ||
payload, response = register_response | ||
access_token = response.data["tokens"]["access"] | ||
headers = {"Authorization": f"Bearer {access_token}"} | ||
user = api_client().get("/users/", headers=headers) | ||
yield user, headers | ||
|
||
def test_return_all_categories(self, category_factory, api_client): | ||
category_factory.create_batch(4) | ||
response = api_client().get(self.endpoint, format="json") | ||
assert response.status_code == status.HTTP_200_OK | ||
assert len(json.loads(response.content)) == 4 | ||
|
||
def test_create_category(self, get_user, api_client): | ||
user, headers = get_user | ||
user = User.objects.get(id=user.data["id"]) | ||
user.is_staff = True | ||
user.save() | ||
if user.is_superuser and user.is_authenticated: | ||
payload = {"name": "devops"} | ||
response = api_client().post( | ||
f"{self.endpoint}", payload, format="json", headers=headers | ||
) | ||
assert response.status_code == status.HTTP_201_CREATED | ||
assert json.loads(response.content) == payload |