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.

122 lines
4.0 KiB

  1. # Copyright 2017 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import fields
  4. from odoo.tests.common import TransactionCase
  5. class TestParser(TransactionCase):
  6. def test_getting_parser(self):
  7. expected_parser = [
  8. "name",
  9. "active",
  10. "credit_limit",
  11. "color",
  12. ("category_id", ["name"]),
  13. ("country_id", ["name", "code"]),
  14. (
  15. "child_ids",
  16. [
  17. "name",
  18. "id",
  19. "email",
  20. ("country_id", ["name", "code"]),
  21. ("child_ids", ["name"]),
  22. ],
  23. ),
  24. "lang",
  25. "comment",
  26. ]
  27. exporter = self.env.ref("base_jsonify.ir_exp_partner")
  28. parser = exporter.get_json_parser()
  29. self.assertListEqual(parser, expected_parser)
  30. # modify an ir.exports_line to put an alias for a field
  31. self.env.ref("base_jsonify.category_id_name").write(
  32. {"alias": "category_id:category/name"}
  33. )
  34. expected_parser[4] = ("category_id:category", ["name"])
  35. parser = exporter.get_json_parser()
  36. self.assertEqual(parser, expected_parser)
  37. def test_json_export(self):
  38. parser = [
  39. "lang",
  40. "comment",
  41. "credit_limit",
  42. "name",
  43. "color",
  44. (
  45. "child_ids:children",
  46. [
  47. ("child_ids:children", ["name"]),
  48. "email",
  49. ("country_id:country", ["code", "name"]),
  50. "name",
  51. "id",
  52. ],
  53. ),
  54. ("country_id:country", ["code", "name"]),
  55. "active",
  56. ("category_id", ["name"]),
  57. "create_date",
  58. "date",
  59. ]
  60. partner = self.env["res.partner"].create(
  61. {
  62. "name": "Akretion",
  63. "country_id": self.env.ref("base.fr").id,
  64. "lang": "en_US", # default
  65. "category_id": [(0, 0, {"name": "Inovator"})],
  66. "child_ids": [
  67. (
  68. 0,
  69. 0,
  70. {
  71. "name": "Sebatien Beau",
  72. "country_id": self.env.ref("base.fr").id,
  73. },
  74. )
  75. ],
  76. "date": fields.Date.today(),
  77. }
  78. )
  79. expected_json = {
  80. "lang": "en_US",
  81. "comment": None,
  82. "credit_limit": 0.0,
  83. "name": "Akretion",
  84. "color": 0,
  85. "country": {"code": "FR", "name": "France"},
  86. "active": True,
  87. "category_id": [{"name": "Inovator"}],
  88. "children": [
  89. {
  90. "id": partner.child_ids.id,
  91. "country": {"code": "FR", "name": "France"},
  92. "children": [],
  93. "name": "Sebatien Beau",
  94. "email": None,
  95. }
  96. ],
  97. "create_date": fields.Datetime.to_string(partner.create_date),
  98. "date": fields.Date.to_string(partner.date),
  99. }
  100. json_partner = partner.jsonify(parser)
  101. self.assertDictEqual(json_partner[0], expected_json)
  102. json_partner = partner.jsonify(parser)
  103. self.assertDictEqual(json_partner[0], expected_json)
  104. # Check that only boolean fields have boolean values into json
  105. # By default if a field is not set into Odoo, the value is always False
  106. # This value is not the expected one into the json
  107. partner.write({"child_ids": [(6, 0, [])], "active": False, "lang": False})
  108. json_partner = partner.jsonify(parser)
  109. expected_json["active"] = False
  110. expected_json["lang"] = None
  111. expected_json["children"] = []
  112. self.assertDictEqual(json_partner[0], expected_json)