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

FIX pylint (cherry picked from commit eadcae217103fd8f2f8d3db87c6fb659e048812b) Conflicts: partner_identification/__openerp__.py Set version to 8.0.1.0.0. Remove dependency on sales_team, as the relevant change is not in 8.0. Change emails to the new ones (cherry picked from commit 3455ae614e28d7807fc19d7be54512cbe59d44ec) Update new name (cherry picked from commit 5b592d7562fddac0cf48c71e6607cf17c009e993) [FIX] try me on runbot link (cherry picked from commit bd587b6058a17814cee5496a0ed3c126600f6fd6) Conflicts: partner_identification/README.rst Changed runbot to 8.0 [IMP] partner_identification: Add context override (#373) Allow for context override of validations using ``id_no_validate`` (cherry picked from commit 76c2e7b784916cdca0753a46a7b2be75edc1d70d) [10.0][IMP] partner_identification: Add field computation and inverses (#419) * [IMP] partner_identification: Add field computation and inverses * Add methods to allow for computation and inverse of an ID field of a specific category type * [IMP] partner_identification: Add search option (cherry picked from commit 19c5fb6de2a710dd50248fd843465f454de887bf) [FIX] partner_identification: Infinite loop in search (#436) (cherry picked from commit fa9b390dc62f66ef33acd7aacdfb3b79912ebc28) [FIX] partner-contact CI interactions (cherry picked from commit bc93e7bbc3e0f059b228970f0a05e57f0efba310) [ADD][8.0] Backport of the 9.0 module. (cherry picked from commit a42540381d448c3a62fabf69043ed23bf1aeca3e) [8.0][MIG] partner_identification backport
8 years ago
  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 openerp import fields, models
  5. from openerp.tests import common
  6. from openerp.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()
  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)