diff --git a/web/pgadmin/utils/javascript/tests/test_javascript_bundler.py b/web/pgadmin/utils/javascript/tests/test_javascript_bundler.py index be485d2b4..b3fb7b1c4 100644 --- a/web/pgadmin/utils/javascript/tests/test_javascript_bundler.py +++ b/web/pgadmin/utils/javascript/tests/test_javascript_bundler.py @@ -10,12 +10,14 @@ import sys -from pgadmin.utils.route import BaseTestGenerator if sys.version_info < (3, 3): - import mock + from mock import patch else: - import unittest.mock as mock + from unittest.mock import patch +from pgadmin.utils.route import BaseTestGenerator +from pgadmin.utils.javascript.javascript_bundler import JavascriptBundler +from pgadmin.utils.javascript.javascript_bundler import JsState class JavascriptBundlerTestCase(BaseTestGenerator): """This tests that the javascript bundler tool causes the application to bundle, @@ -23,96 +25,88 @@ class JavascriptBundlerTestCase(BaseTestGenerator): scenarios = [('scenario name: JavascriptBundlerTestCase', dict())] - def setUp(self): - self.skipTest("Test currently disabled for all server versions.") - self.mockSubprocess = mock.Mock() - self.mockOs = mock.Mock() - sys.modules['subprocess'] = self.mockSubprocess - sys.modules['os'] = self.mockOs + def __init__(self, methodName='runTest'): + super(BaseTestGenerator, self).__init__(methodName) + self.mockOs = None + self.mockSubprocessCall = None - def runTest(self): - from pgadmin.utils.javascript.javascript_bundler import JavascriptBundler - from pgadmin.utils.javascript.javascript_bundler import JsState - self.JavascriptBundler = JavascriptBundler - self.JsState = JsState + @patch('pgadmin.utils.javascript.javascript_bundler.os') + @patch('pgadmin.utils.javascript.javascript_bundler.call') + def runTest(self, subprocessMock, osMock): + self.mockOs = osMock + self.mockSubprocessCall = subprocessMock self._bundling_succeeds() - self.resetTestState() + self.reset_test_state() self._bundling_fails_and_there_is_no_existing_bundle() - self.resetTestState() + self.reset_test_state() self._bundling_fails_when_bundling_returns_nonzero() - self.resetTestState() + self.reset_test_state() self._bundling_fails_and_there_is_no_existing_bundle_directory() - self.resetTestState() + self.reset_test_state() self._bundling_fails_but_there_was_existing_bundle() - self.resetTestState() + self.reset_test_state() - def resetTestState(self): - self.mockSubprocess.reset_mock() - self.mockSubprocess.call.side_effect = None + def reset_test_state(self): + self.mockSubprocessCall.reset_mock() + self.mockSubprocessCall.side_effect = None self.mockOs.reset_mock() self.mockOs.listdir.side_effect = None self.mockOs.path.exists.side_effect = None def _bundling_succeeds(self): - javascriptBundler = self.JavascriptBundler() - self.assertEqual(len(self.mockSubprocess.method_calls), 0) - self.mockSubprocess.call.return_value = 0 + javascript_bundler = JavascriptBundler() + self.assertEqual(len(self.mockSubprocessCall.method_calls), 0) + self.mockSubprocessCall.return_value = 0 self.mockOs.listdir.return_value = [u'history.js', u'reactComponents.js'] - javascriptBundler.bundle() - self.mockSubprocess.call.assert_called_once_with(['yarn', 'run', 'bundle:dev']) + javascript_bundler.bundle() + self.mockSubprocessCall.assert_called_once_with(['yarn', 'run', 'bundle:dev']) - reportedState = javascriptBundler.report() - expectedState = self.JsState.NEW - self.assertEqual(reportedState, expectedState) + self.__assertState(javascript_bundler, JsState.NEW) def _bundling_fails_when_bundling_returns_nonzero(self): - javascriptBundler = self.JavascriptBundler() - self.assertEqual(len(self.mockSubprocess.method_calls), 0) + javascript_bundler = JavascriptBundler() + self.assertEqual(len(self.mockSubprocessCall.method_calls), 0) self.mockOs.listdir.return_value = [] - self.mockSubprocess.call.return_value = 99 + self.mockSubprocessCall.return_value = 99 - javascriptBundler.bundle() + javascript_bundler.bundle() - reportedState = javascriptBundler.report() - expectedState = self.JsState.NONE - self.assertEqual(reportedState, expectedState) + self.__assertState(javascript_bundler, JsState.NONE) def _bundling_fails_and_there_is_no_existing_bundle(self): - javascriptBundler = self.JavascriptBundler() - self.mockSubprocess.call.side_effect = OSError("mock exception behavior") + javascript_bundler = JavascriptBundler() + self.mockSubprocessCall.side_effect = OSError("mock exception behavior") self.mockOs.path.exists.return_value = True self.mockOs.listdir.return_value = [] - javascriptBundler.bundle() + javascript_bundler.bundle() - reportedState = javascriptBundler.report() - expectedState = self.JsState.NONE - self.assertEqual(reportedState, expectedState) + self.__assertState(javascript_bundler, JsState.NONE) def _bundling_fails_and_there_is_no_existing_bundle_directory(self): - javascriptBundler = self.JavascriptBundler() - self.mockSubprocess.call.side_effect = OSError("mock exception behavior") + javascript_bundler = JavascriptBundler() + self.mockSubprocessCall.side_effect = OSError("mock exception behavior") self.mockOs.path.exists.return_value = False self.mockOs.listdir.side_effect = OSError("mock exception behavior") - javascriptBundler.bundle() + javascript_bundler.bundle() - reportedState = javascriptBundler.report() - expectedState = self.JsState.NONE - self.assertEqual(reportedState, expectedState) + self.__assertState(javascript_bundler, JsState.NONE) def _bundling_fails_but_there_was_existing_bundle(self): - javascriptBundler = self.JavascriptBundler() - self.mockSubprocess.call.side_effect = OSError("mock exception behavior") + javascript_bundler = JavascriptBundler() + self.mockSubprocessCall.side_effect = OSError("mock exception behavior") self.mockOs.path.exists.return_value = True self.mockOs.listdir.return_value = [u'history.js', u'reactComponents.js'] - javascriptBundler.bundle() - self.mockSubprocess.call.assert_called_once_with(['yarn', 'run', 'bundle:dev']) + javascript_bundler.bundle() + self.mockSubprocessCall.assert_called_once_with(['yarn', 'run', 'bundle:dev']) - reportedState = javascriptBundler.report() - expectedState = self.JsState.OLD - self.assertEqual(reportedState, expectedState) + self.__assertState(javascript_bundler, JsState.OLD) + + def __assertState(self, javascript_bundler, expected_state): + reported_state = javascript_bundler.report() + self.assertEqual(reported_state, expected_state)