Skip to content

Commit 7270e67

Browse files
authored
[QI2-1437] Add command for getting backend information (#177)
1 parent 93286df commit 7270e67

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

quantuminspire/cli/command_list.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ def list_backend_types() -> None:
5656
console.print(table)
5757

5858

59+
@backend_types_app.command("get")
60+
def get_backend_types(backend_type_id: int = typer.Argument(..., help="The id of the backend type")) -> None:
61+
"""Get a backend.
62+
63+
Get the configuration of a specific backend.
64+
"""
65+
backend = RemoteBackend()
66+
backend_types = backend.get_backend_types().items
67+
68+
backend_type = next((bt for bt in backend_types if bt.id == backend_type_id), None)
69+
if backend_type is None:
70+
console.print(f"Backend type with id {backend_type_id} not found.")
71+
raise typer.Exit(1)
72+
else:
73+
console.print(backend_type.model_dump())
74+
75+
5976
def load_algorithm_from_file(file_path: Path) -> FileAlgorithm:
6077
"""Load an algorithm from a file."""
6178
if file_path.suffix == ".py":

tests/cli/test_command_list.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import MagicMock
22

33
from compute_api_client import BackendStatus, JobStatus
4+
from pydantic import BaseModel
45
from pytest_mock import MockerFixture
56
from typer.testing import CliRunner
67

@@ -34,6 +35,38 @@ class MockBackendType:
3435
mock_remote_backend_inst.get_backend_types.assert_called_once()
3536

3637

38+
def test_backend_get(mocker: MockerFixture) -> None:
39+
class MockBackendType(BaseModel):
40+
id: int = 1
41+
name: str = "Mock backend"
42+
43+
mock_remote_backend_inst = MagicMock()
44+
paginated_mock = MagicMock()
45+
46+
paginated_mock.items = [MockBackendType()]
47+
mock_remote_backend_inst.get_backend_types.return_value = paginated_mock
48+
mocker.patch("quantuminspire.cli.command_list.RemoteBackend", return_value=mock_remote_backend_inst)
49+
50+
result = runner.invoke(app, ["backends", "get", "1"])
51+
52+
assert result.exit_code == 0
53+
mock_remote_backend_inst.get_backend_types.assert_called_once()
54+
55+
56+
def test_backend_get_no_backend(mocker: MockerFixture) -> None:
57+
mock_remote_backend_inst = MagicMock()
58+
paginated_mock = MagicMock()
59+
60+
paginated_mock.items = []
61+
mock_remote_backend_inst.get_backend_types.return_value = paginated_mock
62+
mocker.patch("quantuminspire.cli.command_list.RemoteBackend", return_value=mock_remote_backend_inst)
63+
64+
result = runner.invoke(app, ["backends", "get", "1"])
65+
66+
assert result.exit_code == 1
67+
mock_remote_backend_inst.get_backend_types.assert_called_once()
68+
69+
3770
def test_file_upload_hybrid(mocker: MockerFixture) -> None:
3871
mock_remote_backend_inst = MagicMock()
3972
mocker.patch("quantuminspire.cli.command_list.RemoteBackend", return_value=mock_remote_backend_inst)

0 commit comments

Comments
 (0)