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.

203 lines
7.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from odoo.tests.common import TransactionCase
  5. class TestDarkroomModal(TransactionCase):
  6. def test_default_res_model_id_model_in_context(self):
  7. """Should return correct ir.model record when context has model name"""
  8. active_model = 'res.users'
  9. test_model = self.env['darkroom.modal'].with_context({
  10. 'active_model': active_model,
  11. })
  12. test_result = test_model._default_res_model_id()
  13. expected = self.env['ir.model'].search([('model', '=', active_model)])
  14. self.assertEqual(test_result, expected)
  15. def test_default_res_model_id_no_valid_info_in_context(self):
  16. """Should return empty ir.model recordset when missing/invalid info"""
  17. test_model = self.env['darkroom.modal'].with_context({})
  18. test_result = test_model._default_res_model_id()
  19. self.assertEqual(test_result, self.env['ir.model'])
  20. def test_default_res_record_id_id_in_context(self):
  21. """Should return correct value when ID in context"""
  22. active_record_id = 5
  23. test_model = self.env['darkroom.modal'].with_context({
  24. 'active_record_id': active_record_id,
  25. })
  26. test_result = test_model._default_res_record_id()
  27. self.assertEqual(test_result, active_record_id)
  28. def test_default_res_record_id_no_id_in_context(self):
  29. """Should return 0 when no ID in context"""
  30. test_model = self.env['darkroom.modal'].with_context({})
  31. test_result = test_model._default_res_record_id()
  32. self.assertEqual(test_result, 0)
  33. def test_default_res_record_model_and_id_in_context(self):
  34. """Should return correct record when context has model name and ID"""
  35. active_model = 'res.users'
  36. active_record_id = 1
  37. test_model = self.env['darkroom.modal'].with_context({
  38. 'active_model': active_model,
  39. 'active_record_id': active_record_id,
  40. })
  41. test_result = test_model._default_res_record()
  42. expected = self.env[active_model].browse(active_record_id)
  43. self.assertEqual(test_result, expected)
  44. def test_default_res_record_model_but_no_id_in_context(self):
  45. """Should return right empty recordset if model but no ID in context"""
  46. active_model = 'res.users'
  47. test_model = self.env['darkroom.modal'].with_context({
  48. 'active_model': active_model,
  49. })
  50. test_result = test_model._default_res_record()
  51. self.assertEqual(test_result, self.env[active_model])
  52. def test_default_res_record_no_valid_model_info_in_context(self):
  53. """Should return None if context has missing/invalid model info"""
  54. active_model = 'bad.model.name'
  55. test_model = self.env['darkroom.modal'].with_context({
  56. 'active_model': active_model,
  57. })
  58. test_result = test_model._default_res_record()
  59. self.assertIsNone(test_result)
  60. def test_default_res_field_id_model_and_field_in_context(self):
  61. """Should return correct ir.model.fields record when info in context"""
  62. active_model = 'res.users'
  63. active_field = 'name'
  64. test_model = self.env['darkroom.modal'].with_context({
  65. 'active_model': active_model,
  66. 'active_field': active_field,
  67. })
  68. test_result = test_model._default_res_field_id()
  69. self.assertEqual(test_result.name, active_field)
  70. self.assertEqual(test_result.model_id.model, active_model)
  71. def test_default_res_field_id_no_valid_field_in_context(self):
  72. """Should return empty recordset if field info missing/invalid"""
  73. active_model = 'res.users'
  74. active_field = 'totally.not.a.real.field.name'
  75. test_model = self.env['darkroom.modal'].with_context({
  76. 'active_model': active_model,
  77. 'active_field': active_field,
  78. })
  79. test_result = test_model._default_res_field_id()
  80. self.assertEqual(test_result, self.env['ir.model.fields'])
  81. def test_default_res_field_id_no_valid_model_in_context(self):
  82. """Should return empty recordset if model info missing/invalid"""
  83. active_field = 'name'
  84. test_model = self.env['darkroom.modal'].with_context({
  85. 'active_field': active_field,
  86. })
  87. test_result = test_model._default_res_field_id()
  88. self.assertEqual(test_result, self.env['ir.model.fields'])
  89. def test_default_image_all_info_in_context(self):
  90. """Should return value of correct field if all info in context"""
  91. active_model = 'res.users'
  92. active_record_id = 1
  93. active_field = 'name'
  94. test_model = self.env['darkroom.modal'].with_context({
  95. 'active_model': active_model,
  96. 'active_record_id': active_record_id,
  97. 'active_field': active_field,
  98. })
  99. test_result = test_model._default_image()
  100. expected = self.env[active_model].browse(active_record_id).name
  101. self.assertEqual(test_result, expected)
  102. def test_default_image_no_valid_field_in_context(self):
  103. """Should return None if missing/invalid field info in context"""
  104. active_model = 'res.users'
  105. active_record_id = 1
  106. test_model = self.env['darkroom.modal'].with_context({
  107. 'active_model': active_model,
  108. 'active_record_id': active_record_id,
  109. })
  110. test_result = test_model._default_image()
  111. self.assertIsNone(test_result)
  112. def test_default_image_no_valid_id_in_context(self):
  113. """Should return False/None if missing/invalid record ID in context"""
  114. active_model = 'res.users'
  115. active_field = 'name'
  116. test_model = self.env['darkroom.modal'].with_context({
  117. 'active_model': active_model,
  118. 'active_field': active_field,
  119. })
  120. test_result = test_model._default_image()
  121. self.assertFalse(test_result)
  122. def test_default_image_no_valid_model_in_context(self):
  123. """Should return None if missing/invalid model info in context"""
  124. active_record_id = 1
  125. active_field = 'name'
  126. test_model = self.env['darkroom.modal'].with_context({
  127. 'active_record_id': active_record_id,
  128. 'active_field': active_field,
  129. })
  130. test_result = test_model._default_image()
  131. self.assertIsNone(test_result)
  132. def test_action_save_record_count_in_self(self):
  133. """Should raise correct error if not called on recordset of 1"""
  134. test_wizard = self.env['darkroom.modal'].with_context({
  135. 'active_model': 'res.users',
  136. 'active_record_id': 1,
  137. 'active_field': 'name',
  138. }).create({})
  139. test_wizard_set = test_wizard + test_wizard.copy()
  140. with self.assertRaises(ValueError):
  141. self.env['darkroom.modal'].action_save()
  142. with self.assertRaises(ValueError):
  143. test_wizard_set.action_save()
  144. def test_action_save_update_source(self):
  145. """Should update source record correctly"""
  146. active_model = 'res.users'
  147. active_record_id = 1
  148. test_wizard = self.env['darkroom.modal'].with_context({
  149. 'active_model': active_model,
  150. 'active_record_id': active_record_id,
  151. 'active_field': 'name',
  152. }).create({})
  153. test_name = 'Test Name'
  154. test_wizard.image = test_name
  155. test_wizard.action_save()
  156. result = self.env[active_model].browse(active_record_id).name
  157. self.assertEqual(result, test_name)
  158. def test_action_save_return_action(self):
  159. """Should return correct action"""
  160. test_wizard = self.env['darkroom.modal'].with_context({
  161. 'active_model': 'res.users',
  162. 'active_record_id': 1,
  163. 'active_field': 'name',
  164. }).create({})
  165. test_value = test_wizard.action_save()
  166. self.assertEqual(test_value, {'type': 'ir.actions.act_window_close'})