Skip to content

test: migrate mcp controller tests to testcontainers#34297

Merged
asukaminato0721 merged 3 commits intolanggenius:mainfrom
YB0y:test/migrate-mcp-controller-tests-to-testcontainers
Mar 31, 2026
Merged

test: migrate mcp controller tests to testcontainers#34297
asukaminato0721 merged 3 commits intolanggenius:mainfrom
YB0y:test/migrate-mcp-controller-tests-to-testcontainers

Conversation

@YB0y
Copy link
Copy Markdown
Contributor

@YB0y YB0y commented Mar 30, 2026

Summary

Migrate tests from api/tests/unit_tests/controllers/mcp/test_mcp.py to testcontainers integration tests at api/tests/test_containers_integration_tests/controllers/mcp/test_mcp.py.

Removed mock_db and mock_session autouse fixtures that were replacing module.db and module.Session with fakes - the controller now uses real Session(db.engine) from the testcontainers PostgreSQL instance. All 17 tests migrated: MCP request handling (success, notification initialized, invalid notification, inactive server, invalid payload, missing request ID, no response), server/app lookup (server not found, app not found), app availability (no workflow, no model config), app modes (workflow with user input form, chat with model config), invalid request format, server status validation, user input form conversion, and invalid user input form validation.

Part of #32454

@YB0y YB0y requested a review from Nov1c444 as a code owner March 30, 2026 16:34
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-30 16:36:17.870383122 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-30 16:36:06.171202192 +0000
@@ -670,6 +670,12 @@
    --> tests/test_containers_integration_tests/controllers/console/auth/test_oauth.py:197:9
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
   --> tests/test_containers_integration_tests/controllers/console/helpers.py:40:48
+ERROR `SimpleNamespace` is not assignable to attribute `mcp_ns` with type `Namespace` [bad-assignment]
+  --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:26:21
+ERROR Attribute `payload` of class `Namespace` is a read-only property and cannot be set [read-only]
+  --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:30:5
+ERROR Argument `DummyServer` is not assignable to parameter `mcp_server` with type `AppMCPServer` in function `controllers.mcp.mcp.MCPAppApi._validate_server_status` [bad-argument-type]
+   --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:453:37
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
   --> tests/test_containers_integration_tests/controllers/web/test_wraps.py:74:33
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
@@ -2104,14 +2110,6 @@
    --> tests/unit_tests/controllers/inner_api/plugin/test_plugin_wraps.py:286:35
 ERROR Argument `type[PluginTestPayload]` is not assignable to parameter `payload_type` with type `type[BaseModel]` in function `controllers.inner_api.plugin.wraps.plugin_data` [bad-argument-type]
    --> tests/unit_tests/controllers/inner_api/plugin/test_plugin_wraps.py:299:35
-ERROR `SimpleNamespace` is not assignable to attribute `db` with type `SQLAlchemy` [bad-assignment]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:19:17
-ERROR `SimpleNamespace` is not assignable to attribute `mcp_ns` with type `Namespace` [bad-assignment]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:40:21
-ERROR Attribute `payload` of class `Namespace` is a read-only property and cannot be set [read-only]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:44:5
-ERROR Argument `DummyServer` is not assignable to parameter `mcp_server` with type `AppMCPServer` in function `controllers.mcp.mcp.MCPAppApi._validate_server_status` [bad-argument-type]
-   --> tests/unit_tests/controllers/mcp/test_mcp.py:467:37
 ERROR Cannot index into `Iterable[bytes]` [bad-index]
    --> tests/unit_tests/controllers/service_api/app/test_audio.py:213:16
 ERROR Cannot index into `Response` [bad-index]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-30 17:40:27.805888900 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-30 17:40:17.239874542 +0000
@@ -670,6 +670,12 @@
    --> tests/test_containers_integration_tests/controllers/console/auth/test_oauth.py:197:9
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
   --> tests/test_containers_integration_tests/controllers/console/helpers.py:40:48
+ERROR `SimpleNamespace` is not assignable to attribute `mcp_ns` with type `Namespace` [bad-assignment]
+  --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:26:21
+ERROR Attribute `payload` of class `Namespace` is a read-only property and cannot be set [read-only]
+  --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:30:5
+ERROR Argument `DummyServer` is not assignable to parameter `mcp_server` with type `AppMCPServer` in function `controllers.mcp.mcp.MCPAppApi._validate_server_status` [bad-argument-type]
+   --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:454:37
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
   --> tests/test_containers_integration_tests/controllers/web/test_wraps.py:74:33
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
@@ -2104,14 +2110,6 @@
    --> tests/unit_tests/controllers/inner_api/plugin/test_plugin_wraps.py:286:35
 ERROR Argument `type[PluginTestPayload]` is not assignable to parameter `payload_type` with type `type[BaseModel]` in function `controllers.inner_api.plugin.wraps.plugin_data` [bad-argument-type]
    --> tests/unit_tests/controllers/inner_api/plugin/test_plugin_wraps.py:299:35
