Skip to content

Commit 50b3b19

Browse files
committed
Test downloading data in more than one chunk
Add test cases to test_fetcher and test_download that decrease default chunk size and download data in more than one chunk. Small code-style improvements. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 29e3419 commit 50b3b19

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

tests/test_download.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,29 @@ def test_download_url_to_tempfileobj(self):
112112
self.assertEqual(self.target_data_length, len(temp_file_data))
113113

114114

115+
# Test: Download url in more than one chunk.
116+
def test_download_url_in_chunks(self):
117+
118+
# Set smaller chunk size to ensure that the file will be downloaded
119+
# in more than one chunk
120+
default_chunk_size = tuf.settings.CHUNK_SIZE
121+
tuf.settings.CHUNK_SIZE = 4
122+
# We don't have access to chunks from download_file()
123+
# so we just confirm that the expectation of more than one chunk is
124+
# correct and verify that no errors are raised during download
125+
chunks_count = self.target_data_length/tuf.settings.CHUNK_SIZE
126+
self.assertGreater(chunks_count, 1)
127+
128+
download_file = download.safe_download
129+
with download_file(self.url, self.target_data_length, self.fetcher) as temp_fileobj:
130+
temp_fileobj.seek(0)
131+
temp_file_data = temp_fileobj.read().decode('utf-8')
132+
self.assertEqual(self.target_data, temp_file_data)
133+
self.assertEqual(self.target_data_length, len(temp_file_data))
134+
135+
# Restore default settings
136+
tuf.settings.CHUNK_SIZE = default_chunk_size
137+
115138

116139
# Test: Incorrect lengths.
117140
def test_download_url_to_tempfileobj_and_lengths(self):

tests/test_fetcher.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
<Purpose>
1717
Unit test for RequestsFetcher.
1818
"""
19+
# Help with Python 2 compatibility, where the '/' operator performs
20+
# integer division.
21+
from __future__ import division
1922

2023
import logging
2124
import os
2225
import io
2326
import sys
2427
import unittest
2528
import tempfile
29+
import math
2630

2731
import tuf
2832
import tuf.exceptions
@@ -31,7 +35,6 @@
3135

3236
from tests import utils
3337

34-
3538
logger = logging.getLogger(__name__)
3639

3740

@@ -77,15 +80,12 @@ def tearDown(self):
7780

7881
# Test: Normal case.
7982
def test_fetch(self):
80-
8183
for chunk in self.fetcher.fetch(self.url, self.file_length):
8284
self.temp_file.write(chunk)
8385

8486
self.temp_file.seek(0)
8587
temp_file_data = self.temp_file.read().decode('utf-8')
8688
self.assertEqual(self.file_contents, temp_file_data)
87-
self.assertEqual(self.file_length, len(temp_file_data))
88-
8989

9090
# Test if fetcher downloads file up to a required length
9191
def test_fetch_restricted_length(self):
@@ -96,7 +96,7 @@ def test_fetch_restricted_length(self):
9696
self.assertEqual(self.temp_file.tell(), self.file_length-4)
9797

9898

99-
# Test if fetcher does not downlad more than actual file length
99+
# Test that fetcher does not download more than actual file length
100100
def test_fetch_upper_length(self):
101101
for chunk in self.fetcher.fetch(self.url, self.file_length+4):
102102
self.temp_file.write(chunk)
@@ -107,9 +107,34 @@ def test_fetch_upper_length(self):
107107

108108
# Test incorrect URL parsing
109109
def test_url_parsing(self):
110-
with self.assertRaises(tuf.exceptions.URLParsingError) as cm:
111-
for chunk in self.fetcher.fetch(self.random_string(), self.file_length):
112-
self.temp_file.write(chunk)
110+
with self.assertRaises(tuf.exceptions.URLParsingError):
111+
self.fetcher.fetch(self.random_string(), self.file_length)
112+
113+
114+
# Test: Normal case with url data downloaded in more than one chunk
115+
def test_fetch_in_chunks(self):
116+
# Set smaller chunk size to ensure that the file will be downloaded
117+
# in more than one chunk
118+
default_chunk_size = tuf.settings.CHUNK_SIZE
119+
tuf.settings.CHUNK_SIZE = 4
120+
121+
# expected_chunks_count: 3
122+
expected_chunks_count = math.ceil(self.file_length/tuf.settings.CHUNK_SIZE)
123+
self.assertEqual(expected_chunks_count, 3)
124+
125+
chunks_count = 0
126+
for chunk in self.fetcher.fetch(self.url, self.file_length):
127+
self.temp_file.write(chunk)
128+
chunks_count+=1
129+
130+
self.temp_file.seek(0)
131+
temp_file_data = self.temp_file.read().decode('utf-8')
132+
self.assertEqual(self.file_contents, temp_file_data)
133+
# Check that we calculate chunks as expected
134+
self.assertEqual(chunks_count, expected_chunks_count)
135+
136+
# Restore default settings
137+
tuf.settings.CHUNK_SIZE = default_chunk_size
113138

114139

115140

0 commit comments

Comments
 (0)