From 8570fb5e8cf50aeb37a63870af7bdc6abed03246 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Wed, 21 Dec 2016 09:41:57 +0000 Subject: [PATCH] Fix tests --- openlp/core/common/httputils.py | 2 ++ .../openlp_core_common/test_httputils.py | 19 ++++++++++++++++++- .../openlp_core_ui/test_firsttimeform.py | 18 ------------------ 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/openlp/core/common/httputils.py b/openlp/core/common/httputils.py index 638e8a98a..b3cb50ef3 100644 --- a/openlp/core/common/httputils.py +++ b/openlp/core/common/httputils.py @@ -207,8 +207,10 @@ def url_get_file(callback, url, f_path, sha256=None): Download a file given a URL. The file is retrieved in chunks, giving the ability to cancel the download at any point. Returns False on download error. + :param callback: the class which needs to be updated :param url: URL to download :param f_path: Destination file + :param sha256: The check sum value to be checked against the download value """ block_count = 0 block_size = 4096 diff --git a/tests/functional/openlp_core_common/test_httputils.py b/tests/functional/openlp_core_common/test_httputils.py index 7a8d478c5..8709e9cfb 100644 --- a/tests/functional/openlp_core_common/test_httputils.py +++ b/tests/functional/openlp_core_common/test_httputils.py @@ -22,9 +22,11 @@ """ Functional tests to test the AppLocation class and related methods. """ +import socket +import os from unittest import TestCase -from openlp.core.common.httputils import get_user_agent, get_web_page, get_url_file_size +from openlp.core.common.httputils import get_user_agent, get_web_page, get_url_file_size, url_get_file from tests.functional import MagicMock, patch @@ -245,3 +247,18 @@ class TestHttpUtils(TestCase): # THEN: The correct methods are called with the correct arguments and a web page is returned mock_urlopen.assert_called_with(fake_url, timeout=30) + + @patch('openlp.core.ui.firsttimeform.urllib.request.urlopen') + def test_socket_timeout(self, mocked_urlopen): + """ + Test socket timeout gets caught + """ + # GIVEN: Mocked urlopen to fake a network disconnect in the middle of a download + mocked_urlopen.side_effect = socket.timeout() + + # WHEN: Attempt to retrieve a file + url_get_file(url='http://localhost/test', f_path=self.tempfile) + + # THEN: socket.timeout should have been caught + # NOTE: Test is if $tmpdir/tempfile is still there, then test fails since ftw deletes bad downloaded files + self.assertFalse(os.path.exists(self.tempfile), 'FTW url_get_file should have caught socket.timeout') \ No newline at end of file diff --git a/tests/functional/openlp_core_ui/test_firsttimeform.py b/tests/functional/openlp_core_ui/test_firsttimeform.py index 5dd1430cd..ec26f60fe 100644 --- a/tests/functional/openlp_core_ui/test_firsttimeform.py +++ b/tests/functional/openlp_core_ui/test_firsttimeform.py @@ -23,7 +23,6 @@ Package to test the openlp.core.ui.firsttimeform package. """ import os -import socket import tempfile import urllib from unittest import TestCase @@ -236,20 +235,3 @@ class TestFirstTimeForm(TestCase, TestMixin): # THEN: the critical_error_message_box should have been called self.assertEquals(mocked_message_box.mock_calls[1][1][0], 'Network Error 407', 'first_time_form should have caught Network Error') - - @patch('openlp.core.ui.firsttimeform.urllib.request.urlopen') - def test_socket_timeout(self, mocked_urlopen): - """ - Test socket timeout gets caught - """ - # GIVEN: Mocked urlopen to fake a network disconnect in the middle of a download - first_time_form = FirstTimeForm(None) - first_time_form.initialize(MagicMock()) - mocked_urlopen.side_effect = socket.timeout() - - # WHEN: Attempt to retrieve a file - first_time_form.url_get_file(url='http://localhost/test', f_path=self.tempfile) - - # THEN: socket.timeout should have been caught - # NOTE: Test is if $tmpdir/tempfile is still there, then test fails since ftw deletes bad downloaded files - self.assertFalse(os.path.exists(self.tempfile), 'FTW url_get_file should have caught socket.timeout')