Skip to content

Commit 91a59ae

Browse files
added tests
1 parent 8cd7bac commit 91a59ae

File tree

3 files changed

+257
-178
lines changed

3 files changed

+257
-178
lines changed

tests/utils/test_artifact.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
from unittest.mock import patch
55

6-
from a2a.types import DataPart, Part, TextPart
6+
from a2a.types import (
7+
Artifact,
8+
DataPart,
9+
Part,
10+
TextPart,
11+
)
712
from a2a.utils.artifact import (
13+
get_artifact_text,
814
new_artifact,
915
new_data_artifact,
1016
new_text_artifact,
@@ -83,5 +89,70 @@ def test_new_data_artifact_assigns_name_description(self):
8389
self.assertEqual(artifact.description, description)
8490

8591

92+
class TestGetArtifactText(unittest.TestCase):
93+
def test_get_artifact_text_single_part(self):
94+
# Setup
95+
artifact = Artifact(
96+
name="test-artifact",
97+
parts=[Part(root=TextPart(text='Hello world'))],
98+
artifact_id='test-artifact-id',
99+
)
100+
101+
# Exercise
102+
result = get_artifact_text(artifact)
103+
104+
# Verify
105+
assert result == 'Hello world'
106+
107+
def test_get_artifact_text_multiple_parts(self):
108+
# Setup
109+
artifact = Artifact(
110+
name="test-artifact",
111+
parts=[
112+
Part(root=TextPart(text='First line')),
113+
Part(root=TextPart(text='Second line')),
114+
Part(root=TextPart(text='Third line')),
115+
],
116+
artifact_id='test-artifact-id',
117+
)
118+
119+
# Exercise
120+
result = get_artifact_text(artifact)
121+
122+
# Verify - default delimiter is newline
123+
assert result == 'First line\nSecond line\nThird line'
124+
125+
def test_get_artifact_text_custom_delimiter(self):
126+
# Setup
127+
artifact = Artifact(
128+
name="test-artifact",
129+
parts=[
130+
Part(root=TextPart(text='First part')),
131+
Part(root=TextPart(text='Second part')),
132+
Part(root=TextPart(text='Third part')),
133+
],
134+
artifact_id='test-artifact-id',
135+
)
136+
137+
# Exercise
138+
result = get_artifact_text(artifact, delimiter=' | ')
139+
140+
# Verify
141+
assert result == 'First part | Second part | Third part'
142+
143+
def test_get_artifact_text_empty_parts(self):
144+
# Setup
145+
artifact = Artifact(
146+
name="test-artifact",
147+
parts=[],
148+
artifact_id='test-artifact-id',
149+
)
150+
151+
# Exercise
152+
result = get_artifact_text(artifact)
153+
154+
# Verify
155+
assert result == ''
156+
86157
if __name__ == '__main__':
87158
unittest.main()

tests/utils/test_message.py

Lines changed: 0 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@
44

55
from a2a.types import (
66
DataPart,
7-
FilePart,
8-
FileWithBytes,
9-
FileWithUri,
107
Message,
118
Part,
129
Role,
1310
TextPart,
1411
)
1512
from a2a.utils.message import (
16-
get_data_parts,
17-
get_file_parts,
1813
get_message_text,
19-
get_text_parts,
2014
new_agent_parts_message,
2115
new_agent_text_message,
2216
)
@@ -147,177 +141,6 @@ def test_new_agent_parts_message(self):
147141
assert message.message_id == 'abcdefab-cdef-abcd-efab-cdefabcdefab'
148142

149143

150-
class TestGetTextParts:
151-
def test_get_text_parts_single_text_part(self):
152-
# Setup
153-
parts = [Part(root=TextPart(text='Hello world'))]
154-
155-
# Exercise
156-
result = get_text_parts(parts)
157-
158-
# Verify
159-
assert result == ['Hello world']
160-
161-
def test_get_text_parts_multiple_text_parts(self):
162-
# Setup
163-
parts = [
164-
Part(root=TextPart(text='First part')),
165-
Part(root=TextPart(text='Second part')),
166-
Part(root=TextPart(text='Third part')),
167-
]
168-
169-
# Exercise
170-
result = get_text_parts(parts)
171-
172-
# Verify
173-
assert result == ['First part', 'Second part', 'Third part']
174-
175-
def test_get_text_parts_empty_list(self):
176-
# Setup
177-
parts = []
178-
179-
# Exercise
180-
result = get_text_parts(parts)
181-
182-
# Verify
183-
assert result == []
184-
185-
186-
class TestGetDataParts:
187-
def test_get_data_parts_single_data_part(self):
188-
# Setup
189-
parts = [Part(root=DataPart(data={'key': 'value'}))]
190-
191-
# Exercise
192-
result = get_data_parts(parts)
193-
194-
# Verify
195-
assert result == [{'key': 'value'}]
196-
197-
def test_get_data_parts_multiple_data_parts(self):
198-
# Setup
199-
parts = [
200-
Part(root=DataPart(data={'key1': 'value1'})),
201-
Part(root=DataPart(data={'key2': 'value2'})),
202-
]
203-
204-
# Exercise
205-
result = get_data_parts(parts)
206-
207-
# Verify
208-
assert result == [{'key1': 'value1'}, {'key2': 'value2'}]
209-
210-
def test_get_data_parts_mixed_parts(self):
211-
# Setup
212-
parts = [
213-
Part(root=TextPart(text='some text')),
214-
Part(root=DataPart(data={'key1': 'value1'})),
215-
Part(root=DataPart(data={'key2': 'value2'})),
216-
]
217-
218-
# Exercise
219-
result = get_data_parts(parts)
220-
221-
# Verify
222-
assert result == [{'key1': 'value1'}, {'key2': 'value2'}]
223-
224-
def test_get_data_parts_no_data_parts(self):
225-
# Setup
226-
parts = [
227-
Part(root=TextPart(text='some text')),
228-
]
229-
230-
# Exercise
231-
result = get_data_parts(parts)
232-
233-
# Verify
234-
assert result == []
235-
236-
def test_get_data_parts_empty_list(self):
237-
# Setup
238-
parts = []
239-
240-
# Exercise
241-
result = get_data_parts(parts)
242-
243-
# Verify
244-
assert result == []
245-
246-
247-
class TestGetFileParts:
248-
def test_get_file_parts_single_file_part(self):
249-
# Setup
250-
file_with_uri = FileWithUri(
251-
uri='file://path/to/file', mimeType='text/plain'
252-
)
253-
parts = [Part(root=FilePart(file=file_with_uri))]
254-
255-
# Exercise
256-
result = get_file_parts(parts)
257-
258-
# Verify
259-
assert result == [file_with_uri]
260-
261-
def test_get_file_parts_multiple_file_parts(self):
262-
# Setup
263-
file_with_uri1 = FileWithUri(
264-
uri='file://path/to/file1', mime_type='text/plain'
265-
)
266-
file_with_bytes = FileWithBytes(
267-
bytes='ZmlsZSBjb250ZW50',
268-
mime_type='application/octet-stream', # 'file content'
269-
)
270-
parts = [
271-
Part(root=FilePart(file=file_with_uri1)),
272-
Part(root=FilePart(file=file_with_bytes)),
273-
]
274-
275-
# Exercise
276-
result = get_file_parts(parts)
277-
278-
# Verify
279-
assert result == [file_with_uri1, file_with_bytes]
280-
281-
def test_get_file_parts_mixed_parts(self):
282-
# Setup
283-
file_with_uri = FileWithUri(
284-
uri='file://path/to/file', mime_type='text/plain'
285-
)
286-
parts = [
287-
Part(root=TextPart(text='some text')),
288-
Part(root=FilePart(file=file_with_uri)),
289-
]
290-
291-
# Exercise
292-
result = get_file_parts(parts)
293-
294-
# Verify
295-
assert result == [file_with_uri]
296-
297-
def test_get_file_parts_no_file_parts(self):
298-
# Setup
299-
parts = [
300-
Part(root=TextPart(text='some text')),
301-
Part(root=DataPart(data={'key': 'value'})),
302-
]
303-
304-
# Exercise
305-
result = get_file_parts(parts)
306-
307-
# Verify
308-
assert result == []
309-
310-
def test_get_file_parts_empty_list(self):
311-
# Setup
312-
parts = []
313-
314-
# Exercise
315-
result = get_file_parts(parts)
316-
317-
# Verify
318-
assert result == []
319-
320-
321144
class TestGetMessageText:
322145
def test_get_message_text_single_part(self):
323146
# Setup

0 commit comments

Comments
 (0)