Skip to content

Commit b07870a

Browse files
additional tests
1 parent ed21eb0 commit b07870a

File tree

1 file changed

+79
-11
lines changed

1 file changed

+79
-11
lines changed

services/web/server/tests/unit/with_dbs/04/garbage_collector/test_projects_document_service.py

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ async def test_remove_project_documents_as_admin_with_real_connections(
7474
await increment_and_return_project_document_version(
7575
redis_client=redis_document_client, project_uuid=project_id
7676
)
77-
# # Also add some invalid keys that should be ignored
78-
# await redis_document_client.redis.set("projects:invalid-uuid:version", "1.0")
79-
# await redis_document_client.redis.set("other:key:pattern", "1.0")
8077

8178
# Verify keys exist before cleanup
8279
for key in test_keys:
@@ -102,8 +99,9 @@ async def test_remove_project_documents_as_admin_with_real_connections(
10299
connections.append((sio_client_3, session_id_3))
103100

104101
try:
105-
# Execute the function under test
106-
await _project_document_service.remove_project_documents_as_admin(client.app)
102+
await _project_document_service.remove_project_documents_as_admin(
103+
client.app
104+
) # <-- This is the function being tested
107105

108106
# Verify results:
109107
# - Projects 0 and 1 should still have their documents (users connected)
@@ -127,10 +125,6 @@ async def test_remove_project_documents_as_admin_with_real_connections(
127125
== 0
128126
)
129127

130-
# Invalid keys should remain untouched
131-
# assert await redis_document_client.redis.exists("projects:invalid-uuid:version") == 1
132-
# assert await redis_document_client.redis.exists("other:key:pattern") == 1
133-
134128
finally:
135129
# Cleanup: Disconnect all SocketIO clients
136130
for sio_client, _ in connections:
@@ -140,8 +134,82 @@ async def test_remove_project_documents_as_admin_with_real_connections(
140134
# Cleanup: Remove test keys from Redis
141135
for key in test_keys:
142136
await redis_document_client.redis.delete(key)
143-
# await redis_document_client.redis.delete("projects:invalid-uuid:version")
144-
# await redis_document_client.redis.delete("other:key:pattern")
137+
138+
139+
@pytest.mark.parametrize(
140+
"user_role",
141+
[
142+
UserRole.USER,
143+
],
144+
)
145+
async def test_remove_project_documents_as_admin_with_known_opened_projects(
146+
client: TestClient,
147+
logged_user: UserInfoDict,
148+
sample_project_uuids: list[ProjectID],
149+
mocker,
150+
):
151+
"""Test that project documents are NOT removed when projects are in known opened projects list."""
152+
from servicelib.redis import increment_and_return_project_document_version
153+
from simcore_service_webserver.projects._project_document_service import (
154+
get_redis_document_manager_client_sdk,
155+
)
156+
157+
# Get the real Redis document manager client
158+
redis_document_client = get_redis_document_manager_client_sdk(client.app)
159+
160+
# Setup: Create real Redis keys for project documents
161+
test_keys = []
162+
for project_id in sample_project_uuids:
163+
key = f"projects:{project_id}:version"
164+
test_keys.append(key)
165+
# Set a document version in Redis
166+
await increment_and_return_project_document_version(
167+
redis_client=redis_document_client, project_uuid=project_id
168+
)
169+
170+
# Verify keys exist before cleanup
171+
for key in test_keys:
172+
assert await redis_document_client.redis.exists(key) == 1
173+
174+
# Mock list_opened_project_ids to return the first two projects as "known opened"
175+
known_opened_projects = sample_project_uuids[:2] # First two projects are "opened"
176+
mocker.patch(
177+
"simcore_service_webserver.projects._project_document_service.list_opened_project_ids",
178+
return_value=known_opened_projects,
179+
)
180+
181+
try:
182+
# Execute the function
183+
await _project_document_service.remove_project_documents_as_admin(client.app)
184+
185+
# Verify results:
186+
# - Projects 0 and 1 should still have their documents (in known opened projects)
187+
# - Project 2 should have its document removed (not in known opened projects and no socket connections)
188+
assert (
189+
await redis_document_client.redis.exists(
190+
f"projects:{sample_project_uuids[0]}:version"
191+
)
192+
== 1
193+
), "Project 0 should be kept because it's in known opened projects"
194+
195+
assert (
196+
await redis_document_client.redis.exists(
197+
f"projects:{sample_project_uuids[1]}:version"
198+
)
199+
== 1
200+
), "Project 1 should be kept because it's in known opened projects"
201+
202+
assert (
203+
await redis_document_client.redis.exists(
204+
f"projects:{sample_project_uuids[2]}:version"
205+
)
206+
== 0
207+
), "Project 2 should be removed because it's not in known opened projects and has no socket connections"
208+
209+
finally:
210+
# Cleanup: Remove remaining test keys from Redis
211+
for key in test_keys:
212+
await redis_document_client.redis.delete(key)
145213

146214

147215
# async def test_remove_project_documents_as_admin_with_connected_users(

0 commit comments

Comments
 (0)