def test_when_all_fail(self, kwargs={"max_retries": 3, "backoff": 0}): self.request_failures = (kwargs["max_retries"] * self.num_pages) + self.num_pages self.results = RemotePetition.async_query(**kwargs) self.unpacked_results = RemotePetition.unpack_query(self.results) assert self.validate_responses(success=0, failed=10) assert self.validate_function(success=0, call_count=4)
def test_validate_state(self): invalid_state, valid_states = "unknown", self.query_states error_message = f"Invalid state param: '{invalid_state}', valid: {valid_states}" for state in valid_states: RemotePetition.validate_state(state) error = None with pytest.raises(ValueError) as error: RemotePetition.validate_state(invalid_state) assert str(error.value) == error_message
def test_handle(self, futures): responses = self.futures["success"] + self.futures["failed"] result = RemotePetition.handle_async_responses(responses) assert (len(result["success"]) + len(result["failed"])) == len(responses) assert len(result["success"]) == len(self.futures["success"]) assert len(result["failed"]) == len(self.futures["failed"])
def test_when_populate_and_none_fail(self, kwargs={ "max_retries": 3, "backoff": 0 }): petitions = range(0, 10) self.request_failures = 0 self.results = RemotePetition.async_get(petitions, **kwargs) assert self.validate_responses(success=10, failed=0) assert self.validate_function(call_count=1)
def test_when_poll_and_some_fail(self, petitions, kwargs={ "max_retries": 3, "backoff": 0 }): self.request_failures = (kwargs["max_retries"] * len(petitions)) + 5 self.results = RemotePetition.async_get(petitions=petitions, **kwargs) assert self.validate_responses(success=5, failed=5) assert self.validate_function(call_count=4)
def test_will_retry_untill_max_retries_exceeded(self, futures, configure): for i in self.attempts: self.result = RemotePetition.eval_async_retry(**self.kwargs) if i < self.kwargs["max_retries"]: self.validate(i) self.kwargs = {**self.kwargs, **self.result} continue assert any(self.kwargs["results"]["failed"]) assert self.result is False
def test_find_page_nums(self): expected = { "first": None, "self": "1", "next": "2", "prev": None, "last": "100" } links = self.make_links(self_num=1, next_num=2, prev_num=None, last_num=100) assert RemotePetition.find_page_nums(links) == expected
def test_will_not_retry_without_failures(self, futures, configure): for i in self.attempts: self.result = RemotePetition.eval_async_retry(**self.kwargs) if any(self.kwargs["results"]["failed"]): self.validate(i) succeeded = [ self.kwargs["results"]["failed"].pop(0) for i in range(10) ] self.kwargs["results"]["success"].append(succeeded) self.kwargs = {**self.kwargs, **self.result} continue assert i < self.kwargs["max_retries"] assert self.result is False
def petitions(self, count=10): return [ ObjDict(id=id, url=RemotePetition.url_addr(id)) for id in range(count) ]
def test_page_url_template(self, state="open"): result = RemotePetition.page_url_template(state, archived=False) assert result == self.page_url_template(state)
def test_url_addr(self): assert RemotePetition.url_addr(self.id, archived=False) == self.petition_url()
def test_get_base_url(self): assert RemotePetition.get_base_url(archived=False) == self.base_url assert RemotePetition.get_base_url( archived=True) == self.base_archive_url
def fetch_petitions(self): logger.info(f"fetching remote petitions: {self.petition_ids}") return Remote.async_fetch(self.petition_ids) logger.info("petition remote fetch completed")
def fetch_query(self): logger.info(f"querying remote for state: {self.state}") responses = Remote.async_query(state=self.state, indexes=self.indexes) return Remote.unpack_query(responses)