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.

131 lines
4.7 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. '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. @classmethod
  49. def tearDownClass(cls):
  50. cls.env.registry.leave_test_mode()
  51. super(TestResPartner, cls).tearDownClass()
  52. def setUp(self):
  53. super(TestResPartner, self).setUp()
  54. bad_cat = self.env['res.partner.id_category'].create({
  55. 'code': 'another_code',
  56. 'name': 'another_name',
  57. })
  58. self.env['res.partner.id_number'].create({
  59. 'name': 'Bad ID',
  60. 'category_id': bad_cat.id,
  61. 'partner_id': self.env.user.partner_id.id,
  62. })
  63. self.partner_id_category = self.env['res.partner.id_category'].create({
  64. 'code': 'id_code',
  65. 'name': 'id_name',
  66. })
  67. self.partner = self.env.user.partner_id
  68. self.partner_id = self.env['res.partner.id_number'].create({
  69. 'name': 'Good ID',
  70. 'category_id': self.partner_id_category.id,
  71. 'partner_id': self.partner.id,
  72. })
  73. def test_compute_identification(self):
  74. """ It should set the proper field to the proper ID name. """
  75. self.partner._compute_identification('name', 'id_code')
  76. self.assertEqual(self.partner.name, self.partner_id.name)
  77. def test_inverse_identification_saves(self):
  78. """ It should set the ID name to the proper field value. """
  79. self.partner._inverse_identification('name', 'id_code')
  80. self.assertEqual(self.partner_id.name, self.partner.name)
  81. def test_inverse_identification_creates_new_category(self):
  82. """ It should create a new category of the type if non-existent. """
  83. self.partner._inverse_identification('name', 'new_code_type')
  84. category = self.env['res.partner.id_category'].search([
  85. ('code', '=', 'new_code_type'),
  86. ])
  87. self.assertTrue(category)
  88. def test_inverse_identification_creates_new_id(self):
  89. """ It should create a new ID of the type if non-existent. """
  90. category = self.env['res.partner.id_category'].create({
  91. 'code': 'new_code_type',
  92. 'name': 'new_code_type',
  93. })
  94. self.partner._inverse_identification('name', 'new_code_type')
  95. identification = self.env['res.partner.id_number'].search([
  96. ('category_id', '=', category.id),
  97. ('partner_id', '=', self.partner.id),
  98. ])
  99. self.assertEqual(identification.name, self.partner.name)
  100. def test_inverse_identification_multi_exception(self):
  101. """ It should not allow a write when multiple IDs of same type. """
  102. self.env['res.partner.id_number'].create({
  103. 'name': 'Another ID',
  104. 'category_id': self.partner_id_category.id,
  105. 'partner_id': self.partner.id,
  106. })
  107. with self.assertRaises(ValidationError):
  108. self.partner._inverse_identification('name', 'id_code')
  109. def test_search_identification(self):
  110. """ It should return the right record when searched by ID. """
  111. self.partner.social_security = 'Test'
  112. partner = self.env['res.partner'].search([
  113. ('social_security', '=', 'Test'),
  114. ])
  115. self.assertEqual(partner, self.partner)