Skip to content

Commit

Permalink
Completed unit test for post category
Browse files Browse the repository at this point in the history
  • Loading branch information
riadelimemmedov committed Mar 28, 2024
1 parent 6743b6b commit 3cdf4e7
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/apps/posts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# !Category
class Category(TimeStampedModel):
name = models.CharField(_("Category name"), max_length=100)
name = models.CharField(_("Category name"), max_length=100, null=True, unique=True)
slug = models.SlugField(max_length=100, unique=True, db_index=True, null=True)

class Meta:
Expand Down
1 change: 1 addition & 0 deletions backend/config/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
register(factories.PetFactory)
register(factories.PetFactoryEndToEnd)
register(factories.TransactionFactory)
register(factories.CategoryFactory)


# !api_client
Expand Down
9 changes: 9 additions & 0 deletions backend/config/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from abstract.constants import Genders, GendersPet, Status, Types
from apps.pet.models import Pet
from apps.posts.models import Category
from apps.transaction.models import Transaction
from apps.user_profile.models import Profile
from config.helpers import generate_metamask_address, generate_session_id
Expand Down Expand Up @@ -139,3 +140,11 @@ class Meta:
lambda _: faker.random_element(["STRIPE", "ETHEREUM"])
)
session_id = factory.LazyAttribute(lambda _: generate_session_id())


# !CategoryFactory
class CategoryFactory(factory.django.DjangoModelFactory):
class Meta:
model = Category

name = factory.LazyAttribute(lambda _: faker.word())
44 changes: 44 additions & 0 deletions backend/config/tests/posts/category/test_post_category.py
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 backend/config/tests/posts/category/test_post_category_endpoint.py
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

0 comments on commit 3cdf4e7

Please sign in to comment.