File: test_transform.py

package info (click to toggle)
anthropic-sdk-python 0.75.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,252 kB
  • sloc: python: 29,737; sh: 177; makefile: 5
file content (191 lines) | stat: -rw-r--r-- 4,793 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from copy import deepcopy

import pytest
from inline_snapshot import snapshot

from anthropic.lib._parse._transform import transform_schema


def test_ref_schema():
    schema = {"$ref": "#/components/schemas/SomeSchema"}
    result = transform_schema(schema)
    assert result == snapshot({"$ref": "#/components/schemas/SomeSchema"})


def test_anyof_schema():
    schema = {
        "anyOf": [
            {"type": "string"},
            {"type": "integer", "minimum": 1},
        ]
    }
    result = transform_schema(schema)
    assert result == snapshot(
        {
            "anyOf": [
                {"type": "string"},
                {
                    "type": "integer",
                    "description": "{minimum: 1}",
                },
            ]
        }
    )


def test_allof():
    schema = {
        "allOf": [
            {"type": "object", "properties": {"name": {"type": "string"}}},
            {"type": "object", "properties": {"age": {"type": "integer", "minimum": 0}}},
        ]
    }
    result = transform_schema(schema)
    assert result == snapshot(
        {
            "allOf": [
                {
                    "type": "object",
                    "properties": {"name": {"type": "string"}},
                    "additionalProperties": False,
                },
                {
                    "type": "object",
                    "properties": {"age": {"type": "integer", "description": "{minimum: 0}"}},
                    "additionalProperties": False,
                },
            ]
        }
    )


def test_object_schema():
    schema = {
        "type": "object",
        "properties": {
            "name": {"type": "string", "default": "John"},
            "age": {"type": "integer", "minimum": 0},
        },
        "required": ["name"],
        "description": "Person object",
    }
    result = transform_schema(schema)
    assert result == snapshot(
        {
            "type": "object",
            "description": "Person object",
            "properties": {
                "name": {"type": "string", "description": "{default: John}"},
                "age": {"type": "integer", "description": "{minimum: 0}"},
            },
            "additionalProperties": False,
            "required": ["name"],
        }
    )


def test_array_schema():
    schema = {
        "type": "array",
        "items": {"type": "string"},
        "minItems": 2,
        "description": "A list of strings",
    }
    result = transform_schema(schema)
    assert result == snapshot(
        {
            "type": "array",
            "description": """\
A list of strings

{minItems: 2}\
""",
            "items": {"type": "string"},
        }
    )


def test_string_schema_with_format_and_default():
    schema = {
        "type": "string",
        "format": "email",
        "default": "user@example.com",
        "description": "User email",
    }
    result = transform_schema(schema)
    assert result == snapshot(
        {
            "type": "string",
            "description": """\
User email

{default: user@example.com}\
""",
            "format": "email",
        }
    )


def test_string_schema_without_format():
    schema = {"type": "string"}
    result = transform_schema(schema)
    assert result == snapshot({"type": "string"})


def test_integer_schema_with_min_max_exclusive():
    schema = {
        "type": "integer",
        "minimum": 1,
        "maximum": 10,
        "exclusiveMinimum": 0,
        "exclusiveMaximum": 20,
        "description": "A number",
    }
    result = transform_schema(schema)
    assert result == snapshot(
        {
            "type": "integer",
            "description": """\
A number

{minimum: 1, maximum: 10, exclusiveMinimum: 0, exclusiveMaximum: 20}\
""",
        }
    )


def test_boolean_schema():
    schema = {"type": "boolean", "description": "A flag"}
    result = transform_schema(schema)
    assert result == snapshot({"type": "boolean", "description": "A flag"})


def test_null_schema():
    schema = {"type": "null"}
    result = transform_schema(schema)
    assert result == snapshot({"type": "null"})


def test_unsupported_type_asserts():
    schema = {"type": "unsupported"}
    with pytest.raises(AssertionError):
        transform_schema(schema)


def test_original_schema_not_mutated():
    original_schema = {
        "type": "object",
        "properties": {
            "name": {"type": "string", "default": "John"},
            "age": {"type": "integer", "minimum": 0},
        },
        "required": ["name"],
        "description": "Person object",
        "additionalProperties": True,
    }

    original_schema_backup = deepcopy(original_schema)

    transform_schema(original_schema)

    assert original_schema == original_schema_backup