|
4 | 4 | from datacommons_client.endpoints.base import API |
5 | 5 | from datacommons_client.endpoints.node import NodeEndpoint |
6 | 6 | from datacommons_client.endpoints.response import NodeResponse |
| 7 | +from datacommons_client.models.node import Name |
7 | 8 | from datacommons_client.models.node import Node |
| 9 | +from datacommons_client.utils.names import DEFAULT_NAME_PROPERTY |
| 10 | +from datacommons_client.utils.names import NAME_WITH_LANGUAGE_PROPERTY |
8 | 11 |
|
9 | 12 |
|
10 | 13 | def test_node_endpoint_initialization(): |
@@ -198,6 +201,132 @@ def test_node_endpoint_fetch_property_values_string_vs_list(): |
198 | 201 | ) |
199 | 202 |
|
200 | 203 |
|
| 204 | +@patch( |
| 205 | + "datacommons_client.endpoints.node.extract_name_from_english_name_property") |
| 206 | +def test_fetch_entity_names_english(mock_extract_name): |
| 207 | + """Test fetching names in English (default behavior).""" |
| 208 | + mock_extract_name.return_value = "Guatemala" |
| 209 | + api_mock = MagicMock() |
| 210 | + endpoint = NodeEndpoint(api=api_mock) |
| 211 | + |
| 212 | + # Mock the response from fetch_property_values |
| 213 | + endpoint.fetch_property_values = MagicMock(return_value=NodeResponse( |
| 214 | + data={ |
| 215 | + "dc/123": { |
| 216 | + "properties": { |
| 217 | + DEFAULT_NAME_PROPERTY: [{ |
| 218 | + "value": "Guatemala" |
| 219 | + }] |
| 220 | + } |
| 221 | + } |
| 222 | + })) |
| 223 | + |
| 224 | + result = endpoint.fetch_entity_names("dc/123") |
| 225 | + endpoint.fetch_property_values.assert_called_once_with( |
| 226 | + node_dcids=["dc/123"], properties=DEFAULT_NAME_PROPERTY) |
| 227 | + assert result == { |
| 228 | + "dc/123": |
| 229 | + Name( |
| 230 | + value="Guatemala", |
| 231 | + language="en", |
| 232 | + property=DEFAULT_NAME_PROPERTY, |
| 233 | + ) |
| 234 | + } |
| 235 | + |
| 236 | + mock_extract_name.assert_called_once() |
| 237 | + |
| 238 | + |
| 239 | +@patch( |
| 240 | + "datacommons_client.endpoints.node.extract_name_from_property_with_language" |
| 241 | +) |
| 242 | +def test_fetch_entity_names_non_english(mock_extract_name): |
| 243 | + """Test fetching names in a non-English language.""" |
| 244 | + mock_extract_name.return_value = ("Californie", "fr") |
| 245 | + api_mock = MagicMock() |
| 246 | + endpoint = NodeEndpoint(api=api_mock) |
| 247 | + |
| 248 | + endpoint.fetch_property_values = MagicMock(return_value=NodeResponse( |
| 249 | + data={ |
| 250 | + "dc/123": { |
| 251 | + "properties": { |
| 252 | + NAME_WITH_LANGUAGE_PROPERTY: [{ |
| 253 | + "value": "Californie", |
| 254 | + "lang": "fr" |
| 255 | + }] |
| 256 | + } |
| 257 | + } |
| 258 | + })) |
| 259 | + |
| 260 | + result = endpoint.fetch_entity_names("dc/123", language="fr") |
| 261 | + endpoint.fetch_property_values.assert_called_once_with( |
| 262 | + node_dcids=["dc/123"], properties=NAME_WITH_LANGUAGE_PROPERTY) |
| 263 | + assert result == { |
| 264 | + "dc/123": |
| 265 | + Name( |
| 266 | + value="Californie", |
| 267 | + language="fr", |
| 268 | + property=NAME_WITH_LANGUAGE_PROPERTY, |
| 269 | + ) |
| 270 | + } |
| 271 | + |
| 272 | + mock_extract_name.assert_called_once() |
| 273 | + |
| 274 | + |
| 275 | +@patch( |
| 276 | + "datacommons_client.endpoints.node.extract_name_from_property_with_language" |
| 277 | +) |
| 278 | +def test_fetch_entity_names_with_fallback(mock_extract_name_lang): |
| 279 | + """Test fallback to another language when target language is unavailable.""" |
| 280 | + mock_extract_name_lang.return_value = ("Chiquimula", "en") |
| 281 | + api_mock = MagicMock() |
| 282 | + endpoint = NodeEndpoint(api=api_mock) |
| 283 | + |
| 284 | + endpoint.fetch_property_values = MagicMock(return_value=NodeResponse( |
| 285 | + data={ |
| 286 | + "dc/123": { |
| 287 | + "properties": { |
| 288 | + NAME_WITH_LANGUAGE_PROPERTY: [{ |
| 289 | + "value": "Chiquimula", |
| 290 | + "lang": "en" |
| 291 | + }] |
| 292 | + } |
| 293 | + } |
| 294 | + })) |
| 295 | + |
| 296 | + result = endpoint.fetch_entity_names("dc/123", |
| 297 | + language="fr", |
| 298 | + fallback_language="en") |
| 299 | + |
| 300 | + assert result == { |
| 301 | + "dc/123": |
| 302 | + Name( |
| 303 | + value="Chiquimula", |
| 304 | + language="en", |
| 305 | + property=NAME_WITH_LANGUAGE_PROPERTY, |
| 306 | + ) |
| 307 | + } |
| 308 | + |
| 309 | + |
| 310 | +@patch( |
| 311 | + "datacommons_client.endpoints.node.extract_name_from_property_with_language" |
| 312 | +) |
| 313 | +def test_fetch_entity_names_no_result(mock_extract_name_lang): |
| 314 | + """Test case when no name is found.""" |
| 315 | + mock_extract_name_lang.return_value = (None, None) |
| 316 | + api_mock = MagicMock() |
| 317 | + endpoint = NodeEndpoint(api=api_mock) |
| 318 | + |
| 319 | + endpoint.fetch_property_values = MagicMock(return_value=NodeResponse( |
| 320 | + data={"dc/999": { |
| 321 | + "properties": {} |
| 322 | + }})) |
| 323 | + |
| 324 | + result = endpoint.fetch_entity_names("dc/999", |
| 325 | + language="es", |
| 326 | + fallback_language="en") |
| 327 | + assert result == {} |
| 328 | + |
| 329 | + |
201 | 330 | @patch("datacommons_client.endpoints.node.fetch_parents_lru") |
202 | 331 | def test_fetch_parents_cached_delegates_to_lru(mock_fetch_lru): |
203 | 332 | mock_fetch_lru.return_value = (Node("B", "B name", "Region"),) |
|
0 commit comments