Files
agent-framework/python/packages/purview/tests/purview/test_settings.py
Eduard van Valkenburg 8457533c69 Python: Replace Pydantic Settings with TypedDict + load_settings() (#3843)
* Replace Pydantic Settings with TypedDict + load_settings()

- Remove pydantic-settings dependency, add python-dotenv
- Delete _pydantic.py (AFBaseSettings, HTTPsUrl)
- Add _settings.py with generic load_settings() function, SecretString,
  type coercion, and Required field validation (SettingNotFoundError)
- Convert all 13 settings classes from AFBaseSettings subclasses to
  TypedDict definitions with load_settings() calls
- Update all consumers from attribute access to dict access
- Add 20 unit tests for load_settings() covering basic loading, dotenv,
  SecretString, type coercion, and required field validation
- Update all existing tests for new settings patterns

* Fix mypy type errors from settings conversion

- Fix str | None attribute access in responses_client (walrus operator)
- Fix SecretString | None narrowing in bedrock (type: ignore after guard)
- Convert _context_provider.py attribute access to dict access (missed file)
- Fix endpoint type narrowing in search_provider and context_provider
- Fix purview: str | None .rstrip(), int | None defaults, urlparse bytes

* Address PR review: required_fields param, type validation, fixes

- Move required field validation from TypedDict annotations (Required)
  to a required_fields parameter on load_settings(), enabling runtime
  decisions about which fields are required
- Remove Required imports and restore from __future__ import annotations
  in ollama and foundry_local
- Add _check_override_type() for deterministic ServiceInitializationError
  on invalid override types (e.g. dict passed for str field)
- Fix all multi-exception test catches back to single exception type
- Fix Ollama host=None: use .get() so None is passed through to SDK default
- Fix Purview processor: use explicit is-None checks instead of or operator
- Remove unused BaseModel import from openai/_shared.py
- Add 4 new tests (24 total): required_fields param, type validation

* Fix type validation: allow int for float fields

_check_override_type now permits int values for float-typed fields,
matching Python's standard numeric promotion behavior.

* fix: wrap urlparse arg with str() to fix mypy bytes endswith error
2026-02-12 08:51:20 +00:00

83 lines
3.3 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
"""Tests for Purview settings."""
import pytest
from agent_framework_purview import PurviewAppLocation, PurviewLocationType, PurviewSettings, get_purview_scopes
class TestPurviewSettings:
"""Test PurviewSettings configuration."""
def test_settings_defaults(self) -> None:
"""Test PurviewSettings with default values."""
settings = PurviewSettings(app_name="Test App")
assert settings["app_name"] == "Test App"
assert settings.get("graph_base_uri") is None
assert settings.get("tenant_id") is None
assert settings.get("purview_app_location") is None
def test_settings_with_custom_values(self) -> None:
"""Test PurviewSettings with custom values."""
app_location = PurviewAppLocation(location_type=PurviewLocationType.APPLICATION, location_value="app-123")
settings = PurviewSettings(
app_name="Test App",
graph_base_uri="https://graph.microsoft-ppe.com",
tenant_id="test-tenant-id",
purview_app_location=app_location,
)
assert settings["graph_base_uri"] == "https://graph.microsoft-ppe.com"
assert settings["tenant_id"] == "test-tenant-id"
assert settings["purview_app_location"].location_value == "app-123"
@pytest.mark.parametrize(
"graph_uri,expected_scope",
[
("https://graph.microsoft.com/v1.0/", "https://graph.microsoft.com/.default"),
("https://graph.microsoft-ppe.com/v1.0/", "https://graph.microsoft-ppe.com/.default"),
],
)
def test_get_scopes(self, graph_uri: str, expected_scope: str) -> None:
"""Test get_scopes returns correct scope for different URIs."""
settings = PurviewSettings(app_name="Test App", graph_base_uri=graph_uri)
scopes = get_purview_scopes(settings)
assert len(scopes) == 1
assert expected_scope in scopes
class TestPurviewAppLocation:
"""Test PurviewAppLocation configuration."""
@pytest.mark.parametrize(
"location_type,location_value,expected_odata_type",
[
(PurviewLocationType.APPLICATION, "app-123", "microsoft.graph.policyLocationApplication"),
(PurviewLocationType.URI, "https://example.com", "microsoft.graph.policyLocationUrl"),
(PurviewLocationType.DOMAIN, "example.com", "microsoft.graph.policyLocationDomain"),
],
)
def test_get_policy_location(
self, location_type: PurviewLocationType, location_value: str, expected_odata_type: str
) -> None:
"""Test get_policy_location returns correct structure for all location types."""
location = PurviewAppLocation(location_type=location_type, location_value=location_value)
policy_location = location.get_policy_location()
assert policy_location["@odata.type"] == expected_odata_type
assert policy_location["value"] == location_value
class TestPurviewLocationType:
"""Test PurviewLocationType enum."""
def test_location_type_values(self) -> None:
"""Test PurviewLocationType enum has expected values."""
assert PurviewLocationType.APPLICATION == "application"
assert PurviewLocationType.URI == "uri"
assert PurviewLocationType.DOMAIN == "domain"