class FileDownloaderTest(LinkbeanTestCase): @mock.patch('urllib2.urlopen') def setUp(self, urlopen_mock): self.url = 'http://test.com/file.tst' self.file = mock.Mock(headers={'Accept-Ranges': 'bytes', 'Content-Length': '1000'}, read=mock.Mock(return_value='1'), code=200, url=self.url) urlopen_mock.return_value = self.file self.downloader = FileDownloader(self.url, download_folder="tests/downloads") def tearDown(self): try: shutil.rmtree('./tests/downloads') except OSError: pass def test_init(self): self.assertEqual(self.downloader.link, 'http://test.com/file.tst') self.assertEqual(self.downloader.sections, 15) self.assertEqual(self.downloader.download_folder, 'tests/downloads') self.assertEqual(self.downloader.file, self.file) @mock.patch('linkbean.downloader.Section') def test_download(self, SectionMock): instance = SectionMock.return_value self.downloader.download() self.assertEqual(SectionMock.call_count, 15) self.assertEqual(instance.start.call_count, 15) self.assertEqual(instance.join.call_count, 15)
def main(): parser = OptionParser() parser.add_option('-u', '--url', dest='url', default='', help='URL to download.') parser.add_option('-c', '--connections', type=int, dest='connections', default=20, help='Parallel connections to download file.') parser.add_option('-o', '--output', dest='output', default='./downloads', help='Parallel connections to download file.') parser.add_option('-s', '--sections', type=int, dest='sections', default=100, help='Sections to split download file.') log_level_msg = 'The log level to be used. Possible values are: ' + \ 'debug, info, warning, error, critical or notset. [default: info].' parser.add_option('-l', '--log-level', dest='log_level', default="warning", help=log_level_msg) (opt, args) = parser.parse_args() logging.basicConfig( format='[linkbean][%(asctime)s] %(levelname)s:%(filename)s:%(funcName)s: %(message)s', level=getattr(logging, opt.log_level.upper()) ) downloader = FileDownloader(opt.url, opt.connections, opt.sections, opt.output) try: downloader.download() except KeyboardInterrupt: pass