You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from psycopg2._psycopg import IntegrityError
  5. import openerp.tests.common as common
  6. from openerp.exceptions import ValidationError
  7. class TestPartnerIdentificationBase(common.TransactionCase):
  8. def test_base_functionalities(self):
  9. """Dummy CRUD test
  10. """
  11. partner_id_category = self.env['res.partner.id_category'].create({
  12. 'code': 'id_code',
  13. 'name': 'id_name',
  14. })
  15. self.assertEqual(partner_id_category.name, 'id_name')
  16. self.assertEqual(partner_id_category.code, 'id_code')
  17. partner_1 = self.env.ref('base.res_partner_1')
  18. self.assertEquals(len(partner_1.id_numbers), 0)
  19. with self.assertRaises(IntegrityError), self.cr.savepoint():
  20. partner_1.write({'id_numbers': [(0, 0, {
  21. 'name': '1234',
  22. })]})
  23. partner_1.write({'id_numbers': [(0, 0, {
  24. 'name': '1234',
  25. 'category_id': partner_id_category.id
  26. })]})
  27. self.assertEquals(len(partner_1.id_numbers), 1)
  28. self.assertEquals(partner_1.id_numbers.name, '1234')
  29. class TestPartnerCategoryValidation(common.TransactionCase):
  30. def test_partner_id_number_validation(self):
  31. partner_id_category = self.env['res.partner.id_category'].create({
  32. 'code': 'id_code',
  33. 'name': 'id_name',
  34. 'validation_code': """
  35. if id_number.name != '1234':
  36. failed = True
  37. """
  38. })
  39. partner_1 = self.env.ref('base.res_partner_1')
  40. with self.assertRaises(ValidationError), self.cr.savepoint():
  41. partner_1.write({'id_numbers': [(0, 0, {
  42. 'name': '01234',
  43. 'category_id': partner_id_category.id
  44. })]})
  45. partner_1.write({'id_numbers': [(0, 0, {
  46. 'name': '1234',
  47. 'category_id': partner_id_category.id
  48. })]})
  49. self.assertEquals(len(partner_1.id_numbers), 1)
  50. self.assertEquals(partner_1.id_numbers.name, '1234')
  51. partner_id_category2 = self.env['res.partner.id_category'].create({
  52. 'code': 'id_code2',
  53. 'name': 'id_name2',
  54. 'validation_code': """
  55. if id_number.name != '1235':
  56. failed = True
  57. """})
  58. # check that the constrains is also checked when we change the
  59. # associated category
  60. with self.assertRaises(ValidationError), self.cr.savepoint():
  61. partner_1.id_numbers.write({
  62. 'category_id': partner_id_category2.id
  63. })
  64. def test_bad_calidation_code(self):
  65. partner_id_category = self.env['res.partner.id_category'].create({
  66. 'code': 'id_code',
  67. 'name': 'id_name',
  68. 'validation_code': """
  69. if id_number.name != '1234' # missing :
  70. failed = True
  71. """
  72. })
  73. partner_1 = self.env.ref('base.res_partner_1')
  74. with self.assertRaises(ValidationError):
  75. partner_1.write({'id_numbers': [(0, 0, {
  76. 'name': '1234',
  77. 'category_id': partner_id_category.id
  78. })]})