|
| 1 | +# Copyright IBM Corp. 2025, 2026 |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +"""Unit tests for the stack_deployment_group_summaries module.""" |
| 5 | + |
| 6 | +from unittest.mock import Mock |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from pytfe._http import HTTPTransport |
| 11 | +from pytfe.errors import InvalidStackConfigurationIDError |
| 12 | +from pytfe.models.stack_deployment_group import ( |
| 13 | + StackDeploymentGroup, |
| 14 | + StackDeploymentGroupStatusCounts, |
| 15 | + StackDeploymentGroupSummary, |
| 16 | + StackDeploymentGroupSummaryListOptions, |
| 17 | +) |
| 18 | +from pytfe.resources.stack_deployment_group_summaries import ( |
| 19 | + StackDeploymentGroupSummaries, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class TestStackDeploymentGroupSummaries: |
| 24 | + """Test the StackDeploymentGroupSummaries service class.""" |
| 25 | + |
| 26 | + @pytest.fixture |
| 27 | + def mock_transport(self): |
| 28 | + """Create a mock HTTPTransport.""" |
| 29 | + return Mock(spec=HTTPTransport) |
| 30 | + |
| 31 | + @pytest.fixture |
| 32 | + def service(self, mock_transport): |
| 33 | + """Create a StackDeploymentGroupSummaries service with mocked transport.""" |
| 34 | + return StackDeploymentGroupSummaries(mock_transport) |
| 35 | + |
| 36 | + @pytest.fixture |
| 37 | + def summary_api_data(self): |
| 38 | + """Typical API response item for a single deployment group summary.""" |
| 39 | + return { |
| 40 | + "id": "sdgs-abc123", |
| 41 | + "type": "stack-deployment-group-summaries", |
| 42 | + "attributes": { |
| 43 | + "name": "dev", |
| 44 | + "status": "succeeded", |
| 45 | + "status-counts": { |
| 46 | + "pending": 0, |
| 47 | + "pre-deploying": 0, |
| 48 | + "pending-operator": 0, |
| 49 | + "acquiring-lock": 0, |
| 50 | + "deploying": 0, |
| 51 | + "succeeded": 3, |
| 52 | + "failed": 0, |
| 53 | + "abandoned": 0, |
| 54 | + }, |
| 55 | + }, |
| 56 | + "relationships": { |
| 57 | + "stack-deployment-group": { |
| 58 | + "data": {"id": "sdg-xyz789", "type": "stack-deployment-groups"} |
| 59 | + } |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + # ── Model tests ────────────────────────────────────────────────────────── |
| 64 | + |
| 65 | + def test_stack_deployment_group_status_counts_parse(self): |
| 66 | + """StackDeploymentGroupStatusCounts parses all count fields.""" |
| 67 | + counts_data = { |
| 68 | + "pending": 1, |
| 69 | + "pre-deploying": 2, |
| 70 | + "pending-operator": 3, |
| 71 | + "acquiring-lock": 4, |
| 72 | + "deploying": 5, |
| 73 | + "succeeded": 6, |
| 74 | + "failed": 7, |
| 75 | + "abandoned": 8, |
| 76 | + } |
| 77 | + counts = StackDeploymentGroupStatusCounts.model_validate(counts_data) |
| 78 | + assert counts.pending == 1 |
| 79 | + assert counts.pre_deploying == 2 |
| 80 | + assert counts.pre_deploying_pending_operator == 3 |
| 81 | + assert counts.acquiring_lock == 4 |
| 82 | + assert counts.deploying == 5 |
| 83 | + assert counts.succeeded == 6 |
| 84 | + assert counts.failed == 7 |
| 85 | + assert counts.abandoned == 8 |
| 86 | + |
| 87 | + def test_stack_deployment_group_summary_parse(self, summary_api_data): |
| 88 | + """StackDeploymentGroupSummary parses name and status correctly.""" |
| 89 | + attrs = dict(summary_api_data["attributes"]) |
| 90 | + attrs["id"] = summary_api_data["id"] |
| 91 | + summary = StackDeploymentGroupSummary.model_validate(attrs) |
| 92 | + assert summary.id == "sdgs-abc123" |
| 93 | + assert summary.name == "dev" |
| 94 | + assert summary.status == "succeeded" |
| 95 | + assert isinstance(summary.status_counts, StackDeploymentGroupStatusCounts) |
| 96 | + assert summary.status_counts.succeeded == 3 |
| 97 | + |
| 98 | + def test_summary_list_options_serialization(self): |
| 99 | + """StackDeploymentGroupSummaryListOptions serializes page[size] correctly.""" |
| 100 | + opts = StackDeploymentGroupSummaryListOptions(page_size=25) |
| 101 | + dumped = opts.model_dump(by_alias=True, exclude_none=True) |
| 102 | + assert dumped["page[size]"] == 25 |
| 103 | + |
| 104 | + # ── list() tests ───────────────────────────────────────────────────────── |
| 105 | + |
| 106 | + def test_list_invalid_configuration_id_raises(self, service): |
| 107 | + """list() with an empty config ID raises InvalidStackConfigurationIDError.""" |
| 108 | + with pytest.raises(InvalidStackConfigurationIDError): |
| 109 | + list(service.list("")) |
| 110 | + |
| 111 | + def test_list_success(self, service, summary_api_data): |
| 112 | + """list() yields StackDeploymentGroupSummary objects from paginated results.""" |
| 113 | + service._list = Mock(return_value=[summary_api_data]) |
| 114 | + |
| 115 | + results = list(service.list("stc-abc123")) |
| 116 | + |
| 117 | + service._list.assert_called_once_with( |
| 118 | + path="/api/v2/stack-configurations/stc-abc123/stack-deployment-group-summaries", |
| 119 | + params={}, |
| 120 | + ) |
| 121 | + assert len(results) == 1 |
| 122 | + assert isinstance(results[0], StackDeploymentGroupSummary) |
| 123 | + assert results[0].id == "sdgs-abc123" |
| 124 | + assert results[0].name == "dev" |
| 125 | + |
| 126 | + def test_list_with_page_size(self, service, summary_api_data): |
| 127 | + """list() passes page[size] param correctly.""" |
| 128 | + service._list = Mock(return_value=[summary_api_data]) |
| 129 | + opts = StackDeploymentGroupSummaryListOptions(page_size=10) |
| 130 | + list(service.list("stc-abc123", options=opts)) |
| 131 | + service._list.assert_called_once_with( |
| 132 | + path="/api/v2/stack-configurations/stc-abc123/stack-deployment-group-summaries", |
| 133 | + params={"page[size]": 10}, |
| 134 | + ) |
| 135 | + |
| 136 | + def test_list_hydrates_group_relation(self, service, summary_api_data): |
| 137 | + """list() hydrates the stack-deployment-group relation as a typed stub.""" |
| 138 | + service._list = Mock(return_value=[summary_api_data]) |
| 139 | + results = list(service.list("stc-abc123")) |
| 140 | + assert isinstance(results[0].stack_deployment_group, StackDeploymentGroup) |
| 141 | + assert results[0].stack_deployment_group.id == "sdg-xyz789" |
| 142 | + |
| 143 | + def test_list_empty(self, service): |
| 144 | + """list() returns an empty iterator when the API returns no items.""" |
| 145 | + service._list = Mock(return_value=[]) |
| 146 | + assert list(service.list("stc-abc123")) == [] |
0 commit comments