-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathtest_downloader.py
More file actions
204 lines (174 loc) · 7.04 KB
/
test_downloader.py
File metadata and controls
204 lines (174 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from contextlib import contextmanager
import unittest
from unittest.mock import Mock, patch, MagicMock
import requests
import databricks.sql.cloudfetch.downloader as downloader
from databricks.sql.common.http import DatabricksHttpClient
from databricks.sql.exc import Error
from databricks.sql.types import SSLOptions
def create_response(**kwargs) -> requests.Response:
result = requests.Response()
for k, v in kwargs.items():
setattr(result, k, v)
result.close = Mock()
return result
class DownloaderTests(unittest.TestCase):
"""
Unit tests for checking downloader logic.
"""
def _setup_time_mock_for_download(self, mock_time, end_time):
"""Helper to setup time mock that handles logging system calls."""
call_count = [0]
def time_side_effect():
call_count[0] += 1
if call_count[0] <= 2: # First two calls (validation, start_time)
return 1000
else: # All subsequent calls (logging, duration calculation)
return end_time
mock_time.side_effect = time_side_effect
@patch("time.time", return_value=1000)
def test_run_link_expired(self, mock_time):
settings = Mock()
result_link = Mock()
# Already expired
result_link.expiryTime = 999
d = downloader.ResultSetDownloadHandler(
settings,
result_link,
ssl_options=SSLOptions(),
chunk_id=0,
session_id_hex=Mock(),
statement_id=Mock(),
)
with self.assertRaises(Error) as context:
d.run()
self.assertTrue("link has expired" in context.exception.message)
mock_time.assert_called_once()
@patch("time.time", return_value=1000)
def test_run_link_past_expiry_buffer(self, mock_time):
settings = Mock(link_expiry_buffer_secs=5)
result_link = Mock()
# Within the expiry buffer time
result_link.expiryTime = 1004
d = downloader.ResultSetDownloadHandler(
settings,
result_link,
ssl_options=SSLOptions(),
chunk_id=0,
session_id_hex=Mock(),
statement_id=Mock(),
)
with self.assertRaises(Error) as context:
d.run()
self.assertTrue("link has expired" in context.exception.message)
mock_time.assert_called_once()
@patch("time.time", return_value=1000)
def test_run_get_response_not_ok(self, mock_time):
http_client = DatabricksHttpClient.get_instance()
settings = Mock(link_expiry_buffer_secs=0, download_timeout=0)
settings.download_timeout = 0
settings.use_proxy = False
result_link = Mock(expiryTime=1001)
with patch.object(
http_client,
"execute",
return_value=create_response(status_code=404, _content=b"1234"),
):
d = downloader.ResultSetDownloadHandler(
settings,
result_link,
ssl_options=SSLOptions(),
chunk_id=0,
session_id_hex=Mock(),
statement_id=Mock(),
)
with self.assertRaises(requests.exceptions.HTTPError) as context:
d.run()
self.assertTrue("404" in str(context.exception))
@patch("time.time")
def test_run_uncompressed_successful(self, mock_time):
self._setup_time_mock_for_download(mock_time, 1000.5)
http_client = DatabricksHttpClient.get_instance()
file_bytes = b"1234567890" * 10
settings = Mock(link_expiry_buffer_secs=0, download_timeout=0, use_proxy=False)
settings.is_lz4_compressed = False
settings.min_cloudfetch_download_speed = 1.0
result_link = Mock(bytesNum=100, expiryTime=1001)
result_link.fileLink = "https://s3.amazonaws.com/bucket/file.arrow?token=abc123"
with patch.object(
http_client,
"execute",
return_value=create_response(status_code=200, _content=file_bytes),
):
d = downloader.ResultSetDownloadHandler(
settings,
result_link,
ssl_options=SSLOptions(),
chunk_id=0,
session_id_hex=Mock(),
statement_id=Mock(),
)
file = d.run()
assert file.file_bytes == b"1234567890" * 10
@patch("time.time")
def test_run_compressed_successful(self, mock_time):
self._setup_time_mock_for_download(mock_time, 1000.2)
http_client = DatabricksHttpClient.get_instance()
file_bytes = b"1234567890" * 10
compressed_bytes = b'\x04"M\x18h@d\x00\x00\x00\x00\x00\x00\x00#\x14\x00\x00\x00\xaf1234567890\n\x00BP67890\x00\x00\x00\x00'
settings = Mock(link_expiry_buffer_secs=0, download_timeout=0, use_proxy=False)
settings.is_lz4_compressed = True
settings.min_cloudfetch_download_speed = 1.0
result_link = Mock(bytesNum=100, expiryTime=1001)
result_link.fileLink = "https://s3.amazonaws.com/bucket/file.arrow?token=xyz789"
with patch.object(
http_client,
"execute",
return_value=create_response(status_code=200, _content=compressed_bytes),
):
d = downloader.ResultSetDownloadHandler(
settings,
result_link,
ssl_options=SSLOptions(),
chunk_id=0,
session_id_hex=Mock(),
statement_id=Mock(),
)
file = d.run()
assert file.file_bytes == b"1234567890" * 10
@patch("time.time", return_value=1000)
def test_download_connection_error(self, mock_time):
http_client = DatabricksHttpClient.get_instance()
settings = Mock(
link_expiry_buffer_secs=0, use_proxy=False, is_lz4_compressed=True
)
result_link = Mock(bytesNum=100, expiryTime=1001)
with patch.object(http_client, "execute", side_effect=ConnectionError("foo")):
d = downloader.ResultSetDownloadHandler(
settings,
result_link,
ssl_options=SSLOptions(),
chunk_id=0,
session_id_hex=Mock(),
statement_id=Mock(),
)
with self.assertRaises(ConnectionError):
d.run()
@patch("time.time", return_value=1000)
def test_download_timeout(self, mock_time):
http_client = DatabricksHttpClient.get_instance()
settings = Mock(
link_expiry_buffer_secs=0, use_proxy=False, is_lz4_compressed=True
)
result_link = Mock(bytesNum=100, expiryTime=1001)
with patch.object(http_client, "execute", side_effect=TimeoutError("foo")):
d = downloader.ResultSetDownloadHandler(
settings,
result_link,
ssl_options=SSLOptions(),
chunk_id=0,
session_id_hex=Mock(),
statement_id=Mock(),
)
with self.assertRaises(TimeoutError):
d.run()