-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtest_workspace.py
121 lines (103 loc) · 4.25 KB
/
test_workspace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from unittest.mock import AsyncMock, patch
import pytest
from codegate.db.models import WorkspaceActive
from codegate.pipeline.cli.commands import Workspace
@pytest.mark.asyncio
@pytest.mark.parametrize(
"mock_workspaces, expected_output",
[
# Case 1: No workspaces
([], ""),
# Case 2: One workspace active
(
[
# We'll make a MagicMock that simulates a workspace
# with 'name' attribute and 'active_workspace_id' set
WorkspaceActive(id="1", name="Workspace1", active_workspace_id="100")
],
"- Workspace1 **(active)**\n",
),
# Case 3: Multiple workspaces, second one active
(
[
WorkspaceActive(id="1", name="Workspace1", active_workspace_id=None),
WorkspaceActive(id="2", name="Workspace2", active_workspace_id="200"),
],
"- Workspace1\n- Workspace2 **(active)**\n",
),
],
)
async def test_list_workspaces(mock_workspaces, expected_output):
"""
Test _list_workspaces with different sets of returned workspaces.
"""
workspace_commands = Workspace()
# Mock DbReader inside workspace_commands
mock_get_workspaces = AsyncMock(return_value=mock_workspaces)
workspace_commands.workspace_crud.get_workspaces = mock_get_workspaces
# Call the method
result = await workspace_commands._list_workspaces()
# Check the result
assert result == expected_output
mock_get_workspaces.assert_awaited_once()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"args, existing_workspaces, expected_message",
[
# Case 1: No workspace name provided
([], [], "Please provide a name. Use `codegate workspace add your_workspace_name`"),
# Case 2: Workspace name is empty string
([""], [], "Please provide a name. Use `codegate workspace add your_workspace_name`"),
# Case 3: Successful add
(["myworkspace"], [], "Workspace **myworkspace** has been added"),
],
)
async def test_add_workspaces(args, existing_workspaces, expected_message):
"""
Test _add_workspace under different scenarios:
- no args
- empty string arg
- workspace already exists
- workspace successfully added
"""
workspace_commands = Workspace()
# Mock the DbReader to return existing_workspaces
mock_db_reader = AsyncMock()
mock_db_reader.get_workspace_by_name.return_value = existing_workspaces
workspace_commands._db_reader = mock_db_reader
# We'll also patch DbRecorder to ensure no real DB operations happen
with patch("codegate.pipeline.cli.commands.WorkspaceCrud", autospec=True) as mock_recorder_cls:
mock_recorder = mock_recorder_cls.return_value
workspace_commands.workspace_crud = mock_recorder
mock_recorder.add_workspace = AsyncMock()
# Call the method
result = await workspace_commands._add_workspace(args)
# Assertions
assert result == expected_message
# If expected_message indicates "added", we expect add_workspace to be called once
if "has been added" in expected_message:
mock_recorder.add_workspace.assert_awaited_once_with(args[0])
else:
mock_recorder.add_workspace.assert_not_awaited()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"user_message, expected_command, expected_args, mocked_execute_response",
[
(["list"], "list", ["list"], "List workspaces output"),
(["add", "myws"], "add", ["add", "myws"], "Added workspace"),
(["activate", "myws"], "activate", ["activate", "myws"], "Activated workspace"),
],
)
async def test_parse_execute_cmd(
user_message, expected_command, expected_args, mocked_execute_response
):
"""
Test parse_execute_cmd to ensure it parses the user message
and calls the correct command with the correct args.
"""
workspace_commands = Workspace()
with patch.object(workspace_commands, "run", return_value=mocked_execute_response) as mock_run:
result = await workspace_commands.exec(user_message)
assert result == mocked_execute_response
# Verify 'execute' was called with the expected command and args
mock_run.assert_awaited_once_with(expected_args)