|
1 | 1 | import datetime
|
2 | 2 | import uuid
|
| 3 | +from functools import partial |
3 | 4 |
|
4 | 5 | import pytest
|
5 | 6 | from isodate.tzinfo import UTC
|
6 | 7 | from isodate.tzinfo import FixedOffset
|
7 | 8 | from openapi_schema_validator import OAS30Validator
|
| 9 | +from openapi_schema_validator import OAS31Validator |
8 | 10 |
|
9 | 11 | from openapi_core.spec.paths import Spec
|
10 | 12 | from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
|
|
23 | 25 |
|
24 | 26 |
|
25 | 27 | @pytest.fixture
|
26 |
| -def unmarshaller_factory(): |
| 28 | +def schema_unmarshaller_factory(): |
27 | 29 | def create_unmarshaller(
|
28 |
| - schema, custom_formatters=None, context=UnmarshalContext.REQUEST |
| 30 | + validator, schema, custom_formatters=None, context=None |
29 | 31 | ):
|
30 | 32 | custom_formatters = custom_formatters or {}
|
31 | 33 | return SchemaUnmarshallersFactory(
|
32 |
| - OAS30Validator, |
| 34 | + validator, |
33 | 35 | custom_formatters=custom_formatters,
|
34 | 36 | context=context,
|
35 | 37 | ).create(schema)
|
36 | 38 |
|
37 | 39 | return create_unmarshaller
|
38 | 40 |
|
39 | 41 |
|
40 |
| -class TestUnmarshal: |
| 42 | +class TestOAS30SchemaUnmarshallerUnmarshal: |
| 43 | + @pytest.fixture |
| 44 | + def unmarshaller_factory(self, schema_unmarshaller_factory): |
| 45 | + return partial(schema_unmarshaller_factory, OAS30Validator) |
| 46 | + |
41 | 47 | def test_no_schema(self, unmarshaller_factory):
|
42 | 48 | spec = None
|
43 | 49 | value = "test"
|
@@ -79,7 +85,11 @@ def unmarshal(self, value):
|
79 | 85 | ).unmarshal(value)
|
80 | 86 |
|
81 | 87 |
|
82 |
| -class TestSchemaUnmarshallerCall: |
| 88 | +class TestOAS30SchemaUnmarshallerCall: |
| 89 | + @pytest.fixture |
| 90 | + def unmarshaller_factory(self, schema_unmarshaller_factory): |
| 91 | + return partial(schema_unmarshaller_factory, OAS30Validator) |
| 92 | + |
83 | 93 | def test_deprecated(self, unmarshaller_factory):
|
84 | 94 | schema = {
|
85 | 95 | "type": "string",
|
@@ -824,3 +834,25 @@ def test_additional_properties_list(self, unmarshaller_factory):
|
824 | 834 | assert result == {
|
825 | 835 | "user_ids": [1, 2, 3, 4],
|
826 | 836 | }
|
| 837 | + |
| 838 | + |
| 839 | +class TestOAS31SchemaUnmarshallerCall: |
| 840 | + @pytest.fixture |
| 841 | + def unmarshaller_factory(self, schema_unmarshaller_factory): |
| 842 | + return partial(schema_unmarshaller_factory, OAS31Validator) |
| 843 | + |
| 844 | + def test_null(self, unmarshaller_factory): |
| 845 | + schema = {"type": "null"} |
| 846 | + spec = Spec.from_dict(schema) |
| 847 | + |
| 848 | + result = unmarshaller_factory(spec)(None) |
| 849 | + |
| 850 | + assert result is None |
| 851 | + |
| 852 | + @pytest.mark.parametrize("value", ["string", 2, 3.14, True, [1, 2], {}]) |
| 853 | + def test_null_invalid(self, unmarshaller_factory, value): |
| 854 | + schema = {"type": "null"} |
| 855 | + spec = Spec.from_dict(schema) |
| 856 | + |
| 857 | + with pytest.raises(InvalidSchemaValue): |
| 858 | + unmarshaller_factory(spec)(value) |
0 commit comments