Debian Codegraph @

tests/lib/_parse/test_transform.py

raw
1 from copy import deepcopy
2
3 import pytest
4 from inline_snapshot import snapshot
5
6 from anthropic.lib._parse._transform import transform_schema
7
8
9 def test_ref_schema():
10 schema = {"$ref": "#/components/schemas/SomeSchema"}
11 result = transform_schema(schema)
12 assert result == snapshot({"$ref": "#/components/schemas/SomeSchema"})
13
14
15 def test_ref_schema_with_defs():
16 # Pydantic v2 emits this shape for `RootModel` types: a root-level
17 # `$ref` with sibling `$defs` holding the referenced schema.
18 schema = {
19 "$ref": "#/$defs/Tier",
20 "$defs": {
21 "Tier": {
22 "type": "string",
23 "enum": ["free", "pro", "enterprise"],
24 "title": "Tier",
25 }
26 },
27 }
28 result = transform_schema(schema)
29 assert result == snapshot(
30 {
31 "$defs": {
32 "Tier": {
33 "type": "string",
34 "enum": ["free", "pro", "enterprise"],
35 "title": "Tier",
36 }
37 },
38 "$ref": "#/$defs/Tier",
39 }
40 )
41
42
43 def test_anyof_schema():
44 schema = {
45 "anyOf": [
46 {"type": "string"},
47 {"type": "integer", "minimum": 1},
48 ]
49 }
50 result = transform_schema(schema)
51 assert result == snapshot(
52 {
53 "anyOf": [
54 {"type": "string"},
55 {
56 "type": "integer",
57 "description": "{minimum: 1}",
58 },
59 ]
60 }
61 )
62
63
64 def test_enum_schema():
65 schema = {
66 "type": "string",
67 "enum": [
68 "foo",
69 "bar",
70 ],
71 }
72 result = transform_schema(schema)
73 assert result == snapshot({"type": "string", "enum": ["foo", "bar"]})
74
75
76 def test_allof():
77 schema = {
78 "allOf": [
79 {"type": "object", "properties": {"name": {"type": "string"}}},
80 {"type": "object", "properties": {"age": {"type": "integer", "minimum": 0}}},
81 ]
82 }
83 result = transform_schema(schema)
84 assert result == snapshot(
85 {
86 "allOf": [
87 {
88 "type": "object",
89 "properties": {"name": {"type": "string"}},
90 "additionalProperties": False,
91 },
92 {
93 "type": "object",
94 "properties": {"age": {"type": "integer", "description": "{minimum: 0}"}},
95 "additionalProperties": False,
96 },
97 ]
98 }
99 )
100
101
102 def test_object_schema():
103 schema = {
104 "type": "object",
105 "properties": {
106 "name": {"type": "string", "default": "John"},
107 "age": {"type": "integer", "minimum": 0},
108 },
109 "required": ["name"],
110 "description": "Person object",
111 }
112 result = transform_schema(schema)
113 assert result == snapshot(
114 {
115 "type": "object",
116 "description": "Person object",
117 "properties": {
118 "name": {"type": "string", "description": "{default: John}"},
119 "age": {"type": "integer", "description": "{minimum: 0}"},
120 },
121 "additionalProperties": False,
122 "required": ["name"],
123 }
124 )
125
126
127 def test_array_schema():
128 schema = {
129 "type": "array",
130 "items": {"type": "string"},
131 "minItems": 2,
132 "description": "A list of strings",
133 }
134 result = transform_schema(schema)
135 assert result == snapshot(
136 {
137 "type": "array",
138 "description": """\
139 A list of strings
140
141 {minItems: 2}\
142 """,
143 "items": {"type": "string"},
144 }
145 )
146
147
148 def test_string_schema_with_format_and_default():
149 schema = {
150 "type": "string",
151 "format": "email",
152 "default": "[email protected]",
153 "description": "User email",
154 }
155 result = transform_schema(schema)
156 assert result == snapshot(
157 {
158 "type": "string",
159 "description": """\
160 User email
161
162 {default: [email protected]}\
163 """,
164 "format": "email",
165 }
166 )
167
168
169 def test_string_schema_without_format():
170 schema = {"type": "string"}
171 result = transform_schema(schema)
172 assert result == snapshot({"type": "string"})
173
174
175 def test_integer_schema_with_min_max_exclusive():
176 schema = {
177 "type": "integer",
178 "minimum": 1,
179 "maximum": 10,
180 "exclusiveMinimum": 0,
181 "exclusiveMaximum": 20,
182 "description": "A number",
183 }
184 result = transform_schema(schema)
185 assert result == snapshot(
186 {
187 "type": "integer",
188 "description": """\
189 A number
190
191 {minimum: 1, maximum: 10, exclusiveMinimum: 0, exclusiveMaximum: 20}\
192 """,
193 }
194 )
195
196
197 def test_boolean_schema():
198 schema = {"type": "boolean", "description": "A flag"}
199 result = transform_schema(schema)
200 assert result == snapshot({"type": "boolean", "description": "A flag"})
201
202
203 def test_null_schema():
204 schema = {"type": "null"}
205 result = transform_schema(schema)
206 assert result == snapshot({"type": "null"})
207
208
209 def test_unsupported_type_asserts():
210 schema = {"type": "unsupported"}
211 with pytest.raises(AssertionError):
212 transform_schema(schema)
213
214
215 def test_original_schema_not_mutated():
216 original_schema = {
217 "type": "object",
218 "properties": {
219 "name": {"type": "string", "default": "John"},
220 "age": {"type": "integer", "minimum": 0},
221 },
222 "required": ["name"],
223 "description": "Person object",
224 "additionalProperties": True,
225 }
226
227 original_schema_backup = deepcopy(original_schema)
228
229 transform_schema(original_schema)
230
231 assert original_schema == original_schema_backup