|
4 | 4 | import pandas as pd |
5 | 5 | import pytest |
6 | 6 |
|
| 7 | +from datacommons_client import use_api_key |
7 | 8 | from datacommons_client.client import DataCommonsClient |
8 | 9 | from datacommons_client.endpoints.base import API |
9 | 10 | from datacommons_client.endpoints.node import NodeEndpoint |
@@ -419,3 +420,54 @@ def test_client_end_to_end_surface_header_propagation_observation( |
419 | 420 | assert headers is not None |
420 | 421 | assert headers.get("x-surface") == "datagemma" |
421 | 422 | assert headers.get("X-API-Key") == "test_key" |
| 423 | + |
| 424 | + |
| 425 | +@patch("datacommons_client.endpoints.base.post_request") |
| 426 | +def test_use_api_key_with_observation_fetch(mock_post_request): |
| 427 | + """Test use_api_key override for observation fetches (non-threaded).""" |
| 428 | + |
| 429 | + # Setup client with default key |
| 430 | + client = DataCommonsClient(api_key="default-key") |
| 431 | + |
| 432 | + # Configure mock to return valid response structure |
| 433 | + mock_post_request.return_value = {"byVariable": {}, "facets": {}} |
| 434 | + |
| 435 | + # Default usage |
| 436 | + client.observation.fetch(variable_dcids="sv1", entity_dcids=["geo1"]) |
| 437 | + mock_post_request.assert_called() |
| 438 | + _, kwargs = mock_post_request.call_args |
| 439 | + assert kwargs["headers"]["X-API-Key"] == "default-key" |
| 440 | + |
| 441 | + # Context override |
| 442 | + with use_api_key("context-key"): |
| 443 | + client.observation.fetch(variable_dcids="sv1", entity_dcids=["geo1"]) |
| 444 | + _, kwargs = mock_post_request.call_args |
| 445 | + assert kwargs["headers"]["X-API-Key"] == "context-key" |
| 446 | + |
| 447 | + # Back to default |
| 448 | + client.observation.fetch(variable_dcids="sv1", entity_dcids=["geo1"]) |
| 449 | + _, kwargs = mock_post_request.call_args |
| 450 | + assert kwargs["headers"]["X-API-Key"] == "default-key" |
| 451 | + |
| 452 | + |
| 453 | +@patch("datacommons_client.endpoints.base.post_request") |
| 454 | +def test_use_api_key_with_node_fetch_place_ancestors(mock_post_request): |
| 455 | + """Test use_api_key propagation for node graph methods (threaded).""" |
| 456 | + |
| 457 | + client = DataCommonsClient(api_key="default-key") |
| 458 | + |
| 459 | + # Configure mock. fetch_place_ancestors expects a dict response or list of nodes. |
| 460 | + # NodeResponse.data is a dict. |
| 461 | + mock_post_request.return_value = {"data": {}} |
| 462 | + |
| 463 | + # Default usage |
| 464 | + client.node.fetch_place_ancestors(place_dcids=["geoId/06"]) |
| 465 | + _, kwargs = mock_post_request.call_args |
| 466 | + assert kwargs["headers"]["X-API-Key"] == "default-key" |
| 467 | + |
| 468 | + # Context override |
| 469 | + with use_api_key("context-key"): |
| 470 | + # Use a different DCID to avoid hitting fetch_relationship_lru cache |
| 471 | + client.node.fetch_place_ancestors(place_dcids=["geoId/07"]) |
| 472 | + _, kwargs = mock_post_request.call_args |
| 473 | + assert kwargs["headers"]["X-API-Key"] == "context-key" |
0 commit comments