forked from dashawn888/sqlite3worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3worker_test.py
158 lines (136 loc) · 5.68 KB
/
sqlite3worker_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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Palantir Technologies
#
# 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.
"""sqlite3worker test routines."""
__author__ = "Shawn Lee"
__email__ = "[email protected]"
__license__ = "MIT"
import os
import tempfile
import threading
import time
import uuid
import unittest
import sqlite3worker
class Sqlite3WorkerTests(unittest.TestCase): # pylint:disable=R0904
"""Test out the sqlite3worker library."""
def setUp(self): # pylint:disable=D0102
self.tmp_file = tempfile.NamedTemporaryFile(
suffix="pytest", prefix="sqlite"
).name
self.sqlite3worker = sqlite3worker.Sqlite3Worker(self.tmp_file)
# Create sql db.
self.sqlite3worker.execute(
"CREATE TABLE tester (timestamp DATETIME, uuid TEXT)"
)
def tearDown(self): # pylint:disable=D0102
self.sqlite3worker.close()
os.unlink(self.tmp_file)
def test_bad_select(self):
"""Test a bad select query."""
query = "select THIS IS BAD SQL"
self.assertIn(
self.sqlite3worker.execute(query),
[
"Query returned error: select THIS IS BAD SQL: []: no such column: THIS",
"Query returned error: select THIS IS BAD SQL: []: no such column: BAD",
],
)
def test_bad_insert(self):
"""Test a bad insert query."""
query = "insert THIS IS BAD SQL"
self.sqlite3worker.execute(query)
# Give it one second to clear the queue.
if self.sqlite3worker.queue_size != 0:
time.sleep(1)
self.assertEqual(self.sqlite3worker.queue_size, 0)
self.assertEqual(self.sqlite3worker.execute("SELECT * from tester"), [])
def test_valid_insert(self):
"""Test a valid insert and select statement."""
self.sqlite3worker.execute(
"INSERT into tester values (?, ?)", ("2010-01-01 13:00:00", "bow")
)
self.assertEqual(
self.sqlite3worker.execute("SELECT * from tester"),
[("2010-01-01 13:00:00", "bow")],
)
self.sqlite3worker.execute(
"INSERT into tester values (?, ?)", ("2011-02-02 14:14:14", "dog")
)
# Give it one second to clear the queue.
if self.sqlite3worker.queue_size != 0:
time.sleep(1)
self.assertEqual(
self.sqlite3worker.execute("SELECT * from tester"),
[("2010-01-01 13:00:00", "bow"), ("2011-02-02 14:14:14", "dog")],
)
def test_run_after_close(self):
"""Test to make sure all events are cleared after object closed."""
self.sqlite3worker.close()
self.sqlite3worker.execute(
"INSERT into tester values (?, ?)", ("2010-01-01 13:00:00", "bow")
)
self.assertEqual(
self.sqlite3worker.execute("SELECT * from tester"), "Close Called"
)
def test_double_close(self):
"""Make sure double closeing messages properly."""
self.sqlite3worker.close()
self.assertEqual(self.sqlite3worker.close(), "Already Closed")
def test_db_closed_propertly(self):
"""Make sure sqlite object is properly closed out."""
self.sqlite3worker.close()
with self.assertRaises(self.sqlite3worker._sqlite3_conn.ProgrammingError):
self.sqlite3worker._sqlite3_conn.total_changes
def test_many_threads(self):
"""Make sure lots of threads work together."""
class threaded(threading.Thread):
def __init__(self, sqlite_obj):
threading.Thread.__init__(self, name=__name__)
self.sqlite_obj = sqlite_obj
self.daemon = True
self.failed = False
self.completed = False
self.start()
def run(self):
for _ in range(5):
token = str(uuid.uuid4())
self.sqlite_obj.execute(
"INSERT into tester values (?, ?)",
("2010-01-01 13:00:00", token),
)
resp = self.sqlite_obj.execute(
"SELECT * from tester where uuid = ?", (token,)
)
if resp != [("2010-01-01 13:00:00", token)]:
self.failed = True
break
self.completed = True
threads = []
for _ in range(5):
threads.append(threaded(self.sqlite3worker))
for i in range(5):
while not threads[i].completed:
time.sleep(0.1)
self.assertEqual(threads[i].failed, False)
threads[i].join()
if __name__ == "__main__":
unittest.main()