forked from tg123/bottle-mysql
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_bottle_pymysql.py
268 lines (184 loc) · 7.2 KB
/
test_bottle_pymysql.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
import os
import unittest
import bottle
import bottle_pymysql
import pymysql
from pymysql import OperationalError
class BottlePyMySQLTest(unittest.TestCase):
USER = os.environ['DBUSER']
PASS = os.environ.get('DBPASS')
DBNAME = os.environ['DBNAME']
SOCKET = os.environ.get('DBSOCKET')
DBHOST = os.environ['DBHOST']
# copy from https://github.com/bottlepy/bottle-sqlite
def test_with_keyword(self):
def test(pymydb):
self.assertTrue(isinstance(pymydb, pymysql.cursors.Cursor))
self._run(test)
def test_without_keyword(self):
def test_1():
pass
self._run(test_1)
def test_2(**kw):
self.assertFalse('pymydb' in kw)
self._run(test_2)
def test_install_conflicts(self):
app = self._app() # install default
app = self._app(app=app, keyword='pymydb2') # install another
def test(pymydb, pymydb2):
self.assertEqual(type(pymydb), type(pymydb2))
self._run(test, app)
def test_echo(self):
def test(pymydb):
pymydb.execute(''' SELECT 1 AS TEST ''')
self.assertEqual({'TEST': 1}, pymydb.fetchone())
# test normal
self._run(test, self._app(dbhost=self.DBHOST))
# test socket
if self.SOCKET:
self._run(test, self._app(dbunixsocket=self.SOCKET))
def _bad_connection_raise(self, app, word, test):
try:
def empty(pymydb):
pass
self._run(empty, app)
except OperationalError as e:
# self.assertTrue(word in e.args[1])
#assert word in e.args[1]
test(word, e)
return
self.fail('should not success')
def test_bad_connection_host(self):
def test(word, e):
assert word in e.args[1]
# bad host
host = '255.255.255.255'
self._bad_connection_raise(self._app(dbhost=host), host, test)
def test_bad_connection_sock(self):
def test(word, e):
assert hasattr(e, 'original_exception')
oex = e.original_exception
assert oex.errno == 2
# bad sock
sock = '/not_exits.sock'
self._bad_connection_raise(self._app(dbunixsocket=sock), sock, test)
def test_dictrow(self):
def equal_type(t):
def test_type(pymydb):
pymydb.execute(''' SELECT 1 AS TEST ''')
self.assertEqual(type(pymydb.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(pymydb):
pymydb.execute(''' SELECT @@session.time_zone as TZ;''')
self.assertEqual({'TZ': tz}, pymydb.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(pymydb):
data = 'test'
# insert
rows = pymydb.execute(''' INSERT INTO `bottle_mysql_test` VALUE (NULL, %s) ''', (data,))
self.assertEqual(rows, 1)
pymydb.execute(''' SELECT last_insert_id() as ID ''')
data_id = pymydb.fetchone()['ID']
self.assertTrue(data_id > 0)
# select
pymydb.execute(''' SELECT * FROM `bottle_mysql_test` WHERE `id` = %s''', (data_id, ))
self.assertEqual({'id': data_id, 'text': data, }, pymydb.fetchone())
# update
data = 'new'
rows = pymydb.execute(''' UPDATE `bottle_mysql_test` SET `text` = %s WHERE `id` = %s''', (data, data_id, ))
self.assertEqual(rows, 1)
pymydb.execute(''' SELECT * FROM `bottle_mysql_test` WHERE `id` = %s''', (data_id, ))
self.assertEqual({'id': data_id, 'text': data, }, pymydb.fetchone())
# delete
rows = pymydb.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('/', pymysql={'autocommit': False})
def insert(pymydb):
self._insert_one(pymydb)
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(pymydb):
self._insert_one(pymydb)
bottle.redirect('/')
self._run(test)
self.assert_records(1)
def test_commit_on_abort(self):
self._create_test_table()
def test(pymydb):
self._insert_one(pymydb)
bottle.abort()
self._run(test)
self.assert_records(0)
def test_escape_string(self):
self._create_test_table()
def test(pymydb):
self._insert_one(pymydb, 'test')
pymydb.execute(
"SELECT COUNT(*) AS c FROM `bottle_mysql_test` WHERE `text` LIKE '%%%s%%'" % pymydb.escape_string('te'))
self.assertEqual({'c': 1}, pymydb.fetchone())
self._run(test)
self.assert_records(1)
def assert_records(self, count):
def query_count(pymydb):
pymydb.execute('''SELECT COUNT(1) AS c FROM `bottle_mysql_test`''')
self.assertEqual({'c': count}, pymydb.fetchone())
self._run(query_count)
def _insert_one(self, pymydb, data='test'):
rows = pymydb.execute(''' INSERT INTO `bottle_mysql_test` VALUE (NULL, %s) ''', (data,))
self.assertEqual(rows, 1)
def _create_test_table(self):
def init(pymydb):
pymydb.execute('''DROP TABLE IF EXISTS `bottle_mysql_test`; ''')
pymydb.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_pymysql.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()