-ERROR `SimpleNamespace` is not assignable to attribute `db` with type `SQLAlchemy` [bad-assignment]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:19:17
-ERROR `SimpleNamespace` is not assignable to attribute `mcp_ns` with type `Namespace` [bad-assignment]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:40:21
-ERROR Attribute `payload` of class `Namespace` is a read-only property and cannot be set [read-only]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:44:5
-ERROR Argument `DummyServer` is not assignable to parameter `mcp_server` with type `AppMCPServer` in function `controllers.mcp.mcp.MCPAppApi._validate_server_status` [bad-argument-type]
-   --> tests/unit_tests/controllers/mcp/test_mcp.py:467:37
 ERROR Cannot index into `Iterable[bytes]` [bad-index]
    --> tests/unit_tests/controllers/service_api/app/test_audio.py:213:16
 ERROR Cannot index into `Response` [bad-index]

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-03-30 18:06:56.268826563 +0000
+++ /tmp/pyrefly_pr.txt	2026-03-30 18:06:46.053865506 +0000
@@ -670,6 +670,12 @@
    --> tests/test_containers_integration_tests/controllers/console/auth/test_oauth.py:197:9
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
   --> tests/test_containers_integration_tests/controllers/console/helpers.py:40:48
+ERROR `SimpleNamespace` is not assignable to attribute `mcp_ns` with type `Namespace` [bad-assignment]
+  --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:27:21
+ERROR Attribute `payload` of class `Namespace` is a read-only property and cannot be set [read-only]
+  --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:31:5
+ERROR Argument `DummyServer` is not assignable to parameter `mcp_server` with type `AppMCPServer` in function `controllers.mcp.mcp.MCPAppApi._validate_server_status` [bad-argument-type]
+   --> tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:460:37
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
   --> tests/test_containers_integration_tests/controllers/web/test_wraps.py:74:33
 ERROR Argument `SimpleNamespace` is not assignable to parameter `webapp_settings` with type `WebAppSettings | None` in function `controllers.web.wraps._validate_user_accessibility` [bad-argument-type]
@@ -2104,14 +2110,6 @@
    --> tests/unit_tests/controllers/inner_api/plugin/test_plugin_wraps.py:286:35
 ERROR Argument `type[PluginTestPayload]` is not assignable to parameter `payload_type` with type `type[BaseModel]` in function `controllers.inner_api.plugin.wraps.plugin_data` [bad-argument-type]
    --> tests/unit_tests/controllers/inner_api/plugin/test_plugin_wraps.py:299:35
-ERROR `SimpleNamespace` is not assignable to attribute `db` with type `SQLAlchemy` [bad-assignment]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:19:17
-ERROR `SimpleNamespace` is not assignable to attribute `mcp_ns` with type `Namespace` [bad-assignment]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:40:21
-ERROR Attribute `payload` of class `Namespace` is a read-only property and cannot be set [read-only]
-  --> tests/unit_tests/controllers/mcp/test_mcp.py:44:5
-ERROR Argument `DummyServer` is not assignable to parameter `mcp_server` with type `AppMCPServer` in function `controllers.mcp.mcp.MCPAppApi._validate_server_status` [bad-argument-type]
-   --> tests/unit_tests/controllers/mcp/test_mcp.py:467:37
 ERROR Cannot index into `Iterable[bytes]` [bad-index]
    --> tests/unit_tests/controllers/service_api/app/test_audio.py:213:16
 ERROR Cannot index into `Response` [bad-index]

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 31, 2026
@asukaminato0721 asukaminato0721 requested a review from Copilot March 31, 2026 00:28
@asukaminato0721 asukaminato0721 added this pull request to the merge queue Mar 31, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Migrates MCP controller tests into the testcontainers integration-test suite so the controller runs against a real PostgreSQL-backed db.engine/Session rather than patched fakes.

Changes:

  • Added/updated api/tests/test_containers_integration_tests/controllers/mcp/test_mcp.py to cover MCP request/notification handling and various validation/error cases under a containerized Flask request context.
  • Removed the prior approach of autouse fixtures that replaced module.db/module.Session with fakes, relying instead on the testcontainers app configuration.
Comments suppressed due to low confidence (3)

api/tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:75

  • Patching module.handle_mcp_request doesn’t bypass the DB side-effects in MCPAppApi._handle_mcp_request (it still queries/creates EndUser, commits, and opens an extra session before calling handle_mcp_request). This makes these tests slower and can leak state into the shared testcontainers DB. Patch MCPAppApi._handle_mcp_request instead (or patch _retrieve_end_user/_create_end_user) to keep these controller tests isolated from DB writes unless the test is explicitly verifying DB behavior.
    api/tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:41
  • The module-level _TENANT_ID/_APP_ID/_SERVER_ID constants are shared across all tests in this module. Since the controller now uses a real database session, any created records (e.g., EndUser on initialize) will persist and can change the code path for later tests, making them order-dependent. Prefer generating IDs per test (fixture) or per DummyServer instance, or explicitly cleaning up inserted rows.
    api/tests/test_containers_integration_tests/controllers/mcp/test_mcp.py:73
  • @pytest.mark.usefixtures("flask_req_ctx_with_containers") applies the containerized Flask request context to every test in the class, including tests that only call helper methods (e.g. _convert_user_input_form, _validate_server_status) and don’t need Flask/DB. Splitting pure unit-style tests into a separate class without this fixture (or applying the fixture only to the tests that call post) will reduce unnecessary container/app overhead.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Merged via the queue into langgenius:main with commit 15aa807 Mar 31, 2026
31 checks passed
@YB0y YB0y deleted the test/migrate-mcp-controller-tests-to-testcontainers branch April 1, 2026 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants