def test_call_providers_until_finding_a_working_one(self, providers_mock): valid_mock, unavailable_mock, = self.fill_providers_mock_with( providers_mock, ["unavailable", "valid", "unavailable"]) self.assertEqual(fetch_from_providers(""), valid_mock.get_cep_data.return_value) valid_mock.get_cep_data.assert_called_once() unavailable_mock.get_cep_data.assert_called_once()
def test_subsequent_providers_should_not_be_run_after_a_working_one( self, providers_mock): valid_mock, unavailable_mock, = self.fill_providers_mock_with( providers_mock, ["valid", "unavailable", "unavailable"]) self.assertEqual(fetch_from_providers(""), valid_mock.get_cep_data.return_value) valid_mock.get_cep_data.assert_called_once() unavailable_mock.assert_not_called()
def test_all_providers_should_be_tried_until_a_working_one( self, providers_mock): valid_mock, unavailable_mock, = self.fill_providers_mock_with( providers_mock, ["unavailable", "unavailable", "valid"]) self.assertEqual(fetch_from_providers(""), valid_mock.get_cep_data.return_value) self.assertEqual(unavailable_mock.get_cep_data.call_count, 2) valid_mock.get_cep_data.assert_called_once()
def get_cep_data(cep: str) -> Optional[CEPAddress]: cep_cache = get_installed_cache() try: return cep_cache[cep] except KeyError: cep_address = fetch_from_providers(cep) if cep_address: cep_cache[cep] = cep_address return cep_address
def test_no_available_providers_should_raise(self, providers_mock): valid_mock, unavailable_mock, = self.fill_providers_mock_with( providers_mock, ["unavailable", "unavailable", "unavailable"]) with self.assertRaises(NoAvailableCepProviders): fetch_from_providers("") self.assertEqual(unavailable_mock.get_cep_data.call_count, 3)