Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

product_tests.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from django.contrib.auth.models import User
  2. from django.core.urlresolvers import reverse
  3. from django_dynamic_fixture import G
  4. from oscar.test import WebTestCase
  5. from oscar.apps.catalogue.models import ProductClass, Category, Product
  6. class ProductWebTest(WebTestCase):
  7. is_staff = True
  8. def setUp(self):
  9. self.user = User.objects.create_user(username='testuser',
  10. email='test@email.com',
  11. password='somefancypassword')
  12. self.user.is_staff = self.is_staff
  13. self.user.save()
  14. def get(self, url, **kwargs):
  15. kwargs['user'] = self.user
  16. return self.app.get(url, **kwargs)
  17. class TestGatewayPage(ProductWebTest):
  18. is_staff = True
  19. def test_redirects_to_list_page_when_no_query_param(self):
  20. url = reverse('dashboard:catalogue-product-create')
  21. response = self.get(url)
  22. self.assertRedirects(response,
  23. reverse('dashboard:catalogue-product-list'),
  24. status_code=301)
  25. def test_redirects_to_list_page_when_invalid_query_param(self):
  26. url = reverse('dashboard:catalogue-product-create')
  27. response = self.get(url + '?product_class=bad')
  28. self.assertRedirects(response,
  29. reverse('dashboard:catalogue-product-list'),
  30. status_code=301)
  31. def test_redirects_to_form_page_when_valid_query_param(self):
  32. pclass = G(ProductClass)
  33. url = reverse('dashboard:catalogue-product-create')
  34. response = self.get(url + '?product_class=%d' % pclass.id)
  35. self.assertRedirects(response,
  36. reverse('dashboard:catalogue-product-create',
  37. kwargs={'product_class_id': pclass.id}),
  38. status_code=301)
  39. class TestCreateGroupProduct(ProductWebTest):
  40. is_staff = True
  41. def setUp(self):
  42. self.pclass = G(ProductClass)
  43. super(TestCreateGroupProduct, self).setUp()
  44. def submit(self, title=None, category=None, upc=None):
  45. url = reverse('dashboard:catalogue-product-create',
  46. kwargs={'product_class_id': self.pclass.id})
  47. product_form = self.get(url).form
  48. product_form['title'] = title
  49. product_form['upc'] = upc
  50. if category:
  51. product_form['productcategory_set-0-category'] = category.id
  52. return product_form.submit()
  53. def test_title_is_required(self):
  54. response = self.submit(title='')
  55. self.assertContains(response, "Parent products must have a title")
  56. self.assertEquals(Product.objects.count(), 0)
  57. def test_requires_a_category(self):
  58. response = self.submit(title="Nice T-Shirt")
  59. self.assertContains(response,
  60. "A top-level product must have at least one category")
  61. self.assertEquals(Product.objects.count(), 0)
  62. def test_doesnt_smoke(self):
  63. category = G(Category)
  64. response = self.submit(category=category)
  65. self.assertContains(response, "Parent products must have a title")
  66. self.assertEquals(Product.objects.count(), 0)
  67. def test_doesnt_allow_duplicate_upc(self):
  68. G(Product, parent=None, upc="12345")
  69. category = G(Category)
  70. self.assertTrue(Product.objects.get(upc="12345"))
  71. response = self.submit(title="Nice T-Shirt", category=category,
  72. upc="12345")
  73. self.assertEquals(Product.objects.count(), 1)
  74. self.assertNotEquals(Product.objects.get(upc='12345').title,
  75. 'Nice T-Shirt')
  76. self.assertContains(response,
  77. "Product with this UPC already exists.")
  78. class TestCreateChildProduct(ProductWebTest):
  79. is_staff = True
  80. def setUp(self):
  81. self.pclass = G(ProductClass)
  82. self.parent = G(Product)
  83. super(TestCreateChildProduct, self).setUp()
  84. def test_categories_are_not_required(self):
  85. url = reverse('dashboard:catalogue-product-create',
  86. kwargs={'product_class_id': self.pclass.id})
  87. page = self.get(url)
  88. product_form = page.form
  89. product_form['title'] = 'Nice T-Shirt'
  90. product_form['parent'] = self.parent.id
  91. page = product_form.submit()
  92. try:
  93. product = Product.objects.get(title='Nice T-Shirt')
  94. except Product.DoesNotExist:
  95. self.fail('creating a child product did not work: %s' % page.body)
  96. self.assertEquals(product.parent, self.parent)