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.

172 lines
6.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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 os
  5. import mock
  6. from openerp.modules import get_module_path
  7. from openerp.tests.common import TransactionCase
  8. from openerp.tools import mute_logger
  9. from openerp.addons.module_auto_update.addon_hash import addon_hash
  10. from ..models.module_deprecated import PARAM_DEPRECATED
  11. model = 'openerp.addons.module_auto_update.models.module'
  12. class TestModule(TransactionCase):
  13. def setUp(self):
  14. super(TestModule, self).setUp()
  15. module_name = 'module_auto_update'
  16. self.env["ir.config_parameter"].set_param(PARAM_DEPRECATED, "1")
  17. self.own_module = self.env['ir.module.module'].search([
  18. ('name', '=', module_name),
  19. ])
  20. self.own_dir_path = get_module_path(module_name)
  21. keep_langs = self.env['res.lang'].search([]).mapped('code')
  22. self.own_checksum = addon_hash(
  23. self.own_dir_path,
  24. exclude_patterns=['*.pyc', '*.pyo', '*.pot', 'static/*'],
  25. keep_langs=keep_langs,
  26. )
  27. self.own_writeable = os.access(self.own_dir_path, os.W_OK)
  28. @mock.patch('%s.get_module_path' % model)
  29. def create_test_module(self, vals, get_module_path_mock):
  30. get_module_path_mock.return_value = self.own_dir_path
  31. test_module = self.env['ir.module.module'].create(vals)
  32. return test_module
  33. def test_store_checksum_installed_state_installed(self):
  34. """It should set the module's checksum_installed equal to
  35. checksum_dir when vals contain a ``latest_version`` str."""
  36. self.own_module.checksum_installed = 'test'
  37. self.own_module._store_checksum_installed({'latest_version': '1.0'})
  38. self.assertEqual(
  39. self.own_module.checksum_installed, self.own_module.checksum_dir,
  40. )
  41. def test_store_checksum_installed_state_uninstalled(self):
  42. """It should clear the module's checksum_installed when vals
  43. contain ``"latest_version": False``"""
  44. self.own_module.checksum_installed = 'test'
  45. self.own_module._store_checksum_installed({'latest_version': False})
  46. self.assertIs(self.own_module.checksum_installed, False)
  47. def test_store_checksum_installed_vals_contain_checksum_installed(self):
  48. """It should not set checksum_installed to False or checksum_dir when
  49. a checksum_installed is included in vals"""
  50. self.own_module.checksum_installed = 'test'
  51. self.own_module._store_checksum_installed({
  52. 'state': 'installed',
  53. 'checksum_installed': 'test',
  54. })
  55. self.assertEqual(
  56. self.own_module.checksum_installed, 'test',
  57. 'Providing checksum_installed in vals did not prevent overwrite',
  58. )
  59. def test_store_checksum_installed_with_retain_context(self):
  60. """It should not set checksum_installed to False or checksum_dir when
  61. self has context retain_checksum_installed=True"""
  62. self.own_module.checksum_installed = 'test'
  63. self.own_module.with_context(
  64. retain_checksum_installed=True,
  65. )._store_checksum_installed({'state': 'installed'})
  66. self.assertEqual(
  67. self.own_module.checksum_installed, 'test',
  68. 'Providing retain_checksum_installed context did not prevent '
  69. 'overwrite',
  70. )
  71. def test_button_uninstall_cancel(self):
  72. """It should preserve checksum_installed when cancelling uninstall"""
  73. self.own_module.write({'state': 'to remove'})
  74. self.own_module.checksum_installed = 'test'
  75. self.own_module.button_uninstall_cancel()
  76. self.assertEqual(
  77. self.own_module.checksum_installed, 'test',
  78. 'Uninstall cancellation does not preserve checksum_installed',
  79. )
  80. def test_button_upgrade_cancel(self):
  81. """It should preserve checksum_installed when cancelling upgrades"""
  82. self.own_module.write({'state': 'to upgrade'})
  83. self.own_module.checksum_installed = 'test'
  84. self.own_module.button_upgrade_cancel()
  85. self.assertEqual(
  86. self.own_module.checksum_installed, 'test',
  87. 'Upgrade cancellation does not preserve checksum_installed',
  88. )
  89. def test_create(self):
  90. """It should call _store_checksum_installed method"""
  91. _store_checksum_installed_mock = mock.MagicMock()
  92. try:
  93. self.env['ir.module.module']._patch_method(
  94. '_store_checksum_installed',
  95. _store_checksum_installed_mock,
  96. )
  97. vals = {
  98. 'name': 'module_auto_update_test_module',
  99. 'state': 'installed',
  100. }
  101. self.create_test_module(vals)
  102. _store_checksum_installed_mock.assert_called_once_with(vals)
  103. finally:
  104. self.env['ir.module.module']._revert_method(
  105. '_store_checksum_installed',
  106. )
  107. @mute_logger("openerp.modules.module")
  108. @mock.patch('%s.get_module_path' % model)
  109. def test_get_module_list(self, module_path_mock):
  110. """It should change the state of modules with different
  111. checksum_dir and checksum_installed to 'to upgrade'"""
  112. module_path_mock.return_value = self.own_dir_path
  113. vals = {
  114. 'name': 'module_auto_update_test_module',
  115. 'state': 'installed',
  116. }
  117. test_module = self.create_test_module(vals)
  118. test_module.checksum_installed = 'test'
  119. self.env['base.module.upgrade'].get_module_list()
  120. self.assertEqual(
  121. test_module.state, 'to upgrade',
  122. 'List update does not mark upgradeable modules "to upgrade"',
  123. )
  124. @mock.patch('%s.get_module_path' % model)
  125. def test_get_module_list_only_changes_installed(self, module_path_mock):
  126. """It should not change the state of a module with a former state
  127. other than 'installed' to 'to upgrade'"""
  128. module_path_mock.return_value = self.own_dir_path
  129. vals = {
  130. 'name': 'module_auto_update_test_module',
  131. 'state': 'uninstalled',
  132. }
  133. test_module = self.create_test_module(vals)
  134. self.env['base.module.upgrade'].get_module_list()
  135. self.assertNotEqual(
  136. test_module.state, 'to upgrade',
  137. 'List update changed state of an uninstalled module',
  138. )
  139. def test_write(self):
  140. """It should call _store_checksum_installed method"""
  141. _store_checksum_installed_mock = mock.MagicMock()
  142. self.env['ir.module.module']._patch_method(
  143. '_store_checksum_installed',
  144. _store_checksum_installed_mock,
  145. )
  146. vals = {'state': 'installed'}
  147. self.own_module.write(vals)
  148. _store_checksum_installed_mock.assert_called_once_with(vals)
  149. self.env['ir.module.module']._revert_method(
  150. '_store_checksum_installed',
  151. )