-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_sql.py
289 lines (268 loc) · 10.3 KB
/
test_sql.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# vim: set et sw=4 sts=4 fileencoding=utf-8:
#
# Copyright (c) 2013-2017 Dave Jones <[email protected]>
# Copyright (c) 2013 Mime Consulting Ltd. <[email protected]>
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import (
unicode_literals,
absolute_import,
print_function,
division,
)
import sqlite3
try:
import ipaddress
except ImportError:
import ipaddr as ipaddress
from collections import namedtuple
import pytest
from lars import sql, datatypes
# XXX Make Py2 str same as Py3
str = type('')
Row = namedtuple('Row', (
'timestamp', 'client', 'method', 'url', 'time_taken', 'status', 'size',
))
@pytest.fixture
def db():
# Construct an in-memory database for testing
return sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES)
@pytest.fixture
def rows():
return [
Row(
datatypes.datetime('2002-06-24 16:40:23'),
datatypes.address('172.224.24.114'),
'POST',
datatypes.url('/Default.htm'),
0.67,
200,
7930,
),
Row(
datatypes.datetime('2002-05-02 20:18:01'),
datatypes.address('172.22.255.255'),
'GET',
datatypes.url('/images/picture.jpg'),
0.1,
302,
16328,
),
Row(
datatypes.datetime('2002-05-29 12:34:56'),
datatypes.address('9.180.235.203'),
'HEAD',
datatypes.url('/images/picture.jpg'),
0.1,
202,
None,
),
]
@pytest.fixture
def rows_null_first():
return [
Row(
datatypes.datetime('2002-06-24 16:40:23'),
datatypes.address('172.224.24.114'),
None,
None,
0.01,
408,
0,
),
Row(
datatypes.datetime('2002-05-02 20:18:01'),
datatypes.address('172.22.255.255'),
'GET',
datatypes.url('/images/picture.jpg'),
0.1,
302,
16328,
),
Row(
datatypes.datetime('2002-05-29 12:34:56'),
datatypes.address('9.180.235.203'),
'HEAD',
datatypes.url('/images/picture.jpg'),
0.1,
202,
None,
),
]
class FakeDbModule(object):
def __init__(self):
self.paramstyle = 'qmark'
self.Error = Exception
def test_exceptions():
exc = sql.SQLError('Something went wrong!', 1)
assert str(exc) == 'Something went wrong! while processing row 1'
exc = sql.SQLError('Something else went wrong!')
assert str(exc) == 'Something else went wrong!'
def test_target_init():
# Test passing in deficient database modules
with pytest.raises(NameError):
db_module = FakeDbModule()
del db_module.paramstyle
sql.SQLTarget(db_module, None, 'foo')
with pytest.raises(NameError):
db_module = FakeDbModule()
del db_module.Error
sql.SQLTarget(db_module, None, 'foo')
with pytest.raises(ValueError):
db_module = FakeDbModule()
sql.SQLTarget(db_module, None, 'foo', commit=0)
with pytest.raises(ValueError):
db_module = FakeDbModule()
sql.SQLTarget(db_module, None, 'foo', insert=0)
with pytest.raises(ValueError):
db_module = FakeDbModule()
sql.SQLTarget(db_module, None, 'foo', commit=100, insert=13)
def test_target_write(db, rows):
# Construct some test rows with appropriate namedtuples
with sql.SQLTarget(sqlite3, db, table='foo', create_table=True) as target:
target.write(rows[0])
target.write(rows[1])
target.write(rows[2])
with pytest.raises(TypeError):
target.write(('foo',))
cursor = db.cursor()
# Ensure the table got created and contains 2 rows which accurately reflect
# the rows we fed in
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 3
cursor.execute("SELECT * FROM foo WHERE method = ?", (rows[0].method,))
data = cursor.fetchall()[0]
assert data[0] == rows[0].timestamp
assert data[1] == str(rows[0].client)
assert data[2] == rows[0].method
assert data[3] == str(rows[0].url)
assert data[4] == rows[0].time_taken
assert data[5] == rows[0].status
assert data[6] == rows[0].size
cursor.execute("SELECT * FROM foo WHERE method = ?", (rows[1].method,))
data = cursor.fetchall()[0]
assert data[0] == rows[1].timestamp
assert data[1] == str(rows[1].client)
assert data[2] == rows[1].method
assert data[3] == str(rows[1].url)
assert data[4] == rows[1].time_taken
assert data[5] == rows[1].status
assert data[6] == rows[1].size
cursor.execute("SELECT * FROM foo WHERE method = ?", (rows[2].method,))
data = cursor.fetchall()[0]
assert data[0] == rows[2].timestamp
assert data[1] == str(rows[2].client)
assert data[2] == rows[2].method
assert data[3] == str(rows[2].url)
assert data[4] == rows[2].time_taken
assert data[5] == rows[2].status
assert data[6] == rows[2].size
def test_target_ip_integers(db, rows):
# Test writing IP addresses as integers instead of strings
with sql.SQLTarget(
sqlite3, db, table='foo', create_table=True,
ip_type='INTEGER') as target:
target.write(rows[0])
target.write(rows[1])
target.write(rows[2])
cursor = db.cursor()
cursor.execute("SELECT client FROM foo WHERE method = ?", (rows[0].method,))
assert cursor.fetchall()[0][0] == int(ipaddress.IPv4Address(rows[0].client))
cursor.execute("SELECT client FROM foo WHERE method = ?", (rows[1].method,))
assert cursor.fetchall()[0][0] == int(ipaddress.IPv4Address(rows[1].client))
cursor.execute("SELECT client FROM foo WHERE method = ?", (rows[2].method,))
assert cursor.fetchall()[0][0] == int(ipaddress.IPv4Address(rows[2].client))
def test_target_auto_drop(db, rows):
# Test auto-DROP with a raise in the case the drop fails
with pytest.raises(sql.SQLError):
with sql.SQLTarget(
sqlite3, db, 'foo', create_table=True, drop_table=True,
ignore_drop_errors=False) as target:
target.write(rows[0])
# Test auto-DROP with ignored errors in case the drop fails
with sql.SQLTarget(sqlite3, db, 'foo', create_table=True, drop_table=True,
ignore_drop_errors=True) as target:
target.write(rows[0])
# Recreate the table, dropping the first
with sql.SQLTarget(
sqlite3, db, 'foo', create_table=True, drop_table=True,
ignore_drop_errors=False) as target:
target.write(rows[0])
target.write(rows[1])
# Check there's only two rows in the table
cursor = db.cursor()
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 2
# Target the same table without recreating it
with sql.SQLTarget(sqlite3, db, 'foo') as target:
target.write(rows[0])
# Check there's now three rows in the table
cursor = db.cursor()
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 3
def test_target_auto_commit(db, rows):
with sql.SQLTarget(
sqlite3, db, 'foo', create_table=True, commit=2) as target:
target.write(rows[0])
target.write(rows[1])
# Check there's only two rows in the table
cursor = db.cursor()
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 2
def test_target_insert_error(db, rows):
try:
with sql.SQLTarget(sqlite3, db, 'foo', create_table=False) as target:
target.write(rows[0])
except Exception as e:
# Check that the exception includes the row that generated the error
assert e.row == rows[0]
assert isinstance(e, sql.SQLError)
def test_target_multi_row_insert(db, rows):
if sqlite3.sqlite_version_info >= (3, 7, 11):
# Test multi-row INSERT if sqlite3 version supports it
cursor = db.cursor()
with sql.SQLTarget(
sqlite3, db, 'foo', create_table=True, insert=2) as target:
target.write(rows[0])
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 0
target.write(rows[1])
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 2
target.write(rows[2])
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 2
cursor.execute('SELECT COUNT(*) FROM foo')
assert cursor.fetchall()[0][0] == 3
cursor.execute('DROP TABLE foo')
try:
with sql.SQLTarget(
sqlite3, db, 'foo', create_table=False, insert=2) as target:
target.write(rows[0])
except Exception as e:
# Check that when inserting multiple rows we don't bother to
# include a specific row in exceptions that occur
assert e.row is None
assert isinstance(e, sql.SQLError)
def test_target_null_on_create(db, rows_null_first, recwarn):
with sql.SQLTarget(sqlite3, db, table='foo', create_table=True) as target:
target.write(rows_null_first[0])
target.write(rows_null_first[1])
assert recwarn.pop(sql.SQLWarning)