from decimal import Decimal from django.core.exceptions import ValidationError from django.test import TestCase from shop.models import ProductVariant from shop.tests.mixins import CreateProductsMixin class TestProductVariants(TestCase, CreateProductsMixin): def setUp(self): self.product = self.create_product() self.size_m = self.create_attribute_value('Talla', 'M') self.size_l = self.create_attribute_value('Talla', 'L') def test_product_without_variants_has_variants_false(self): assert self.product.has_variants is False def test_product_with_variants_has_variants_true(self): self.create_product_variant(self.product, sku='V1', attribute_values=[self.size_m]) assert self.product.has_variants is True def test_variant_price_returns_current_price(self): variant = self.create_product_variant(self.product, sku='V1', price=Decimal('15.00'), attribute_values=[self.size_m]) assert variant.price.price == Decimal('15.00') def test_variant_str_includes_attribute_values(self): variant = self.create_product_variant(self.product, sku='V1', attribute_values=[self.size_m]) assert 'Talla: M' in str(variant) def test_min_variant_price_returns_lowest_current_price(self): self.create_product_variant(self.product, sku='V1', price=Decimal('20.00'), attribute_values=[self.size_m]) self.create_product_variant(self.product, sku='V2', price=Decimal('12.00'), attribute_values=[self.size_l]) assert self.product.min_variant_price.price == Decimal('12.00') def test_duplicated_attribute_combination_is_invalid(self): self.create_product_variant(self.product, sku='V1', attribute_values=[self.size_m]) duplicated = self.create_product_variant(self.product, sku='V2', attribute_values=[self.size_m]) with self.assertRaises(ValidationError): duplicated.full_clean() def test_different_attribute_combination_is_valid(self): self.create_product_variant(self.product, sku='V1', attribute_values=[self.size_m]) other = self.create_product_variant(self.product, sku='V2', attribute_values=[self.size_l]) other.full_clean()