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.

154 lines
6.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
  4. import json
  5. from odoo.tests.common import TransactionCase
  6. class TestImproveTrackingValue(TransactionCase):
  7. def setUp(self):
  8. super(TestImproveTrackingValue, self).setUp()
  9. self.model = self.env['mail.tracking.value']
  10. self.msg = self.env['mail.message'].create({
  11. 'message_type': 'email'
  12. })
  13. self.mr = self.env.ref('base.res_partner_title_mister')
  14. self.dr = self.env.ref('base.res_partner_title_doctor')
  15. self.mm = self.env.ref('base.res_partner_title_madam')
  16. self.pf = self.env.ref('base.res_partner_title_prof')
  17. def test_change_one2many(self):
  18. """Test tracking one2many changes"""
  19. tracking = self.model.create_tracking_values(
  20. self.mr, self.dr, 'testing_col',
  21. {'string': 'TestingField', 'type': 'one2many'})
  22. self.assertEqual(tracking['old_value_char'], self.mr.display_name)
  23. self.assertEqual(tracking['new_value_char'], self.dr.display_name)
  24. self.assertEqual(tracking['old_value_text'], json.dumps([self.mr.id]))
  25. self.assertEqual(tracking['new_value_text'], json.dumps([self.dr.id]))
  26. def test_change_one2many_empty(self):
  27. """Test tracking one2many changes emtpy"""
  28. tracking = self.model.create_tracking_values(
  29. self.mr, None, 'testing_col',
  30. {'string': 'TestingField', 'type': 'one2many'})
  31. self.assertEqual(tracking['old_value_char'], self.mr.display_name)
  32. self.assertEqual(tracking['new_value_char'], '')
  33. self.assertEqual(tracking['old_value_text'], json.dumps([self.mr.id]))
  34. self.assertEqual(tracking['new_value_text'], json.dumps([]))
  35. def test_change_many2many(self):
  36. """Test tracking many2many changes"""
  37. oldvalue = self.env['res.partner.title'].browse([self.mr.id,
  38. self.dr.id])
  39. newvalue = self.env['res.partner.title'].browse([self.mm.id,
  40. self.pf.id])
  41. tracking = self.model.create_tracking_values(
  42. oldvalue, newvalue, 'testing_col',
  43. {'string': 'TestingField', 'type': 'many2many'})
  44. self.assertEqual(tracking['old_value_char'],
  45. self.mr.display_name + ', ' + self.dr.display_name)
  46. self.assertEqual(tracking['new_value_char'],
  47. self.mm.display_name + ', ' + self.pf.display_name)
  48. self.assertEqual(tracking['old_value_text'],
  49. json.dumps(oldvalue.ids))
  50. self.assertEqual(tracking['new_value_text'],
  51. json.dumps(newvalue.ids))
  52. def test_char_tracking_value(self):
  53. r = self.model.create({
  54. 'mail_message_id': self.msg.id,
  55. 'field_type': 'char',
  56. 'old_value_char': 'weakness',
  57. 'new_value_char': 'strength',
  58. 'field': 'test',
  59. 'field_desc': 'desc',
  60. })
  61. self.assertEqual(r.old_value_formatted, 'weakness')
  62. self.assertEqual(r.new_value_formatted, 'strength')
  63. def test_many2many_tracking_value(self):
  64. r = self.model.create({
  65. 'mail_message_id': self.msg.id,
  66. 'field_type': 'many2many',
  67. 'old_value_char': '123',
  68. 'new_value_char': '456',
  69. 'field': 'test',
  70. 'field_desc': 'desc',
  71. })
  72. self.assertEqual(r.old_value_formatted, '123')
  73. self.assertEqual(r.new_value_formatted, '456')
  74. def test_one2many_tracking_value(self):
  75. r = self.model.create({
  76. 'mail_message_id': self.msg.id,
  77. 'field_type': 'one2many',
  78. 'old_value_char': '123',
  79. 'new_value_char': '456',
  80. 'field': 'test',
  81. 'field_desc': 'desc',
  82. })
  83. self.assertEqual(r.old_value_formatted, '123')
  84. self.assertEqual(r.new_value_formatted, '456')
  85. def test_integer_tracking_value(self):
  86. r = self.model.create({
  87. 'mail_message_id': self.msg.id,
  88. 'field_type': 'integer',
  89. 'old_value_integer': 1,
  90. 'new_value_integer': 3,
  91. 'field': 'test',
  92. 'field_desc': 'desc',
  93. })
  94. self.assertEqual(r.old_value_formatted, str(1))
  95. self.assertEqual(r.new_value_formatted, str(3))
  96. def test_float_tracking_value(self):
  97. r = self.model.create({
  98. 'mail_message_id': self.msg.id,
  99. 'field_type': 'float',
  100. 'old_value_float': 1.1,
  101. 'new_value_float': 3.14159,
  102. 'field': 'test',
  103. 'field_desc': 'desc',
  104. })
  105. self.assertEqual(r.old_value_formatted, str(1.1))
  106. self.assertEqual(r.new_value_formatted, str(3.14159))
  107. def test_monetary_tracking_value(self):
  108. r = self.model.create({
  109. 'mail_message_id': self.msg.id,
  110. 'field_type': 'monetary',
  111. 'old_value_monetary': 3.45,
  112. 'new_value_monetary': 5.45,
  113. 'field': 'test',
  114. 'field_desc': 'desc',
  115. })
  116. self.assertEqual(r.old_value_formatted, str(3.45))
  117. self.assertEqual(r.new_value_formatted, str(5.45))
  118. def test_datetime_tracking_value(self):
  119. r = self.model.create({
  120. 'mail_message_id': self.msg.id,
  121. 'field_type': 'datetime',
  122. 'old_value_datetime': '2018-01-01',
  123. 'new_value_datetime': '2018-01-04',
  124. 'field': 'test',
  125. 'field_desc': 'desc',
  126. })
  127. self.assertEqual(r.old_value_formatted, '2018-01-01 00:00:00')
  128. self.assertEqual(r.new_value_formatted, '2018-01-04 00:00:00')
  129. def test_text_tracking_value(self):
  130. r = self.model.create({
  131. 'mail_message_id': self.msg.id,
  132. 'field_type': 'text',
  133. 'old_value_text': 'previous',
  134. 'new_value_text': 'next',
  135. 'field': 'test',
  136. 'field_desc': 'desc',
  137. })
  138. self.assertEqual(r.old_value_formatted, 'previous')
  139. self.assertEqual(r.new_value_formatted, 'next')