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.

120 lines
3.8 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. parser = [
  36. 'lang',
  37. 'comment',
  38. 'credit_limit',
  39. 'name',
  40. 'color',
  41. ('child_ids:children', [
  42. ('child_ids:children', ['name']),
  43. 'email',
  44. ('country_id:country', ['code', 'name']),
  45. 'name',
  46. 'id',
  47. ]),
  48. ('country_id:country', ['code', 'name']),
  49. 'active',
  50. ('category_id', ['name']),
  51. 'create_date',
  52. 'date',
  53. ]
  54. partner = self.env['res.partner'].create({
  55. 'name': 'Akretion',
  56. 'country_id': self.env.ref('base.fr').id,
  57. 'lang': 'en_US', # default
  58. 'category_id': [(0, 0, {'name': 'Inovator'})],
  59. 'child_ids': [
  60. (0, 0, {
  61. 'name': 'Sebatien Beau',
  62. 'country_id': self.env.ref('base.fr').id
  63. })
  64. ],
  65. 'date': fields.Date.today()
  66. })
  67. expected_json = {
  68. 'lang': 'en_US',
  69. 'comment': None,
  70. 'credit_limit': 0.0,
  71. 'name': 'Akretion',
  72. 'color': 0,
  73. 'country': {
  74. 'code': 'FR',
  75. 'name': 'France'
  76. },
  77. 'active': True,
  78. 'category_id': [
  79. {'name': 'Inovator'}
  80. ],
  81. 'children': [{
  82. 'id': partner.child_ids.id,
  83. 'country': {
  84. 'code': 'FR',
  85. 'name': 'France'
  86. },
  87. 'children': [],
  88. 'name': 'Sebatien Beau',
  89. 'email': None
  90. }],
  91. 'create_date': fields.Datetime.to_string(partner.create_date),
  92. 'date': fields.Date.to_string(partner.date)
  93. }
  94. json_partner = partner.jsonify(parser)
  95. self.assertDictEqual(json_partner[0], expected_json)
  96. json_partner = partner.jsonify(parser)
  97. self.assertDictEqual(json_partner[0], expected_json)
  98. # Check that only boolean fields have boolean values into json
  99. # By default if a field is not set into Odoo, the value is always False
  100. # This value is not the expected one into the json
  101. partner.write({'child_ids': [(6, 0, [])],
  102. 'active': False,
  103. 'lang': False})
  104. json_partner = partner.jsonify(parser)
  105. expected_json['active'] = False
  106. expected_json['lang'] = None
  107. expected_json['children'] = []
  108. self.assertDictEqual(json_partner[0], expected_json)