您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

catalogue_tests.py 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. from django.test import TestCase
  2. from django.core.exceptions import ValidationError
  3. from oscar.apps.catalogue.models import (Product, ProductClass, Category,
  4. ProductAttribute,
  5. AttributeOptionGroup,
  6. AttributeOption)
  7. from oscar.apps.catalogue.categories import create_from_breadcrumbs
  8. class TestProductClassModel(TestCase):
  9. def test_slug_is_auto_created(self):
  10. books = ProductClass.objects.create(
  11. name="Book",
  12. )
  13. self.assertEqual('book', books.slug)
  14. def test_has_attribute_for_whether_shipping_is_required(self):
  15. ""
  16. ProductClass.objects.create(
  17. name="Download",
  18. requires_shipping=False
  19. )
  20. class TestCategoryFactory(TestCase):
  21. def setUp(self):
  22. Category.objects.all().delete()
  23. def test_can_create_single_level_category(self):
  24. trail = 'Books'
  25. category = create_from_breadcrumbs(trail)
  26. self.assertIsNotNone(category)
  27. self.assertEquals(category.name, 'Books')
  28. self.assertEquals(category.slug, 'books')
  29. def test_can_create_parent_and_child_categories(self):
  30. trail = 'Books > Science-Fiction'
  31. category = create_from_breadcrumbs(trail)
  32. self.assertIsNotNone(category)
  33. self.assertEquals(category.name, 'Science-Fiction')
  34. self.assertEquals(category.get_depth(), 2)
  35. self.assertEquals(category.get_parent().name, 'Books')
  36. self.assertEquals(2, Category.objects.count())
  37. self.assertEquals(category.slug, 'books/science-fiction')
  38. def test_can_create_multiple_categories(self):
  39. trail = 'Books > Science-Fiction > Star Trek'
  40. create_from_breadcrumbs(trail)
  41. trail = 'Books > Factual > Popular Science'
  42. category = create_from_breadcrumbs(trail)
  43. self.assertIsNotNone(category)
  44. self.assertEquals(category.name, 'Popular Science')
  45. self.assertEquals(category.get_depth(), 3)
  46. self.assertEquals(category.get_parent().name, 'Factual')
  47. self.assertEquals(5, Category.objects.count())
  48. self.assertEquals(category.slug, 'books/factual/popular-science', )
  49. def test_can_use_alternative_separator(self):
  50. trail = 'Food|Cheese|Blue'
  51. create_from_breadcrumbs(trail, separator='|')
  52. self.assertEquals(3, len(Category.objects.all()))
  53. def test_updating_subtree_slugs_when_moving_category_to_new_parent(self):
  54. trail = 'A > B > C'
  55. create_from_breadcrumbs(trail)
  56. trail = 'A > B > D'
  57. create_from_breadcrumbs(trail)
  58. trail = 'A > E > F'
  59. create_from_breadcrumbs(trail)
  60. trail = 'A > E > G'
  61. create_from_breadcrumbs(trail)
  62. trail = 'T'
  63. target = create_from_breadcrumbs(trail)
  64. category = Category.objects.get(name='A')
  65. category.move(target, pos='first-child')
  66. c1 = Category.objects.get(name='A')
  67. self.assertEqual(c1.slug, 't/a')
  68. self.assertEqual(c1.full_name, 'T > A')
  69. child = Category.objects.get(name='F')
  70. self.assertEqual(child.slug, 't/a/e/f')
  71. self.assertEqual(child.full_name, 'T > A > E > F')
  72. child = Category.objects.get(name='D')
  73. self.assertEqual(child.slug, 't/a/b/d')
  74. self.assertEqual(child.full_name, 'T > A > B > D')
  75. def test_updating_subtree_when_moving_category_to_new_sibling(self):
  76. trail = 'A > B > C'
  77. create_from_breadcrumbs(trail)
  78. trail = 'A > B > D'
  79. create_from_breadcrumbs(trail)
  80. trail = 'A > E > F'
  81. create_from_breadcrumbs(trail)
  82. trail = 'A > E > G'
  83. create_from_breadcrumbs(trail)
  84. category = Category.objects.get(name='E')
  85. target = Category.objects.get(name='A')
  86. category.move(target, pos='right')
  87. child = Category.objects.get(name='E')
  88. self.assertEqual(child.slug, 'e')
  89. self.assertEqual(child.full_name, 'E')
  90. child = Category.objects.get(name='F')
  91. self.assertEqual(child.slug, 'e/f')
  92. self.assertEqual(child.full_name, 'E > F')
  93. class TestCategory(TestCase):
  94. def test_supports_has_children_method(self):
  95. """Supports has_children method"""
  96. root = Category.add_root(name="Products")
  97. self.assertFalse(root.has_children())
  98. root.add_child(name="Books")
  99. self.assertTrue(root.has_children())
  100. def test_enforces_slug_uniqueness(self):
  101. root = Category.add_root(name="Products")
  102. root.add_child(name="Books")
  103. with self.assertRaises(ValidationError):
  104. root.add_child(name="Books")
  105. class ProductTests(TestCase):
  106. def setUp(self):
  107. self.product_class,_ = ProductClass.objects.get_or_create(name='Clothing')
  108. class ProductCreationTests(ProductTests):
  109. def setUp(self):
  110. super(ProductCreationTests, self).setUp()
  111. ProductAttribute.objects.create(product_class=self.product_class,
  112. name='Number of pages',
  113. code='num_pages',
  114. type='integer')
  115. Product.ENABLE_ATTRIBUTE_BINDING = True
  116. def tearDown(self):
  117. Product.ENABLE_ATTRIBUTE_BINDING = False
  118. def test_create_products_with_attributes(self):
  119. product = Product(upc='1234',
  120. product_class=self.product_class,
  121. title='testing')
  122. product.attr.num_pages = 100
  123. product.save()
  124. class TopLevelProductTests(ProductTests):
  125. def test_top_level_products_must_have_titles(self):
  126. self.assertRaises(ValidationError, Product.objects.create, product_class=self.product_class)
  127. class VariantProductTests(ProductTests):
  128. def setUp(self):
  129. super(VariantProductTests, self).setUp()
  130. self.parent = Product.objects.create(title="Parent product", product_class=self.product_class)
  131. def test_variant_products_dont_need_titles(self):
  132. Product.objects.create(parent=self.parent, product_class=self.product_class)
  133. def test_variant_products_dont_need_a_product_class(self):
  134. Product.objects.create(parent=self.parent)
  135. def test_variant_products_inherit_parent_titles(self):
  136. p = Product.objects.create(parent=self.parent, product_class=self.product_class)
  137. self.assertEquals("Parent product", p.get_title())
  138. def test_variant_products_inherit_product_class(self):
  139. p = Product.objects.create(parent=self.parent)
  140. self.assertEquals("Clothing", p.get_product_class().name)
  141. class ProductAttributeCreationTests(TestCase):
  142. def setUp(self):
  143. self.product_class,_ = ProductClass.objects.get_or_create(
  144. name='Clothing'
  145. )
  146. self.option_group = AttributeOptionGroup.objects.create(name='group')
  147. self.option_1 = AttributeOption.objects.create(group=self.option_group, option='first')
  148. self.option_2 = AttributeOption.objects.create(group=self.option_group, option='second')
  149. def test_validating_option_attribute(self):
  150. pa = ProductAttribute.objects.create(product_class=self.product_class,
  151. name='test group',
  152. code='test_group',
  153. type='option',
  154. option_group=self.option_group)
  155. self.assertRaises(ValidationError, pa.get_validator(), 'invalid')
  156. try:
  157. pa.get_validator()(self.option_1)
  158. except ValidationError:
  159. self.fail("valid option '%s' not validated" % self.option_1)
  160. try:
  161. pa.get_validator()(self.option_2)
  162. except ValidationError:
  163. self.fail("valid option '%s' not validated" % self.option_1)
  164. invalid_option = AttributeOption()
  165. invalid_option.option = 'invalid option'
  166. self.assertRaises(ValidationError, pa.get_validator(),
  167. invalid_option)