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.

129 lines
4.3 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. # Enforces TZ to validate the serialization result of a Datetime
  39. self.env.user.tz = "Europe/Brussels"
  40. parser = [
  41. "lang",
  42. "comment",
  43. "credit_limit",
  44. "name",
  45. "color",
  46. (
  47. "child_ids:children",
  48. [
  49. ("child_ids:children", ["name"]),
  50. "email",
  51. ("country_id:country", ["code", "name"]),
  52. "name",
  53. "id",
  54. ],
  55. ),
  56. ("country_id:country", ["code", "name"]),
  57. "active",
  58. ("category_id", ["name"]),
  59. "create_date",
  60. "date",
  61. ]
  62. partner = self.env["res.partner"].create(
  63. {
  64. "name": "Akretion",
  65. "country_id": self.env.ref("base.fr").id,
  66. "lang": "en_US", # default
  67. "category_id": [(0, 0, {"name": "Inovator"})],
  68. "child_ids": [
  69. (
  70. 0,
  71. 0,
  72. {
  73. "name": "Sebatien Beau",
  74. "country_id": self.env.ref("base.fr").id,
  75. },
  76. )
  77. ],
  78. "date": fields.Date.from_string("2019-10-31"),
  79. }
  80. )
  81. # put our own create date to ease tests
  82. self.env.cr.execute(
  83. "update res_partner set create_date=%s where id=%s",
  84. ("2019-10-31 14:39:49", partner.id),
  85. )
  86. expected_json = {
  87. "lang": "en_US",
  88. "comment": None,
  89. "credit_limit": 0.0,
  90. "name": "Akretion",
  91. "color": 0,
  92. "country": {"code": "FR", "name": "France"},
  93. "active": True,
  94. "category_id": [{"name": "Inovator"}],
  95. "children": [
  96. {
  97. "id": partner.child_ids.id,
  98. "country": {"code": "FR", "name": "France"},
  99. "children": [],
  100. "name": "Sebatien Beau",
  101. "email": None,
  102. }
  103. ],
  104. "create_date": "2019-10-31T15:39:49+01:00",
  105. "date": "2019-10-31",
  106. }
  107. json_partner = partner.jsonify(parser)
  108. self.assertDictEqual(json_partner[0], expected_json)
  109. json_partner = partner.jsonify(parser)
  110. self.assertDictEqual(json_partner[0], expected_json)
  111. # Check that only boolean fields have boolean values into json
  112. # By default if a field is not set into Odoo, the value is always False
  113. # This value is not the expected one into the json
  114. partner.write({"child_ids": [(6, 0, [])], "active": False, "lang": False})
  115. json_partner = partner.jsonify(parser)
  116. expected_json["active"] = False
  117. expected_json["lang"] = None
  118. expected_json["children"] = []
  119. self.assertDictEqual(json_partner[0], expected_json)