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.

212 lines
8.1 KiB

7 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. import logging
  5. import tempfile
  6. import mock
  7. from odoo.modules import get_module_path
  8. from odoo.tests.common import TransactionCase
  9. from .. import post_init_hook
  10. _logger = logging.getLogger(__name__)
  11. try:
  12. from checksumdir import dirhash
  13. except ImportError:
  14. _logger.debug('Cannot `import checksumdir`.')
  15. model = 'odoo.addons.module_auto_update.models.module'
  16. class TestModule(TransactionCase):
  17. def setUp(self):
  18. super(TestModule, self).setUp()
  19. module_name = 'module_auto_update'
  20. self.own_module = self.env['ir.module.module'].search([
  21. ('name', '=', module_name),
  22. ])
  23. self.own_dir_path = get_module_path(module_name)
  24. self.own_checksum = dirhash(
  25. self.own_dir_path,
  26. 'sha1',
  27. excluded_extensions=['pyc', 'pyo'],
  28. )
  29. @mock.patch('%s.get_module_path' % model)
  30. def create_test_module(self, vals, get_module_path_mock):
  31. get_module_path_mock.return_value = self.own_dir_path
  32. test_module = self.env['ir.module.module'].create(vals)
  33. return test_module
  34. def test_compute_checksum_dir(self):
  35. """It should compute the directory's SHA-1 hash"""
  36. self.assertEqual(
  37. self.own_module.checksum_dir, self.own_checksum,
  38. 'Module directory checksum not computed properly',
  39. )
  40. def test_compute_checksum_dir_ignore_excluded(self):
  41. """It should exclude .pyc/.pyo extensions from checksum
  42. calculations"""
  43. with tempfile.NamedTemporaryFile(
  44. suffix='.pyc', dir=self.own_dir_path):
  45. self.assertEqual(
  46. self.own_module.checksum_dir, self.own_checksum,
  47. 'SHA1 checksum does not ignore excluded extensions',
  48. )
  49. def test_compute_checksum_dir_recomputes_when_file_added(self):
  50. """It should return a different value when a non-.pyc/.pyo file is
  51. added to the module directory"""
  52. with tempfile.NamedTemporaryFile(
  53. suffix='.py', dir=self.own_dir_path):
  54. self.assertNotEqual(
  55. self.own_module.checksum_dir, self.own_checksum,
  56. 'SHA1 checksum not recomputed',
  57. )
  58. def test_store_checksum_installed_state_installed(self):
  59. """It should set the module's checksum_installed equal to
  60. checksum_dir when vals contain state 'installed'"""
  61. self.own_module.checksum_installed = 'test'
  62. self.own_module._store_checksum_installed({'state': 'installed'})
  63. self.assertEqual(
  64. self.own_module.checksum_installed, self.own_module.checksum_dir,
  65. 'Setting state to installed does not store checksum_dir '
  66. 'as checksum_installed',
  67. )
  68. def test_store_checksum_installed_state_uninstalled(self):
  69. """It should clear the module's checksum_installed when vals
  70. contain state 'uninstalled'"""
  71. self.own_module.checksum_installed = 'test'
  72. self.own_module._store_checksum_installed({'state': 'uninstalled'})
  73. self.assertEqual(
  74. self.own_module.checksum_installed, False,
  75. 'Setting state to uninstalled does not clear checksum_installed',
  76. )
  77. def test_store_checksum_installed_vals_contain_checksum_installed(self):
  78. """It should not set checksum_installed to False or checksum_dir when
  79. a checksum_installed is included in vals"""
  80. self.own_module.checksum_installed = 'test'
  81. self.own_module._store_checksum_installed({
  82. 'state': 'installed',
  83. 'checksum_installed': 'test',
  84. })
  85. self.assertEqual(
  86. self.own_module.checksum_installed, 'test',
  87. 'Providing checksum_installed in vals did not prevent overwrite',
  88. )
  89. def test_store_checksum_installed_with_retain_context(self):
  90. """It should not set checksum_installed to False or checksum_dir when
  91. self has context retain_checksum_installed=True"""
  92. self.own_module.checksum_installed = 'test'
  93. self.own_module.with_context(
  94. retain_checksum_installed=True,
  95. )._store_checksum_installed({'state': 'installed'})
  96. self.assertEqual(
  97. self.own_module.checksum_installed, 'test',
  98. 'Providing retain_checksum_installed context did not prevent '
  99. 'overwrite',
  100. )
  101. def test_button_uninstall_cancel(self):
  102. """It should preserve checksum_installed when cancelling uninstall"""
  103. self.own_module.write({'state': 'to remove'})
  104. self.own_module.checksum_installed = 'test'
  105. self.own_module.button_uninstall_cancel()
  106. self.assertEqual(
  107. self.own_module.checksum_installed, 'test',
  108. 'Uninstall cancellation does not preserve checksum_installed',
  109. )
  110. def test_button_upgrade_cancel(self):
  111. """It should preserve checksum_installed when cancelling upgrades"""
  112. self.own_module.write({'state': 'to upgrade'})
  113. self.own_module.checksum_installed = 'test'
  114. self.own_module.button_upgrade_cancel()
  115. self.assertEqual(
  116. self.own_module.checksum_installed, 'test',
  117. 'Upgrade cancellation does not preserve checksum_installed',
  118. )
  119. def test_create(self):
  120. """It should call _store_checksum_installed method"""
  121. _store_checksum_installed_mock = mock.MagicMock()
  122. self.env['ir.module.module']._patch_method(
  123. '_store_checksum_installed',
  124. _store_checksum_installed_mock,
  125. )
  126. vals = {
  127. 'name': 'module_auto_update_test_module',
  128. 'state': 'installed',
  129. }
  130. self.create_test_module(vals)
  131. _store_checksum_installed_mock.assert_called_once_with(vals)
  132. self.env['ir.module.module']._revert_method(
  133. '_store_checksum_installed',
  134. )
  135. @mock.patch('%s.get_module_path' % model)
  136. def test_update_list(self, get_module_path_mock):
  137. """It should change the state of modules with different
  138. checksum_dir and checksum_installed to 'to upgrade'"""
  139. get_module_path_mock.return_value = self.own_dir_path
  140. vals = {
  141. 'name': 'module_auto_update_test_module',
  142. 'state': 'installed',
  143. }
  144. test_module = self.create_test_module(vals)
  145. test_module.checksum_installed = 'test'
  146. self.env['ir.module.module'].update_list()
  147. self.assertEqual(
  148. test_module.state, 'to upgrade',
  149. 'List update does not mark upgradeable modules "to upgrade"',
  150. )
  151. def test_update_list_only_changes_installed(self):
  152. """It should not change the state of a module with a former state
  153. other than 'installed' to 'to upgrade'"""
  154. vals = {
  155. 'name': 'module_auto_update_test_module',
  156. 'state': 'uninstalled',
  157. }
  158. test_module = self.create_test_module(vals)
  159. self.env['ir.module.module'].update_list()
  160. self.assertNotEqual(
  161. test_module.state, 'to upgrade',
  162. 'List update changed state of an uninstalled module',
  163. )
  164. def test_write(self):
  165. """It should call _store_checksum_installed method"""
  166. _store_checksum_installed_mock = mock.MagicMock()
  167. self.env['ir.module.module']._patch_method(
  168. '_store_checksum_installed',
  169. _store_checksum_installed_mock,
  170. )
  171. vals = {'state': 'installed'}
  172. self.own_module.write(vals)
  173. _store_checksum_installed_mock.assert_called_once_with(vals)
  174. self.env['ir.module.module']._revert_method(
  175. '_store_checksum_installed',
  176. )
  177. def test_post_init_hook(self):
  178. """It should set checksum_installed equal to checksum_dir for all
  179. installed modules"""
  180. installed_modules = self.env['ir.module.module'].search([
  181. ('state', '=', 'installed'),
  182. ])
  183. post_init_hook(self.env.cr, None)
  184. self.assertListEqual(
  185. installed_modules.mapped('checksum_dir'),
  186. installed_modules.mapped('checksum_installed'),
  187. 'Installed modules did not have checksum_installed stored',
  188. )