Skip to content

Commit

Permalink
Complete tests for pet
Browse files Browse the repository at this point in the history
  • Loading branch information
riadelimemmedov committed Feb 17, 2024
1 parent 53d92c3 commit be16f5d
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 73 deletions.
16 changes: 15 additions & 1 deletion backend/apps/pet/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 5.0.1 on 2024-02-16 04:19
# Generated by Django 5.0.1 on 2024-02-17 04:37

import colorfield.fields
import django.core.validators
import django_extensions.db.fields
from django.db import migrations, models

Expand Down Expand Up @@ -61,6 +62,19 @@ class Migration(migrations.Migration):
verbose_name="Gender",
),
),
(
"pet_photo_url",
models.FileField(
blank=True,
upload_to="",
validators=[
django.core.validators.FileExtensionValidator(
allowed_extensions=["png", "jpg", "jpeg"]
)
],
verbose_name="Pet photo",
),
),
("location", models.CharField(max_length=50, verbose_name="Location")),
("status", models.BooleanField(default=True, verbose_name="Status")),
(
Expand Down
19 changes: 19 additions & 0 deletions backend/apps/pet/migrations/0002_pet_city.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.1 on 2024-02-17 04:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("pet", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="pet",
name="city",
field=models.CharField(default=1, max_length=50, verbose_name="City"),
preserve_default=False,
),
]
29 changes: 0 additions & 29 deletions backend/apps/pet/migrations/0002_pet_pet_photo_url.py

This file was deleted.

28 changes: 0 additions & 28 deletions backend/apps/pet/migrations/0003_alter_pet_pet_photo_url.py

This file was deleted.

1 change: 1 addition & 0 deletions backend/apps/pet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Pet(TimeStampedModel):
validators=[FileExtensionValidator(allowed_extensions=["png", "jpg", "jpeg"])],
)
location = models.CharField(_("Location"), max_length=50)
city = models.CharField(_("City"), max_length=50)
status = models.BooleanField(_("Status"), default=True)
vaccinated = models.BooleanField(_("Vaccinated"), default=True)
description = models.TextField(_("Description"), max_length=150)
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/upload/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.1 on 2024-02-16 05:43
# Generated by Django 5.0.1 on 2024-02-17 04:37

from django.db import migrations, models

Expand Down
2 changes: 1 addition & 1 deletion backend/apps/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.1 on 2024-02-16 04:19
# Generated by Django 5.0.1 on 2024-02-17 04:37

import django.utils.timezone
from django.db import migrations, models
Expand Down
32 changes: 20 additions & 12 deletions backend/config/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models.signals import post_save
from faker import Faker

from abstract.constants import Genders, Status, Types
from abstract.constants import Genders, GendersPet, Status, Types
from apps.pet.models import Pet
from apps.user_profile.models import Profile

# Faker
faker = Faker()

# *User

User = get_user_model()
Expand Down Expand Up @@ -59,14 +63,18 @@ class PetFactory(factory.django.DjangoModelFactory):
class Meta:
model = Pet

name = "Max"
age = 3
breed = "Labrador"
color = "#D859FF"
weight = 25.5
gender = "male"
pet_photo_url = SimpleUploadedFile("test.jpg", b"")
location = "New York"
status = True
vaccinated = True
description = "Energetic and playful pup"
for _ in range(10):
name = faker.name()
age = faker.random_int(min=1, max=15)
breed = faker.name()
color = faker.hex_color()
weight = faker.random_int(min=1, max=50)
gender = faker.random_element(["MALE", "FEMALE"])
pet_photo_url = faker.file_extension(
category="image",
)
location = faker.address()
city = faker.city()
status = faker.boolean(chance_of_getting_true=50)
vaccinated = faker.boolean(chance_of_getting_true=50)
description = faker.text(max_nb_chars=150)
167 changes: 166 additions & 1 deletion backend/config/tests/pet/test_pet.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,180 @@
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


