Skip to content

Commit e30992c

Browse files
committed
Refactored tests
1 parent cc4a38c commit e30992c

File tree

3 files changed

+74
-38
lines changed

3 files changed

+74
-38
lines changed

tests/test_model_field.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
],
1919
)
2020
def test_json_schema_invalid(schema: dict):
21+
"""Test Django returns errors if JSON schema is invalid."""
22+
2123
class FakeModel(models.Model):
2224
json_field = CustomJSONField(schema=schema)
2325

@@ -52,7 +54,6 @@ class Meta:
5254
app_label = "test_app"
5355

5456
instance = FakeModel()
55-
5657
assert instance.check() == []
5758

5859

@@ -61,6 +62,8 @@ class Meta:
6162
[10, 10.00, list(), tuple(), set(), "", b"", True, None],
6263
)
6364
def test_schema_type_invalid(schema: Any):
65+
"""Test Django raises exception if JSON schema is not typed correctly."""
66+
6467
with pytest.raises(ValueError) as e:
6568
CustomJSONField(schema=schema)
6669

@@ -78,9 +81,17 @@ def test_schema_type_invalid(schema: Any):
7881
"required": ["name", "age"],
7982
},
8083
),
84+
(
85+
"invalid_string",
86+
{
87+
"const": "custom_string",
88+
},
89+
),
8190
],
8291
)
83-
def test_validate_value_against_schema(value, schema):
92+
def test_validate_value_against_schema(value: Any, schema: Any):
93+
"""Test Django raises exception if value doesn't match JSON schema."""
94+
8495
class FakeModel(models.Model):
8596
json_field = CustomJSONField(schema=schema)
8697

tests/test_openapi_schema.py

+58-35
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,70 @@
88

99

1010
@pytest.mark.parametrize(
11-
"schema",
11+
"schema,expected",
1212
[
13-
{
14-
"type": "object",
15-
"properties": {"name": {"type": "string"}},
16-
"required": ["name"],
17-
"additionalProperties": True,
18-
},
19-
{
20-
"type": "object",
21-
"properties": {"name": {"type": "string"}},
22-
},
23-
{
24-
"items": {"type": "integer"},
25-
"type": "array",
26-
"maxLength": 1,
27-
"minLength": 1,
28-
},
29-
{
30-
"items": {"type": "integer"},
31-
"type": "array",
32-
},
33-
{
34-
"type": "number",
35-
},
36-
{
37-
"type": "string",
38-
},
39-
{
40-
"type": "integer",
41-
},
42-
{
43-
"type": "boolean",
44-
},
13+
(
14+
{
15+
"type": "object",
16+
"properties": {"name": {"type": "string"}},
17+
"required": ["name"],
18+
"additionalProperties": True,
19+
},
20+
{
21+
"type": "object",
22+
"properties": {"name": {"type": "string"}},
23+
"required": ["name"],
24+
"additionalProperties": True,
25+
},
26+
),
27+
(
28+
{
29+
"type": "object",
30+
"properties": {"name": {"type": "string"}},
31+
},
32+
{
33+
"type": "object",
34+
"properties": {"name": {"type": "string"}},
35+
},
36+
),
37+
(
38+
{
39+
"items": {"type": "integer"},
40+
"type": "array",
41+
"maxLength": 1,
42+
"minLength": 1,
43+
},
44+
{
45+
"items": {"type": "integer"},
46+
"type": "array",
47+
"maxLength": 1,
48+
"minLength": 1,
49+
},
50+
),
51+
(
52+
{
53+
"items": {"type": "integer"},
54+
"type": "array",
55+
},
56+
{
57+
"items": {"type": "integer"},
58+
"type": "array",
59+
},
60+
),
61+
# basic types
62+
({"type": "number"}, {"type": "number"}),
63+
({"type": "string"}, {"type": "string"}),
64+
({"type": "integer"}, {"type": "integer"}),
65+
({"type": "boolean"}, {"type": "boolean"}),
4566
],
4667
)
47-
def test_map_serializer_field_ok(schema: dict):
68+
def test_map_serializer_field_ok(schema: Any, expected: Any):
69+
"""Test correct mapping of JSON schema to OpenAPI schema."""
70+
4871
json_field = CustomJSONField(schema=schema)
4972
extension = CustomJSONFieldSerializerExtension(json_field)
5073
data = extension.map_serializer_field(Mock(), "response")
51-
assert data == schema
74+
assert data == expected
5275

5376

5477
@pytest.mark.parametrize(

tests/test_serializer_field.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
),
1818
],
1919
)
20-
def test_validate(value: dict, schema: dict):
20+
def test_validate_invalid_value(value: dict, schema: dict):
21+
"""Test serializer raises an exception if value does not match JSON schema."""
22+
2123
class FakeSerializer(serializers.Serializer):
2224
json_field = CustomJSONField(schema=schema)
2325

0 commit comments

Comments
 (0)