Skip to content

Commit 87759a3

Browse files
committed
chore(internal): simplify http snapshots
1 parent 4547171 commit 87759a3

25 files changed

+1365
-535
lines changed

.inline-snapshot/external/cd8d3d185e7a993935ab650e0e5c6a7970758bac7d0487f9b1afaad2cc095f3c.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ $ ./scripts/test
9999
### Snapshots
100100

101101
Some tests use [inline-snapshot](https://15r10nk.github.io/inline-snapshot/latest/). To update them after making changes, rerun the tests with the `--inline-snapshot=fix` and `-n0` options:
102-
103102
```bash
104103
./scripts/test --inline-snapshot=fix -n0
105104
```
@@ -108,15 +107,14 @@ Some tests use [inline-snapshot](https://15r10nk.github.io/inline-snapshot/lates
108107
> `inline-snapshot` is incompatible with [pytest-xdist](https://github.com/pytest-dev/pytest-xdist), so you need to disable parallel execution `(-n0)` when using the `--inline-snapshot` option.
109108
110109
In addition, some tests capture snapshots of the HTTP requests they make.
111-
To refresh these snapshots, run the tests with the `ANTHROPIC_LIVE=1` environment variable enabled.
112-
110+
To refresh these snapshots, run the tests with the `--http-record` flag:
113111
```bash
114-
ANTHROPIC_LIVE=1 ./scripts/test --inline-snapshot=fix
112+
./scripts/test --inline-snapshot=fix --http-record
115113
```
116114

117115
> [!NOTE]
118-
> Sometimes it makes sense to update only the inline snapshots `(--inline-snapshot=fix)` without refreshing the HTTP snapshots `(ANTHROPIC_LIVE=1)`.
119-
> This is useful when the endpoint hasnt changed, but your code handles the response differently and the assertions need updating.
116+
> Sometimes it makes sense to update only the inline snapshots `(--inline-snapshot=fix)` without refreshing the HTTP snapshots `(--http-record)`.
117+
> This is useful when the endpoint hasn't changed, but your code handles the response differently and the assertions need updating.
120118
121119
## Linting and formatting
122120

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ dev = [
7474
"pytest-xdist>=3.6.1",
7575
"inline-snapshot>=0.28.0",
7676
"griffe>=1",
77+
"http-snapshot[httpx]==0.1.4",
7778
]
7879
pydantic-v1 = [
7980
"pydantic>=1.9.0,<2",

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ def client(request: FixtureRequest) -> Iterator[Anthropic]:
5858
yield client
5959

6060

61+
@pytest.fixture(scope="function")
62+
def snapshot_client(snapshot_sync_httpx_client: httpx.Client, is_recording: bool) -> Iterator[Anthropic]:
63+
with Anthropic(http_client=snapshot_sync_httpx_client, api_key=None if is_recording else api_key) as client:
64+
yield client
65+
66+
67+
@pytest.fixture(scope="function")
68+
async def async_snapshot_client(
69+
snapshot_async_httpx_client: httpx.AsyncClient, is_recording: bool
70+
) -> AsyncIterator[AsyncAnthropic]:
71+
async with AsyncAnthropic(
72+
http_client=snapshot_async_httpx_client, api_key=None if is_recording else api_key
73+
) as client:
74+
yield client
75+
76+
6177
@pytest.fixture(scope="session")
6278
async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncAnthropic]:
6379
param = getattr(request, "param", True)

tests/lib/_parse/__inline_snapshot__/test_beta_messages/TestAsyncMessages.test_stream_with_raw_schema/48aac7c3-f271-47b3-854b-af4ed31e10bb.json

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"response": {
4+
"status_code": 200,
5+
"headers": {
6+
"date": "Fri, 05 Dec 2025 15:06:08 GMT",
7+
"content-type": "text/event-stream; charset=utf-8",
8+
"connection": "keep-alive",
9+
"cf-ray": "9a947204ac019fb2-AMS",
10+
"cache-control": "no-cache",
11+
"request-id": "req_011CVokJu9sXWCZuA7nVS1g3",
12+
"strict-transport-security": "max-age=31536000; includeSubDomains; preload",
13+
"anthropic-organization-id": "5576611f-a1ee-427c-9800-298d9579899c",
14+
"x-envoy-upstream-service-time": "3888",
15+
"cf-cache-status": "DYNAMIC",
16+
"x-robots-tag": "none",
17+
"server": "cloudflare"
18+
},
19+
"body": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"model\":\"claude-sonnet-4-5-20250929\",\"id\":\"msg_015jm362jgYXWyFRvq8NHxPr\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":135,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"output_tokens\":1,\"service_tier\":\"standard\"}} }\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"[\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"12\"} }\n\nevent: ping\ndata: {\"type\": \"ping\"}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"345,\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"67890]\"} }\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0 }\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":135,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"output_tokens\":10} }\n\nevent: message_stop\ndata: {\"type\":\"message_stop\" }\n\n"
20+
}
21+
}
22+
]

tests/lib/_parse/test_beta_messages.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
from typing import Any, cast
2+
13
import pytest
2-
from respx import MockRouter
34
from inline_snapshot import external, snapshot
45

56
from anthropic import AsyncAnthropic, _compat
67
from anthropic.types.beta.parsed_beta_message import ParsedBetaMessage
78

8-
from ..snapshots import make_async_stream_snapshot_request
9-
109

1110
@pytest.mark.skipif(_compat.PYDANTIC_V1, reason="tool runner not supported with pydantic v1")
1211
class TestAsyncMessages:
13-
async def test_stream_with_raw_schema(self, async_client: AsyncAnthropic, respx_mock: MockRouter) -> None:
12+
@pytest.mark.parametrize(
13+
"http_snapshot",
14+
[
15+
cast(Any, external("uuid:606342ef-f614-4bc1-b5e7-2c3305a48bf3.json")),
16+
],
17+
)
18+
async def test_stream_with_raw_schema(self, async_snapshot_client: AsyncAnthropic) -> None:
1419
async def async_stream_parse(client: AsyncAnthropic) -> ParsedBetaMessage[None]:
1520
async with client.beta.messages.stream(
1621
model="claude-sonnet-4-5",
@@ -32,12 +37,6 @@ async def async_stream_parse(client: AsyncAnthropic) -> ParsedBetaMessage[None]:
3237
) as stream:
3338
return await stream.get_final_message()
3439

35-
response = await make_async_stream_snapshot_request(
36-
async_stream_parse,
37-
content_snapshot=external("uuid:48aac7c3-f271-47b3-854b-af4ed31e10bb.json"),
38-
respx_mock=respx_mock,
39-
mock_client=async_client,
40-
path="/v1/messages?beta=true",
41-
)
40+
response = await async_stream_parse(async_snapshot_client)
4241

4342
assert response.content[0].text == snapshot("[12345,67890]")

tests/lib/snapshots.py

Lines changed: 0 additions & 259 deletions
This file was deleted.

0 commit comments

Comments
 (0)