You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

catalogue_tests.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from django.test import TestCase
  2. from django.core.exceptions import ValidationError
  3. from oscar.apps.catalogue.models import (Product, ProductClass,
  4. ProductAttribute,
  5. AttributeOptionGroup,
  6. AttributeOption)
  7. class TestProductClassModel(TestCase):
  8. def test_slug_is_auto_created(self):
  9. books = ProductClass.objects.create(
  10. name="Book",
  11. )
  12. self.assertEqual('book', books.slug)
  13. def test_has_attribute_for_whether_shipping_is_required(self):
  14. ""
  15. ProductClass.objects.create(
  16. name="Download",
  17. requires_shipping=False
  18. )
  19. class ProductTests(TestCase):
  20. def setUp(self):
  21. self.product_class,_ = ProductClass.objects.get_or_create(name='Clothing')
  22. class ProductCreationTests(ProductTests):
  23. def setUp(self):
  24. super(ProductCreationTests, self).setUp()
  25. ProductAttribute.objects.create(product_class=self.product_class,
  26. name='Number of pages',
  27. code='num_pages',
  28. type='integer')
  29. Product.ENABLE_ATTRIBUTE_BINDING = True
  30. def tearDown(self):
  31. Product.ENABLE_ATTRIBUTE_BINDING = False
  32. def test_create_products_with_attributes(self):
  33. product = Product(upc='1234',
  34. product_class=self.product_class,
  35. title='testing')
  36. product.attr.num_pages = 100
  37. product.save()
  38. class TopLevelProductTests(ProductTests):
  39. def test_top_level_products_must_have_titles(self):
  40. self.assertRaises(ValidationError, Product.objects.create, product_class=self.product_class)
  41. class VariantProductTests(ProductTests):
  42. def setUp(self):
  43. super(VariantProductTests, self).setUp()
  44. self.parent = Product.objects.create(title="Parent product", product_class=self.product_class)
  45. def test_variant_products_dont_need_titles(self):
  46. Product.objects.create(parent=self.parent, product_class=self.product_class)
  47. def test_variant_products_dont_need_a_product_class(self):
  48. Product.objects.create(parent=self.parent)
  49. def test_variant_products_inherit_parent_titles(self):
  50. p = Product.objects.create(parent=self.parent, product_class=self.product_class)
  51. self.assertEquals("Parent product", p.get_title())
  52. def test_variant_products_inherit_product_class(self):
  53. p = Product.objects.create(parent=self.parent)
  54. self.assertEquals("Clothing", p.get_product_class().name)
  55. class ProductAttributeCreationTests(TestCase):
  56. def setUp(self):
  57. self.product_class,_ = ProductClass.objects.get_or_create(
  58. name='Clothing'
  59. )
  60. self.option_group = AttributeOptionGroup.objects.create(name='group')
  61. self.option_1 = AttributeOption.objects.create(group=self.option_group, option='first')
  62. self.option_2 = AttributeOption.objects.create(group=self.option_group, option='second')
  63. def test_validating_option_attribute(self):
  64. pa = ProductAttribute.objects.create(product_class=self.product_class,
  65. name='test group',
  66. code='test_group',
  67. type='option',
  68. option_group=self.option_group)
  69. self.assertRaises(ValidationError, pa.get_validator(), 'invalid')
  70. try:
  71. pa.get_validator()(self.option_1)
  72. except ValidationError:
  73. self.fail("valid option '%s' not validated" % self.option_1)
  74. try:
  75. pa.get_validator()(self.option_2)
  76. except ValidationError:
  77. self.fail("valid option '%s' not validated" % self.option_1)
  78. invalid_option = AttributeOption()
  79. invalid_option.option = 'invalid option'
  80. self.assertRaises(ValidationError, pa.get_validator(),
  81. invalid_option)