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.

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