def test_responses_are_cached(self): country_code_to_name('country:GB') country_code_to_name('country:GB') assert country_code_to_name.cache_info().hits == 1 assert country_code_to_name.cache_info().misses == 1 assert country_code_to_name.cache_info().maxsize == 128
def test_404_raises(self, rmock): rmock.get( 'https://country.register.gov.uk/records/GB.json', status_code=404, ) with pytest.raises(requests.exceptions.RequestException): country_code_to_name('country:GB')
def test_responses_are_cached(self, rmock): rmock.get('https://country.register.gov.uk/records/GB.json', json=self.GB_COUNTRY_JSON, status_code=200) country_code_to_name('country:GB') country_code_to_name('country:GB') assert len(rmock.request_history) == 1 assert country_code_to_name.cache_info().hits == 1 assert country_code_to_name.cache_info().misses == 1 assert country_code_to_name.cache_info().maxsize == 128
def test_correct_url_requested_and_code_converted_to_name( self, rmock, full_code, expected_url, response, expected_name): rmock.get(expected_url, json=response, status_code=200) country_name = country_code_to_name(full_code) assert country_name == expected_name
def test_retries_if_not_200(self, rmock): rmock.get( 'https://country.register.gov.uk/records/GB.json', [{'json': {}, 'status_code': 500}, {'json': self.GB_COUNTRY_JSON, 'status_code': 200}], ) country_name = country_code_to_name('country:GB') assert country_name == 'United Kingdom' assert len(rmock.request_history) == 2
def test_non_existent_country_raises(self): with pytest.raises(RegisterKeyNotFound): country_code_to_name('country:WAKANDA')
def test_code_converted_to_name(self, full_code, expected_name): country_name = country_code_to_name(full_code) assert country_name == expected_name
def _format_field(field_name, field_value): if field_name == 'supplierRegisteredCountry': field_value = country_code_to_name(field_value) return field_name, field_value