Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion googlemaps/addressvalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ def _addressvalidation_extract(response):
# raise exceptions.ApiError(response.status_code, error)


def addressvalidation(client, addressLines, regionCode=None , locality=None, enableUspsCass=None):
def addressvalidation(
client,
addressLines,
regionCode=None,
locality=None,
enableUspsCass=None,
administrativeArea=None,
postalCode=None,
):
"""
The Google Maps Address Validation API returns a verification of an address
See https://developers.google.com/maps/documentation/address-validation/overview
Expand All @@ -57,6 +65,11 @@ def addressvalidation(client, addressLines, regionCode=None , locality=None, ena
:type locality: string
:param enableUspsCass For the "US" and "PR" regions only, you can optionally enable the Coding Accuracy Support System (CASS) from the United States Postal Service (USPS)
:type locality: boolean
:param administrativeArea: (optional) Highest administrative subdivision
which is used for postal addresses of a country or region
:type administrativeArea: string
:param postalCode: (optional) Postal code of the address
:type postalCode: string
"""

params = {
Expand All @@ -71,6 +84,12 @@ def addressvalidation(client, addressLines, regionCode=None , locality=None, ena
if locality is not None:
params["address"]["locality"] = locality

if administrativeArea is not None:
params["address"]["administrativeArea"] = administrativeArea

if postalCode is not None:
params["address"]["postalCode"] = postalCode

if enableUspsCass is not False or enableUspsCass is not None:
params["enableUspsCass"] = enableUspsCass

Expand Down
56 changes: 55 additions & 1 deletion tests/test_addressvalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

"""Tests for the addressvalidation module."""

import json

import responses

import googlemaps
Expand Down Expand Up @@ -45,4 +47,56 @@ def test_simple_addressvalidation(self):
self.assertURLEqual(
"https://addressvalidation.googleapis.com/v1:validateAddress?" "key=%s" % self.key,
responses.calls[0].request.url,
)
)

@responses.activate
def test_addressvalidation_with_administrative_area_and_postal_code(self):
responses.add(
responses.POST,
"https://addressvalidation.googleapis.com/v1:validateAddress",
body='{"result": {}}',
status=200,
content_type="application/json",
)

self.client.addressvalidation(
["1600 Amphitheatre Pkwy"],
regionCode="US",
locality="Mountain View",
enableUspsCass=True,
administrativeArea="CA",
postalCode="94043",
)

self.assertEqual(1, len(responses.calls))
sent_body = json.loads(responses.calls[0].request.body)
self.assertEqual(
{
"address": {
"addressLines": ["1600 Amphitheatre Pkwy"],
"regionCode": "US",
"locality": "Mountain View",
"administrativeArea": "CA",
"postalCode": "94043",
},
"enableUspsCass": True,
},
sent_body,
)

@responses.activate
def test_addressvalidation_omits_unset_optional_fields(self):
responses.add(
responses.POST,
"https://addressvalidation.googleapis.com/v1:validateAddress",
body='{"result": {}}',
status=200,
content_type="application/json",
)

self.client.addressvalidation(["1600 Amphitheatre Pkwy"], regionCode="US")

self.assertEqual(1, len(responses.calls))
sent_body = json.loads(responses.calls[0].request.body)
self.assertNotIn("administrativeArea", sent_body["address"])
self.assertNotIn("postalCode", sent_body["address"])