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.6 KiB

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