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.

127 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. ('child_ids', [
  15. 'name',
  16. 'id',
  17. 'email',
  18. ('country_id', ['name', 'code']),
  19. ('child_ids', ['name']),
  20. ]),
  21. 'lang',
  22. 'comment'
  23. ]
  24. exporter = self.env.ref('base_jsonify.ir_exp_partner')
  25. parser = exporter.get_json_parser()
  26. self.assertListEqual(parser, expected_parser)
  27. # modify an ir.exports_line to put an alias for a field
  28. self.env.ref('base_jsonify.category_id_name').write({
  29. 'alias': 'category_id:category/name'
  30. })
  31. expected_parser[4] = ('category_id:category', ['name'])
  32. parser = exporter.get_json_parser()
  33. self.assertEqual(parser, expected_parser)
  34. def test_json_export(self):
  35. # Enforces TZ to validate the serialization result of a Datetime
  36. self.env.user.tz = "Europe/Brussels"
  37. parser = [
  38. 'lang',
  39. 'comment',
  40. 'credit_limit',
  41. 'name',
  42. 'color',
  43. ('child_ids:children', [
  44. ('child_ids:children', ['name']),
  45. 'email',
  46. ('country_id:country', ['code', 'name']),
  47. 'name',
  48. 'id',
  49. ]),
  50. ('country_id:country', ['code', 'name']),
  51. 'active',
  52. ('category_id', ['name']),
  53. 'create_date',
  54. 'date',
  55. ]
  56. partner = self.env['res.partner'].create({
  57. 'name': 'Akretion',
  58. 'country_id': self.env.ref('base.fr').id,
  59. 'lang': 'en_US', # default
  60. 'category_id': [(0, 0, {'name': 'Inovator'})],
  61. 'child_ids': [
  62. (0, 0, {
  63. 'name': 'Sebatien Beau',
  64. 'country_id': self.env.ref('base.fr').id
  65. })
  66. ],
  67. 'date': fields.Date.from_string("2019-10-31")
  68. })
  69. self.env.cr.execute(
  70. "update res_partner set create_date=%s where id=%s",
  71. ("2019-10-31 14:39:49", partner.id),
  72. )
  73. partner.refresh()
  74. expected_json = {
  75. 'lang': 'en_US',
  76. 'comment': None,
  77. 'credit_limit': 0.0,
  78. 'name': 'Akretion',
  79. 'color': 0,
  80. 'country': {
  81. 'code': 'FR',
  82. 'name': 'France'
  83. },
  84. 'active': True,
  85. 'category_id': [
  86. {'name': 'Inovator'}
  87. ],
  88. 'children': [{
  89. 'id': partner.child_ids.id,
  90. 'country': {
  91. 'code': 'FR',
  92. 'name': 'France'
  93. },
  94. 'children': [],
  95. 'name': 'Sebatien Beau',
  96. 'email': None
  97. }],
  98. "create_date": "2019-10-31T15:39:49+01:00",
  99. "date": "2019-10-31",
  100. }
  101. json_partner = partner.jsonify(parser)
  102. self.assertDictEqual(json_partner[0], expected_json)
  103. json_partner = partner.jsonify(parser)
  104. self.assertDictEqual(json_partner[0], expected_json)
  105. # Check that only boolean fields have boolean values into json
  106. # By default if a field is not set into Odoo, the value is always False
  107. # This value is not the expected one into the json
  108. partner.write({'child_ids': [(6, 0, [])],
  109. 'active': False,
  110. 'lang': False})
  111. json_partner = partner.jsonify(parser)
  112. expected_json['active'] = False
  113. expected_json['lang'] = None
  114. expected_json['children'] = []
  115. self.assertDictEqual(json_partner[0], expected_json)