Fix tests

This commit is contained in:
Tim Bentley 2016-12-21 09:41:57 +00:00
parent a368a1d695
commit 8570fb5e8c
3 changed files with 20 additions and 19 deletions

View File

@ -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 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. point. Returns False on download error.
:param callback: the class which needs to be updated
:param url: URL to download :param url: URL to download
:param f_path: Destination file :param f_path: Destination file
:param sha256: The check sum value to be checked against the download value
""" """
block_count = 0 block_count = 0
block_size = 4096 block_size = 4096

View File

@ -22,9 +22,11 @@
""" """
Functional tests to test the AppLocation class and related methods. Functional tests to test the AppLocation class and related methods.
""" """
import socket
import os
from unittest import TestCase 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 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 # 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) 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')

View File

@ -23,7 +23,6 @@
Package to test the openlp.core.ui.firsttimeform package. Package to test the openlp.core.ui.firsttimeform package.
""" """
import os import os
import socket
import tempfile import tempfile
import urllib import urllib
from unittest import TestCase from unittest import TestCase
@ -236,20 +235,3 @@ class TestFirstTimeForm(TestCase, TestMixin):
# THEN: the critical_error_message_box should have been called # THEN: the critical_error_message_box should have been called
self.assertEquals(mocked_message_box.mock_calls[1][1][0], 'Network Error 407', self.assertEquals(mocked_message_box.mock_calls[1][1][0], 'Network Error 407',
'first_time_form should have caught Network Error') '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')