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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from oscar.apps.catalogue.categories import create_from_breadcrumbs
  6. class CategoryTests(TestCase):
  7. def setUp(self):
  8. Category.objects.all().delete()
  9. def test_creating_category_root(self):
  10. trail = 'Books'
  11. category = create_from_breadcrumbs(trail)
  12. self.assertIsNotNone(category)
  13. self.assertEquals(category.name, 'Books')
  14. self.assertEquals(category.slug, 'books')
  15. def test_creating_parent_and_child_categories(self):
  16. trail = 'Books > Science-Fiction'
  17. category = create_from_breadcrumbs(trail)
  18. self.assertIsNotNone(category)
  19. self.assertEquals(category.name, 'Science-Fiction')
  20. self.assertEquals(category.get_depth(), 2)
  21. self.assertEquals(category.get_parent().name, 'Books')
  22. self.assertEquals(2, Category.objects.count())
  23. self.assertEquals(category.slug, 'books/science-fiction')
  24. def test_creating_multiple_categories(self):
  25. trail = 'Books > Science-Fiction > Star Trek'
  26. create_from_breadcrumbs(trail)
  27. trail = 'Books > Factual > Popular Science'
  28. category = create_from_breadcrumbs(trail)
  29. self.assertIsNotNone(category)
  30. self.assertEquals(category.name, 'Popular Science')
  31. self.assertEquals(category.get_depth(), 3)
  32. self.assertEquals(category.get_parent().name, 'Factual')
  33. self.assertEquals(5, Category.objects.count())
  34. self.assertEquals(category.slug, 'books/factual/popular-science', )
  35. def test_alternative_separator_can_be_used(self):
  36. trail = 'Food|Cheese|Blue'
  37. create_from_breadcrumbs(trail, separator='|')
  38. self.assertEquals(3, len(Category.objects.all()))
  39. class ProductTests(TestCase):
  40. def setUp(self):
  41. self.product_class,_ = ProductClass.objects.get_or_create(name='Clothing')
  42. class ProductCreationTests(ProductTests):
  43. def setUp(self):
  44. super(ProductCreationTests, self).setUp()
  45. ProductAttribute.objects.create(product_class=self.product_class,
  46. name='Number of pages',
  47. code='num_pages',
  48. type='integer')
  49. Product.ENABLE_ATTRIBUTE_BINDING = True
  50. def tearDown(self):
  51. Product.ENABLE_ATTRIBUTE_BINDING = False
  52. def test_create_products_with_attributes(self):
  53. product = Product(upc='1234',
  54. product_class=self.product_class,
  55. title='testing')
  56. product.attr.num_pages = 100
  57. product.save()
  58. class TopLevelProductTests(ProductTests):
  59. def test_top_level_products_must_have_titles(self):
  60. self.assertRaises(ValidationError, Product.objects.create, product_class=self.product_class)
  61. class VariantProductTests(ProductTests):
  62. def setUp(self):
  63. super(VariantProductTests, self).setUp()
  64. self.parent = Product.objects.create(title="Parent product", product_class=self.product_class)
  65. def test_variant_products_dont_need_titles(self):
  66. Product.objects.create(parent=self.parent, product_class=self.product_class)
  67. def test_variant_products_dont_need_a_product_class(self):
  68. Product.objects.create(parent=self.parent)
  69. def test_variant_products_inherit_parent_titles(self):
  70. p = Product.objects.create(parent=self.parent, product_class=self.product_class)
  71. self.assertEquals("Parent product", p.get_title())
  72. def test_variant_products_inherit_product_class(self):
  73. p = Product.objects.create(parent=self.parent)
  74. self.assertEquals("Clothing", p.get_product_class().name)