| 1 |
"""Live integration tests against the real gateway. |
| 2 |
|
| 3 |
Gated on `ANTHROPIC_LIVE=1`; the regular suite never hits the network. Expects |
| 4 |
`ANTHROPIC_GOOGLE_CLOUD_{PROJECT,WORKSPACE_ID}` (or `..._BASE_URL`) and |
| 5 |
Application Default Credentials in the environment. |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import os |
| 11 |
from typing import Iterator, AsyncIterator |
| 12 |
|
| 13 |
import httpx2 |
| 14 |
import pytest |
| 15 |
|
| 16 |
from anthropic import APIStatusError |
| 17 |
from anthropic.lib.google_cloud import AnthropicGoogleCloud, AsyncAnthropicGoogleCloud |
| 18 |
|
| 19 |
pytestmark = [ |
| 20 |
pytest.mark.skipif(os.environ.get("ANTHROPIC_LIVE") != "1", reason="Set ANTHROPIC_LIVE=1 to run live tests"), |
| 21 |
# google-auth emits a Python-EOL FutureWarning on import; the repo runs with |
| 22 |
# filterwarnings=error, so silence it for this live-test module. |
| 23 |
pytest.mark.filterwarnings("ignore::FutureWarning"), |
| 24 |
pytest.mark.filterwarnings("ignore::DeprecationWarning"), |
| 25 |
] |
| 26 |
|
| 27 |
MODEL = "claude-haiku-4-5" |
| 28 |
LOCATION = os.environ.get("ANTHROPIC_GOOGLE_CLOUD_LOCATION", "us-central1") |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def sync_client() -> Iterator[AnthropicGoogleCloud]: |
| 33 |
client = AnthropicGoogleCloud( |
| 34 |
location=LOCATION, |
| 35 |
max_retries=1, |
| 36 |
http_client=httpx2.Client(timeout=60.0), |
| 37 |
) |
| 38 |
try: |
| 39 |
yield client |
| 40 |
finally: |
| 41 |
client.close() |
| 42 |
|
| 43 |
|
| 44 |
@pytest.fixture |
| 45 |
async def async_client() -> AsyncIterator[AsyncAnthropicGoogleCloud]: |
| 46 |
client = AsyncAnthropicGoogleCloud( |
| 47 |
location=LOCATION, |
| 48 |
max_retries=1, |
| 49 |
http_client=httpx2.AsyncClient(timeout=60.0), |
| 50 |
) |
| 51 |
try: |
| 52 |
yield client |
| 53 |
finally: |
| 54 |
await client.close() |
| 55 |
|
| 56 |
|
| 57 |
class TestSyncLive: |
| 58 |
def test_non_streaming(self, sync_client: AnthropicGoogleCloud) -> None: |
| 59 |
message = sync_client.messages.create( |
| 60 |
model=MODEL, |
| 61 |
max_tokens=32, |
| 62 |
messages=[{"role": "user", "content": "Say hello in one word."}], |
| 63 |
) |
| 64 |
assert message.role == "assistant" |
| 65 |
assert message.content[0].type == "text" |
| 66 |
assert message.usage.output_tokens > 0 |
| 67 |
|
| 68 |
def test_streaming(self, sync_client: AnthropicGoogleCloud) -> None: |
| 69 |
events: list[str] = [] |
| 70 |
with sync_client.messages.stream( |
| 71 |
model=MODEL, |
| 72 |
max_tokens=32, |
| 73 |
messages=[{"role": "user", "content": "Say hello in one word."}], |
| 74 |
) as stream: |
| 75 |
for event in stream: |
| 76 |
events.append(event.type) |
| 77 |
final = stream.get_final_message() |
| 78 |
|
| 79 |
assert events[0] == "message_start" |
| 80 |
assert "content_block_delta" in events |
| 81 |
assert events[-1] == "message_stop" |
| 82 |
assert final.role == "assistant" |
| 83 |
assert final.content[0].type == "text" |
| 84 |
|
| 85 |
def test_bad_model_surfaces_typed_error(self, sync_client: AnthropicGoogleCloud) -> None: |
| 86 |
with pytest.raises(APIStatusError): |
| 87 |
sync_client.messages.create( |
| 88 |
model="not-a-real-model", |
| 89 |
max_tokens=16, |
| 90 |
messages=[{"role": "user", "content": "hi"}], |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
class TestAsyncLive: |
| 95 |
async def test_non_streaming(self, async_client: AsyncAnthropicGoogleCloud) -> None: |
| 96 |
message = await async_client.messages.create( |
| 97 |
model=MODEL, |
| 98 |
max_tokens=32, |
| 99 |
messages=[{"role": "user", "content": "Say hello in one word."}], |
| 100 |
) |
| 101 |
assert message.role == "assistant" |
| 102 |
assert message.content[0].type == "text" |
| 103 |
|
| 104 |
async def test_streaming(self, async_client: AsyncAnthropicGoogleCloud) -> None: |
| 105 |
events: list[str] = [] |
| 106 |
async with async_client.messages.stream( |
| 107 |
model=MODEL, |
| 108 |
max_tokens=32, |
| 109 |
messages=[{"role": "user", "content": "Say hello in one word."}], |
| 110 |
) as stream: |
| 111 |
async for event in stream: |
| 112 |
events.append(event.type) |
| 113 |
final = await stream.get_final_message() |
| 114 |
|
| 115 |
assert events[0] == "message_start" |
| 116 |
assert "content_block_delta" in events |
| 117 |
assert events[-1] == "message_stop" |
| 118 |
assert final.content[0].type == "text" |
| 119 |
|
| 120 |
async def test_bad_model_surfaces_typed_error(self, async_client: AsyncAnthropicGoogleCloud) -> None: |
| 121 |
with pytest.raises(APIStatusError): |
| 122 |
await async_client.messages.create( |
| 123 |
model="not-a-real-model", |
| 124 |
max_tokens=16, |
| 125 |
messages=[{"role": "user", "content": "hi"}], |
| 126 |
) |