-
Notifications
You must be signed in to change notification settings - Fork 313
test: adding tests for ID Generator and associated classes #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f29a51d
[TEST] adding tests for ID Generator and associated classes
didier-durand 8302509
Update tests/server/test_id_generator.py
didier-durand b1bdd89
Update tests/server/test_id_generator.py
didier-durand f6d99a2
Update tests/server/test_id_generator.py
didier-durand 00a442f
test: fixing pylint issues
didier-durand 6877dd4
test: reformatting code for lint
didier-durand 2afc4a0
test: fix ruff issues
didier-durand 7450e9a
test: executed `ruff format` on new code
didier-durand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| """Tests for IDGenerator abstract base class.""" | ||
|
|
||
| import uuid | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from pydantic import ValidationError | ||
|
|
||
| from a2a.server.id_generator import ( | ||
| IDGenerator, | ||
| IDGeneratorContext, | ||
| UUIDGenerator, | ||
| ) | ||
|
|
||
|
|
||
| class TestIDGeneratorContext: | ||
| """Tests for IDGeneratorContext.""" | ||
|
|
||
| def test_context_creation_with_all_fields(self): | ||
| """Test creating context with all fields populated.""" | ||
| context = IDGeneratorContext( | ||
| task_id='task_123', context_id='context_456' | ||
| ) | ||
| assert context.task_id == 'task_123' | ||
| assert context.context_id == 'context_456' | ||
|
|
||
| def test_context_creation_with_defaults(self): | ||
| """Test creating context with default None values.""" | ||
| context = IDGeneratorContext() | ||
| assert context.task_id is None | ||
| assert context.context_id is None | ||
|
|
||
| def test_context_creation_with_partial_fields(self): | ||
| """Test creating context with only some fields populated.""" | ||
| context = IDGeneratorContext(task_id='task_123') | ||
| assert context.task_id == 'task_123' | ||
| assert context.context_id is None | ||
|
|
||
| def test_context_mutability(self): | ||
| """Test that context fields can be updated (Pydantic models are mutable by default).""" | ||
| context = IDGeneratorContext(task_id='task_123') | ||
| context.task_id = 'task_456' | ||
| assert context.task_id == 'task_456' | ||
|
|
||
| def test_context_validation(self): | ||
| """Test that context raises validation error for invalid types.""" | ||
| with pytest.raises(ValidationError): | ||
| IDGeneratorContext(task_id={'not': 'a string'}) # noqa | ||
|
|
||
|
|
||
| class TestIDGenerator: | ||
| """Tests for IDGenerator abstract base class.""" | ||
|
|
||
| def test_cannot_instantiate_abstract_class(self): | ||
| """Test that IDGenerator cannot be instantiated directly.""" | ||
| with pytest.raises(TypeError): | ||
| IDGenerator() | ||
|
|
||
| def test_subclass_must_implement_generate(self): | ||
| """Test that subclasses must implement the generate method.""" | ||
|
|
||
| class IncompleteGenerator(IDGenerator): | ||
| pass | ||
|
|
||
| with pytest.raises(TypeError): | ||
| IncompleteGenerator() | ||
|
|
||
| def test_valid_subclass_implementation(self): | ||
| """Test that a valid subclass can be instantiated.""" | ||
|
|
||
| class ValidGenerator(IDGenerator): # pylint: disable=C0115,R0903 | ||
| def generate(self, context: IDGeneratorContext) -> str: | ||
| return 'test_id' | ||
|
|
||
| generator = ValidGenerator() | ||
| assert generator.generate(IDGeneratorContext()) == 'test_id' | ||
|
|
||
|
|
||
| class TestUUIDGenerator: | ||
| """Tests for UUIDGenerator implementation.""" | ||
|
|
||
| def test_generate_returns_string(self): | ||
| """Test that generate returns a string.""" | ||
| generator = UUIDGenerator() | ||
| context = IDGeneratorContext() | ||
| result = generator.generate(context) | ||
| assert isinstance(result, str) | ||
|
|
||
| def test_generate_returns_valid_uuid(self): | ||
| """Test that generate returns a valid UUID format.""" | ||
| generator = UUIDGenerator() | ||
| context = IDGeneratorContext() | ||
| result = generator.generate(context) | ||
| uuid.UUID(result) | ||
|
|
||
| def test_generate_returns_uuid_version_4(self): | ||
| """Test that generate returns a UUID version 4.""" | ||
| generator = UUIDGenerator() | ||
| context = IDGeneratorContext() | ||
| result = generator.generate(context) | ||
| parsed_uuid = uuid.UUID(result) | ||
| assert parsed_uuid.version == 4 | ||
|
|
||
| def test_generate_ignores_context(self): | ||
| """Test that generate ignores the context parameter.""" | ||
| generator = UUIDGenerator() | ||
| context1 = IDGeneratorContext(task_id='task_1', context_id='context_1') | ||
| context2 = IDGeneratorContext(task_id='task_2', context_id='context_2') | ||
| result1 = generator.generate(context1) | ||
| result2 = generator.generate(context2) | ||
| uuid.UUID(result1) | ||
| uuid.UUID(result2) | ||
| assert result1 != result2 | ||
|
|
||
| def test_generate_produces_unique_ids(self): | ||
| """Test that multiple calls produce unique IDs.""" | ||
| generator = UUIDGenerator() | ||
| context = IDGeneratorContext() | ||
| ids = [generator.generate(context) for _ in range(100)] | ||
| # All IDs should be unique | ||
| assert len(ids) == len(set(ids)) | ||
|
|
||
| def test_generate_with_empty_context(self): | ||
| """Test that generate works with an empty context.""" | ||
| generator = UUIDGenerator() | ||
| context = IDGeneratorContext() | ||
| result = generator.generate(context) | ||
| assert isinstance(result, str) | ||
| uuid.UUID(result) | ||
|
|
||
| def test_generate_with_populated_context(self): | ||
| """Test that generate works with a populated context.""" | ||
| generator = UUIDGenerator() | ||
| context = IDGeneratorContext( | ||
| task_id='task_123', context_id='context_456' | ||
| ) | ||
| result = generator.generate(context) | ||
| assert isinstance(result, str) | ||
| uuid.UUID(result) | ||
|
|
||
| @patch('uuid.uuid4') | ||
| def test_generate_calls_uuid4(self, mock_uuid4): | ||
| """Test that generate uses uuid.uuid4() internally.""" | ||
| mock_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678') | ||
| mock_uuid4.return_value = mock_uuid | ||
| generator = UUIDGenerator() | ||
| context = IDGeneratorContext() | ||
| result = generator.generate(context) | ||
| mock_uuid4.assert_called_once() | ||
| assert result == str(mock_uuid) | ||
|
|
||
| def test_generator_is_instance_of_id_generator(self): | ||
| """Test that UUIDGenerator is an instance of IDGenerator.""" | ||
| generator = UUIDGenerator() | ||
| assert isinstance(generator, IDGenerator) | ||
|
|
||
| def test_multiple_generators_produce_different_ids(self): | ||
| """Test that multiple generator instances produce different IDs.""" | ||
| generator1 = UUIDGenerator() | ||
| generator2 = UUIDGenerator() | ||
| context = IDGeneratorContext() | ||
| result1 = generator1.generate(context) | ||
| result2 = generator2.generate(context) | ||
| assert result1 != result2 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.