Skip to content

Commit ef1b77c

Browse files
committed
Refactored tests
1 parent e9218cd commit ef1b77c

File tree

3 files changed

+86
-45
lines changed

3 files changed

+86
-45
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

+68-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from unittest.mock import Mock
1+
from typing import Any
2+
from unittest.mock import Mock, patch
23

34
import pytest
45

@@ -7,58 +8,85 @@
78

89

910
@pytest.mark.parametrize(
10-
"schema",
11+
"schema,expected",
1112
[
12-
{
13-
"type": "object",
14-
"properties": {"name": {"type": "string"}},
15-
"required": ["name"],
16-
"additionalProperties": True,
17-
},
18-
{
19-
"type": "object",
20-
"properties": {"name": {"type": "string"}},
21-
},
22-
{
23-
"items": {"type": "integer"},
24-
"type": "array",
25-
"maxLength": 1,
26-
"minLength": 1,
27-
},
28-
{
29-
"items": {"type": "integer"},
30-
"type": "array",
31-
},
32-
{
33-
"type": "number",
34-
},
35-
{
36-
"type": "string",
37-
},
38-
{
39-
"type": "integer",
40-
},
41-
{
42-
"type": "boolean",
43-
},
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"}),
4466
],
4567
)
46-
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+
4771
json_field = CustomJSONField(schema=schema)
4872
extension = CustomJSONFieldSerializerExtension(json_field)
4973
data = extension.map_serializer_field(Mock(), "response")
50-
assert data == schema
74+
assert data == expected
5175

5276

5377
@pytest.mark.parametrize(
5478
"schema",
5579
[
56-
{
57-
"type": "test",
58-
},
80+
{"type": "unknown_type"},
5981
],
6082
)
61-
def test_map_serializer_field_invalid_schema(schema: dict):
83+
@patch(
84+
"django_custom_jsonfield.rest_framework.serializers.jsonschema.validators.validator_for",
85+
new=Mock(),
86+
)
87+
def test_map_serializer_field_fallback(schema: Any):
88+
"""Test fallback to string."""
89+
6290
json_field = CustomJSONField(schema=schema)
6391
extension = CustomJSONFieldSerializerExtension(json_field)
6492
data = extension.map_serializer_field(Mock(), "response")

tests/test_serializer_field.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from rest_framework import exceptions
2+
from rest_framework import exceptions, serializers
33

44
from django_custom_jsonfield.rest_framework.serializers import CustomJSONField
55

@@ -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

@@ -26,7 +28,7 @@ class FakeSerializer(serializers.Serializer):
2628

2729
expected_errors = {
2830
"json_field": [
29-
ErrorDetail(
31+
exceptions.ErrorDetail(
3032
string="Value does not match the JSON schema.",
3133
code="invalid_data",
3234
),

0 commit comments

Comments
 (0)