-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathtest_modelstudio_memory.py
More file actions
322 lines (260 loc) · 8.2 KB
/
test_modelstudio_memory.py
File metadata and controls
322 lines (260 loc) · 8.2 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- coding: utf-8 -*-
# pylint:disable=redefined-outer-name
import asyncio
import os
import uuid
from datetime import datetime
import pytest
from agentscope_runtime.tools.modelstudio_memory import (
AddMemory,
AddMemoryInput,
AddMemoryOutput,
CreateProfileSchema,
CreateProfileSchemaInput,
CreateProfileSchemaOutput,
DeleteMemory,
DeleteMemoryInput,
DeleteMemoryOutput,
GetUserProfile,
GetUserProfileInput,
GetUserProfileOutput,
ListMemory,
ListMemoryInput,
ListMemoryOutput,
Message,
MemoryNode,
ProfileAttribute,
SearchMemory,
SearchMemoryInput,
SearchMemoryOutput,
)
NO_DASHSCOPE_KEY = os.getenv("DASHSCOPE_API_KEY", "") == ""
def generate_test_user_id() -> str:
"""Generate a unique test user ID for each test run."""
mmdd = datetime.now().strftime("%m%d")
user_uuid = str(uuid.uuid4())[:8]
return f"test_memory_user_{mmdd}_{user_uuid}"
@pytest.fixture
def add_memory_component():
"""Fixture for AddMemory component."""
component = AddMemory()
yield component
@pytest.fixture
def search_memory_component():
"""Fixture for SearchMemory component."""
component = SearchMemory()
yield component
@pytest.fixture
def list_memory_component():
"""Fixture for ListMemory component."""
component = ListMemory()
yield component
@pytest.fixture
def delete_memory_component():
"""Fixture for DeleteMemory component."""
component = DeleteMemory()
yield component
@pytest.fixture
def create_profile_schema_component():
"""Fixture for CreateProfileSchema component."""
component = CreateProfileSchema()
yield component
@pytest.fixture
def get_user_profile_component():
"""Fixture for GetUserProfile component."""
component = GetUserProfile()
yield component
@pytest.mark.asyncio
@pytest.mark.skipif(
NO_DASHSCOPE_KEY,
reason="DASHSCOPE_API_KEY not set",
)
async def test_add_memory_success(add_memory_component):
"""Test adding a memory node."""
test_user_id = generate_test_user_id()
# Prepare test data
messages = [
Message(role="user", content="I love playing basketball on weekends"),
Message(
role="assistant",
content="That's great! Basketball is a fun and healthy activity.",
),
]
input_data = AddMemoryInput(
user_id=test_user_id,
messages=messages,
meta_data={"source": "pytest", "test": "add_memory"},
)
result = await add_memory_component.arun(input_data)
# Assertions
assert isinstance(result, AddMemoryOutput)
assert isinstance(result.memory_nodes, list)
if result.memory_nodes:
for node in result.memory_nodes:
assert isinstance(node, MemoryNode)
@pytest.mark.asyncio
@pytest.mark.skipif(
NO_DASHSCOPE_KEY,
reason="DASHSCOPE_API_KEY not set",
)
async def test_search_memory_success(search_memory_component):
"""Test searching memory nodes."""
test_user_id = generate_test_user_id()
# SearchMemoryInput requires messages for context
messages = [
Message(role="user", content="basketball"),
]
input_data = SearchMemoryInput(
user_id=test_user_id,
messages=messages,
top_k=5,
)
result = await search_memory_component.arun(input_data)
# Assertions
assert isinstance(result, SearchMemoryOutput)
assert isinstance(result.memory_nodes, list)
if result.memory_nodes:
for node in result.memory_nodes:
assert isinstance(node, MemoryNode)
@pytest.mark.asyncio
@pytest.mark.skipif(
NO_DASHSCOPE_KEY,
reason="DASHSCOPE_API_KEY not set",
)
async def test_list_memory_success(list_memory_component):
"""Test listing memory nodes with pagination."""
test_user_id = generate_test_user_id()
input_data = ListMemoryInput(
user_id=test_user_id,
page_size=10,
page_num=1,
)
result = await list_memory_component.arun(input_data)
# Assertions
assert isinstance(result, ListMemoryOutput)
assert isinstance(result.memory_nodes, list)
assert isinstance(result.total, int)
if result.total >= 0:
for node in result.memory_nodes:
assert isinstance(node, MemoryNode)
@pytest.mark.asyncio
@pytest.mark.skipif(
NO_DASHSCOPE_KEY,
reason="DASHSCOPE_API_KEY not set",
)
async def test_delete_memory_success(
add_memory_component,
delete_memory_component,
):
"""Test deleting a memory node."""
test_user_id = generate_test_user_id()
# First, add a memory node
messages = [
Message(role="user", content="Remember that I like playing football"),
Message(role="assistant", content="Understood, test message received"),
]
add_input = AddMemoryInput(
user_id=test_user_id,
messages=messages,
meta_data={"test": "delete_memory"},
)
add_result = await add_memory_component.arun(add_input)
# Wait for several seconds for the memory to be processed
await asyncio.sleep(3)
if add_result.memory_nodes:
memory_node_id = add_result.memory_nodes[0].memory_node_id
# Now delete it
delete_input = DeleteMemoryInput(
user_id=test_user_id,
memory_node_id=memory_node_id,
)
result = await delete_memory_component.arun(delete_input)
# Assertions
assert isinstance(result, DeleteMemoryOutput)
assert result.request_id is not None
@pytest.mark.asyncio
@pytest.mark.skipif(
NO_DASHSCOPE_KEY,
reason="DASHSCOPE_API_KEY not set",
)
async def test_create_profile_schema_success(create_profile_schema_component):
"""Test creating a user profile schema."""
# Generate unique schema name
mmdd = datetime.now().strftime("%m%d%H%M%S")
schema_name = f"test_schema_{mmdd}"
# Define profile attributes
attributes = [
ProfileAttribute(
name="age",
description="User's age",
),
ProfileAttribute(
name="occupation",
description="User's occupation or job title",
),
ProfileAttribute(
name="hobbies",
description="User's hobbies and interests",
),
]
input_data = CreateProfileSchemaInput(
name=schema_name,
description="Test profile schema for pytest",
attributes=attributes,
)
# Call the async run method
result = await create_profile_schema_component.arun(input_data)
# Assertions
assert isinstance(result, CreateProfileSchemaOutput)
assert result.profile_schema_id is not None
@pytest.mark.asyncio
@pytest.mark.skipif(
NO_DASHSCOPE_KEY,
reason="DASHSCOPE_API_KEY not set",
)
async def test_get_user_profile_success(
create_profile_schema_component,
get_user_profile_component,
):
"""Test retrieving a user profile."""
test_user_id = generate_test_user_id()
# First, create a profile schema
mmdd = datetime.now().strftime("%m%d%H%M%S")
schema_name = f"test_profile_schema_{mmdd}"
attributes = [
ProfileAttribute(
name="test_field",
description="Test field",
),
]
schema_input = CreateProfileSchemaInput(
name=schema_name,
attributes=attributes,
)
schema_result = await create_profile_schema_component.arun(schema_input)
schema_id = schema_result.profile_schema_id
# Now get the user profile
input_data = GetUserProfileInput(
schema_id=schema_id,
user_id=test_user_id,
)
result = await get_user_profile_component.arun(input_data)
# Assertions
assert isinstance(result, GetUserProfileOutput)
if result.profile is not None:
assert isinstance(result.profile.attributes, list)
def test_get_user_profile_input_memory_library_id():
"""Test that GetUserProfileInput accepts optional memory_library_id."""
# Without memory_library_id
input_without = GetUserProfileInput(
schema_id="schema_123",
user_id="user_456",
)
assert input_without.memory_library_id is None
# With memory_library_id
input_with = GetUserProfileInput(
schema_id="schema_123",
user_id="user_456",
memory_library_id="lib_789",
)
assert input_with.memory_library_id == "lib_789"