You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/repositories/interface.py
+13-12Lines changed: 13 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -37,17 +37,18 @@ def __init__(self):
37
37
defrepository_exception_handler(method):
38
38
"""
39
39
Decorator that standardizes error handling and logging for repository coroutine methods.
40
-
40
+
41
41
Parameters:
42
42
method (Callable): The asynchronous repository method to wrap.
43
-
43
+
44
44
Returns:
45
45
wrapper (Callable): An async wrapper that:
46
46
- re-raises PyMongoError after logging the exception,
47
47
- re-raises RepositoryNotInitializedException after logging the exception,
48
48
- logs any other exception and raises an HTTPException with status 500 and detail 'Unexpected error ocurred',
49
49
- always logs completion of the repository method call with the repository name, method name, and kwargs.
50
50
"""
51
+
51
52
@functools.wraps(method)
52
53
asyncdefwrapper(self, *args, **kwargs):
53
54
try:
@@ -96,9 +97,9 @@ class RepositoryInterface:
96
97
def__new__(cls, *args, **kwargs):
97
98
"""
98
99
Ensure a single thread-safe instance exists for the subclass.
99
-
100
+
100
101
Creates and returns the singleton instance for this subclass, creating it if absent while holding a global thread lock to prevent concurrent instantiation.
Initialize the repository instance for a specific API model and configure its connection pool.
114
-
115
+
115
116
Parameters:
116
117
model (ApiBaseModel): The API model used for validation and to determine the repository's collection.
117
118
max_pool_size (int, optional): Maximum size of the MongoDB connection pool. Defaults to 3.
118
-
119
+
119
120
Notes:
120
121
If the instance is already initialized, this constructor will not reconfigure it. Initialization of the underlying connection is started asynchronously.
Perform idempotent, retry-safe asynchronous initialization of the repository instance.
132
-
133
+
133
134
Ensures a per-instance asyncio.Lock exists and acquires it to run initialization exactly once; on success it marks the instance as initialized and sets the internal _initialized_event so awaiters can proceed. If initialization fails, the original exception from _initialize_connection is propagated after logging.
134
135
"""
135
136
ifgetattr(self, '_initialized', False):
@@ -155,7 +156,7 @@ async def _async_init(self):
155
156
def_initialize(self):
156
157
"""
157
158
Ensure the repository's asynchronous initializer is executed: run it immediately if no event loop is active, otherwise schedule it on the running loop.
158
-
159
+
159
160
If there is no running asyncio event loop, this method runs self._async_init() to completion on the current thread, blocking until it finishes. If an event loop is running, it schedules self._async_init() as a background task on that loop and returns immediately.
160
161
"""
161
162
try:
@@ -168,7 +169,7 @@ def _initialize(self):
168
169
asyncdef__aenter__(self):
169
170
"""
170
171
Waits for repository initialization to complete and returns the repository instance.
171
-
172
+
172
173
Returns:
173
174
RepositoryInterface: The initialized repository instance.
Initialize the MongoDB async client, store the connection string, and bind the collection for this repository instance.
184
-
185
+
185
186
This method fetches the MongoDB connection string from secrets, creates an AsyncMongoClient configured with pool and timeout settings, and sets self._collection to the repository's collection named by the model. On success it logs the initialized client; on failure it raises a ConnectionError.
186
-
187
+
187
188
Raises:
188
189
ConnectionError: If the client or collection cannot be initialized.
Copy file name to clipboardExpand all lines: tests/unit/test_mcp/test_mcp_server.py
+15-14Lines changed: 15 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -6,48 +6,49 @@
6
6
fromfastmcp.clientimportClient
7
7
fromfastapi.routingimportAPIRoute
8
8
9
-
fromsrc.apiimportapp, rest_app
9
+
fromsrc.apiimportapp
10
10
fromsrc.mcp.serverimportbuild_mcp
11
11
12
12
13
13
@pytest.fixture(autouse=True)
14
14
defreset_mcp_state():
15
15
"""
16
16
Ensure the FastAPI app has no lingering MCP state before and after a test.
17
-
18
-
This fixture deletes app.state.mcp if it exists, yields control to the test, and then deletes app.state.mcp again to guarantee the MCP state is cleared between tests.
17
+
This fixture deletes app.state.mcp if it exists,
18
+
yields control to the test, and then deletes app.state.mcp
19
+
again to guarantee the MCP state is cleared between tests.
0 commit comments