File: test_read_only_write_only.py

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (109 lines) | stat: -rw-r--r-- 3,106 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
from dataclasses import is_dataclass

import pytest

from openapi_core.testing import MockRequest
from openapi_core.testing import MockResponse
from openapi_core.unmarshalling.request.unmarshallers import (
    V30RequestUnmarshaller,
)
from openapi_core.unmarshalling.response.unmarshallers import (
    V30ResponseUnmarshaller,
)
from openapi_core.validation.request.exceptions import InvalidRequestBody
from openapi_core.validation.response.exceptions import InvalidData


@pytest.fixture(scope="class")
def schema_path(schema_path_factory):
    return schema_path_factory.from_file("data/v3.0/read_only_write_only.yaml")


@pytest.fixture(scope="class")
def request_unmarshaller(schema_path):
    return V30RequestUnmarshaller(schema_path)


@pytest.fixture(scope="class")
def response_unmarshaller(schema_path):
    return V30ResponseUnmarshaller(schema_path)


class TestReadOnly:
    def test_write_a_read_only_property(self, request_unmarshaller):
        data = json.dumps(
            {
                "id": 10,
                "name": "Pedro",
            }
        ).encode()

        request = MockRequest(
            host_url="", method="POST", path="/users", data=data
        )

        result = request_unmarshaller.unmarshal(request)

        assert len(result.errors) == 1
        assert type(result.errors[0]) == InvalidRequestBody
        assert result.body is None

    def test_read_only_property_response(self, response_unmarshaller):
        data = json.dumps(
            {
                "id": 10,
                "name": "Pedro",
            }
        ).encode()

        request = MockRequest(host_url="", method="POST", path="/users")

        response = MockResponse(data)

        result = response_unmarshaller.unmarshal(request, response)

        assert not result.errors
        assert is_dataclass(result.data)
        assert result.data.__class__.__name__ == "User"
        assert result.data.id == 10
        assert result.data.name == "Pedro"


class TestWriteOnly:
    def test_write_only_property(self, request_unmarshaller):
        data = json.dumps(
            {
                "name": "Pedro",
                "hidden": False,
            }
        ).encode()

        request = MockRequest(
            host_url="", method="POST", path="/users", data=data
        )

        result = request_unmarshaller.unmarshal(request)

        assert not result.errors
        assert is_dataclass(result.body)
        assert result.body.__class__.__name__ == "User"
        assert result.body.name == "Pedro"
        assert result.body.hidden == False

    def test_read_a_write_only_property(self, response_unmarshaller):
        data = json.dumps(
            {
                "id": 10,
                "name": "Pedro",
                "hidden": True,
            }
        ).encode()

        request = MockRequest(host_url="", method="POST", path="/users")
        response = MockResponse(data)

        result = response_unmarshaller.unmarshal(request, response)

        assert result.errors == [InvalidData()]
        assert result.data is None