|
5 | 5 | from datacommons_client.endpoints.response import NodeResponse |
6 | 6 | from datacommons_client.endpoints.response import ObservationResponse |
7 | 7 | from datacommons_client.endpoints.response import ResolveResponse |
| 8 | +from datacommons_client.models.node import Arcs |
8 | 9 | from datacommons_client.models.node import Node |
9 | 10 | from datacommons_client.models.node import NodeGroup |
10 | 11 | from datacommons_client.models.observation import Facet |
@@ -182,7 +183,11 @@ def test_flatten_arcs(): |
182 | 183 | result = flatten_properties(response.data) |
183 | 184 |
|
184 | 185 | assert "dc/03lw9rhpendw5" in result |
185 | | - assert result["dc/03lw9rhpendw5"].value == "191 Peachtree Tower" |
| 186 | + assert result["dc/03lw9rhpendw5"] == { |
| 187 | + "name": [ |
| 188 | + Node(value="191 Peachtree Tower", provenanceId="dc/base/EIA_860") |
| 189 | + ] |
| 190 | + } |
186 | 191 |
|
187 | 192 |
|
188 | 193 | def test_flatten_multiple_arcs_with_multiple_nodes(): |
@@ -283,6 +288,234 @@ def test_unpack_arcs_multiple_properties(): |
283 | 288 | assert result == expected |
284 | 289 |
|
285 | 290 |
|
| 291 | +def test_extract_connected_dcids(): |
| 292 | + """Test that extract_connected_dcids is successful when multiple dcid and multiple |
| 293 | + properties are in the response.""" |
| 294 | + json_data = { |
| 295 | + "data": { |
| 296 | + "geoId/06": { |
| 297 | + "arcs": { |
| 298 | + "containedInPlace": { |
| 299 | + "nodes": [{ |
| 300 | + "dcid": "country/USA", |
| 301 | + "name": "United States", |
| 302 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 303 | + "types": ["Country"] |
| 304 | + }, { |
| 305 | + "dcid": "usc/PacificDivision", |
| 306 | + "name": "Pacific Division", |
| 307 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 308 | + "types": ["CensusDivision"] |
| 309 | + }] |
| 310 | + }, |
| 311 | + "name": { |
| 312 | + "nodes": [{ |
| 313 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 314 | + "value": "California" |
| 315 | + }] |
| 316 | + } |
| 317 | + } |
| 318 | + }, |
| 319 | + "geoId/07": { |
| 320 | + "arcs": { |
| 321 | + "containedInPlace": { |
| 322 | + "nodes": [{ |
| 323 | + "dcid": "country/USA", |
| 324 | + "name": "United States", |
| 325 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 326 | + "types": ["Country"] |
| 327 | + }] |
| 328 | + }, |
| 329 | + } |
| 330 | + } |
| 331 | + } |
| 332 | + } |
| 333 | + response = NodeResponse.from_json(json_data) |
| 334 | + result = response.extract_connected_dcids(subject_dcid='geoId/06', |
| 335 | + property_dcid='containedInPlace') |
| 336 | + assert result == ['country/USA', 'usc/PacificDivision'] |
| 337 | + |
| 338 | + |
| 339 | +def test_extract_connected_dcids_with_nonexistent_dcid(): |
| 340 | + """Test that extract_connected_dcids returns empty when requested dcid is not in the |
| 341 | + NodeResponse.""" |
| 342 | + json_data = { |
| 343 | + "data": { |
| 344 | + "geoId/06": { |
| 345 | + "arcs": { |
| 346 | + "name": { |
| 347 | + "nodes": [{ |
| 348 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 349 | + "value": "California" |
| 350 | + }] |
| 351 | + } |
| 352 | + } |
| 353 | + }, |
| 354 | + } |
| 355 | + } |
| 356 | + response = NodeResponse.from_json(json_data) |
| 357 | + result = response.extract_connected_dcids(subject_dcid='geoId/07', |
| 358 | + property_dcid='name') |
| 359 | + assert result == [] |
| 360 | + |
| 361 | + |
| 362 | +def test_extract_connected_dcids_with_nonexistent_property(): |
| 363 | + """Test that extract_connected_dcids returns empty when requested property is not in |
| 364 | + the NodeResponse.""" |
| 365 | + json_data = { |
| 366 | + "data": { |
| 367 | + "geoId/06": { |
| 368 | + "arcs": { |
| 369 | + "name": { |
| 370 | + "nodes": [{ |
| 371 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 372 | + "value": "California" |
| 373 | + }] |
| 374 | + } |
| 375 | + } |
| 376 | + }, |
| 377 | + } |
| 378 | + } |
| 379 | + response = NodeResponse.from_json(json_data) |
| 380 | + result = response.extract_connected_dcids(subject_dcid='geoId/06', |
| 381 | + property_dcid='containedInPlace') |
| 382 | + assert result == [] |
| 383 | + |
| 384 | + |
| 385 | +def test_extract_connected_dcids_does_not_include_none_for_value_only_nodes(): |
| 386 | + """Test that extract_connected_dcids does not include None in the returned list |
| 387 | + when the nodes in the response only contain values and not dcids.""" |
| 388 | + json_data = { |
| 389 | + "data": { |
| 390 | + "geoId/06": { |
| 391 | + "arcs": { |
| 392 | + "name": { |
| 393 | + "nodes": [{ |
| 394 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 395 | + "value": "California" |
| 396 | + }] |
| 397 | + } |
| 398 | + } |
| 399 | + }, |
| 400 | + } |
| 401 | + } |
| 402 | + response = NodeResponse.from_json(json_data) |
| 403 | + result = response.extract_connected_dcids(subject_dcid='geoId/06', |
| 404 | + property_dcid='name') |
| 405 | + assert result == [] |
| 406 | + |
| 407 | + |
| 408 | +def test_extract_connected_dcids_with_node_type_filter(): |
| 409 | + """Test that extract_connected_dcids returns dcids with the corresponding |
| 410 | + node_type.""" |
| 411 | + |
| 412 | + json_data = { |
| 413 | + "data": { |
| 414 | + "geoId/06": { |
| 415 | + "arcs": { |
| 416 | + "relatedPlaces": { |
| 417 | + "nodes": [{ |
| 418 | + "dcid": "country/USA", |
| 419 | + "name": "United States", |
| 420 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 421 | + "types": ["Country"] |
| 422 | + }, { |
| 423 | + "dcid": "usc/PacificDivision", |
| 424 | + "name": "Pacific Division", |
| 425 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 426 | + "types": ["CensusDivision"] |
| 427 | + }, { |
| 428 | + "dcid": "node3", |
| 429 | + }] |
| 430 | + } |
| 431 | + } |
| 432 | + }, |
| 433 | + } |
| 434 | + } |
| 435 | + response = NodeResponse.from_json(json_data) |
| 436 | + result = response.extract_connected_dcids(subject_dcid='geoId/06', |
| 437 | + property_dcid='relatedPlaces', |
| 438 | + connected_node_types="Country") |
| 439 | + assert result == ['country/USA'] |
| 440 | + |
| 441 | + |
| 442 | +def test_extract_connected_dcids_with_multiple_node_type_filter(): |
| 443 | + """Test that extract_connected_dcids returns dcids with the corresponding |
| 444 | + connected_node_types.""" |
| 445 | + json_data = { |
| 446 | + "data": { |
| 447 | + "geoId/06": { |
| 448 | + "arcs": { |
| 449 | + "relatedPlaces": { |
| 450 | + "nodes": [{ |
| 451 | + "dcid": "country/USA", |
| 452 | + "name": "United States", |
| 453 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 454 | + "types": ["Country"] |
| 455 | + }, { |
| 456 | + "dcid": "usc/PacificDivision", |
| 457 | + "name": "Pacific Division", |
| 458 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 459 | + "types": ["CensusDivision"] |
| 460 | + }, { |
| 461 | + "dcid": "node3", |
| 462 | + "types": ["City"] |
| 463 | + }] |
| 464 | + } |
| 465 | + } |
| 466 | + }, |
| 467 | + } |
| 468 | + } |
| 469 | + response = NodeResponse.from_json(json_data) |
| 470 | + result = response.extract_connected_dcids( |
| 471 | + subject_dcid='geoId/06', |
| 472 | + property_dcid='relatedPlaces', |
| 473 | + connected_node_types=["Country", "City"]) |
| 474 | + assert result == ['country/USA', 'node3'] |
| 475 | + |
| 476 | + |
| 477 | +def test_extract_connected_nodes_with_multiple_node_type_filter(): |
| 478 | + """Test that extract_connected_nodes returns only nodes with the corresponding |
| 479 | + connected_node_types.""" |
| 480 | + |
| 481 | + json_data = { |
| 482 | + "data": { |
| 483 | + "geoId/06": { |
| 484 | + "arcs": { |
| 485 | + "relatedPlaces": { |
| 486 | + "nodes": [{ |
| 487 | + "dcid": "country/USA", |
| 488 | + "name": "United States", |
| 489 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 490 | + "types": ["Country"] |
| 491 | + }, { |
| 492 | + "dcid": "usc/PacificDivision", |
| 493 | + "name": "Pacific Division", |
| 494 | + "provenanceId": "dc/base/WikidataOtherIdGeos", |
| 495 | + "types": ["CensusDivision"] |
| 496 | + }, { |
| 497 | + "dcid": "node3", |
| 498 | + "types": ["City"] |
| 499 | + }] |
| 500 | + } |
| 501 | + } |
| 502 | + }, |
| 503 | + } |
| 504 | + } |
| 505 | + response = NodeResponse.from_json(json_data) |
| 506 | + result = response.extract_connected_nodes( |
| 507 | + subject_dcid='geoId/06', |
| 508 | + property_dcid='relatedPlaces', |
| 509 | + connected_node_types=["Country", "City"]) |
| 510 | + assert result == [ |
| 511 | + Node(dcid="country/USA", |
| 512 | + name="United States", |
| 513 | + provenanceId="dc/base/WikidataOtherIdGeos", |
| 514 | + types=["Country"]), |
| 515 | + Node(dcid="node3", types=["City"]) |
| 516 | + ] |
| 517 | + |
| 518 | + |
286 | 519 | ### ----- Test Observation Response ----- ### |
287 | 520 |
|
288 | 521 |
|
|
0 commit comments