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.

117 lines
4.2 KiB

  1. # Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from psycopg2._psycopg import IntegrityError
  4. from odoo.tests import common
  5. from odoo.exceptions import ValidationError
  6. from odoo.tools import mute_logger
  7. class TestPartnerIdentificationBase(common.TransactionCase):
  8. def test_create_id_category(self):
  9. partner_id_category = self.env['res.partner.id_category'].create({
  10. 'code': 'id_code',
  11. 'name': 'id_name',
  12. })
  13. self.assertEqual(partner_id_category.name, 'id_name')
  14. self.assertEqual(partner_id_category.code, 'id_code')
  15. @mute_logger('odoo.sql_db')
  16. def test_update_partner_with_no_category(self):
  17. partner_1 = self.env.ref('base.res_partner_1')
  18. self.assertEqual(len(partner_1.id_numbers), 0)
  19. # create without required category
  20. with self.assertRaises(IntegrityError):
  21. partner_1.write({'id_numbers': [(0, 0, {
  22. 'name': '1234',
  23. })]})
  24. def test_update_partner_with_category(self):
  25. partner_1 = self.env.ref('base.res_partner_1')
  26. partner_id_category = self.env['res.partner.id_category'].create({
  27. 'code': 'new_code',
  28. 'name': 'new_name',
  29. })
  30. # successful creation
  31. partner_1.write({'id_numbers': [(0, 0, {
  32. 'name': '1234',
  33. 'category_id': partner_id_category.id
  34. })]})
  35. self.assertEqual(len(partner_1.id_numbers), 1)
  36. self.assertEqual(partner_1.id_numbers.name, '1234')
  37. # delete
  38. partner_1.write({'id_numbers': [(5, 0, 0)]})
  39. self.assertEqual(len(partner_1.id_numbers), 0)
  40. class TestPartnerCategoryValidation(common.TransactionCase):
  41. def test_partner_id_number_validation(self):
  42. partner_id_category = self.env['res.partner.id_category'].create({
  43. 'code': 'id_code',
  44. 'name': 'id_name',
  45. 'validation_code': """
  46. if id_number.name != '1234':
  47. failed = True
  48. """
  49. })
  50. partner_1 = self.env.ref('base.res_partner_1')
  51. with self.assertRaises(ValidationError), self.cr.savepoint():
  52. partner_1.write({'id_numbers': [(0, 0, {
  53. 'name': '01234',
  54. 'category_id': partner_id_category.id
  55. })]})
  56. partner_1.write({'id_numbers': [(0, 0, {
  57. 'name': '1234',
  58. 'category_id': partner_id_category.id
  59. })]})
  60. self.assertEqual(len(partner_1.id_numbers), 1)
  61. self.assertEqual(partner_1.id_numbers.name, '1234')
  62. partner_id_category2 = self.env['res.partner.id_category'].create({
  63. 'code': 'id_code2',
  64. 'name': 'id_name2',
  65. 'validation_code': """
  66. if id_number.name != '1235':
  67. failed = True
  68. """})
  69. # check that the constrains is also checked when we change the
  70. # associated category
  71. with self.assertRaises(ValidationError), self.cr.savepoint():
  72. partner_1.id_numbers.write({
  73. 'category_id': partner_id_category2.id
  74. })
  75. def test_bad_validation_code(self):
  76. partner_id_category = self.env['res.partner.id_category'].create({
  77. 'code': 'id_code',
  78. 'name': 'id_name',
  79. 'validation_code': """
  80. if id_number.name != '1234' # missing :
  81. failed = True
  82. """
  83. })
  84. partner_1 = self.env.ref('base.res_partner_1')
  85. with self.assertRaises(ValidationError):
  86. partner_1.write({'id_numbers': [(0, 0, {
  87. 'name': '1234',
  88. 'category_id': partner_id_category.id
  89. })]})
  90. def test_bad_validation_code_override(self):
  91. """ It should allow a bad validation code if context overrides. """
  92. partner_id_category = self.env['res.partner.id_category'].create({
  93. 'code': 'id_code',
  94. 'name': 'id_name',
  95. 'validation_code': """
  96. if id_number.name != '1234' # missing :
  97. failed = True
  98. """
  99. })
  100. partner_1 = self.env.ref('base.res_partner_1').with_context(
  101. id_no_validate=True,
  102. )
  103. partner_1.write({'id_numbers': [(0, 0, {
  104. 'name': '1234',
  105. 'category_id': partner_id_category.id
  106. })]})