/satchless/category/tests/category.py

https://github.com/bartels/satchless · Python · 207 lines · 162 code · 41 blank · 4 comment · 0 complexity · d1da9d4d3d0c1c4f42a45d0552adc15d MD5 · raw file

  1. import os
  2. from django.conf.urls.defaults import patterns, include, url
  3. from django.core.urlresolvers import reverse, NoReverseMatch
  4. from django.test import TestCase
  5. from ...util.tests import ViewsTestCase
  6. from ..app import MagicCategorizedProductApp
  7. __all__ = ['Views', 'Models', 'CategorizedProductUrlTests',
  8. 'NonCategorizedProductUrlTests']
  9. class TestCategorizedProductApp(MagicCategorizedProductApp):
  10. allow_uncategorized_product_urls = False
  11. category_app = TestCategorizedProductApp()
  12. class urls:
  13. urlpatterns = patterns('',
  14. url(r'^products/', include(category_app.urls)),
  15. )
  16. class Views(ViewsTestCase):
  17. urls = urls
  18. def setUp(self):
  19. self.animals = category_app.Category.objects.create(slug='animals',
  20. name=u'Animals')
  21. self.birds = category_app.Category.objects.create(slug='birds',
  22. name=u'Birds',
  23. parent=self.animals)
  24. self.parrots = category_app.Category.objects.create(slug='parrots',
  25. name=u'Parrorts',
  26. parent=self.birds)
  27. test_dir = os.path.dirname(__file__)
  28. self.custom_settings = {
  29. 'TEMPLATE_DIRS': (os.path.join(test_dir, '..', 'templates'),
  30. os.path.join(test_dir, 'templates')),
  31. }
  32. self.original_settings = self._setup_settings(self.custom_settings)
  33. def tearDown(self):
  34. self._teardown_settings(self.original_settings,
  35. self.custom_settings)
  36. def test_category_list(self):
  37. self._test_GET_status(reverse('product:category-index'))
  38. def test_category_details(self):
  39. response = self._test_GET_status(self.animals.get_absolute_url())
  40. self.assertTrue('category' in response.context)
  41. self.assertEqual(response.context['category'], self.animals)
  42. response = self._test_GET_status(self.parrots.get_absolute_url())
  43. self.assertTrue('category' in response.context)
  44. self.assertEqual(response.context['category'], self.parrots)
  45. def test_category_product_view(self):
  46. parrot_macaw = category_app.Product.objects.create(slug='macaw')
  47. self.animals.products.add(parrot_macaw)
  48. self.parrots.products.add(parrot_macaw)
  49. response = self._test_GET_status(parrot_macaw.get_absolute_url(category=self.animals))
  50. self.assertTrue('product' in response.context)
  51. self.assertEqual(response.context['product'], parrot_macaw)
  52. self._test_GET_status(parrot_macaw.get_absolute_url(category=self.parrots))
  53. self.assertTrue('product' in response.context)
  54. self.assertEqual(response.context['product'], parrot_macaw)
  55. class Models(TestCase):
  56. def setUp(self):
  57. self.animals = category_app.Category.objects.create(slug='animals',
  58. name=u'Animals')
  59. self.birds = category_app.Category.objects.create(slug='birds',
  60. name=u'Birds',
  61. parent=self.animals)
  62. self.parrots = category_app.Category.objects.create(slug='parrots',
  63. name=u'Parrorts',
  64. parent=self.birds)
  65. def test_paths(self):
  66. birds = category_app.Category.objects.create(slug='birds',
  67. name=u'Birds')
  68. storks = category_app.Category.objects.create(slug='storks',
  69. name=u'Storks',
  70. parent=birds)
  71. forks = category_app.Category.objects.create(slug='forks',
  72. name=u'Forks',
  73. parent=storks)
  74. category_app.Category.objects.create(slug='porks', name=u'Porks',
  75. parent=forks)
  76. borks = category_app.Category.objects.create(slug='borks',
  77. name=u'Borks',
  78. parent=forks)
  79. forks2 = category_app.Category.objects.create(slug='forks',
  80. name=u'Forks',
  81. parent=borks)
  82. yorks = category_app.Category.objects.create(slug='yorks',
  83. name=u'Yorks',
  84. parent=forks2)
  85. category_app.Category.objects.create(slug='orcs', name=u'Orcs',
  86. parent=forks2)
  87. self.assertEqual(
  88. [birds, storks, forks],
  89. category_app.path_from_slugs(['birds', 'storks', 'forks']))
  90. self.assertEqual(
  91. [birds, storks, forks, borks, forks2],
  92. category_app.path_from_slugs(['birds', 'storks', 'forks', 'borks', 'forks']))
  93. self.assertRaises(
  94. category_app.Category.DoesNotExist,
  95. category_app.path_from_slugs,
  96. (['birds', 'storks', 'borks', 'forks']))
  97. self.assertEqual(
  98. [birds, storks, forks, borks, forks2, yorks],
  99. category_app.path_from_slugs(['birds', 'storks', 'forks', 'borks', 'forks', 'yorks']))
  100. self.assertRaises(
  101. category_app.Category.DoesNotExist,
  102. category_app.path_from_slugs,
  103. (['birds', 'storks', 'forks', 'porks', 'forks', 'yorks']))
  104. class CategorizedProductUrlTests(TestCase):
  105. urls = urls
  106. def setUp(self):
  107. self.animals = category_app.Category.objects.create(slug='animals',
  108. name=u'Animals')
  109. self.birds = category_app.Category.objects.create(slug='birds',
  110. name=u'Birds',
  111. parent=self.animals)
  112. self.parrots = category_app.Category.objects.create(slug='parrots',
  113. name=u'Parrorts',
  114. parent=self.birds)
  115. self.parrot_macaw = category_app.Product.objects.create(slug='macaw')
  116. def test_categorised_product_url(self):
  117. self.animals.products.add(self.parrot_macaw)
  118. self.assertTrue('/products/animals/+macaw/' in
  119. self.parrot_macaw.get_absolute_url())
  120. def test_second_tier_categorised_product_url(self):
  121. self.birds.products.add(self.parrot_macaw)
  122. self.assertTrue('/products/animals/birds/+macaw/' in
  123. self.parrot_macaw.get_absolute_url())
  124. def test_third_tier_categorised_product_url(self):
  125. self.parrots.products.add(self.parrot_macaw)
  126. self.assertTrue('/products/animals/birds/parrots/+macaw/' in
  127. self.parrot_macaw.get_absolute_url())
  128. def test_product_url(self):
  129. """Products not in a Category should raise an exception."""
  130. self.assertRaises(NoReverseMatch, self.parrot_macaw.get_absolute_url)
  131. class CategorizedProductAppWithOrphans(MagicCategorizedProductApp):
  132. """A CategorizedProductApp that allows Products not in Categories"""
  133. allow_uncategorized_product_urls = True
  134. class NonCategorizedProductUrlTests(TestCase):
  135. """Urls include satchless-product-details"""
  136. class urls:
  137. urlpatterns = patterns('',
  138. url(r'^products/',
  139. include(CategorizedProductAppWithOrphans().urls)),
  140. )
  141. def setUp(self):
  142. self.animals = category_app.Category.objects.create(slug='animals',
  143. name=u'Animals')
  144. self.birds = category_app.Category.objects.create(slug='birds',
  145. name=u'Birds',
  146. parent=self.animals)
  147. self.parrots = category_app.Category.objects.create(slug='parrots',
  148. name=u'Parrorts',
  149. parent=self.birds)
  150. self.parrot_macaw = category_app.Product.objects.create(slug='macaw')
  151. def test_categorised_product_url(self):
  152. self.animals.products.add(self.parrot_macaw)
  153. self.assertTrue('/products/animals/+macaw/' in
  154. self.parrot_macaw.get_absolute_url())
  155. def test_second_tier_categorised_product_url(self):
  156. self.birds.products.add(self.parrot_macaw)
  157. self.assertTrue('/products/animals/birds/+macaw/' in
  158. self.parrot_macaw.get_absolute_url())
  159. def test_third_tier_categorised_product_url(self):
  160. self.parrots.products.add(self.parrot_macaw)
  161. self.assertTrue('/products/animals/birds/parrots/+macaw/' in
  162. self.parrot_macaw.get_absolute_url())
  163. def test_product_url(self):
  164. """Products not in a Category should have a url."""
  165. self.assertTrue('/products/+1-macaw/' in
  166. self.parrot_macaw.get_absolute_url())