# !TestPetManagers
class TestPetManagers:
def create_mock_pet(self, pet_factory):
pet_factory(
name="Max",
age=3,
breed="Labrador",
color="#ff00ff",
weight=25.5,
gender="Male",
pet_photo_url=SimpleUploadedFile("test.jpg", b""),
location="Street 1",
city="Chicago",
status=True,
vaccinated=True,
description="Energetic and playful pup",
)
pet_factory(
age=2,
breed="Golden Retriever",
color="#f9a602",
weight=28.7,
gender="Female",
pet_photo_url=SimpleUploadedFile("test.jpg", b""),
location="Park Avenue",
city="New York",
status=True,
vaccinated=True,
description="Friendly and obedient companion",
)
pet_factory(
name="Charlie",
age=4,
breed="Beagle",
color="#aabbcc",
weight=15.2,
gender="Male",
pet_photo_url=SimpleUploadedFile("test.jpg", b""),
location="123 Main Street",
city="Los Angeles",
status=False,
vaccinated=True,
description="Loyal and playful companion",
)

def test_str_method(self, pet_factory):
obj = pet_factory()
assert obj.__str__() == "Max - 3"
assert obj.__str__() is not None
TruncateTestData(Pet)

def test_max_length_method(self, pet_factory):
name = "a" * 50
breed = "b" * 50
gender = "c" * 50
location = "d" * 50
city = "e" * 50
description = "f" * 150
obj = pet_factory()
if (
len(name) > 50
or len(breed) > 50
or len(gender) > 50
or len(location) > 50
or len(city) > 50
or len(description) > 150
):
with pytest.raises(ValidationError):
obj.full_clean()
else:
assert len(name) <= 50
assert len(gender) <= 50
assert len(location) <= 50
assert len(city) <= 50
assert len(description) <= 150
TruncateTestData(Pet)

def test_status_active_or_passive(self, pet_factory):
obj = pet_factory()
if obj.status:
assert obj.status is True
obj.status = False
assert obj.status is False
TruncateTestData(Pet)

def test_pet(self, pet_factory):
obj = pet_factory()
assert obj is not None
TruncateTestData(Pet)

def test_active_status(self, pet_factory):
self.create_mock_pet(pet_factory)
qs = Pet.objects.get_active_status().active_status()
for obj in qs:
assert obj.status is True
TruncateTestData(Pet)

def test_active_status_count(self, pet_factory):
self.create_mock_pet(pet_factory)
qs = Pet.objects.get_active_status().active_status_count()
assert qs == 0 or qs > 0
TruncateTestData(Pet)

def test_passive_status(self, pet_factory):
self.create_mock_pet(pet_factory)
qs = Pet.objects.get_passive_status().passive_status()
for obj in qs:
assert obj.status is False
TruncateTestData(Pet)

def test_passive_status_count(self, pet_factory):
self.create_mock_pet(pet_factory)
qs = Pet.objects.get_passive_status().passive_status_count()
assert qs == 0 or qs > 0
TruncateTestData(Pet)

def test_pet_photo_validation(self, pet_factory):
obj = pet_factory()
if obj.pet_photo_url not in ImageExtension:
with pytest.raises(ValidationError):
obj.full_clean()
TruncateTestData(Pet)

def test_age(self, pet_factory):
obj = pet_factory()
if obj.age < 0 or obj.age > 15:
with pytest.raises(ValidationError):
obj.full_clean()
TruncateTestData(Pet)

def test_color(self, pet_factory):
obj = pet_factory()
assert obj.color.startswith("#") is True
TruncateTestData(Pet)

def test_weight(self, pet_factory):
obj = pet_factory()
if obj.weight < 1 or obj.weight > 50:
with pytest.raises(ValidationError):
obj.full_clean()
TruncateTestData(Pet)

def test_gender(self, pet_factory):
obj = pet_factory()
if obj.gender not in GendersPet:
with pytest.raises(ValidationError):
obj.full_clean()
TruncateTestData(Pet)

def test_pet_creation(self, pet_factory):
obj = pet_factory(
name="Max",
age=3,
breed="Labrador",
color="#ff00ff",
weight=25.5,
gender="Male",
pet_photo_url=SimpleUploadedFile("test.jpg", b""),
location="Street 22",
city="New York",
status=True,
vaccinated=True,
description="Energetic and playful pup",
)
assert obj.created is not None
assert obj.modified is not None
TruncateTestData(Pet)

0 comments on commit be16f5d

Please sign in to comment.