Skip to content

Commit 60f4c7d

Browse files
authored
Fix bug in getting entity names (#252)
Fixes this error in fetch_entity_names: https://screenshot.googleplex.com/Bm9BzSUnNYB87N9 There was a recent change that removes auto-flattening when unpacking arcs (#244), but fetch_entity_names was still assuming that values would be flattened. Also updated the tests to use mock responses that are formatted the way the response would actually come back as.
1 parent 44cebf5 commit 60f4c7d

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

datacommons_client/endpoints/node.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,15 @@ def fetch_entity_names(
235235

236236
# Iterate through the fetched data and populate the names dictionary.
237237
for dcid, properties in data.items():
238+
if not properties:
239+
continue
238240
if language == "en":
239-
name = extract_name_from_english_name_property(properties=properties)
241+
name = extract_name_from_english_name_property(
242+
properties=properties.get(name_property, []))
240243
lang_used = "en"
241244
else:
242245
name, lang_used = extract_name_from_property_with_language(
243-
properties=properties,
246+
properties=properties.get(name_property, []),
244247
language=language,
245248
fallback_language=fallback_language,
246249
)

datacommons_client/tests/endpoints/test_node_endpoint.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
from datacommons_client.endpoints.base import API
55
from datacommons_client.endpoints.node import NodeEndpoint
66
from datacommons_client.endpoints.response import NodeResponse
7+
from datacommons_client.models.node import Arcs
78
from datacommons_client.models.node import Name
89
from datacommons_client.models.node import Node
10+
from datacommons_client.models.node import NodeGroup
911
from datacommons_client.utils.names import DEFAULT_NAME_PROPERTY
1012
from datacommons_client.utils.names import NAME_WITH_LANGUAGE_PROPERTY
1113

@@ -212,13 +214,11 @@ def test_fetch_entity_names_english(mock_extract_name):
212214
# Mock the response from fetch_property_values
213215
endpoint.fetch_property_values = MagicMock(return_value=NodeResponse(
214216
data={
215-
"dc/123": {
216-
"properties": {
217-
DEFAULT_NAME_PROPERTY: [{
218-
"value": "Guatemala"
219-
}]
220-
}
221-
}
217+
'dc/123':
218+
Arcs(arcs={
219+
DEFAULT_NAME_PROPERTY:
220+
NodeGroup(nodes=[Node(value='Guatemala')])
221+
})
222222
}))
223223

224224
result = endpoint.fetch_entity_names("dc/123")
@@ -233,7 +233,8 @@ def test_fetch_entity_names_english(mock_extract_name):
233233
)
234234
}
235235

236-
mock_extract_name.assert_called_once()
236+
mock_extract_name.assert_called_once_with(
237+
properties=[Node(value="Guatemala")])
237238

238239

239240
@patch(
@@ -247,14 +248,12 @@ def test_fetch_entity_names_non_english(mock_extract_name):
247248

248249
endpoint.fetch_property_values = MagicMock(return_value=NodeResponse(
249250
data={
250-
"dc/123": {
251-
"properties": {
252-
NAME_WITH_LANGUAGE_PROPERTY: [{
253-
"value": "Californie",
254-
"lang": "fr"
255-
}]
256-
}
257-
}
251+
'dc/123':
252+
Arcs(
253+
arcs={
254+
NAME_WITH_LANGUAGE_PROPERTY:
255+
NodeGroup(nodes=[Node(value='Californie')])
256+
})
258257
}))
259258

260259
result = endpoint.fetch_entity_names("dc/123", language="fr")
@@ -269,7 +268,10 @@ def test_fetch_entity_names_non_english(mock_extract_name):
269268
)
270269
}
271270

272-
mock_extract_name.assert_called_once()
271+
mock_extract_name.assert_called_once_with(
272+
properties=[Node(value='Californie')],
273+
language='fr',
274+
fallback_language=None)
273275

274276

275277
@patch(
@@ -283,14 +285,12 @@ def test_fetch_entity_names_with_fallback(mock_extract_name_lang):
283285

284286
endpoint.fetch_property_values = MagicMock(return_value=NodeResponse(
285287
data={
286-
"dc/123": {
287-
"properties": {
288-
NAME_WITH_LANGUAGE_PROPERTY: [{
289-
"value": "Chiquimula",
290-
"lang": "en"
291-
}]
292-
}
293-
}
288+
'dc/123':
289+
Arcs(
290+
arcs={
291+
NAME_WITH_LANGUAGE_PROPERTY:
292+
NodeGroup(nodes=[Node(value='Chiquimula')])
293+
})
294294
}))
295295

296296
result = endpoint.fetch_entity_names("dc/123",
@@ -305,6 +305,10 @@ def test_fetch_entity_names_with_fallback(mock_extract_name_lang):
305305
property=NAME_WITH_LANGUAGE_PROPERTY,
306306
)
307307
}
308+
mock_extract_name_lang.assert_called_once_with(
309+
properties=[Node(value='Chiquimula')],
310+
language='fr',
311+
fallback_language='en')
308312

309313

310314
@patch(

datacommons_client/tests/test_names.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ def test_extract_name_from_english_name_property_with_list():
1010
assert result == "Test Name"
1111

1212

13+
def test_extract_name_from_english_empty_list():
14+
"""Test extracting name from an empty list."""
15+
result = extract_name_from_english_name_property([])
16+
assert result == ""
17+
18+
1319
def test_extract_name_from_english_not_list():
1420
"""Test extracting name from a single Node (not in a list)."""
1521
property_node = Node(value="Single Node Name")

datacommons_client/utils/names.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def extract_name_from_english_name_property(properties: list | Node) -> str:
1515
Returns:
1616
str: The extracted name.
1717
"""
18+
if not properties:
19+
return ''
20+
1821
if isinstance(properties, Node):
1922
properties = [properties]
2023

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ mock
33
pandas
44
pytest
55
requests==2.32.0
6-
typing_extensions==3.10.0.0
6+
typing_extensions==4.7.1
77
yapf==0.40.2

0 commit comments

Comments
 (0)