File: test_maps_search_client.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (254 lines) | stat: -rw-r--r-- 10,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
from azure.core.credentials import AccessToken, AzureKeyCredential
from azure.maps.search import MapsSearchClient
from azure.maps.search.models import StructuredAddress
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy
from search_preparer import MapsSearchPreparer

# cSpell:disable
class TestMapsSearchClient(AzureRecordedTestCase):
    def setup_method(self, method):
        self.client = MapsSearchClient(
            credential=AzureKeyCredential(os.environ.get('AZURE_SUBSCRIPTION_KEY', "AzureMapsSubscriptionKey"))
        )
        assert self.client is not None

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_fuzzy_search_poi_coordinates(self):
        result = self.client.fuzzy_search("Taipei 101", coordinates=(25.0338053, 121.5640089))
        assert len(result.results) > 0
        top_answer = result.results[0]
        assert top_answer.point_of_interest.name == "Taipei 101"
        assert top_answer.address.street_name == "Xinyi Road Section 5"
        assert top_answer.address.municipality == "Taipei City"
        assert top_answer.address.postal_code == "110"
        assert top_answer.address.country_code_iso3 == "TWN"
        assert top_answer.position.lat > 25 and top_answer.position.lon > 121

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_fuzzy_search_poi_country_set(self):
        result = self.client.fuzzy_search("Taipei 101", country_filter=["TW"])
        assert len(result.results) > 0
        for item in result.results:
            assert item.address.country_code_iso3 == "TWN"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_fuzzy_search_address(self):
        result = self.client.fuzzy_search("No. 221, Sec. 2, Zhishan Rd., Shilin Dist., Taipei City 111, Taiwan (R.O.C.)")
        assert len(result.results) > 0
        top_answer = result.results[0]
        assert top_answer.type == "Point Address"
        assert top_answer.address.street_name == "Zhishan Road Section 2"
        assert top_answer.address.municipality == "Taipei City"
        assert top_answer.address.postal_code == "111"
        assert top_answer.address.country_code_iso3 == "TWN"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_fuzzy_search_top(self):
        result = self.client.fuzzy_search("Taiwan High Speed Rail", top=5)
        assert len(result.results) == 5

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_fuzzy_search_skip(self):
        result = self.client.fuzzy_search("Taiwan High Speed Rail")
        assert len(result.results) > 0

        assert result.total_results > result.num_results
        result2 = self.client.fuzzy_search("Taiwan High Speed Rail", skip=result.num_results)
        assert len(result2.results) > 0 and result2.results[0] != result.results[0]

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_fuzzy_search_multiple_results(self):
        result = self.client.fuzzy_search("Taiwan High Speed Rail")
        assert len(result.results) > 0

        assert result.total_results > result.num_results
        result2 = self.client.fuzzy_search("Taiwan High Speed Rail", skip=result.num_results)
        assert len(result2.results) > 0 and result2.results[0] != result.results[0]

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_point_of_interest(self):
        result = self.client.search_point_of_interest("Taipei")
        assert len(result.results) > 0
        for item in result.results:
            assert item.type == "POI"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_address(self):
        result = self.client.search_address("Taipei")
        assert len(result.results) > 0
        for item in result.results:
            assert item.type != "POI"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_nearby_point_of_interest(self):
        result = self.client.search_nearby_point_of_interest(coordinates=(25.0338053, 121.5640089))
        assert len(result.results) > 0
        for item in result.results:
            assert item.type == "POI"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_point_of_interest_category(self):
        result = self.client.search_point_of_interest_category(
            "RESTAURANT", coordinates=(25.0338053, 121.5640089)
        )
        assert len(result.results) > 0
        for item in result.results:
            assert item.type == "POI"
            assert "RESTAURANT" in [category.code for category in item.point_of_interest.classifications]

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_structured_address(self):
        addr = StructuredAddress(street_number="221",
                                 street_name="Sec. 2, Zhishan Rd.",
                                 municipality_subdivision="Shilin Dist.",
                                 municipality="Taipei City",
                                 country_code="TW")
        result = self.client.search_structured_address(addr)
        assert len(result.results) > 0
        for item in result.results:
            assert item.type != "POI"
        top_answer = result.results[0]
        assert top_answer.type == "Point Address"
        assert top_answer.address.street_name == "Zhishan Road Section 2"
        assert top_answer.address.municipality == "Taipei City"
        assert top_answer.address.postal_code == "111"
        assert top_answer.address.country_code_iso3 == "TWN"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_get_geometries(self):
        result = self.client.get_geometries(geometry_ids=["8bceafe8-3d98-4445-b29b-fd81d3e9adf5"])
        assert len(result) > 0
        assert result[0].provider_id == "8bceafe8-3d98-4445-b29b-fd81d3e9adf5"
        top_answer = result[0]
        assert top_answer.geometry_data.type == "FeatureCollection"
        assert len(top_answer.geometry_data.features) > 0

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_get_point_of_interest_categories(self):
        result = self.client.get_point_of_interest_categories()
        assert len(result) > 0
        assert result[0].name == 'Sports Center'
        assert result[0].id == 7320

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_reverse_search_address(self):
        result = self.client.reverse_search_address(coordinates=(25.0338053, 121.5640089))
        assert len(result.results) > 0
        top_answer = result.results[0]
        assert top_answer.address.building_number == '45'
        assert top_answer.address.postal_code == "110"
        assert top_answer.address.country_code_iso3 == "TWN"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_reverse_search_cross_street_address(self):
        result = self.client.reverse_search_cross_street_address(coordinates=(25.0338053, 121.5640089))
        assert len(result.addresses) > 0
        top_answer = result.addresses[0]
        assert top_answer.address.postal_code == "110"
        assert top_answer.address.country_code_iso3 == "TWN"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_inside_geometry(self):
        geo_json_obj = {
            'type': 'Polygon',
            'coordinates': [
                [[-122.43576049804686, 37.7524152343544],
                [-122.43301391601562, 37.70660472542312],
                [-122.36434936523438, 37.712059855877314],
                [-122.43576049804686, 37.7524152343544]]
            ]
        }
        result = self.client.search_inside_geometry(
            query="pizza",
            geometry=geo_json_obj
        )
        assert len(result.results) > 0
        top_answer = result.results[0]
        assert top_answer.score > 2
        assert top_answer.address.country_subdivision == "CA"
        assert top_answer.address.local_name == "San Francisco"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_along_route(self):
        route_obj = {
            "route": {
                "type": "LineString",
                "coordinates": [
                    [-122.143035,47.653536],
                    [-122.187164,47.617556],
                    [-122.114981,47.570599],
                    [-122.132756,47.654009]
                ]
            }
        }
        result = self.client.search_along_route(
            query="burger",
            route=route_obj,
            max_detour_time=1000
        )
        assert len(result.results) > 0
        top_answer = result.results[0]
        assert top_answer.address.country_subdivision_name == "Washington"
        assert top_answer.address.country_subdivision == "WA"
        assert top_answer.address.country_code == "US"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def test_search_address(self):
        result = self.client.search_address(query="15127 NE 24th Street, Redmond, WA 98052")
        assert len(result.results) > 0
        top_answer = result.results[0]
        assert top_answer.type == 'Point Address'
        assert top_answer.address.country_subdivision == "WA"
        assert top_answer.address.country_code == "US"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def fuzzy_search_batch(self):
        result = self.client.fuzzy_search_batch(
            search_queries=[
                "350 5th Ave, New York, NY 10118&limit=1",
                "400 Broad St, Seattle, WA 98109&limit=3"
            ]
        )
        assert len(result.items) > 0
        top_answer = result.items[0]
        assert top_answer.response.results[0].address.country_subdivision == "NY"
        assert top_answer.response.results[0].address.country_code == "US"

    @MapsSearchPreparer()
    @recorded_by_proxy
    def search_address_batch(self):
        result = self.client.search_address_batch(
            search_queries=[
                "350 5th Ave, New York, NY 10118&limit=1",
                "400 Broad St, Seattle, WA 98109&limit=3"
            ]
        )
        assert len(result.items) > 0
        top_answer = result.items[0]
        assert top_answer.response.results[0].address.country_subdivision == "NY"
        assert top_answer.response.results[0].address.country_code == "US"