|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from dbt_mcp.discovery.client import ModelsFetcher |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def models_fetcher(): |
| 10 | + return ModelsFetcher(paginator=Mock()) |
| 11 | + |
| 12 | + |
| 13 | +async def test_fetch_model_health_wraps_single_node_in_list( |
| 14 | + models_fetcher, mock_api_client, unit_discovery_config |
| 15 | +): |
| 16 | + """fetch_model_health must return a list[dict], not a bare dict.""" |
| 17 | + node = { |
| 18 | + "uniqueId": "model.project.my_model", |
| 19 | + "executionInfo": {"lastSuccessJobDefinitionId": 1, "lastRunStatus": "success"}, |
| 20 | + "tests": [{"name": "not_null", "status": "pass"}], |
| 21 | + "ancestors": [], |
| 22 | + } |
| 23 | + mock_api_client.return_value = { |
| 24 | + "data": {"environment": {"applied": {"models": {"edges": [{"node": node}]}}}} |
| 25 | + } |
| 26 | + |
| 27 | + result = await models_fetcher.fetch_model_health( |
| 28 | + unique_id="model.project.my_model", config=unit_discovery_config |
| 29 | + ) |
| 30 | + |
| 31 | + assert isinstance(result, list), ( |
| 32 | + "fetch_model_health must return a list, not a bare dict" |
| 33 | + ) |
| 34 | + assert len(result) == 1 |
| 35 | + assert result[0] == node |
| 36 | + |
| 37 | + |
| 38 | +async def test_fetch_model_health_empty_edges_returns_empty_list( |
| 39 | + models_fetcher, mock_api_client, unit_discovery_config |
| 40 | +): |
| 41 | + """fetch_model_health returns [] when no model is found.""" |
| 42 | + mock_api_client.return_value = { |
| 43 | + "data": {"environment": {"applied": {"models": {"edges": []}}}} |
| 44 | + } |
| 45 | + |
| 46 | + result = await models_fetcher.fetch_model_health( |
| 47 | + unique_id="model.project.nonexistent", config=unit_discovery_config |
| 48 | + ) |
| 49 | + |
| 50 | + assert result == [] |
0 commit comments