1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
from __future__ import annotations
import pytest
from anthropic._exceptions import AnthropicError
from anthropic.lib.foundry import AnthropicFoundry, AsyncAnthropicFoundry
class TestAnthropicFoundry:
def test_basic_initialization_with_api_key(self) -> None:
"""Test basic client initialization with API key."""
client = AnthropicFoundry(
api_key="test-key",
resource="example-resource",
)
assert client.api_key == "test-key"
assert str(client.base_url) == "https://example-resource.services.ai.azure.com/anthropic/"
def test_initialization_with_base_url(self) -> None:
"""Test client initialization with base_url instead of resource."""
client = AnthropicFoundry(
api_key="test-key",
base_url="https://example.services.ai.azure.com/anthropic/",
)
assert str(client.base_url) == "https://example.services.ai.azure.com/anthropic/"
def test_initialization_with_azure_ad_token_provider(self) -> None:
"""Test client initialization with Azure AD token provider."""
def token_provider() -> str:
return "test-token"
client = AnthropicFoundry(
azure_ad_token_provider=token_provider,
resource="example-resource",
)
assert client._azure_ad_token_provider is not None
assert client._get_azure_ad_token() == "test-token"
def test_initialization_from_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test client initialization falls back to environment variables."""
monkeypatch.setenv("ANTHROPIC_FOUNDRY_API_KEY", "env-key")
monkeypatch.setenv("ANTHROPIC_API_VERSION", "2023-06-01")
monkeypatch.setenv("ANTHROPIC_FOUNDRY_RESOURCE", "env-resource")
client = AnthropicFoundry()
assert client.api_key == "env-key"
assert "env-resource.services.ai.azure.com" in str(client.base_url)
def test_missing_credentials_error(self) -> None:
"""Test error raised when no credentials are provided."""
with pytest.raises(AnthropicError, match="Missing credentials"):
AnthropicFoundry(
resource="example-resource",
)
def test_missing_resource_error(self) -> None:
"""Test error raised when neither resource nor base_url is provided."""
with pytest.raises(ValueError, match="base_url.*resource"):
AnthropicFoundry(
api_key="test-key",
)
class TestAsyncAnthropicFoundry:
@pytest.mark.asyncio
async def test_basic_initialization_with_api_key(self) -> None:
"""Test basic async client initialization with API key."""
client = AsyncAnthropicFoundry(
api_key="test-key",
resource="example-resource",
)
assert client.api_key == "test-key"
assert str(client.base_url) == "https://example-resource.services.ai.azure.com/anthropic/"
@pytest.mark.asyncio
async def test_async_azure_ad_token_provider(self) -> None:
"""Test async client with async Azure AD token provider."""
async def async_token_provider() -> str:
return "async-test-token"
client = AsyncAnthropicFoundry(
azure_ad_token_provider=async_token_provider,
resource="example-resource",
)
token = await client._get_azure_ad_token()
assert token == "async-test-token"
@pytest.mark.asyncio
async def test_initialization_from_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test async client initialization falls back to environment variables."""
monkeypatch.setenv("ANTHROPIC_FOUNDRY_API_KEY", "env-key")
monkeypatch.setenv("ANTHROPIC_API_VERSION", "2023-06-01")
monkeypatch.setenv("ANTHROPIC_FOUNDRY_RESOURCE", "env-resource")
client = AsyncAnthropicFoundry()
assert client.api_key == "env-key"
assert "env-resource.services.ai.azure.com" in str(client.base_url)
|