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.

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