|
| 1 | +import unittest |
| 2 | +from unittest.mock import Mock, patch, MagicMock |
| 3 | + |
| 4 | +import databricks.sql.cloudfetch.downloader as downloader |
| 5 | + |
| 6 | + |
| 7 | +class DownloaderTests(unittest.TestCase): |
| 8 | + """ |
| 9 | + Unit tests for checking downloader logic. |
| 10 | + """ |
| 11 | + |
| 12 | + @patch('time.time', return_value=1000) |
| 13 | + def test_run_link_expired(self, mock_time): |
| 14 | + settings = Mock() |
| 15 | + result_link = Mock() |
| 16 | + # Already expired |
| 17 | + result_link.expiryTime = 999 |
| 18 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 19 | + assert not d.is_link_expired |
| 20 | + d.run() |
| 21 | + assert d.is_link_expired |
| 22 | + mock_time.assert_called_once() |
| 23 | + |
| 24 | + @patch('time.time', return_value=1000) |
| 25 | + def test_run_link_past_expiry_buffer(self, mock_time): |
| 26 | + settings = Mock(link_expiry_buffer_secs=5) |
| 27 | + result_link = Mock() |
| 28 | + # Within the expiry buffer time |
| 29 | + result_link.expiryTime = 1004 |
| 30 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 31 | + assert not d.is_link_expired |
| 32 | + d.run() |
| 33 | + assert d.is_link_expired |
| 34 | + mock_time.assert_called_once() |
| 35 | + |
| 36 | + @patch('requests.Session', return_value=MagicMock(get=MagicMock(return_value=MagicMock(ok=False)))) |
| 37 | + @patch('time.time', return_value=1000) |
| 38 | + def test_run_get_response_not_ok(self, mock_time, mock_session): |
| 39 | + settings = Mock(link_expiry_buffer_secs=0, download_timeout=0) |
| 40 | + settings.download_timeout = 0 |
| 41 | + settings.use_proxy = False |
| 42 | + result_link = Mock(expiryTime=1001) |
| 43 | + |
| 44 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 45 | + d.run() |
| 46 | + |
| 47 | + assert not d.is_file_downloaded_successfully |
| 48 | + assert d.is_download_finished.is_set() |
| 49 | + |
| 50 | + @patch('requests.Session', |
| 51 | + return_value=MagicMock(get=MagicMock(return_value=MagicMock(ok=True, content=b"1234567890" * 9)))) |
| 52 | + @patch('time.time', return_value=1000) |
| 53 | + def test_run_uncompressed_data_length_incorrect(self, mock_time, mock_session): |
| 54 | + settings = Mock(link_expiry_buffer_secs=0, download_timeout=0, use_proxy=False, is_lz4_compressed=False) |
| 55 | + result_link = Mock(bytesNum=100, expiryTime=1001) |
| 56 | + |
| 57 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 58 | + d.run() |
| 59 | + |
| 60 | + assert not d.is_file_downloaded_successfully |
| 61 | + assert d.is_download_finished.is_set() |
| 62 | + |
| 63 | + @patch('requests.Session', return_value=MagicMock(get=MagicMock(return_value=MagicMock(ok=True)))) |
| 64 | + @patch('time.time', return_value=1000) |
| 65 | + def test_run_compressed_data_length_incorrect(self, mock_time, mock_session): |
| 66 | + settings = Mock(link_expiry_buffer_secs=0, download_timeout=0, use_proxy=False) |
| 67 | + settings.is_lz4_compressed = True |
| 68 | + result_link = Mock(bytesNum=100, expiryTime=1001) |
| 69 | + mock_session.return_value.get.return_value.content = \ |
| 70 | + b'\x04"M\x18h@Z\x00\x00\x00\x00\x00\x00\x00\xec\x14\x00\x00\x00\xaf1234567890\n\x008P67890\x00\x00\x00\x00' |
| 71 | + |
| 72 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 73 | + d.run() |
| 74 | + |
| 75 | + assert not d.is_file_downloaded_successfully |
| 76 | + assert d.is_download_finished.is_set() |
| 77 | + |
| 78 | + @patch('requests.Session', |
| 79 | + return_value=MagicMock(get=MagicMock(return_value=MagicMock(ok=True, content=b"1234567890" * 10)))) |
| 80 | + @patch('time.time', return_value=1000) |
| 81 | + def test_run_uncompressed_successful(self, mock_time, mock_session): |
| 82 | + settings = Mock(link_expiry_buffer_secs=0, download_timeout=0, use_proxy=False) |
| 83 | + settings.is_lz4_compressed = False |
| 84 | + result_link = Mock(bytesNum=100, expiryTime=1001) |
| 85 | + |
| 86 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 87 | + d.run() |
| 88 | + |
| 89 | + assert d.result_file == b"1234567890" * 10 |
| 90 | + assert d.is_file_downloaded_successfully |
| 91 | + assert d.is_download_finished.is_set() |
| 92 | + |
| 93 | + @patch('requests.Session', return_value=MagicMock(get=MagicMock(return_value=MagicMock(ok=True)))) |
| 94 | + @patch('time.time', return_value=1000) |
| 95 | + def test_run_compressed_successful(self, mock_time, mock_session): |
| 96 | + settings = Mock(link_expiry_buffer_secs=0, download_timeout=0, use_proxy=False) |
| 97 | + settings.is_lz4_compressed = True |
| 98 | + result_link = Mock(bytesNum=100, expiryTime=1001) |
| 99 | + mock_session.return_value.get.return_value.content = \ |
| 100 | + b'\x04"M\x18h@d\x00\x00\x00\x00\x00\x00\x00#\x14\x00\x00\x00\xaf1234567890\n\x00BP67890\x00\x00\x00\x00' |
| 101 | + |
| 102 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 103 | + d.run() |
| 104 | + |
| 105 | + assert d.result_file == b"1234567890" * 10 |
| 106 | + assert d.is_file_downloaded_successfully |
| 107 | + assert d.is_download_finished.is_set() |
| 108 | + |
| 109 | + @patch('requests.Session.get', side_effect=ConnectionError('foo')) |
| 110 | + @patch('time.time', return_value=1000) |
| 111 | + def test_download_connection_error(self, mock_time, mock_session): |
| 112 | + settings = Mock(link_expiry_buffer_secs=0, use_proxy=False, is_lz4_compressed=True) |
| 113 | + result_link = Mock(bytesNum=100, expiryTime=1001) |
| 114 | + mock_session.return_value.get.return_value.content = \ |
| 115 | + b'\x04"M\x18h@d\x00\x00\x00\x00\x00\x00\x00#\x14\x00\x00\x00\xaf1234567890\n\x00BP67890\x00\x00\x00\x00' |
| 116 | + |
| 117 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 118 | + d.run() |
| 119 | + |
| 120 | + assert not d.is_file_downloaded_successfully |
| 121 | + assert d.is_download_finished.is_set() |
| 122 | + |
| 123 | + @patch('requests.Session.get', side_effect=TimeoutError('foo')) |
| 124 | + @patch('time.time', return_value=1000) |
| 125 | + def test_download_timeout(self, mock_time, mock_session): |
| 126 | + settings = Mock(link_expiry_buffer_secs=0, use_proxy=False, is_lz4_compressed=True) |
| 127 | + result_link = Mock(bytesNum=100, expiryTime=1001) |
| 128 | + mock_session.return_value.get.return_value.content = \ |
| 129 | + b'\x04"M\x18h@d\x00\x00\x00\x00\x00\x00\x00#\x14\x00\x00\x00\xaf1234567890\n\x00BP67890\x00\x00\x00\x00' |
| 130 | + |
| 131 | + d = downloader.ResultSetDownloadHandler(settings, result_link) |
| 132 | + d.run() |
| 133 | + |
| 134 | + assert not d.is_file_downloaded_successfully |
| 135 | + assert d.is_download_finished.is_set() |
| 136 | + |
| 137 | + @patch("threading.Event.wait", return_value=True) |
| 138 | + def test_is_file_download_successful_has_finished(self, mock_wait): |
| 139 | + for timeout in [None, 0, 1]: |
| 140 | + with self.subTest(timeout=timeout): |
| 141 | + settings = Mock(download_timeout=timeout) |
| 142 | + result_link = Mock() |
| 143 | + handler = downloader.ResultSetDownloadHandler(settings, result_link) |
| 144 | + |
| 145 | + status = handler.is_file_download_successful() |
| 146 | + assert status == handler.is_file_downloaded_successfully |
| 147 | + |
| 148 | + def test_is_file_download_successful_times_outs(self): |
| 149 | + settings = Mock(download_timeout=1) |
| 150 | + result_link = Mock() |
| 151 | + handler = downloader.ResultSetDownloadHandler(settings, result_link) |
| 152 | + |
| 153 | + status = handler.is_file_download_successful() |
| 154 | + assert not status |
| 155 | + assert handler.is_download_timedout |
0 commit comments