forked from datacommonsorg/api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_resolve_endpoint.py
More file actions
227 lines (184 loc) · 7.65 KB
/
test_resolve_endpoint.py
File metadata and controls
227 lines (184 loc) · 7.65 KB
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
from unittest.mock import MagicMock
from datacommons_client.endpoints.base import API
from datacommons_client.endpoints.resolve import _resolve_correspondence_expression
from datacommons_client.endpoints.resolve import ResolveEndpoint
from datacommons_client.endpoints.response import ResolveResponse
from datacommons_client.models.resolve import Candidate
from datacommons_client.models.resolve import Entity
def test_fetch():
"""Tests the fetch method of ResolveEndpoint."""
api_mock = MagicMock(spec=API)
api_mock.post = MagicMock(return_value={})
endpoint = ResolveEndpoint(api=api_mock)
response = endpoint.fetch(node_ids="Node1", expression="some_expression")
# Check the response
assert isinstance(response, ResolveResponse)
# Check the post request
api_mock.post.assert_called_once_with(payload={
"nodes": ["Node1"],
"property": "some_expression",
},
endpoint="resolve",
all_pages=True,
next_token=None)
def test_fetch_dcid_by_name():
"""Tests the fetch_dcid_by_name method."""
api_mock = MagicMock(spec=API)
api_mock.post = MagicMock(return_value={})
endpoint = ResolveEndpoint(api=api_mock)
response = endpoint.fetch_dcids_by_name(names=["Entity1"],
entity_type="Place")
# Check the response
assert isinstance(response, ResolveResponse)
# Check the post request
api_mock.post.assert_called_once_with(payload={
"nodes": ["Entity1"],
"property": "<-description{typeOf:Place}->dcid"
},
endpoint="resolve",
all_pages=True,
next_token=None)
def test_fetch_dcid_by_wikidata_id():
"""Tests the fetch_dcid_by_wikidata_id method."""
api_mock = MagicMock(spec=API)
api_mock.post = MagicMock(return_value={})
endpoint = ResolveEndpoint(api=api_mock)
response = endpoint.fetch_dcids_by_wikidata_id(wikidata_ids="Q12345",
entity_type="Country")
# Check the response
assert isinstance(response, ResolveResponse)
# Check the post request
api_mock.post.assert_called_once_with(payload={
"nodes": ["Q12345"],
"property": "<-wikidataId{typeOf:Country}->dcid",
},
endpoint="resolve",
all_pages=True,
next_token=None)
def test_fetch_dcids_list_by_wikidata_id():
"""Tests the fetch_dcid_by_wikidata_id method."""
api_mock = MagicMock(spec=API)
api_mock.post = MagicMock(return_value={})
endpoint = ResolveEndpoint(api=api_mock)
response = endpoint.fetch_dcids_by_wikidata_id(
wikidata_ids=["Q12345", "Q695660"])
# Check the response
assert isinstance(response, ResolveResponse)
# Check the post request
api_mock.post.assert_called_once_with(payload={
"nodes": ["Q12345", "Q695660"],
"property": "<-wikidataId->dcid",
},
endpoint="resolve",
all_pages=True,
next_token=None)
def test_fetch_dcid_by_coordinates():
"""Tests the fetch_dcid_by_coordinates method."""
api_mock = MagicMock(spec=API)
api_mock.post = MagicMock(return_value={})
endpoint = ResolveEndpoint(api=api_mock)
response = endpoint.fetch_dcid_by_coordinates(latitude="37.7749",
longitude="-122.4194",
entity_type="City")
# Check the response
assert isinstance(response, ResolveResponse)
# Check the post request
api_mock.post.assert_called_once_with(payload={
"nodes": ["37.7749#-122.4194"],
"property": "<-geoCoordinate{typeOf:City}->dcid",
},
endpoint="resolve",
all_pages=True,
next_token=None)
def test_resolve_correspondence_expression():
"""Tests the resolve_correspondence_expression function."""
expression = _resolve_correspondence_expression(from_type="description",
to_type="dcid",
entity_type="Place")
assert expression == "<-description{typeOf:Place}->dcid"
expression_no_entity_type = _resolve_correspondence_expression(
from_type="description", to_type="dcid")
assert expression_no_entity_type == "<-description->dcid"
def test_flatten_resolve_response():
"""Tests the flatten_resolve_response function."""
# Mock ResolveResponse with multiple entities
mock_data = ResolveResponse(entities=[
Entity(node="Node1", candidates=[Candidate(dcid="Candidate1")]),
Entity(node="Node2",
candidates=[
Candidate(dcid="Candidate2"),
Candidate(dcid="Candidate3")
]),
Entity(node="Node3", candidates=[]) # No candidates
])
# Call the function
result = mock_data.to_flat_dict()
# Expected output
expected = {
"Node1": "Candidate1", # Single candidate
"Node2": ["Candidate2", "Candidate3"], # Multiple candidates
"Node3": [], # No candidates
}
# Assertions
assert result == expected
def test_fetch_indicators_calls_endpoints_correctly():
"""Tests the fetch_indicators method."""
api_mock = MagicMock()
# Mock response data structure
mock_response_data = {
"entities": [{
"node":
"population",
"candidates": [{
"dcid": "Count_Person",
"dominantType": "StatisticalVariable",
"metadata": {
"score": "0.9",
"sentence": "population count"
},
"typeOf": ["StatisticalVariable"]
}]
}]
}
api_mock.post = MagicMock(return_value=mock_response_data)
endpoint = ResolveEndpoint(api=api_mock)
# Call the method
response = endpoint.fetch_indicators(queries=["population"],
target="custom_only")
# Verify post was called with correct payload
api_mock.post.assert_called_once_with(payload={
"nodes": ["population"],
"resolver": "indicator",
"target": "custom_only"
},
endpoint="resolve",
all_pages=True,
next_token=None)
# Verify response parsing
expected = ResolveResponse(entities=[
Entity(node="population",
candidates=[
Candidate(dcid="Count_Person",
dominantType="StatisticalVariable",
metadata={
"score": "0.9",
"sentence": "population count"
},
typeOf=["StatisticalVariable"])
])
])
assert response == expected
def test_fetch_still_works_with_expression():
"""Tests that fetch still works with expression (regression test)."""
api_mock = MagicMock()
mock_response_data = {"entities": []}
api_mock.post = MagicMock(return_value=mock_response_data)
endpoint = ResolveEndpoint(api=api_mock)
endpoint.fetch(node_ids=["geoId/06"], expression="<-containedInPlace")
api_mock.post.assert_called_once_with(payload={
"nodes": ["geoId/06"],
"property": "<-containedInPlace"
},
endpoint="resolve",
all_pages=True,
next_token=None)