def test_not_singleton(self): downloader_1 = Downloader() downloader_2 = Downloader() downloader_3 = Downloader() assert downloader_1 is not downloader_2 assert downloader_2 is not downloader_3 assert downloader_3 is not downloader_1
def test_request_fatal(self, exception, caplog): caplog.set_level(10) self.request_m.side_effect = [exception] * self.retries + [mock.DEFAULT] with pytest.raises(DownloaderError, match="max retries failed"): Downloader().get(self.url) self.request_m.assert_called() assert self.request_m.call_count == self.retries excname = type(exception).__name__ records_expected: Any = [(10, "GET %r" % self.url)] for i in range(1, self.retries + 1): retries_left = self.retries - i records_expected.append( ( 30, "Catched %s in GET, retries=%d" % (excname, retries_left), ), ) records_expected.append((50, "Download error in GET %r" % self.url)) records_expected = [(self.logger_name,) + x for x in records_expected] assert caplog.record_tuples == records_expected
def test_request_several_errors_no_fatal_override_retries( self, exception, caplog, nerrors ): caplog.set_level(10) self.request_m.side_effect = [exception] * nerrors + [mock.DEFAULT] Downloader().get(self.url, retries=5) self.request_m.assert_called() assert self.request_m.call_count == nerrors + 1 excname = type(exception).__name__ records_expected: Any = [(10, "GET %r" % self.url)] for i in range(1, nerrors + 1): retries_left = 5 - i records_expected.append( ( 30, "Catched %s in GET, retries=%d" % (excname, retries_left), ), ) records_expected = [(self.logger_name,) + x for x in records_expected] assert caplog.record_tuples == records_expected
def test_request_one_error(self, exception, caplog): caplog.set_level(10) self.request_m.side_effect = [exception, mock.DEFAULT] Downloader().get(self.url) self.request_m.assert_called() assert self.request_m.call_count == 2 excname = type(exception).__name__ assert caplog.record_tuples == [ (self.logger_name, 10, "GET %r" % self.url), (self.logger_name, 30, "Catched %s in GET, retries=9" % excname), ]
def test_init(self, silenced, retries): downloader = Downloader(silenced=silenced, retries=retries) assert hasattr(downloader, "logger") if silenced: assert downloader.logger.level == 50 else: assert downloader.logger.level == 0 assert hasattr(downloader, "retries") if not retries: assert downloader.retries == self.settings_m.retries else: assert downloader.retries == retries assert downloader.headers["user-agent"] == USER_AGENT self.request_m.assert_not_called()
def test_request_put(self): Downloader().put(self.url) self.request_m.assert_called_once_with("PUT", self.url, data=mock.ANY)
def test_request_delete(self): Downloader().delete(self.url) self.request_m.assert_called_once_with("DELETE", self.url)
def test_request_post(self): Downloader().post(self.url, data={"hello": "world"}) self.request_m.assert_called_once_with( "POST", self.url, data={"hello": "world"}, json=mock.ANY )
def test_request_get(self): Downloader().get(self.url) self.request_m.assert_called_once_with( "GET", self.url, allow_redirects=mock.ANY )