diff --git a/Dshop/apps/products_catalogue/tests/test_api_products.py b/Dshop/apps/products_catalogue/tests/test_api_products.py index ec4ae55..2bf0344 100644 --- a/Dshop/apps/products_catalogue/tests/test_api_products.py +++ b/Dshop/apps/products_catalogue/tests/test_api_products.py @@ -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(): @@ -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 @@ -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')