Skip to content

Commit 30ab332

Browse files
committed
adds test for infrahubctl repository list command
1 parent e72a562 commit 30ab332

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
List of all Repositories
2+
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
3+
┃ Name ┃ Type ┃ Operational status ┃ Sync status ┃ Internal status ┃
4+
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
5+
│ Demo Edge Repo │ CoreReadOnlyRepository │ unknown │ in-sync │ active │
6+
│ My Own Repo │ CoreRepository │ in-sync │ in-sync │ active │
7+
└────────────────┴────────────────────────┴────────────────────┴─────────────┴─────────────────┘

tests/unit/ctl/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,61 @@ async def mock_repositories_query(httpx_mock: HTTPXMock) -> HTTPXMock:
115115
return httpx_mock
116116

117117

118+
@pytest.fixture
119+
def mock_repositories_list(httpx_mock: HTTPXMock) -> HTTPXMock:
120+
response = {
121+
"data": {
122+
"CoreGenericRepository": {
123+
"edges": [
124+
{
125+
"node": {
126+
"__typename": "CoreReadOnlyRepository",
127+
"name": {
128+
"value": "Demo Edge Repo"
129+
},
130+
"operational_status": {
131+
"value": "unknown"
132+
},
133+
"sync_status": {
134+
"value": "in-sync"
135+
},
136+
"internal_status": {
137+
"value": "active"
138+
}
139+
}
140+
},
141+
{
142+
"node": {
143+
"__typename": "CoreRepository",
144+
"name": {
145+
"value": "My Own Repo"
146+
},
147+
"operational_status": {
148+
"value": "in-sync"
149+
},
150+
"sync_status": {
151+
"value": "in-sync"
152+
},
153+
"internal_status": {
154+
"value": "active"
155+
}
156+
}
157+
}
158+
]
159+
}
160+
}
161+
}
162+
163+
httpx_mock.add_response(
164+
method="POST",
165+
status_code=200,
166+
url="http://mock/graphql/main",
167+
json=response,
168+
match_headers={"X-Infrahub-Tracker": "query-repository-list"},
169+
)
170+
return httpx_mock
171+
172+
118173
@pytest.fixture
119174
def tags_transform_dir():
120175
temp_dir = tempfile.mkdtemp()

tests/unit/ctl/test_repository_app.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
from infrahub_sdk.client import InfrahubClient
99
from infrahub_sdk.ctl.cli_commands import app
1010

11+
from tests.helpers.fixtures import read_fixture
12+
from tests.helpers.utils import strip_color
13+
14+
1115
runner = CliRunner()
1216

1317

@@ -21,10 +25,10 @@ def mock_client() -> mock.Mock:
2125
# ---------------------------------------------------------
2226
# infrahubctl repository command tests
2327
# ---------------------------------------------------------
24-
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
2528
class TestInfrahubctlRepository:
2629
"""Groups the 'infrahubctl repository' test cases."""
2730

31+
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
2832
def test_repo_no_username(self, mock_init_client, mock_client) -> None:
2933
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
3034
mock_cred = mock.AsyncMock()
@@ -84,6 +88,7 @@ def test_repo_no_username(self, mock_init_client, mock_client) -> None:
8488
tracker="mutation-repository-create",
8589
)
8690

91+
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
8792
def test_repo_username(self, mock_init_client, mock_client) -> None:
8893
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
8994
mock_cred = mock.AsyncMock()
@@ -116,6 +121,7 @@ def test_repo_username(self, mock_init_client, mock_client) -> None:
116121
mock_cred.save.assert_called_with(allow_upsert=True)
117122
mock_client.execute_graphql.assert_called_once()
118123
mock_client.execute_graphql.assert_called_with(
124+
119125
query="""
120126
mutation {
121127
CoreRepositoryCreate(
@@ -145,6 +151,7 @@ def test_repo_username(self, mock_init_client, mock_client) -> None:
145151
tracker="mutation-repository-create",
146152
)
147153

154+
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
148155
def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
149156
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
150157
mock_cred = mock.AsyncMock()
@@ -205,6 +212,7 @@ def test_repo_readonly_true(self, mock_init_client, mock_client) -> None:
205212
tracker="mutation-repository-create",
206213
)
207214

215+
@mock.patch("infrahub_sdk.ctl.repository.initialize_client")
208216
def test_repo_description_commit_branch(self, mock_init_client, mock_client) -> None:
209217
"""Case allow no username to be passed in and set it as None rather than blank string that fails."""
210218
mock_cred = mock.AsyncMock()
@@ -236,6 +244,7 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) ->
236244
mock_client.create.assert_called_with(
237245
name="Gitlab",
238246
kind="CorePasswordCredential",
247+
239248
password="mySup3rSecureP@ssw0rd",
240249
username="opsmill",
241250
)
@@ -271,3 +280,9 @@ def test_repo_description_commit_branch(self, mock_init_client, mock_client) ->
271280
branch_name="develop",
272281
tracker="mutation-repository-create",
273282
)
283+
284+
285+
def test_repo_list(self, mock_repositories_list) -> None:
286+
result = runner.invoke(app, ["repository", "list"])
287+
assert result.exit_code == 0
288+
assert strip_color(result.stdout) == read_fixture("output.txt", "integration/test_infrahubctl/repository_list")

0 commit comments

Comments
 (0)