def test_no_results(self): address_validation = PDOKAddressValidation() address_validation._search = MagicMock(return_value=[]) self.assertRaises(NoResultsException, address_validation.validate_address, self.address_dict) address_validation._search.assert_called_with(self.address_dict)
def test_search_successful_request(self): address_validation = PDOKAddressValidation() result = self._get_mocked_response() expected = result["response"]["docs"] with Mocker() as m: m.get(address_validation.address_validation_url, text=dumps(result)) self.assertEqual(address_validation._search(self.address_dict), expected)
def test_in_weesp_validation(self, patched_search): pdok_response = self._load_response_json(JSON_WEESP) patched_search.return_value = pdok_response['response']['docs'] address = {'openbare_ruimte': 'Achtergracht', 'huisnummer': 72} lon, lat = IN_WEESP validation = PDOKAddressValidation() validated_address = validation.validate_address(address=address, lon=lon, lat=lat) self.assertEqual(validated_address['openbare_ruimte'], 'Achtergracht') self.assertEqual(validated_address['postcode'], '1381BP') self.assertEqual(validated_address['huisnummer'], 72) self.assertEqual(validated_address['woonplaats'], 'Weesp')
def test_in_amsterdam_validation(self, patched_search): pdok_response = self._load_response_json(JSON_AMSTERDAM) patched_search.return_value = pdok_response['response']['docs'] address = {'openbare_ruimte': 'Achtergracht', 'huisnummer': 72} lon, lat = IN_AMSTERDAM validation = PDOKAddressValidation() validated_address = validation.validate_address(address=address, lon=lon, lat=lat) self.assertEqual(validated_address['openbare_ruimte'], 'Nieuwe Achtergracht') self.assertEqual(validated_address['postcode'], '1018XZ') self.assertEqual(validated_address['huisnummer'], 72) self.assertEqual(validated_address['woonplaats'], 'Amsterdam')
def test_search_unavailable(self): address_validation = PDOKAddressValidation() with Mocker() as m: m.get(address_validation.address_validation_url, status_code=400) self.assertRaises(AddressValidationUnavailableException, address_validation._search, self.address_dict)
def test_search_connection_error(self): address_validation = PDOKAddressValidation() with Mocker() as m: m.get(address_validation.address_validation_url, exc=ConnectionError) self.assertRaises(AddressValidationUnavailableException, address_validation._search, self.address_dict)
def test_no_results_allow_unverified(self): address_validation = PDOKAddressValidation() address_validation._search = MagicMock(return_value=[]) validation_mixin = AddressValidationMixin() validation_mixin.get_address_validation = MagicMock( return_value=address_validation) location_data = { 'geometrie': Point(4.898466, 52.361585), 'address': self.address_dict, } with self.settings(ALLOW_INVALID_ADDRESS_AS_UNVERIFIED=False): self.assertRaises(ValidationError, validation_mixin.validate_location, location_data) with self.settings(ALLOW_INVALID_ADDRESS_AS_UNVERIFIED=True): try: validation_mixin.validate_location(location_data) except ValidationError: self.fail("Should not raise exception because of setting")
def test_address_found(self): address_validation = PDOKAddressValidation() result = self._get_mocked_response()["response"]["docs"][0] address_validation._search = MagicMock(return_value=[result]) address_validation._search_result_to_address = MagicMock() address_validation.validate_address(self.address_dict) address_validation._search.assert_called_with(self.address_dict) address_validation._search_result_to_address.assert_called_with(result)