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.

126 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import fields, models
  5. from odoo.tests import common
  6. from odoo.exceptions import ValidationError
  7. class ResPartner(models.Model):
  8. _inherit = 'res.partner'
  9. social_security = fields.Char(
  10. compute=lambda s: s._compute_identification(
  11. 'social_security', 'SSN',
  12. ),
  13. inverse=lambda s: s._inverse_identification(
  14. 'social_security', 'SSN',
  15. ),
  16. search=lambda s, *a: s._search_identification(
  17. 'social_security', 'SSN', *a
  18. ),
  19. )
  20. class TestResPartner(common.SavepointCase):
  21. @classmethod
  22. def _init_test_model(cls, model_cls):
  23. """ Build a model from model_cls in order to test abstract models.
  24. Note that this does not actually create a table in the database, so
  25. there may be some unidentified edge cases.
  26. Args:
  27. model_cls (openerp.models.BaseModel): Class of model to initialize
  28. Returns:
  29. model_cls: Instance
  30. """
  31. registry = cls.env.registry
  32. cr = cls.env.cr
  33. inst = model_cls._build_model(registry, cr)
  34. model = cls.env[model_cls._inherit].with_context(todo=[])
  35. model._prepare_setup()
  36. model._setup_base(partial=False)
  37. model._setup_fields(partial=False)
  38. model._setup_complete()
  39. model._auto_init()
  40. model.init()
  41. model._auto_end()
  42. return inst
  43. @classmethod
  44. def setUpClass(cls):
  45. super(TestResPartner, cls).setUpClass()
  46. cls.env.registry.enter_test_mode()
  47. cls._init_test_model(ResPartner)
  48. def setUp(self):
  49. super(TestResPartner, self).setUp()
  50. bad_cat = self.env['res.partner.id_category'].create({
  51. 'code': 'another_code',
  52. 'name': 'another_name',
  53. })
  54. self.env['res.partner.id_number'].create({
  55. 'name': 'Bad ID',
  56. 'category_id': bad_cat.id,
  57. 'partner_id': self.env.user.partner_id.id,
  58. })
  59. self.partner_id_category = self.env['res.partner.id_category'].create({
  60. 'code': 'id_code',
  61. 'name': 'id_name',
  62. })
  63. self.partner = self.env.user.partner_id
  64. self.partner_id = self.env['res.partner.id_number'].create({
  65. 'name': 'Good ID',
  66. 'category_id': self.partner_id_category.id,
  67. 'partner_id': self.partner.id,
  68. })
  69. def test_compute_identification(self):
  70. """ It should set the proper field to the proper ID name. """
  71. self.partner._compute_identification('name', 'id_code')
  72. self.assertEqual(self.partner.name, self.partner_id.name)
  73. def test_inverse_identification_saves(self):
  74. """ It should set the ID name to the proper field value. """
  75. self.partner._inverse_identification('name', 'id_code')
  76. self.assertEqual(self.partner_id.name, self.partner.name)
  77. def test_inverse_identification_creates_new_category(self):
  78. """ It should create a new category of the type if non-existent. """
  79. self.partner._inverse_identification('name', 'new_code_type')
  80. category = self.env['res.partner.id_category'].search([
  81. ('code', '=', 'new_code_type'),
  82. ])
  83. self.assertTrue(category)
  84. def test_inverse_identification_creates_new_id(self):
  85. """ It should create a new ID of the type if non-existent. """
  86. category = self.env['res.partner.id_category'].create({
  87. 'code': 'new_code_type',
  88. 'name': 'new_code_type',
  89. })
  90. self.partner._inverse_identification('name', 'new_code_type')
  91. identification = self.env['res.partner.id_number'].search([
  92. ('category_id', '=', category.id),
  93. ('partner_id', '=', self.partner.id),
  94. ])
  95. self.assertEqual(identification.name, self.partner.name)
  96. def test_inverse_identification_multi_exception(self):
  97. """ It should not allow a write when multiple IDs of same type. """
  98. self.env['res.partner.id_number'].create({
  99. 'name': 'Another ID',
  100. 'category_id': self.partner_id_category.id,
  101. 'partner_id': self.partner.id,
  102. })
  103. with self.assertRaises(ValidationError):
  104. self.partner._inverse_identification('name', 'id_code')
  105. def test_search_identification(self):
  106. """ It should return the right record when searched by ID. """
  107. self.partner.social_security = 'Test'
  108. partner = self.env['res.partner'].search([
  109. ('social_security', '=', 'Test'),
  110. ])
  111. self.assertEqual(partner, self.partner)