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.

110 lines
3.9 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. from odoo.tests import common
  6. from odoo.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. # create without required category
  20. with self.assertRaises(IntegrityError), self.cr.savepoint():
  21. partner_1.write({'id_numbers': [(0, 0, {
  22. 'name': '1234',
  23. })]})
  24. # successful creation
  25. partner_1.write({'id_numbers': [(0, 0, {
  26. 'name': '1234',
  27. 'category_id': partner_id_category.id
  28. })]})
  29. self.assertEquals(len(partner_1.id_numbers), 1)
  30. self.assertEquals(partner_1.id_numbers.name, '1234')
  31. # delete
  32. partner_1.write({'id_numbers': [(5, 0, 0)]})
  33. self.assertEquals(len(partner_1.id_numbers), 0)
  34. class TestPartnerCategoryValidation(common.TransactionCase):
  35. def test_partner_id_number_validation(self):
  36. partner_id_category = self.env['res.partner.id_category'].create({
  37. 'code': 'id_code',
  38. 'name': 'id_name',
  39. 'validation_code': """
  40. if id_number.name != '1234':
  41. failed = True
  42. """
  43. })
  44. partner_1 = self.env.ref('base.res_partner_1')
  45. with self.assertRaises(ValidationError), self.cr.savepoint():
  46. partner_1.write({'id_numbers': [(0, 0, {
  47. 'name': '01234',
  48. 'category_id': partner_id_category.id
  49. })]})
  50. partner_1.write({'id_numbers': [(0, 0, {
  51. 'name': '1234',
  52. 'category_id': partner_id_category.id
  53. })]})
  54. self.assertEquals(len(partner_1.id_numbers), 1)
  55. self.assertEquals(partner_1.id_numbers.name, '1234')
  56. partner_id_category2 = self.env['res.partner.id_category'].create({
  57. 'code': 'id_code2',
  58. 'name': 'id_name2',
  59. 'validation_code': """
  60. if id_number.name != '1235':
  61. failed = True
  62. """})
  63. # check that the constrains is also checked when we change the
  64. # associated category
  65. with self.assertRaises(ValidationError), self.cr.savepoint():
  66. partner_1.id_numbers.write({
  67. 'category_id': partner_id_category2.id
  68. })
  69. def test_bad_validation_code(self):
  70. partner_id_category = self.env['res.partner.id_category'].create({
  71. 'code': 'id_code',
  72. 'name': 'id_name',
  73. 'validation_code': """
  74. if id_number.name != '1234' # missing :
  75. failed = True
  76. """
  77. })
  78. partner_1 = self.env.ref('base.res_partner_1')
  79. with self.assertRaises(ValidationError):
  80. partner_1.write({'id_numbers': [(0, 0, {
  81. 'name': '1234',
  82. 'category_id': partner_id_category.id
  83. })]})
  84. def test_bad_validation_code_override(self):
  85. """ It should allow a bad validation code if context overrides. """
  86. partner_id_category = self.env['res.partner.id_category'].create({
  87. 'code': 'id_code',
  88. 'name': 'id_name',
  89. 'validation_code': """
  90. if id_number.name != '1234' # missing :
  91. failed = True
  92. """
  93. })
  94. partner_1 = self.env.ref('base.res_partner_1').with_context(
  95. id_no_validate=True,
  96. )
  97. partner_1.write({'id_numbers': [(0, 0, {
  98. 'name': '1234',
  99. 'category_id': partner_id_category.id
  100. })]})