|
1 |
| -from unittest.mock import Mock |
| 1 | +from typing import Any |
| 2 | +from unittest.mock import Mock, patch |
2 | 3 |
|
3 | 4 | import pytest
|
4 | 5 |
|
|
7 | 8 |
|
8 | 9 |
|
9 | 10 | @pytest.mark.parametrize(
|
10 |
| - "schema", |
| 11 | + "schema,expected", |
11 | 12 | [
|
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"}), |
44 | 66 | ],
|
45 | 67 | )
|
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 | + |
47 | 71 | json_field = CustomJSONField(schema=schema)
|
48 | 72 | extension = CustomJSONFieldSerializerExtension(json_field)
|
49 | 73 | data = extension.map_serializer_field(Mock(), "response")
|
50 |
| - assert data == schema |
| 74 | + assert data == expected |
51 | 75 |
|
52 | 76 |
|
53 | 77 | @pytest.mark.parametrize(
|
54 | 78 | "schema",
|
55 | 79 | [
|
56 |
| - { |
57 |
| - "type": "test", |
58 |
| - }, |
| 80 | + {"type": "unknown_type"}, |
59 | 81 | ],
|
60 | 82 | )
|
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 | + |
62 | 90 | json_field = CustomJSONField(schema=schema)
|
63 | 91 | extension = CustomJSONFieldSerializerExtension(json_field)
|
64 | 92 | data = extension.map_serializer_field(Mock(), "response")
|
|
0 commit comments