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.

64 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Akretion (http://www.akretion.com).
  3. # Copyright 2019 ACSONE SA/NV
  4. # @author Sébastien BEAU <sebastien.beau@akretion.com>
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  6. import mock
  7. import odoo.tests.common as common
  8. class TestOnchange(common.TransactionCase):
  9. def test_playing_onchange_on_model(self):
  10. res_partner = self.env["res.partner"]
  11. with mock.patch.object(
  12. res_partner.__class__, "write"
  13. ) as patched_write:
  14. result = self.env["res.partner"].play_onchanges(
  15. {"company_type": "company"}, ["company_type"]
  16. )
  17. patched_write.assert_not_called()
  18. self.assertEqual(result["is_company"], True)
  19. def test_playing_onchange_on_record(self):
  20. company = self.env.ref("base.main_company")
  21. with mock.patch.object(company.__class__, "write") as patched_write:
  22. result = company.play_onchanges(
  23. {"email": "contact@akretion.com"}, ["email"]
  24. )
  25. patched_write.assert_not_called()
  26. modified_fields = set(result.keys())
  27. self.assertSetEqual(
  28. modified_fields, {"rml_footer", "rml_footer_readonly"}
  29. )
  30. self.assertEqual(
  31. result["rml_footer"],
  32. u"Phone: +1 555 123 8069 | Email: contact@akretion.com | "
  33. u"Website: http://www.example.com",
  34. )
  35. self.assertEqual(result["rml_footer_readonly"], result["rml_footer"])
  36. # check that the original record is not modified
  37. self.assertFalse(company._get_dirty())
  38. self.assertEqual(company.email, u"info@yourcompany.example.com")
  39. def test_onchange_record_with_dirty_field(self):
  40. company = self.env.ref("base.main_company")
  41. company._set_dirty("name")
  42. self.assertListEqual(company._get_dirty(), ["name"])
  43. company.play_onchanges({"email": "contact@akretion.com"}, ["email"])
  44. self.assertListEqual(company._get_dirty(), ["name"])
  45. def test_onchange_wrong_key(self):
  46. res_partner = self.env["res.partner"]
  47. with mock.patch.object(
  48. res_partner.__class__, "write"
  49. ) as patched_write:
  50. # we specify a wrong field name... This field should be
  51. # ignored
  52. result = self.env["res.partner"].play_onchanges(
  53. {"company_type": "company"}, ["company_type", "wrong_key"]
  54. )
  55. patched_write.assert_not_called()
  56. self.assertEqual(result["is_company"], True)