From 5db3c5fa1f9c3cf889079aa383eb4ef1bbc9162b Mon Sep 17 00:00:00 2001 From: Fajar Pamungkas Date: Mon, 4 Oct 2021 12:26:18 +0700 Subject: [PATCH] fix multiple avoid distance matrix #382 --- coverage.xml | 817 ++++++++++++++++++++++++++++++++++ googlemaps/convert.py | 14 + googlemaps/distance_matrix.py | 4 +- tests/test_convert.py | 11 + 4 files changed, 845 insertions(+), 1 deletion(-) create mode 100644 coverage.xml diff --git a/coverage.xml b/coverage.xml new file mode 100644 index 00000000..c9e1d9c9 --- /dev/null +++ b/coverage.xml @@ -0,0 +1,817 @@ + + + + + + /Users/fpamungkas/Documents/opensource/yakopoto/google-maps-services-python + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/googlemaps/convert.py b/googlemaps/convert.py index 2b3d056e..d9d5f4bc 100644 --- a/googlemaps/convert.py +++ b/googlemaps/convert.py @@ -142,6 +142,20 @@ def join_list(sep, arg): """ return sep.join(as_list(arg)) +def to_list(arg, sep="|"): + """Coerces arg into a list depends on supplied separator. + If arg is already list-like return arg. + + :type sep: string + """ + if _is_list(arg): + return arg + + if isinstance(arg, str): + elements = arg.split(sep) + return [element for element in elements if element] + + return as_list(arg) def as_list(arg): """Coerces arg into a list. If arg is already list-like, returns arg. diff --git a/googlemaps/distance_matrix.py b/googlemaps/distance_matrix.py index a30cbe09..8e084be1 100755 --- a/googlemaps/distance_matrix.py +++ b/googlemaps/distance_matrix.py @@ -107,7 +107,9 @@ def distance_matrix(client, origins, destinations, params["language"] = language if avoid: - if avoid not in ["tolls", "highways", "ferries"]: + valid_avoid_values = {"tolls", "highways", "ferries"} + supplied_avoid_values = set(convert.to_list(avoid)) + if supplied_avoid_values - valid_avoid_values: raise ValueError("Invalid route restriction.") params["avoid"] = avoid diff --git a/tests/test_convert.py b/tests/test_convert.py index 39546aee..fba994ad 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -165,6 +165,17 @@ def test_polyline_round_trip(self): actual_polyline = convert.encode_polyline(points) self.assertEqual(test_polyline, actual_polyline) + def test_to_list(self): + test_to_list = "tolls|highways" + + elements = convert.to_list(test_to_list) + + self.assertEqual(["tolls", "highways"], elements) + test_to_list = "tolls|highways|" + + elements = convert.to_list(test_to_list) + self.assertEqual(["tolls", "highways"], elements) + @pytest.mark.parametrize( "value, expected",