-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.py
201 lines (153 loc) · 5.57 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import unittest
from sqlalchemy import create_engine, Column, Integer, Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import Session
import bottle
from bottle.ext import sqlalchemy
Base = declarative_base()
def accept_only_kwargs(route):
def wrapper(**kwargs):
return route(**kwargs)
return wrapper
class Entity(Base):
__tablename__ = 'entity'
id = Column(Integer, Sequence('id_seq'), primary_key=True)
class AnotherPlugin(object):
def apply(self, callback, route):
def wrapper(*args, **kwargs):
return callback(param=1, *args, **kwargs)
return wrapper
class SQLAlchemyPluginTest(unittest.TestCase):
def setUp(self):
self.engine = create_engine('sqlite:///:memory:')
self.app = bottle.Bottle(catchall=False)
def test_without_metadata(self):
sqlalchemy.Plugin(self.engine, create=False)
def test_without_metadata_create_table_raises(self):
plugin = sqlalchemy.Plugin(self.engine, create=True)
self.assertRaises(bottle.PluginError, self.app.install, plugin)
def test_with_commit(self):
@self.app.get('/')
def test(db):
entity = Entity()
db.add(entity)
self._db = db
self._install_plugin(self.engine, Base.metadata, create=True)
self._request_path('/')
self.assertEqual(self._db.query(Entity).count(), 1)
def test_with_keyword(self):
@self.app.get('/')
def test(db):
self.assertTrue(isinstance(db, Session))
self._install_plugin(self.engine)
self._request_path('/')
def test_without_keyword(self):
@self.app.get('/', sqlalchemy=dict(use_kwargs=True))
def test():
pass
@self.app.get('/2')
@accept_only_kwargs
def test_2(db):
pass
@self.app.get('/3', sqlalchemy=dict(use_kwargs=True))
@accept_only_kwargs
def test_3(db):
pass
self._install_plugin(self.engine)
self._request_path('/')
self.assertRaises(TypeError, self._request_path, '/2')
self._request_path('/3')
def test_py3_kwonly_func(self):
try:
exec("""
@self.app.get('/')
def test(db, *args, kwonly=True):
pass
""")
except SyntaxError:
try:
self.skipTest("Only python>=3.4 support kwonly args.")
except AttributeError:
pass
else:
self._install_plugin(self.engine)
self._request_path('/')
def test_install_conflicts(self):
self._install_plugin(self.engine)
self._install_plugin(self.engine, keyword='db2')
@self.app.get('/')
def test(db, db2):
pass
# I have two plugins working with different names
self._request_path('/')
def test_route_with_view(self):
@self.app.get('/', apply=[accept_only_kwargs])
def test(db):
pass
self.app.install(sqlalchemy.Plugin(self.engine, Base.metadata))
self._request_path('/')
def test_route_based_keyword_config(self):
@self.app.get('/', sqlalchemy=dict(keyword='db_keyword'))
def test(db_keyword):
pass
self._install_plugin(self.engine, create=False)
self._request_path('/')
def test_route_based_commit_config(self):
@self.app.get('/', sqlalchemy=dict(commit=False))
def test(db):
entity = Entity()
db.add(entity)
self._db = db
self._install_plugin(self.engine, Base.metadata, create=True)
self._request_path('/')
self.assertEqual(self._db.query(Entity).count(), 0)
def test_route_based_create_config(self):
@self.app.get('/', sqlalchemy=dict(create=True))
def test(db):
entity = Entity()
db.add(entity)
self._install_plugin(self.engine, Base.metadata, create=False)
self._request_path('/')
def test_commit_on_redirect(self):
@self.app.get('/')
def test(db):
entity = Entity()
db.add(entity)
self._db = db
bottle.redirect('/')
self._install_plugin(self.engine, Base.metadata, create=True)
self._request_path('/')
self.assertEqual(self._db.query(Entity).count(), 1)
def test_commit_on_abort(self):
@self.app.get('/')
def test(db):
entity = Entity()
db.add(entity)
self._db = db
bottle.abort()
self._install_plugin(self.engine, Base.metadata, create=True)
self._request_path('/')
self.assertEqual(self._db.query(Entity).count(), 0)
def test_install_other_plugin_after(self):
self._install_plugin(self.engine)
self.app.install(AnotherPlugin())
@self.app.get('/')
def test(db, param):
self.assertTrue(db is not None)
self.assertEqual(param, 1)
self._request_path('/')
def test_install_other_plugin_before(self):
self.app.install(AnotherPlugin())
self._install_plugin(self.engine)
@self.app.get('/')
def test(db, param):
self.assertTrue(db is not None)
self.assertEqual(param, 1)
self._request_path('/')
def _request_path(self, path, method='GET'):
self.app({'PATH_INFO': path, 'REQUEST_METHOD': method},
lambda x, y: None)
def _install_plugin(self, *args, **kwargs):
self.app.install(sqlalchemy.Plugin(*args, **kwargs))
if __name__ == '__main__':
unittest.main()