| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- from django.test import TestCase
- from django.core.exceptions import ValidationError
-
- from oscar.apps.catalogue.models import (Product, ProductClass,
- ProductAttribute,
- AttributeOptionGroup,
- AttributeOption)
-
-
- class TestProductClassModel(TestCase):
-
- def test_slug_is_auto_created(self):
- books = ProductClass.objects.create(
- name="Book",
- )
- self.assertEqual('book', books.slug)
-
- def test_has_attribute_for_whether_shipping_is_required(self):
- ""
- ProductClass.objects.create(
- name="Download",
- requires_shipping=False
- )
-
-
- class ProductTests(TestCase):
-
- def setUp(self):
- self.product_class,_ = ProductClass.objects.get_or_create(name='Clothing')
-
-
- class ProductCreationTests(ProductTests):
-
- def setUp(self):
- super(ProductCreationTests, self).setUp()
- ProductAttribute.objects.create(product_class=self.product_class,
- name='Number of pages',
- code='num_pages',
- type='integer')
- Product.ENABLE_ATTRIBUTE_BINDING = True
-
- def tearDown(self):
- Product.ENABLE_ATTRIBUTE_BINDING = False
-
- def test_create_products_with_attributes(self):
- product = Product(upc='1234',
- product_class=self.product_class,
- title='testing')
- product.attr.num_pages = 100
- product.save()
-
-
- class TopLevelProductTests(ProductTests):
-
- def test_top_level_products_must_have_titles(self):
- self.assertRaises(ValidationError, Product.objects.create, product_class=self.product_class)
-
-
- class VariantProductTests(ProductTests):
-
- def setUp(self):
- super(VariantProductTests, self).setUp()
- self.parent = Product.objects.create(title="Parent product", product_class=self.product_class)
-
- def test_variant_products_dont_need_titles(self):
- Product.objects.create(parent=self.parent, product_class=self.product_class)
-
- def test_variant_products_dont_need_a_product_class(self):
- Product.objects.create(parent=self.parent)
-
- def test_variant_products_inherit_parent_titles(self):
- p = Product.objects.create(parent=self.parent, product_class=self.product_class)
- self.assertEquals("Parent product", p.get_title())
-
- def test_variant_products_inherit_product_class(self):
- p = Product.objects.create(parent=self.parent)
- self.assertEquals("Clothing", p.get_product_class().name)
-
-
- class ProductAttributeCreationTests(TestCase):
-
- def setUp(self):
- self.product_class,_ = ProductClass.objects.get_or_create(
- name='Clothing'
- )
- self.option_group = AttributeOptionGroup.objects.create(name='group')
- self.option_1 = AttributeOption.objects.create(group=self.option_group, option='first')
- self.option_2 = AttributeOption.objects.create(group=self.option_group, option='second')
-
- def test_validating_option_attribute(self):
- pa = ProductAttribute.objects.create(product_class=self.product_class,
- name='test group',
- code='test_group',
- type='option',
- option_group=self.option_group)
-
- self.assertRaises(ValidationError, pa.get_validator(), 'invalid')
-
- try:
- pa.get_validator()(self.option_1)
- except ValidationError:
- self.fail("valid option '%s' not validated" % self.option_1)
-
- try:
- pa.get_validator()(self.option_2)
- except ValidationError:
- self.fail("valid option '%s' not validated" % self.option_1)
-
- invalid_option = AttributeOption()
- invalid_option.option = 'invalid option'
- self.assertRaises(ValidationError, pa.get_validator(),
- invalid_option)
|