Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

product_tests.py 4.4KB

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