Skip to content

Commit

Permalink
test: added test test_product_detail_with_images
Browse files Browse the repository at this point in the history
  • Loading branch information
knop-k committed Feb 20, 2024
1 parent a436f74 commit 01acddf
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions Dshop/apps/products_catalogue/tests/test_api_products.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import pytest
from django.urls import reverse
from apps.products_catalogue.models import Category, Product
from apps.products_catalogue.models import Category, Product, ProductImage
# pylama:ignore=W0404, W0611
from apps.users.conftest import api_client, login_url, login_data, user_instance, user_instance_token

from PIL import Image
from django.core.files.uploadedfile import SimpleUploadedFile
import tempfile
import os

@pytest.fixture
def create_category():
Expand Down Expand Up @@ -42,6 +45,30 @@ def authenticated_api_client(api_client, user_instance_token):
return api_client


@pytest.fixture
def create_product_with_images(create_category):
product = Product.objects.create(
name="Product with images",
category=create_category,
price=29.99,
short_description="Product short description",
full_description="Product full description",
is_active=True
)

with tempfile.TemporaryDirectory() as temp_dir:
image_path1 = os.path.join(temp_dir, 'image1.jpg')
Image.new('RGB', (100, 100)).save(image_path1)
image1 = SimpleUploadedFile('image1.jpg', open(image_path1, 'rb').read(), content_type='image/jpeg')
ProductImage.objects.create(product=product, image=image1, is_featured=True)

image_path2 = os.path.join(temp_dir, 'image2.jpg')
Image.new('RGB', (150, 150)).save(image_path2)
image2 = SimpleUploadedFile('image2.jpg', open(image_path2, 'rb').read(), content_type='image/jpeg')
ProductImage.objects.create(product=product, image=image2, is_featured=False)
return product


def assert_active_object(data):
assert data['category'] is not None

Expand All @@ -53,11 +80,34 @@ def assert_active_object(data):
"short_description": "short desc",
"full_description": "full_description",
"parent_product": None,
"images": []
}
for key, value in data.items():
assert fields_values[key] == value


def assert_product_with_images(data):
assert 'images' in data
assert len(data['images']) == 2
expected_image_data1 = {
"image": data['images'][0]['image'],
}
expected_image_data2 = {
"image": data['images'][1]['image'],
}
assert expected_image_data1 in data['images']
assert expected_image_data2 in data['images']


@pytest.mark.django_db
def test_product_detail_with_images(authenticated_api_client, create_product_with_images):
url = reverse('products-api-detail', kwargs={'pk': create_product_with_images.id})
response = authenticated_api_client.get(url)

assert response.status_code == 200
assert_product_with_images(response.data)


@pytest.mark.django_db
def test_access_protected_resource(authenticated_api_client, create_active_product, create_inactive_product):
url = reverse('products-api-list')
Expand Down

0 comments on commit 01acddf

Please sign in to comment.