Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

product_tests.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_testsupport.testcases 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. def test_redirects_to_list_page_when_invalid_query_param(self):
  25. url = reverse('dashboard:catalogue-product-create')
  26. response = self.get(url + '?product_class=bad')
  27. self.assertRedirects(response,
  28. reverse('dashboard:catalogue-product-list'))
  29. def test_redirects_to_form_page_when_valid_query_param(self):
  30. pclass = G(ProductClass)
  31. url = reverse('dashboard:catalogue-product-create')
  32. response = self.get(url + '?product_class=%d' % pclass.id)
  33. self.assertRedirects(response,
  34. reverse('dashboard:catalogue-product-create',
  35. kwargs={'product_class_id': pclass.id}))
  36. class TestCreateGroupProduct(ProductWebTest):
  37. is_staff = True
  38. def setUp(self):
  39. self.pclass = G(ProductClass)
  40. super(TestCreateGroupProduct, self).setUp()
  41. def submit(self, title=None, category=None, upc=None):
  42. url = reverse('dashboard:catalogue-product-create',
  43. kwargs={'product_class_id': self.pclass.id})
  44. product_form = self.get(url).form
  45. product_form['title'] = title
  46. product_form['upc'] = upc
  47. if category:
  48. product_form['productcategory_set-0-category'] = category.id
  49. return product_form.submit()
  50. def test_title_is_required(self):
  51. response = self.submit(title='')
  52. self.assertContains(response, "Parent products must have a title")
  53. self.assertEquals(Product.objects.count(), 0)
  54. def test_requires_a_category(self):
  55. response = self.submit(title="Nice T-Shirt")
  56. self.assertContains(response,
  57. "A top-level product must have at least one category")
  58. self.assertEquals(Product.objects.count(), 0)
  59. def test_doesnt_smoke(self):
  60. category = G(Category)
  61. response = self.submit(category=category)
  62. self.assertContains(response, "Parent products must have a title")
  63. self.assertEquals(Product.objects.count(), 0)
  64. def test_doesnt_allow_duplicate_upc(self):
  65. G(Product, parent=None, upc="12345")
  66. category = G(Category)
  67. self.assertTrue(Product.objects.get(upc="12345"))
  68. response = self.submit(title="Nice T-Shirt", category=category,
  69. upc="12345")
  70. self.assertEquals(Product.objects.count(), 1)
  71. self.assertNotEquals(Product.objects.get(upc='12345').title,
  72. 'Nice T-Shirt')
  73. self.assertContains(response,
  74. "Product with this UPC already exists.")
  75. class TestCreateChildProduct(ProductWebTest):
  76. is_staff = True
  77. def setUp(self):
  78. self.pclass = G(ProductClass)
  79. self.parent = G(Product)
  80. super(TestCreateChildProduct, self).setUp()
  81. def test_categories_are_not_required(self):
  82. url = reverse('dashboard:catalogue-product-create',
  83. kwargs={'product_class_id': self.pclass.id})
  84. page = self.get(url)
  85. product_form = page.form
  86. product_form['title'] = 'Nice T-Shirt'
  87. product_form['parent'] = self.parent.id
  88. page = product_form.submit()
  89. try:
  90. product = Product.objects.get(title='Nice T-Shirt')
  91. except Product.DoesNotExist:
  92. self.fail('creating a child product did not work: %s' % page.body)
  93. self.assertEquals(product.parent, self.parent)