選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tests.py 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django.test import TestCase, Client
  2. from django.core.exceptions import ValidationError
  3. from django.core.urlresolvers import reverse
  4. from oscar.apps.catalogue.models import Product, ProductClass, Category
  5. from oscar.apps.catalogue.utils import breadcrumbs_to_category
  6. class CategoryTests(TestCase):
  7. def test_create_category_root(self):
  8. trail = 'Books'
  9. category = breadcrumbs_to_category(trail)
  10. self.assertIsNotNone(category)
  11. self.assertEquals(category.name, 'Books')
  12. self.assertEquals(category.slug, 'books')
  13. def test_subcategory(self):
  14. trail = 'Books > Science-Fiction'
  15. category = breadcrumbs_to_category(trail)
  16. self.assertIsNotNone(category)
  17. self.assertEquals(category.name, 'Science-Fiction')
  18. self.assertEquals(category.get_depth(), 2)
  19. self.assertEquals(category.get_parent().name, 'Books')
  20. self.assertEquals(2, Category.objects.count())
  21. self.assertEquals(category.slug, 'books/science-fiction')
  22. def test_subsubcategory(self):
  23. trail = 'Books > Science-Fiction > Star Trek'
  24. breadcrumbs_to_category(trail)
  25. trail = 'Books > Factual > Popular Science'
  26. category = breadcrumbs_to_category(trail)
  27. self.assertIsNotNone(category)
  28. self.assertEquals(category.name, 'Popular Science')
  29. self.assertEquals(category.get_depth(), 3)
  30. self.assertEquals(category.get_parent().name, 'Factual')
  31. self.assertEquals(5, Category.objects.count())
  32. self.assertEquals(category.slug, 'books/factual/popular-science', )
  33. class ItemTests(TestCase):
  34. def setUp(self):
  35. self.product_class,_ = ProductClass.objects.get_or_create(name='Clothing')
  36. class TopLevelItemTests(ItemTests):
  37. def test_top_level_products_must_have_titles(self):
  38. self.assertRaises(ValidationError, Product.objects.create, product_class=self.product_class)
  39. class VariantItemTests(ItemTests):
  40. def setUp(self):
  41. super(VariantItemTests, self).setUp()
  42. self.parent = Product.objects.create(title="Parent product", product_class=self.product_class)
  43. def test_variant_products_dont_need_titles(self):
  44. p = Product.objects.create(parent=self.parent, product_class=self.product_class)
  45. def test_variant_products_dont_need_a_product_class(self):
  46. p = Product.objects.create(parent=self.parent)
  47. def test_variant_products_inherit_parent_titles(self):
  48. p = Product.objects.create(parent=self.parent, product_class=self.product_class)
  49. self.assertEquals("Parent product", p.get_title())
  50. def test_variant_products_inherit_product_class(self):
  51. p = Product.objects.create(parent=self.parent)
  52. self.assertEquals("Clothing", p.get_product_class().name)
  53. class SingleProductViewTest(TestCase):
  54. fixtures = ['sample-products']
  55. def setUp(self):
  56. self.client = Client()
  57. def test_canonical_urls_are_enforced(self):
  58. p = Product.objects.get(id=1)
  59. args = {'product_slug': 'wrong-slug',
  60. 'pk': p.id}
  61. wrong_url = reverse('catalogue:detail', kwargs=args)
  62. response = self.client.get(wrong_url)
  63. self.assertEquals(301, response.status_code)