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