1
|
|
|
from abc import abstractmethod |
2
|
|
|
|
3
|
|
|
from flask import g |
4
|
|
|
|
5
|
|
|
from app.context import _ContextLocalData |
6
|
|
|
from tests import BaseTestCase |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class TestContextLocalData(BaseTestCase): |
10
|
|
|
def setUp(self): |
11
|
|
|
super(TestContextLocalData, self).setUp() |
12
|
|
|
|
13
|
|
|
self.test_context_local_data = _ContextLocalData("test", None) |
14
|
|
|
|
15
|
|
|
@property |
16
|
|
|
@abstractmethod |
17
|
|
|
def proxy_object(self): |
18
|
|
|
pass |
19
|
|
|
|
20
|
|
|
def _test_set(self): |
21
|
|
|
""" |
22
|
|
|
set 테스트 |
23
|
|
|
""" |
24
|
|
|
|
25
|
|
|
with self.app.test_request_context(): |
26
|
|
|
self.test_context_local_data.set(self.proxy_object, 0) |
27
|
|
|
self.assertEqual(0, self.proxy_object.test) |
28
|
|
|
|
29
|
|
|
def _test_set_outside_context(self): |
30
|
|
|
""" |
31
|
|
|
context 바깥에서의 set 테스트 |
32
|
|
|
""" |
33
|
|
|
|
34
|
|
|
with self.assertRaises(RuntimeError): |
35
|
|
|
self.test_context_local_data.set(self.proxy_object, 0) |
36
|
|
|
|
37
|
|
|
def _test_get(self): |
38
|
|
|
""" |
39
|
|
|
get 테스트 |
40
|
|
|
""" |
41
|
|
|
|
42
|
|
|
with self.app.test_request_context(): |
43
|
|
|
self.proxy_object.test = 0 |
44
|
|
|
self.assertEqual(0, self.test_context_local_data.get(self.proxy_object)) |
45
|
|
|
|
46
|
|
|
def _test_get_outside_context(self): |
47
|
|
|
""" |
48
|
|
|
context 바깥에서의 get 테스트 |
49
|
|
|
""" |
50
|
|
|
|
51
|
|
|
with self.assertRaises(RuntimeError): |
52
|
|
|
_ = self.test_context_local_data.get(self.proxy_object) |
53
|
|
|
|
54
|
|
|
def _test_get_default_value(self): |
55
|
|
|
""" |
56
|
|
|
get의 기본값 반환 테스트 |
57
|
|
|
""" |
58
|
|
|
|
59
|
|
|
with self.app.test_request_context(): |
60
|
|
|
self.assertEqual(None, self.test_context_local_data.get(self.proxy_object)) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
class TestContextLocalDataOnGObject(TestContextLocalData): |
64
|
|
|
@property |
65
|
|
|
def proxy_object(self): |
66
|
|
|
return g |
67
|
|
|
|
68
|
|
|
def test_set(self): |
69
|
|
|
self._test_set() |
70
|
|
|
|
71
|
|
|
def test_set_outside_context(self): |
72
|
|
|
self._test_set_outside_context() |
73
|
|
|
|
74
|
|
|
def test_get(self): |
75
|
|
|
self._test_get() |
76
|
|
|
|
77
|
|
|
def test_get_outside_context(self): |
78
|
|
|
self._test_get_outside_context() |
79
|
|
|
|
80
|
|
|
def test_get_default_value(self): |
81
|
|
|
self._test_get_default_value() |
82
|
|
|
|