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
|
|
|
|