Passed
Push — master ( dc71c6...0c27db )
by Mingyu
01:32
created

build.tests.test_app.decorators.test_validation   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A TestValidateWithPydantic.test_context_property_binding_with_payload_location_json() 0 5 2
A TestValidateWithPydantic.test_context_property_binding_with_payload_location_args() 0 5 2
A TestValidateWithPydantic.test_validation_pass_with_payload_location_args() 0 3 2
A TestValidateWithPydantic.initialize_function_and_call() 0 19 1
A TestValidateWithPydantic.setUp() 0 2 1
A TestValidateWithPydantic.test_validation_pass_with_payload_location_json() 0 3 2
A TestValidateWithPydantic.test_validation_error_with_payload_location_json() 0 4 3
A TestValidateWithPydantic.test_validation_error_with_payload_location_args() 0 4 3
A TestValidateWithPydantic.test_json_force_load() 0 13 2
1
import json
2
from typing import Type
3
4
from pydantic import BaseModel, ValidationError, constr
5
6
from app.context import context_property
7
from app.decorators.validation import validate_with_pydantic, PayloadLocation
8
from tests import BaseTestCase
9
10
11
class TestValidateWithPydantic(BaseTestCase):
12
    """
13
    validate_with_schematics로 데코레이팅된 함수를 만들고,
14
    요청 데이터와 함께 request context를 열어서 이를 호출하는 방식으로 테스트를 진행합니다.
15
    """
16
17
    class TestSchema(BaseModel):
18
        foo: constr(min_length=1)
19
20
    def setUp(self):
21
        super(TestValidateWithPydantic, self).setUp()
22
23
    def initialize_function_and_call(
24
        self,
25
        payload_location: PayloadLocation,
26
        schema: Type[BaseModel] = TestSchema,
27
        json_force_load: bool = False,
28
    ):
29
        """
30
        인자 정보를 통해 `validate_with_pydantic`으로 데코레이팅된 함수를 생성하고, 호출합니다.
31
        """
32
33
        @validate_with_pydantic(
34
            payload_location=payload_location,
35
            model=schema,
36
            json_force_load=json_force_load,
37
        )
38
        def handler():
39
            pass
40
41
        handler()
42
43
    def test_validation_pass_with_payload_location_args(self):
44
        with self.app.test_request_context(query_string={"foo": "bar"}):
45
            self.initialize_function_and_call(PayloadLocation.ARGS)
46
47
    def test_validation_pass_with_payload_location_json(self):
48
        with self.app.test_request_context(json={"foo": "bar"}):
49
            self.initialize_function_and_call(PayloadLocation.JSON)
50
51
    def test_validation_error_with_payload_location_args(self):
52
        with self.app.test_request_context(query_string={"foo": ""}):
53
            with self.assertRaises(ValidationError) as e:
54
                self.initialize_function_and_call(PayloadLocation.ARGS)
55
56
    def test_validation_error_with_payload_location_json(self):
57
        with self.app.test_request_context(json={"foo": ""}):
58
            with self.assertRaises(ValidationError):
59
                self.initialize_function_and_call(PayloadLocation.JSON)
60
61
    def test_context_property_binding_with_payload_location_args(self):
62
        with self.app.test_request_context(query_string={"foo": "bar"}):
63
            self.initialize_function_and_call(PayloadLocation.ARGS)
64
            self.assertEqual(
65
                self.TestSchema(foo="bar"), context_property.request_payload
66
            )
67
68
    def test_context_property_binding_with_payload_location_json(self):
69
        with self.app.test_request_context(json={"foo": "bar"}):
70
            self.initialize_function_and_call(PayloadLocation.JSON)
71
            self.assertEqual(
72
                self.TestSchema(foo="bar"), context_property.request_payload
73
            )
74
75
    def test_json_force_load(self):
76
        with self.app.test_request_context(data=json.dumps({"foo": "bar"})):
77
            # TODO
78
            # self.initialize_function_and_call(
79
            #     PayloadLocation.JSON, json_force_load=False
80
            # )
81
            # print(context_property.request_payload)
82
83
            self.initialize_function_and_call(
84
                PayloadLocation.JSON, json_force_load=True
85
            )
86
            self.assertEqual(
87
                self.TestSchema(foo="bar"), context_property.request_payload
88
            )
89