Passed
Push — master ( 622bd5...daf764 )
by Mingyu
57s
created

TestValidateWithPydantic.setUp()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
from typing import Type
2
3
from pydantic import BaseModel, ValidationError, constr
4
5
from app.context import context_property
6
from app.decorators.validation import validate_with_pydantic, PayloadLocation
7
from tests import BaseTestCase
8
9
10
class TestValidateWithPydantic(BaseTestCase):
11
    """
12
    validate_with_schematics로 데코레이팅된 함수를 만들고,
13
    요청 데이터와 함께 request context를 열어서 이를 호출하는 방식으로 테스트를 진행합니다.
14
    """
15
16
    class TestSchema(BaseModel):
17
        foo: constr(min_length=1)
18
19
    def setUp(self):
20
        super(TestValidateWithPydantic, self).setUp()
21
22
    def initialize_function_and_call(
23
        self, payload_location: PayloadLocation, schema: Type[BaseModel] = TestSchema
24
    ):
25
        """
26
        인자 정보를 통해 `validate_with_pydantic`으로 데코레이팅된 함수를 생성하고, 호출합니다.
27
        """
28
29
        @validate_with_pydantic(payload_location, schema)
30
        def handler():
31
            pass
32
33
        handler()
34
35
    def test_validation_pass_with_payload_location_args(self):
36
        with self.app.test_request_context(query_string={"foo": "bar"}):
37
            self.initialize_function_and_call(PayloadLocation.ARGS)
38
39
    def test_validation_pass_with_payload_location_json(self):
40
        with self.app.test_request_context(json={"foo": "bar"}):
41
            self.initialize_function_and_call(PayloadLocation.JSON)
42
43
    def test_validation_error_with_payload_location_args(self):
44
        with self.app.test_request_context(query_string={"foo": ""}):
45
            with self.assertRaises(ValidationError):
46
                self.initialize_function_and_call(PayloadLocation.ARGS)
47
48
    def test_validation_error_with_payload_location_json(self):
49
        with self.app.test_request_context(json={"foo": ""}):
50
            with self.assertRaises(ValidationError):
51
                self.initialize_function_and_call(PayloadLocation.JSON)
52
53
    def test_context_property_binding_with_payload_location_args(self):
54
        with self.app.test_request_context(query_string={"foo": "bar"}):
55
            self.initialize_function_and_call(PayloadLocation.ARGS)
56
            self.assertEqual(
57
                self.TestSchema(foo="bar"), context_property.request_payload
58
            )
59
60
    def test_context_property_binding_with_payload_location_json(self):
61
        with self.app.test_request_context(json={"foo": "bar"}):
62
            self.initialize_function_and_call(PayloadLocation.JSON)
63
            self.assertEqual(
64
                self.TestSchema(foo="bar"), context_property.request_payload
65
            )
66