Skip to content

Commit 49d21bf

Browse files
didier-durandholtskinner
authored andcommitted
test: adding tests for id_generator.py
1 parent 6fa6a6c commit 49d21bf

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import uuid
2+
3+
import pytest
4+
5+
from pydantic import ValidationError
6+
7+
from a2a.server.id_generator import (
8+
IDGenerator,
9+
IDGeneratorContext,
10+
UUIDGenerator,
11+
)
12+
13+
14+
class TestIDGeneratorContext:
15+
"""Tests for IDGeneratorContext."""
16+
17+
def test_context_creation_with_all_fields(self):
18+
"""Test creating context with all fields populated."""
19+
context = IDGeneratorContext(
20+
task_id='task_123', context_id='context_456'
21+
)
22+
assert context.task_id == 'task_123'
23+
assert context.context_id == 'context_456'
24+
25+
def test_context_creation_with_defaults(self):
26+
"""Test creating context with default None values."""
27+
context = IDGeneratorContext()
28+
assert context.task_id is None
29+
assert context.context_id is None
30+
31+
def test_context_creation_with_partial_fields(self):
32+
"""Test creating context with only some fields populated."""
33+
context = IDGeneratorContext(task_id='task_123')
34+
assert context.task_id == 'task_123'
35+
assert context.context_id is None
36+
context = IDGeneratorContext(context_id='context_456')
37+
assert context.task_id is None
38+
assert context.context_id == 'context_456'
39+
40+
def test_context_mutability(self):
41+
"""Test that context fields can be updated (Pydantic models are mutable by default)."""
42+
context = IDGeneratorContext(task_id='task_123')
43+
context.task_id = 'task_456'
44+
assert context.task_id == 'task_456'
45+
46+
def test_context_validation(self):
47+
"""Test that context raises validation error for invalid types."""
48+
with pytest.raises(ValidationError):
49+
IDGeneratorContext(task_id={'not': 'a string'})
50+
51+
class TestIDGenerator:
52+
"""Tests for IDGenerator abstract base class."""
53+
54+
def test_cannot_instantiate_abstract_class(self):
55+
"""Test that IDGenerator cannot be instantiated directly."""
56+
with pytest.raises(TypeError):
57+
IDGenerator()
58+
59+
def test_subclass_must_implement_generate(self):
60+
"""Test that subclasses must implement the generate method."""
61+
62+
class IncompleteGenerator(IDGenerator):
63+
pass
64+
65+
with pytest.raises(TypeError):
66+
IncompleteGenerator()
67+
68+
def test_valid_subclass_implementation(self):
69+
"""Test that a valid subclass can be instantiated."""
70+
71+
class ValidGenerator(IDGenerator): # pylint: disable=C0115,R0903
72+
def generate(self, context: IDGeneratorContext) -> str:
73+
return 'test_id'
74+
75+
generator = ValidGenerator()
76+
assert generator.generate(IDGeneratorContext()) == 'test_id'
77+
78+
79+
class TestUUIDGenerator:
80+
"""Tests for UUIDGenerator implementation."""
81+
82+
def test_generate_returns_string(self):
83+
"""Test that generate returns a valid v4 UUID string."""
84+
generator = UUIDGenerator()
85+
context = IDGeneratorContext()
86+
result = generator.generate(context)
87+
assert isinstance(result, str)
88+
parsed_uuid = uuid.UUID(result)
89+
assert parsed_uuid.version == 4
90+
91+
def test_generate_produces_unique_ids(self):
92+
"""Test that multiple calls produce unique IDs."""
93+
generator = UUIDGenerator()
94+
context = IDGeneratorContext()
95+
ids = [generator.generate(context) for _ in range(100)]
96+
# All IDs should be unique
97+
assert len(ids) == len(set(ids))
98+
99+
def test_generate_with_none_context(self):
100+
"""Test that generate works with context set to None."""
101+
generator = UUIDGenerator()
102+
result = generator.generate(None)
103+
assert isinstance(result, str)
104+
uuid.UUID(result)
105+
106+
def test_generate_with_empty_context(self):
107+
"""Test that generate works with an empty context."""
108+
generator = UUIDGenerator()
109+
context = IDGeneratorContext()
110+
result = generator.generate(context)
111+
assert isinstance(result, str)
112+
uuid.UUID(result)

0 commit comments

Comments
 (0)