Skip to content

Commit 6d813f4

Browse files
authored
Merge branch 'main' into dependabot/github_actions/github-actions-1a272fb5f1
2 parents 8c0a643 + df78a94 commit 6d813f4

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
@pytest.mark.parametrize(
32+
'kwargs, expected_task_id, expected_context_id',
33+
[
34+
({'task_id': 'task_123'}, 'task_123', None),
35+
({'context_id': 'context_456'}, None, 'context_456'),
36+
],
37+
)
38+
def test_context_creation_with_partial_fields(
39+
self, kwargs, expected_task_id, expected_context_id
40+
):
41+
"""Test creating context with only some fields populated."""
42+
context = IDGeneratorContext(**kwargs)
43+
assert context.task_id == expected_task_id
44+
assert context.context_id == expected_context_id
45+
46+
def test_context_mutability(self):
47+
"""Test that context fields can be updated (Pydantic models are mutable by default)."""
48+
context = IDGeneratorContext(task_id='task_123')
49+
context.task_id = 'task_456'
50+
assert context.task_id == 'task_456'
51+
52+
def test_context_validation(self):
53+
"""Test that context raises validation error for invalid types."""
54+
with pytest.raises(ValidationError):
55+
IDGeneratorContext(task_id={'not': 'a string'})
56+
57+
58+
class TestIDGenerator:
59+
"""Tests for IDGenerator abstract base class."""
60+
61+
def test_cannot_instantiate_abstract_class(self):
62+
"""Test that IDGenerator cannot be instantiated directly."""
63+
with pytest.raises(TypeError):
64+
IDGenerator()
65+
66+
def test_subclass_must_implement_generate(self):
67+
"""Test that subclasses must implement the generate method."""
68+
69+
class IncompleteGenerator(IDGenerator):
70+
pass
71+
72+
with pytest.raises(TypeError):
73+
IncompleteGenerator()
74+
75+
def test_valid_subclass_implementation(self):
76+
"""Test that a valid subclass can be instantiated."""
77+
78+
class ValidGenerator(IDGenerator): # pylint: disable=C0115,R0903
79+
def generate(self, context: IDGeneratorContext) -> str:
80+
return 'test_id'
81+
82+
generator = ValidGenerator()
83+
assert generator.generate(IDGeneratorContext()) == 'test_id'
84+
85+
86+
@pytest.fixture
87+
def generator():
88+
"""Returns a UUIDGenerator instance."""
89+
return UUIDGenerator()
90+
91+
92+
@pytest.fixture
93+
def context():
94+
"""Returns a IDGeneratorContext instance."""
95+
return IDGeneratorContext()
96+
97+
98+
class TestUUIDGenerator:
99+
"""Tests for UUIDGenerator implementation."""
100+
101+
def test_generate_returns_string(self, generator, context):
102+
"""Test that generate returns a valid v4 UUID string."""
103+
result = generator.generate(context)
104+
assert isinstance(result, str)
105+
parsed_uuid = uuid.UUID(result)
106+
assert parsed_uuid.version == 4
107+
108+
def test_generate_produces_unique_ids(self, generator, context):
109+
"""Test that multiple calls produce unique IDs."""
110+
ids = [generator.generate(context) for _ in range(100)]
111+
# All IDs should be unique
112+
assert len(ids) == len(set(ids))
113+
114+
@pytest.mark.parametrize(
115+
'context_arg',
116+
[
117+
None,
118+
IDGeneratorContext(),
119+
],
120+
ids=[
121+
'none_context',
122+
'empty_context',
123+
],
124+
)
125+
def test_generate_works_with_various_contexts(self, context_arg):
126+
"""Test that generate works with various context inputs."""
127+
generator = UUIDGenerator()
128+
result = generator.generate(context_arg)
129+
assert isinstance(result, str)
130+
parsed_uuid = uuid.UUID(result)
131+
assert parsed_uuid.version == 4

0 commit comments

Comments
 (0)