Fix up some issues with one of the tests

This commit is contained in:
Raoul Snyman 2017-09-25 13:34:05 -07:00
parent 4bb031b22e
commit e90836e817
2 changed files with 11 additions and 11 deletions

View File

@ -142,7 +142,7 @@ def url_get_file(callback, url, file_path, sha256=None):
:param callback: the class which needs to be updated :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 file_path: Destination file
:param sha256: The check sum value to be checked against the download value :param sha256: The check sum value to be checked against the download value
""" """
block_count = 0 block_count = 0
@ -151,7 +151,7 @@ def url_get_file(callback, url, file_path, sha256=None):
log.debug('url_get_file: %s', url) log.debug('url_get_file: %s', url)
while retries < CONNECTION_RETRIES: while retries < CONNECTION_RETRIES:
try: try:
with open(file_path, 'wb') as saved_file: with file_path.open('wb') as saved_file:
response = requests.get(url, timeout=float(CONNECTION_TIMEOUT), stream=True) response = requests.get(url, timeout=float(CONNECTION_TIMEOUT), stream=True)
if sha256: if sha256:
hasher = hashlib.sha256() hasher = hashlib.sha256()
@ -168,20 +168,22 @@ def url_get_file(callback, url, file_path, sha256=None):
if sha256 and hasher.hexdigest() != sha256: if sha256 and hasher.hexdigest() != sha256:
log.error('sha256 sums did not match for file %s, got %s, expected %s', file_path, hasher.hexdigest(), log.error('sha256 sums did not match for file %s, got %s, expected %s', file_path, hasher.hexdigest(),
sha256) sha256)
os.remove(file_path) if file_path.exists():
file_path.unlink()
return False return False
break break
except IOError: except IOError:
trace_error_handler(log) trace_error_handler(log)
os.remove(file_path)
if retries > CONNECTION_RETRIES: if retries > CONNECTION_RETRIES:
if file_path.exists():
file_path.unlink()
return False return False
else: else:
retries += 1 retries += 1
time.sleep(0.1) time.sleep(0.1)
continue continue
if callback.was_cancelled: if callback.was_cancelled and file_path.exists():
os.remove(file_path) file_path.unlink()
return True return True

View File

@ -228,8 +228,7 @@ class TestHttpUtils(TestCase, TestMixin):
assert file_size == 100 assert file_size == 100
@patch('openlp.core.common.httputils.requests') @patch('openlp.core.common.httputils.requests')
@patch('openlp.core.common.httputils.os.remove') def test_socket_timeout(self, mocked_requests):
def test_socket_timeout(self, mocked_remove, mocked_requests):
""" """
Test socket timeout gets caught Test socket timeout gets caught
""" """
@ -237,9 +236,8 @@ class TestHttpUtils(TestCase, TestMixin):
mocked_requests.get.side_effect = IOError mocked_requests.get.side_effect = IOError
# WHEN: Attempt to retrieve a file # WHEN: Attempt to retrieve a file
url_get_file(MagicMock(), url='http://localhost/test', file_path=self.tempfile) url_get_file(MagicMock(), url='http://localhost/test', file_path=Path(self.tempfile))
# THEN: socket.timeout should have been caught # 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 # NOTE: Test is if $tmpdir/tempfile is still there, then test fails since ftw deletes bad downloaded files
mocked_remove.assert_called_with(self.tempfile) assert not os.path.exists(self.tempfile), 'tempfile should have been deleted'
assert mocked_remove.call_count == 3, 'os.remove() should have been called 3 times'