From a7334cc892f684d5fe393ae987a5efe962c090bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4limaa?= Date: Thu, 3 Dec 2026 16:59:35 +0800 Subject: [PATCH] fix: omit unset enableUspsCass from Address Validation requests --- googlemaps/addressvalidation.py | 2 +- tests/test_addressvalidation.py | 56 +++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/googlemaps/addressvalidation.py b/googlemaps/addressvalidation.py index 149f3b48..357195b2 100644 --- a/googlemaps/addressvalidation.py +++ b/googlemaps/addressvalidation.py @@ -71,7 +71,7 @@ def addressvalidation(client, addressLines, regionCode=None , locality=None, ena if locality is not None: params["address"]["locality"] = locality - if enableUspsCass is not False or enableUspsCass is not None: + if enableUspsCass is not None: params["enableUspsCass"] = enableUspsCass return client._request("/v1:validateAddress", {}, # No GET params diff --git a/tests/test_addressvalidation.py b/tests/test_addressvalidation.py index d1f2f589..10d27704 100644 --- a/tests/test_addressvalidation.py +++ b/tests/test_addressvalidation.py @@ -18,6 +18,8 @@ """Tests for the addressvalidation module.""" +import json + import responses import googlemaps @@ -29,20 +31,40 @@ def setUp(self): self.key = "AIzaSyD_sJl0qMA65CYHMBokVfMNA7AKyt5ERYs" self.client = googlemaps.Client(self.key) - @responses.activate - def test_simple_addressvalidation(self): - responses.add( - responses.POST, - "https://addressvalidation.googleapis.com/v1:validateAddress", - body='{"address": {"regionCode": "US","locality": "Mountain View","addressLines": "1600 Amphitheatre Pkwy"},"enableUspsCass":true}', - status=200, - content_type="application/json", - ) - - results = self.client.addressvalidation('1600 Amphitheatre Pk', regionCode='US', locality='Mountain View', enableUspsCass=True) - - self.assertEqual(1, len(responses.calls)) - self.assertURLEqual( - "https://addressvalidation.googleapis.com/v1:validateAddress?" "key=%s" % self.key, - responses.calls[0].request.url, - ) \ No newline at end of file + def test_enable_usps_cass_serialization(self): + endpoint = "https://addressvalidation.googleapis.com/v1:validateAddress" + address = { + "addressLines": ["1600 Amphitheatre Parkway"], + "regionCode": "US", + "locality": "Mountain View", + } + cases = [ + ({}, {"address": address}), + ({"enableUspsCass": True}, { + "address": address, "enableUspsCass": True, + }), + ({"enableUspsCass": False}, { + "address": address, "enableUspsCass": False, + }), + ] + for kwargs, expected in cases: + with self.subTest(kwargs=kwargs): + with responses.RequestsMock() as http: + http.add( + responses.POST, + endpoint, + json={"result": {}}, + status=200, + ) + + self.client.addressvalidation( + address["addressLines"], + regionCode=address["regionCode"], + locality=address["locality"], + **kwargs + ) + + request = http.calls[0].request + self.assertURLEqual("%s?key=%s" % (endpoint, self.key), request.url) + body = json.loads(request.body) + self.assertEqual(expected, body) \ No newline at end of file