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.

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