diff --git a/parserator_web/templates/parserator_web/index.html b/parserator_web/templates/parserator_web/index.html index a72d9c80..0dcf86e1 100644 --- a/parserator_web/templates/parserator_web/index.html +++ b/parserator_web/templates/parserator_web/index.html @@ -11,13 +11,13 @@

U.S. addres

Dealing with some messy or unstructured addresses? We can parse them for you.

Try it out! Parse an address in the United States into fields like AddressNumber, StreetName and ZipCode.

-
+ {% csrf_token %}
- + @@ -38,5 +38,43 @@

Parsing results

{% endblock %} {% block extra_js %} - + {% endblock %} diff --git a/parserator_web/views.py b/parserator_web/views.py index 0be3f4a9..a0f029f2 100644 --- a/parserator_web/views.py +++ b/parserator_web/views.py @@ -14,11 +14,24 @@ class AddressParse(APIView): renderer_classes = [JSONRenderer] def get(self, request): - # TODO: Flesh out this method to parse an address string using the - # parse() method and return the parsed components to the frontend. - return Response({}) + address_string = request.query_params.get('address', None) + if not address_string: + raise ParseError(detail="No address provided") + + address_components, address_type = self.parse(address_string) + + if address_components is None: + raise ParseError(detail="Invalid address format") + + return Response({ + "input_string": address_string, + "address_components": address_components, + "address_type": address_type + }) def parse(self, address): - # TODO: Implement this method to return the parsed components of a - # given address using usaddress: https://github.com/datamade/usaddress - return address_components, address_type + try: + parsed_address, address_type = usaddress.tag(address) + return parsed_address, address_type + except usaddress.RepeatedLabelError: + return None, None diff --git a/tests/test_views.py b/tests/test_views.py index bfd5d0b7..7aedeeb3 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,15 +1,27 @@ import pytest +from django.urls import reverse - +@pytest.mark.django_db def test_api_parse_succeeds(client): - # TODO: Finish this test. Send a request to the API and confirm that the - # data comes back in the appropriate format. + # This test verifies that the API successfully parses a valid address address_string = '123 main st chicago il' - pytest.fail() - + response = client.get(reverse('parse_address'), {'address': address_string}) + + assert response.status_code == 200 + data = response.json() + assert 'input_string' in data + assert data['input_string'] == address_string + assert 'address_components' in data + assert 'address_type' in data + assert len(data['address_components']) > 0 # Ensure some components are parsed +@pytest.mark.django_db def test_api_parse_raises_error(client): - # TODO: Finish this test. The address_string below will raise a - # RepeatedLabelError, so ParseAddress.parse() will not be able to parse it. + # This test verifies that the API returns an error for an invalid address address_string = '123 main st chicago il 123 main st' - pytest.fail() + response = client.get(reverse('parse_address'), {'address': address_string}) + + assert response.status_code == 400 + data = response.json() + assert 'error' in data + assert data['error'] == 'Invalid address format'