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.

111 lines
3.5 KiB

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