-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Python: Add tests to Purview Package #3513
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR extends the Purview Python package’s test suite to cover additional edge cases in the processor, middleware, client, and cache components, with a focus on payment-required behavior, correlation IDs, caching semantics, and error handling.
Changes:
- Added processor tests around corrupted cache entries, tenant-level payment-required exception caching, and background re-processing behavior when scopes are modified.
- Expanded middleware and chat middleware tests to cover streaming behavior, 402 Payment Required handling, and
ignore_exceptionsbehavior in both pre- and post-check paths. - Extended Purview client and cache tests to cover correlation ID propagation, 402 handling, deserialization failures, invalid JSON bodies,
ContentActivitiescalls, and conservative cache size-estimation fallbacks.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
python/packages/purview/tests/test_processor.py |
Adds tests for cache behavior (including corrupted entries), tenant-level payment-required caching, and background retry logic when protection scopes change. |
python/packages/purview/tests/test_middleware.py |
Adds agent middleware tests for streaming (post-check skip), 402 behavior, and exception propagation based on ignore_payment_required and ignore_exceptions. |
python/packages/purview/tests/test_client.py |
Extends client tests to cover ETag header precedence, 402 handling (both _post and process_content), correlation ID header/span behavior, deserialization-fallback paths, and content activities. |
python/packages/purview/tests/test_chat_middleware.py |
Adds chat middleware tests for 402 behavior and exception propagation in pre- and post-checks, mirroring agent middleware semantics. |
python/packages/purview/tests/test_cache.py |
Adds a test ensuring _estimate_size falls back to a conservative size when both JSON serialization and sys.getsizeof fail. |
| async def test_process_with_scopes_ignores_unexpected_cached_value_type( | ||
| self, processor: ScopedContentProcessor, mock_client: AsyncMock, process_content_request_factory | ||
| ) -> None: | ||
| """Test that a corrupted cache entry does not crash processing.""" | ||
| from agent_framework_purview._models import ( | ||
| ExecutionMode, | ||
| PolicyLocation, | ||
| PolicyScope, | ||
| ProcessContentResponse, | ||
| ProtectionScopeActivities, | ||
| ProtectionScopesResponse, | ||
| ) | ||
|
|
||
| request = process_content_request_factory() | ||
|
|
||
| # Return a valid, inline scope so we stay on the normal (non-background) path. | ||
| scope_location = PolicyLocation(**{ | ||
| "@odata.type": "microsoft.graph.policyLocationApplication", | ||
| "value": "app-id", | ||
| }) | ||
| scope = PolicyScope(**{ | ||
| "activities": ProtectionScopeActivities.UPLOAD_TEXT, | ||
| "locations": [scope_location], | ||
| "execution_mode": ExecutionMode.EVALUATE_INLINE, | ||
| }) | ||
| mock_client.get_protection_scopes = AsyncMock(return_value=ProtectionScopesResponse(**{"value": [scope]})) | ||
| mock_client.process_content = AsyncMock( | ||
| return_value=ProcessContentResponse(**{"id": "ok", "protectionScopeState": "notModified"}) | ||
| ) | ||
|
|
||
| # First cache read is the tenant payment key (None). Second is the scopes cache (corrupt value). | ||
| processor._cache.get = AsyncMock(side_effect=[None, "corrupt-value"]) # type: ignore[method-assign] | ||
| processor._cache.set = AsyncMock() # type: ignore[method-assign] | ||
|
|
||
| response = await processor._process_with_scopes(request) | ||
|
|
||
| assert response.id == "ok" | ||
| mock_client.get_protection_scopes.assert_called_once() | ||
| mock_client.process_content.assert_called_once() |
Copilot
AI
Jan 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test assumes that _process_with_scopes will gracefully ignore a non-ProtectionScopesResponse value returned from the cache and fall back to calling get_protection_scopes, but the current implementation in _processor.py uses ps_resp after only assigning it when cached_ps_resp is a ProtectionScopesResponse. If the cache returns a non-None, non-ProtectionScopesResponse value, ps_resp is left undefined and an UnboundLocalError will be raised before any client call, so this test will currently fail. Either _process_with_scopes should be updated to treat unexpected cached types as a cache miss (and always initialize ps_resp), or this test should be relaxed to match the existing behavior.
Motivation and Context
Add Tests for test coverage
Description
Add Tests for test coverage. Increased test coverage from 84 to 89 percent.
Contribution Checklist