-
Notifications
You must be signed in to change notification settings - Fork 16
/
bottle_mysql_test.py
239 lines (163 loc) · 6.2 KB
/
bottle_mysql_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import unittest
import bottle
import bottle_mysql
import MySQLdb
from _mysql_exceptions import OperationalError
class BottleMySQLTest(unittest.TestCase):
USER = 'root'
PASS = ''
DBNAME = 'bottle_mysql_test'
SOCKET = '/var/run/mysqld/mysqld.sock'
DBHOST = '127.0.0.1'
# copy from https://github.com/bottlepy/bottle-sqlite
def test_with_keyword(self):
def test(db):
self.assertTrue(isinstance(db, MySQLdb.cursors.BaseCursor))
self._run(test)
def test_without_keyword(self):
def test_1():
pass
self._run(test_1)
def test_2(**kw):
self.assertFalse('db' in kw)
self._run(test_2)
def test_install_conflicts(self):
app = self._app() # install default
app = self._app(app=app, keyword='db2') # install another
def test(db, db2):
self.assertEqual(type(db), type(db2))
self._run(test, app)
def test_echo(self):
def test(db):
db.execute(''' SELECT 1 AS TEST ''')
self.assertEqual({'TEST': 1}, db.fetchone())
# test normal
self._run(test, self._app(dbhost=self.DBHOST))
# test socket
self._run(test, self._app(dbunixsocket=self.SOCKET))
def test_bad_connection(self):
def should_raise(app, word):
try:
def empty(db):
pass
self._run(empty, app)
except OperationalError as e:
self.assertTrue(word in e.args[1])
return
self.fail('should not success')
# bad sock
sock = '/not_exits.sock'
should_raise(self._app(dbunixsocket=sock), sock)
# bad host
host = '255.255.255.255'
should_raise(self._app(dbhost=host), host)
def test_dictrow(self):
def equal_type(t):
def test_type(db):
db.execute(''' SELECT 1 AS TEST ''')
self.assertEqual(type(db.fetchone()), t)
return test_type
# default
self._run(equal_type(dict), app=self._app())
# disable dictrows
self._run(equal_type(tuple), app=self._app(dictrows=False))
def test_timezone(self):
def equal_tz(tz):
def query_timezone(db):
db.execute(''' SELECT @@session.time_zone as TZ;''')
self.assertEqual({'TZ': tz}, db.fetchone())
return query_timezone
tz = '-08:00'
self._run(equal_tz(tz), app=self._app(timezone=tz))
# default tz
self._run(equal_tz('SYSTEM'))
def test_crud(self):
self._create_test_table()
def crud(db):
data = 'test'
# insert
rows = db.execute(''' INSERT INTO `bottle_mysql_test` VALUE (NULL, %s) ''', (data,))
self.assertEqual(rows, 1)
db.execute(''' SELECT last_insert_id() as ID ''')
data_id = db.fetchone()['ID']
self.assertTrue(data_id > 0)
# select
db.execute(''' SELECT * FROM `bottle_mysql_test` WHERE `id` = %s''', (data_id, ))
self.assertEqual({'id': data_id, 'text': data, }, db.fetchone())
# update
data = 'new'
rows = db.execute(''' UPDATE `bottle_mysql_test` SET `text` = %s WHERE `id` = %s''', (data, data_id, ))
self.assertEqual(rows, 1)
db.execute(''' SELECT * FROM `bottle_mysql_test` WHERE `id` = %s''', (data_id, ))
self.assertEqual({'id': data_id, 'text': data, }, db.fetchone())
# delete
rows = db.execute(''' DELETE FROM `bottle_mysql_test` WHERE `id` = %s''', (data_id, ))
self.assertEqual(rows, 1)
self._run(crud)
def test_autocommit(self):
self._create_test_table()
self._run(self._insert_one)
self.assert_records(1)
def test_not_autocommit(self):
self._create_test_table()
app = self._app()
# config with override
@app.get('/', mysql={'autocommit': False})
def insert(db):
self._insert_one(db)
self._request(app, '/')
self.assert_records(0)
# config with construct
self._run(self._insert_one, self._app(autocommit=False))
self.assert_records(0)
def test_commit_on_redirect(self):
self._create_test_table()
def test(db):
self._insert_one(db)
bottle.redirect('/')
self._run(test)
self.assert_records(1)
def test_commit_on_abort(self):
self._create_test_table()
def test(db):
self._insert_one(db)
bottle.abort()
self._run(test)
self.assert_records(0)
def assert_records(self, count):
def query_count(db):
db.execute('''SELECT COUNT(1) AS c FROM `bottle_mysql_test`''')
self.assertEqual({'c': count}, db.fetchone())
self._run(query_count)
def _insert_one(self, db, data='test'):
rows = db.execute(''' INSERT INTO `bottle_mysql_test` VALUE (NULL, %s) ''', (data,))
self.assertEqual(rows, 1)
def _create_test_table(self):
def init(db):
db.execute('''DROP TABLE IF EXISTS `bottle_mysql_test`; ''')
db.execute('''
CREATE TABLE `bottle_mysql_test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`text` varchar(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
''')
self._run(init)
def _run(self, f, app=None):
if not app:
app = self._app()
app.get('/')(f)
self._request(app, '/')
def _app(self, **kwargs):
app = kwargs.pop('app', bottle.Bottle(catchall=False))
kwargs.setdefault('dbuser', self.USER)
kwargs.setdefault('dbpass', self.PASS)
kwargs.setdefault('dbname', self.DBNAME)
kwargs.setdefault('dbhost', self.DBHOST)
plugin = bottle_mysql.Plugin(**kwargs)
app.install(plugin)
return app
def _request(self, app, path, method='GET'):
return app({'PATH_INFO': path, 'REQUEST_METHOD': method}, lambda x, y: None)
if __name__ == '__main__':
unittest.main()