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.

87 lines
3.4 KiB

  1. # Copyright 2017 LasLabs Inc.
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo.tests import common
  4. from odoo.exceptions import ValidationError
  5. from .fake_models import ResPartner, setup_test_model, teardown_test_model
  6. class TestResPartner(common.SavepointCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super().setUpClass()
  10. setup_test_model(cls.env, ResPartner)
  11. bad_cat = cls.env['res.partner.id_category'].create({
  12. 'code': 'another_code',
  13. 'name': 'another_name',
  14. })
  15. cls.env['res.partner.id_number'].create({
  16. 'name': 'Bad ID',
  17. 'category_id': bad_cat.id,
  18. 'partner_id': cls.env.user.partner_id.id,
  19. })
  20. cls.partner_id_category = cls.env['res.partner.id_category'].create({
  21. 'code': 'id_code',
  22. 'name': 'id_name',
  23. })
  24. cls.partner = cls.env.ref('base.main_partner')
  25. cls.partner_id = cls.env['res.partner.id_number'].create({
  26. 'name': 'Good ID',
  27. 'category_id': cls.partner_id_category.id,
  28. 'partner_id': cls.partner.id,
  29. })
  30. @classmethod
  31. def tearDownClass(cls):
  32. teardown_test_model(cls.env, ResPartner)
  33. super().tearDownClass()
  34. def test_compute_identification(self):
  35. """ It should set the proper field to the proper ID name. """
  36. self.partner._compute_identification('name', 'id_code')
  37. self.assertEqual(self.partner.name, self.partner_id.name)
  38. def test_inverse_identification_saves(self):
  39. """ It should set the ID name to the proper field value. """
  40. self.partner._inverse_identification('name', 'id_code')
  41. self.assertEqual(self.partner_id.name, self.partner.name)
  42. def test_inverse_identification_creates_new_category(self):
  43. """ It should create a new category of the type if non-existent. """
  44. self.partner._inverse_identification('name', 'new_code_type')
  45. category = self.env['res.partner.id_category'].search([
  46. ('code', '=', 'new_code_type'),
  47. ])
  48. self.assertTrue(category)
  49. def test_inverse_identification_creates_new_id(self):
  50. """ It should create a new ID of the type if non-existent. """
  51. category = self.env['res.partner.id_category'].create({
  52. 'code': 'new_code_type',
  53. 'name': 'new_code_type',
  54. })
  55. self.partner._inverse_identification('name', 'new_code_type')
  56. identification = self.env['res.partner.id_number'].search([
  57. ('category_id', '=', category.id),
  58. ('partner_id', '=', self.partner.id),
  59. ])
  60. self.assertEqual(identification.name, self.partner.name)
  61. def test_inverse_identification_multi_exception(self):
  62. """ It should not allow a write when multiple IDs of same type. """
  63. self.env['res.partner.id_number'].create({
  64. 'name': 'Another ID',
  65. 'category_id': self.partner_id_category.id,
  66. 'partner_id': self.partner.id,
  67. })
  68. with self.assertRaises(ValidationError):
  69. self.partner._inverse_identification('name', 'id_code')
  70. def test_search_identification(self):
  71. """ It should return the right record when searched by ID. """
  72. self.partner.social_security = 'Test'
  73. partner = self.env['res.partner'].search([
  74. ('social_security', '=', 'Test'),
  75. ])
  76. self.assertEqual(partner, self.partner)