test_module.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # coding: utf-8
  2. # Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp.tests.common import TransactionCase
  6. from openerp.exceptions import ValidationError
  7. class TestModule(TransactionCase):
  8. def setUp(self):
  9. super(TestModule, self).setUp()
  10. self.product_obj = self.env['product.product']
  11. self.main_company = self.env.ref('base.main_company')
  12. # Test Section
  13. def _create_product(self, ean13, company_id):
  14. return self.product_obj.create({
  15. 'name': 'Test',
  16. 'ean13': ean13,
  17. 'company_id': company_id,
  18. 'categ_id': self.env.ref('product.product_category_all').id
  19. })
  20. def test_01_create_duplicate(self):
  21. """constrains of unicity for ean13 and company_id"""
  22. # Create a first product with an ean13, shoud work
  23. product = self._create_product('3760138839329', self.main_company.id)
  24. # Create another product with the same ean13, in another company
  25. # should work
  26. self._create_product('3760138839329', False)
  27. # Create again, with the same ean13, should fail
  28. with self.assertRaises(ValidationError):
  29. self._create_product('3760138839329', self.main_company.id)
  30. # Create again, with the same ean13, should fail even if original
  31. # product is disabled
  32. product.active = False
  33. with self.assertRaises(ValidationError):
  34. self._create_product('3760138839329', self.main_company.id)
  35. def test_02_copy_product(self):
  36. """Copy product should not copy ean13 field."""
  37. # Create a first product with an ean13, shoud work
  38. product = self._create_product('3760138839329', self.main_company.id)
  39. new_product = product.copy()
  40. self.assertEqual(
  41. new_product.ean13, False,
  42. "Copy a product should set the ean13 field to false")
  43. def test_03_duplicate_view(self):
  44. """Check if existing duplicates are correctly displayed"""
  45. # Create two products
  46. product1 = self._create_product(False, self.main_company.id)
  47. product2 = self._create_product(False, self.main_company.id)
  48. sql_req = """
  49. UPDATE product_product
  50. SET ean13 = %s where id in %s
  51. """
  52. args = ('3760138839329', (product1.id, product2.id),)
  53. self.env.cr.execute(sql_req, args) # pylint: disable=invalid-commit
  54. res = self.product_obj.search([('ean_duplicates_exist', '=', True)])
  55. self.assertEqual(
  56. len(res), 2,
  57. "Incorrect result of the function _search_ean_duplicates_exist")