Skip to content

Commit b1f784a

Browse files
committed
Fix test_tool_service and test_server_service
Signed-off-by: Mihai Criveti <[email protected]>
1 parent 32a8162 commit b1f784a

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

tests/unit/mcpgateway/services/test_server_service.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,22 @@ class TestServerService:
104104
async def test_register_server(
105105
self, server_service, test_db, mock_tool, mock_resource, mock_prompt
106106
):
107-
"""Successful registration returns populated ServerRead."""
107+
"""
108+
Successful registration returns a populated ServerRead.
109+
We DO NOT patch DbServer – we let SQLAlchemy keep the real mapped class
110+
so that select(DbServer) works correctly.
111+
"""
112+
# Pretend there is no existing server with the same name
108113
mock_scalar = Mock()
109114
mock_scalar.scalar_one_or_none.return_value = None
110115
test_db.execute = Mock(return_value=mock_scalar)
116+
117+
# Basic Session stubs
111118
test_db.add = Mock()
112119
test_db.commit = Mock()
113120
test_db.refresh = Mock()
114121

115-
# Create a mock server instance that will be returned by db.add
116-
mock_db_server = MagicMock(spec=DbServer)
117-
mock_db_server.id = 1
118-
mock_db_server.name = "test_server"
119-
mock_db_server.description = "A test server"
120-
mock_db_server.icon = "server-icon"
121-
mock_db_server.created_at = "2023-01-01T00:00:00"
122-
mock_db_server.updated_at = "2023-01-01T00:00:00"
123-
mock_db_server.is_active = True
124-
125-
# Mock the relationship lists as simple lists
126-
mock_db_server.tools = []
127-
mock_db_server.resources = []
128-
mock_db_server.prompts = []
129-
130-
# Stub db.get to resolve associated items
122+
# Resolve associated objects
131123
test_db.get = Mock(
132124
side_effect=lambda cls, _id: {
133125
(DbTool, 101): mock_tool,
@@ -136,7 +128,7 @@ async def test_register_server(
136128
}.get((cls, _id))
137129
)
138130

139-
# Patch conversion helper
131+
# Stub helper that converts to the public schema
140132
server_service._notify_server_added = AsyncMock()
141133
server_service._convert_server_to_read = Mock(
142134
return_value=ServerRead(
@@ -172,10 +164,10 @@ async def test_register_server(
172164
associated_prompts=["301"],
173165
)
174166

175-
# Mock the DbServer constructor to return our mock instance
176-
with patch('mcpgateway.services.server_service.DbServer', return_value=mock_db_server):
177-
result = await server_service.register_server(test_db, server_create)
167+
# Run
168+
result = await server_service.register_server(test_db, server_create)
178169

170+
# Assertions
179171
test_db.add.assert_called_once()
180172
test_db.commit.assert_called_once()
181173
test_db.refresh.assert_called_once()
@@ -186,6 +178,7 @@ async def test_register_server(
186178
assert 201 in result.associated_resources
187179
assert 301 in result.associated_prompts
188180

181+
189182
@pytest.mark.asyncio
190183
async def test_register_server_name_conflict(self, server_service, mock_server, test_db):
191184
"""Server name clash is surfaced as ServerError (wrapped by service)."""
@@ -206,11 +199,16 @@ async def test_register_server_name_conflict(self, server_service, mock_server,
206199

207200
@pytest.mark.asyncio
208201
async def test_register_server_invalid_associated_tool(self, server_service, test_db):
209-
"""Non-existent associated tool raises ServerError."""
202+
"""
203+
Non‑existent associated tool raises ServerError.
204+
No patching of DbServer – keep the real mapped class intact.
205+
"""
206+
# No existing server with the same name
210207
mock_scalar = Mock()
211208
mock_scalar.scalar_one_or_none.return_value = None
212209
test_db.execute = Mock(return_value=mock_scalar)
213210

211+
# Simulate lookup failure for tool id 999
214212
test_db.get = Mock(return_value=None)
215213
test_db.rollback = Mock()
216214

@@ -220,19 +218,13 @@ async def test_register_server_invalid_associated_tool(self, server_service, tes
220218
associated_tools=["999"],
221219
)
222220

223-
# Mock the DbServer constructor
224-
mock_db_server = MagicMock(spec=DbServer)
225-
mock_db_server.tools = []
226-
mock_db_server.resources = []
227-
mock_db_server.prompts = []
228-
229-
with patch('mcpgateway.services.server_service.DbServer', return_value=mock_db_server):
230-
with pytest.raises(ServerError) as exc:
231-
await server_service.register_server(test_db, server_create)
221+
with pytest.raises(ServerError) as exc:
222+
await server_service.register_server(test_db, server_create)
232223

233224
assert "Tool with id 999 does not exist" in str(exc.value)
234225
test_db.rollback.assert_called_once()
235226

227+
236228
# --------------------------- list & get ----------------------------- #
237229
@pytest.mark.asyncio
238230
async def test_list_servers(self, server_service, mock_server, test_db):

0 commit comments

Comments
 (0)