Skip to content

Commit e5acfa3

Browse files
🧪 Add tests mostly for broken things.
1 parent 7152da5 commit e5acfa3

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

‎tests/test_json_schema.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from typing import Union
2+
3+
import pytest
4+
from openapi_pydantic.v3.v3_1 import DataType
5+
6+
from lapidary.render.model.conv_schema import OpenApi30SchemaConverter
7+
from lapidary.render.model.openapi import Schema
8+
from lapidary.render.model.python import (
9+
AnnotatedType,
10+
AnnotatedVariable,
11+
ModulePath,
12+
NameRef,
13+
SchemaClass,
14+
)
15+
from lapidary.render.model.stack import Stack
16+
from lapidary.render.runtime import JsonValue, ModelBase
17+
18+
19+
def test_no_type_is_json_value():
20+
converter = OpenApi30SchemaConverter(Schema(), Stack(('#', 'schemas', 'model')), ModulePath('root'), None)
21+
annotation = converter.process_schema().as_annotation('root')
22+
assert annotation == JsonValue
23+
24+
25+
@pytest.mark.skip('error')
26+
def test_additional_properties_schema():
27+
schema = Schema(
28+
type=DataType.OBJECT,
29+
additionalProperties=Schema(
30+
type=DataType.STRING,
31+
),
32+
)
33+
converter = OpenApi30SchemaConverter(schema, Stack(('#', 'schemas', 'model')), ModulePath('root'), None)
34+
annotation = converter.process_schema().as_annotation('root')
35+
expected = AnnotatedType(
36+
NameRef.from_type(dict),
37+
(
38+
AnnotatedType(NameRef.from_type(str)),
39+
AnnotatedType(NameRef.from_type(str)),
40+
),
41+
)
42+
assert annotation == expected
43+
44+
45+
@pytest.mark.skip('not implemented')
46+
def test_properties_and_additional_properties_schema():
47+
schema = Schema(
48+
type=DataType.OBJECT,
49+
properties={'prop1': Schema(type=DataType.NUMBER)},
50+
additionalProperties=Schema(
51+
type=DataType.STRING,
52+
),
53+
)
54+
converter = OpenApi30SchemaConverter(schema, Stack(('#', 'schemas', 'model')), ModulePath('root'), None)
55+
typ = converter.process_schema().as_type('root')
56+
assert '__pydantic_extra__' in [field.name for field in typ.fields]
57+
58+
59+
@pytest.mark.skip('not implemented')
60+
def test_named_nullable_ignored():
61+
"""
62+
pydantic.JsonValue is nullable (Union with None) while in OpenAPI 3.0 flavor of JSON Schema, an empty schema is not
63+
"""
64+
schema = Schema(nullable=True)
65+
converter = OpenApi30SchemaConverter(schema, Stack(('#', 'schemas', 'model')), ModulePath('root'), None)
66+
typ = converter.process_schema().as_annotation('root')
67+
#
68+
assert typ != JsonValue
69+
70+
71+
@pytest.mark.skip('buggy')
72+
def test_doesnt_make_nullable_with_enum():
73+
"""Parent schema is not nullable so resulting type shouldn't be either"""
74+
schema = Schema(
75+
anyOf=[
76+
Schema(type=DataType.STRING),
77+
Schema(enum=[None]),
78+
]
79+
)
80+
converter = OpenApi30SchemaConverter(schema, Stack(('#', 'schemas', 'model')), ModulePath('root'), None)
81+
typ = converter.process_schema().as_annotation('root')
82+
expected = AnnotatedType(NameRef.from_type(str))
83+
assert typ == expected
84+
85+
86+
@pytest.mark.skip('not implemented')
87+
def test_read_write_property():
88+
schema = Schema(
89+
type=DataType.OBJECT,
90+
allOf=[
91+
Schema(
92+
properties={
93+
'prop1': Schema(
94+
type=DataType.NUMBER,
95+
readOnly=True,
96+
)
97+
}
98+
),
99+
Schema(
100+
properties={
101+
'prop1': Schema(
102+
type=DataType.STRING,
103+
writeOnly=True,
104+
)
105+
}
106+
),
107+
],
108+
)
109+
converter = OpenApi30SchemaConverter(schema, Stack(('#', 'schemas', 'model')), ModulePath('root'), None)
110+
model = converter.process_schema().as_type('root')
111+
expected = SchemaClass(
112+
name='model',
113+
base_type=ModelBase,
114+
fields=[
115+
AnnotatedVariable(
116+
'prop1',
117+
AnnotatedType(
118+
NameRef.from_type(Union),
119+
(
120+
AnnotatedType(NameRef.from_type(str)),
121+
AnnotatedType(NameRef.from_type(int)),
122+
),
123+
),
124+
required=True,
125+
alias=None,
126+
)
127+
],
128+
)
129+
assert model == expected

0 commit comments

Comments
 (0)