Skip to content

Conversation

@Rishabh4275
Copy link
Contributor

@Rishabh4275 Rishabh4275 commented Jan 30, 2026

Motivation and Context

Add Tests for test coverage

Description

Add Tests for test coverage. Increased test coverage from 84 to 89 percent.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings January 30, 2026 05:37
Copy link
Contributor

Copilot AI left a 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_exceptions behavior 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, ContentActivities calls, 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.

Comment on lines +245 to +283
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()
Copy link

Copilot AI Jan 30, 2026

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.

Copilot uses AI. Check for mistakes.
@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/purview/agent_framework_purview
   _processor.py1641093%160, 236–239, 244, 272, 274, 280, 282
TOTAL16248198287% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3694 221 💤 0 ❌ 0 🔥 1m 12s ⏱️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants