Passed
Push — master ( dae811...ce5c8c )
by Mingyu
01:30
created

build.tests.test_app.decorators.test_validation   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A TestValidate.test_json_validation_error() 0 8 3
A TestValidate.test_query_params_validation() 0 6 2
A TestValidate.test_json_validation() 0 6 2
A TestValidate.test_path_params_validation_error() 0 9 3
A TestValidate.test_path_params_validation() 0 7 2
A TestValidate.setUp() 0 2 1
A TestValidate.initialize_function_and_call() 0 22 3
A TestValidate.test_json_force_load() 0 6 2
A TestValidate.test_query_params_validation_error() 0 8 3
1
import json
2
3
from pydantic import BaseModel, ValidationError, constr
4
5
from app.context import context_property
6
from app.decorators.validation import validate
7
from tests import BaseTestCase
8
9
10
class TestValidate(BaseTestCase):
11
    """
12
    validate로 데코레이팅된 함수를 만들고,
13
    요청 데이터와 함께 request context를 열어서 이를 호출하는 방식으로 테스트를 진행합니다.
14
    """
15
16
    class TestSchema(BaseModel):
17
        foo: constr(min_length=1)
18
19
    def setUp(self):
20
        super(TestValidate, self).setUp()
21
22
    def initialize_function_and_call(
23
        self,
24
        decorator_kwargs: dict = None,
25
        handler_kwargs: dict = None
26
    ):
27
        """
28
        인자 정보를 통해 `validate_with_pydantic`으로 데코레이팅된 함수를 생성하고, 호출합니다.
29
        """
30
31
        if decorator_kwargs is None:
32
            decorator_kwargs = {}
33
34
        if handler_kwargs is None:
35
            handler_kwargs = {}
36
37
        @validate(
38
            **decorator_kwargs
39
        )
40
        def handler(**kwargs):
41
            pass
42
43
        handler(**handler_kwargs)
44
45
    def test_path_params_validation(self):
46
        with self.app.test_request_context():
47
            self.initialize_function_and_call(
48
                decorator_kwargs={'path_params': self.TestSchema},
49
                handler_kwargs={'foo': 'bar'}
50
            )
51
            self.assertEqual(self.TestSchema(foo='bar'), context_property.request_path_params)
52
53
    def test_path_params_validation_error(self):
54
        with self.app.test_request_context():
55
            with self.assertRaises(ValidationError):
56
                self.initialize_function_and_call(
57
                    decorator_kwargs={'path_params': self.TestSchema},
58
                    handler_kwargs={'foo': ''}
59
                )
60
61
            self.assertIsNone(context_property.request_path_params)
62
63
    def test_query_params_validation(self):
64
        with self.app.test_request_context(query_string={'foo': 'bar'}):
65
            self.initialize_function_and_call(
66
                decorator_kwargs={'query_params': self.TestSchema},
67
            )
68
            self.assertEqual(self.TestSchema(foo='bar'), context_property.request_query_params)
69
70
    def test_query_params_validation_error(self):
71
        with self.app.test_request_context(query_string={'foo': ''}):
72
            with self.assertRaises(ValidationError):
73
                self.initialize_function_and_call(
74
                    decorator_kwargs={'query_params': self.TestSchema},
75
                )
76
77
            self.assertIsNone(context_property.request_query_params)
78
79
    def test_json_validation(self):
80
        with self.app.test_request_context(json={'foo': 'bar'}):
81
            self.initialize_function_and_call(
82
                decorator_kwargs={'json': self.TestSchema},
83
            )
84
            self.assertEqual(self.TestSchema(foo='bar'), context_property.request_json)
85
86
    def test_json_validation_error(self):
87
        with self.app.test_request_context(json={'foo': ''}):
88
            with self.assertRaises(ValidationError):
89
                self.initialize_function_and_call(
90
                    decorator_kwargs={'json': self.TestSchema},
91
                )
92
93
            self.assertIsNone(context_property.request_query_params)
94
95
    def test_json_force_load(self):
96
        with self.app.test_request_context(data=json.dumps({"foo": "bar"})):
97
            self.initialize_function_and_call(
98
                decorator_kwargs={'json': self.TestSchema, 'json_force_load': True},
99
            )
100
            self.assertEqual(self.TestSchema(foo='bar'), context_property.request_json)
101