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.exceptions import ValidationError
  4. from odoo.tests import common
  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", "name": "another_name"}
  13. )
  14. cls.env["res.partner.id_number"].create(
  15. {
  16. "name": "Bad ID",
  17. "category_id": bad_cat.id,
  18. "partner_id": cls.env.user.partner_id.id,
  19. }
  20. )
  21. cls.partner_id_category = cls.env["res.partner.id_category"].create(
  22. {"code": "id_code", "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. {
  27. "name": "Good ID",
  28. "category_id": cls.partner_id_category.id,
  29. "partner_id": cls.partner.id,
  30. }
  31. )
  32. @classmethod
  33. def tearDownClass(cls):
  34. teardown_test_model(cls.env, ResPartner)
  35. super().tearDownClass()
  36. def test_compute_identification(self):
  37. """ It should set the proper field to the proper ID name. """
  38. self.partner._compute_identification("name", "id_code")
  39. self.assertEqual(self.partner.name, self.partner_id.name)
  40. def test_inverse_identification_saves(self):
  41. """ It should set the ID name to the proper field value. """
  42. self.partner._inverse_identification("name", "id_code")
  43. self.assertEqual(self.partner_id.name, self.partner.name)
  44. def test_inverse_identification_creates_new_category(self):
  45. """ It should create a new category of the type if non-existent. """
  46. self.partner._inverse_identification("name", "new_code_type")
  47. category = self.env["res.partner.id_category"].search(
  48. [("code", "=", "new_code_type")]
  49. )
  50. self.assertTrue(category)
  51. def test_inverse_identification_creates_new_id(self):
  52. """ It should create a new ID of the type if non-existent. """
  53. category = self.env["res.partner.id_category"].create(
  54. {"code": "new_code_type", "name": "new_code_type"}
  55. )
  56. self.partner._inverse_identification("name", "new_code_type")
  57. identification = self.env["res.partner.id_number"].search(
  58. [("category_id", "=", category.id), ("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. {
  65. "name": "Another ID",
  66. "category_id": self.partner_id_category.id,
  67. "partner_id": self.partner.id,
  68. }
  69. )
  70. with self.assertRaises(ValidationError):
  71. self.partner._inverse_identification("name", "id_code")
  72. def test_search_identification(self):
  73. """ It should return the right record when searched by ID. """
  74. self.partner.social_security = "Test"
  75. partner = self.env["res.partner"].search([("social_security", "=", "Test")])
  76. self.assertEqual(partner, self.partner)