def test__download(self, request): url = 'http://' destination = '/tmp/x' report = Mock(state=DOWNLOAD_SUCCEEDED) downloader = Mock() downloader.download_one.return_value = report source = Mock() source.get_downloader.return_value = downloader # test Serial._download(url, destination, source) # validation source.get_downloader.assert_called_once_with() request.assert_called_once_with(url, destination) downloader.download_one.assert_called_once_with(request.return_value, events=True)
def test__download(self, request): url = "http://" destination = "/tmp/x" report = Mock(state=DOWNLOAD_SUCCEEDED) downloader = Mock() downloader.download_one.return_value = report source = Mock() source.get_downloader.return_value = downloader # test Serial._download(url, destination, source) # validation source.get_downloader.assert_called_once_with() request.assert_called_once_with(url, destination) downloader.download_one.assert_called_once_with(request.return_value, events=True)
def test__download(self, request): url = 'http://' destination = '/tmp/x' report = Mock(state=DOWNLOAD_SUCCEEDED) downloader = Mock() downloader.download_one.return_value = report source = Mock() source.get_downloader.return_value = downloader primary = Mock() # test serial = Serial(primary, None, None, None) serial._download(url, destination, source) # validation source.get_downloader.assert_called_once_with(primary.session) request.assert_called_once_with(url, destination) downloader.download_one.assert_called_once_with(request.return_value, events=True)
def test_init(self): primary = Mock() container = Mock() requests = Mock() listener = Mock() # test batch = Serial(primary, container, requests, listener) # validation self.assertEqual(batch.primary, primary) self.assertEqual(batch.container, container) self.assertEqual(batch.requests, requests) self.assertEqual(batch.listener, listener)
def test__download_failed(self, request): url = 'http://' destination = '/tmp/x' report = Mock(state=None) downloader = Mock() downloader.download_one.return_value = report source = Mock() source.get_downloader.return_value = downloader primary = Mock() # test serial = Serial(primary, None, None, None) self.assertRaises(DownloadFailed, serial._download, url, destination, source) # validation source.get_downloader.assert_called_once_with(primary.session) request.assert_called_once_with(url, destination) downloader.download_one.assert_called_once_with(request.return_value, events=True)
def test_download_failed(self, download, failed, started): download.side_effect = DownloadFailed() primary = Mock() sources = [ Mock(id=1, url='u1'), Mock(id=2, url='u2'), Mock(id=3, url='u3'), Mock(id=4, url='u4') ] container = Mock(sources=sources) requests = [ Mock(downloaded=False, sources=[(s, s.url) for s in sources[0:2]]), Mock(downloaded=False, sources=[(s, s.url) for s in sources[2:4]]) ] listener = Mock() # test batch = Serial(primary, container, requests, listener) report = batch() # validation self.assertEqual(started.call_args_list, [call(r) for r in requests]) self.assertEqual(started.return_value.call_count, len(requests)) for r in requests: r.find_sources.assert_called_once_with(primary, sources) download_calls = [] for r in requests: for s, u in r.sources: download_calls.append(call(u, r.destination, s)) self.assertEqual(download.call_args_list, download_calls) self.assertEqual(failed.call_args_list, [call(r) for r in requests]) self.assertEqual(failed.return_value.call_count, len(requests)) self.assertEqual(report.total_sources, len(sources)) self.assertEqual(len(report.downloads), len(sources)) for s in sources: details = report.downloads[s.id] self.assertEqual(details.total_succeeded, 0) self.assertEqual(details.total_failed, 1)
def test_download_succeeded(self, download, succeeded, started): primary = Mock() sources = [ Mock(id=1, url='u1'), Mock(id=2, url='u2'), Mock(id=3, url='u3'), Mock(id=4, url='u4') ] container = Mock(sources=sources) requests = [ Mock(downloaded=False, sources=[(s, s.url) for s in sources[0:2]]), Mock(downloaded=False, sources=[(s, s.url) for s in sources[2:4]]) ] listener = Mock() # test batch = Serial(primary, container, requests, listener) report = batch() # validation self.assertEqual(started.call_args_list, [call(r) for r in requests]) self.assertEqual(started.return_value.call_count, len(requests)) for r in requests: r.find_sources.assert_called_once_with(primary, sources) self.assertEqual( download.call_args_list, [call(r.sources[0][1], r.destination, r.sources[0][0]) for r in requests]) self.assertEqual(succeeded.call_args_list, [call(r) for r in requests]) self.assertEqual(succeeded.return_value.call_count, len(requests)) self.assertEqual(report.total_sources, 4) self.assertEqual(len(report.downloads), 2) details = report.downloads[1] self.assertEqual(details.total_succeeded, 1) self.assertEqual(details.total_failed, 0) details = report.downloads[3] self.assertEqual(details.total_succeeded, 1) self.assertEqual(details.total_failed, 0)