-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_sqlite.py
More file actions
160 lines (117 loc) · 4.57 KB
/
test_sqlite.py
File metadata and controls
160 lines (117 loc) · 4.57 KB
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
import math
import threading
import pytest
from sqlobject import SQLObject, StringCol, FloatCol
from sqlobject.compat import string_type
from sqlobject.sqlbuilder import Select
from sqlobject.tests.dbtest import getConnection, setupClass, supports
from sqlobject.tests.dbtest import setSQLiteConnectionFactory
from .test_basic import SOTestSO1
try:
connection = getConnection()
except (AttributeError, NameError):
# The module was imported during documentation building
pass
else:
if connection.dbName != "sqlite":
pytestmark = pytest.mark.skip("These tests require SQLite")
class SQLiteFactoryTest(SQLObject):
name = StringCol()
def test_sqlite_factory():
setupClass(SQLiteFactoryTest)
factory = [None]
def SQLiteConnectionFactory(sqlite):
class MyConnection(sqlite.Connection):
pass
factory[0] = MyConnection
return MyConnection
setSQLiteConnectionFactory(SQLiteFactoryTest, SQLiteConnectionFactory)
conn = SQLiteFactoryTest._connection.makeConnection()
assert factory[0]
assert isinstance(conn, factory[0])
def test_sqlite_factory_str():
setupClass(SQLiteFactoryTest)
factory = [None]
def SQLiteConnectionFactory(sqlite):
class MyConnection(sqlite.Connection):
pass
factory[0] = MyConnection
return MyConnection
from sqlobject.sqlite import sqliteconnection
sqliteconnection.SQLiteConnectionFactory = SQLiteConnectionFactory
setSQLiteConnectionFactory(SQLiteFactoryTest, "SQLiteConnectionFactory")
conn = SQLiteFactoryTest._connection.makeConnection()
assert factory[0]
assert isinstance(conn, factory[0])
del sqliteconnection.SQLiteConnectionFactory
def test_sqlite_aggregate():
setupClass(SQLiteFactoryTest)
def SQLiteConnectionFactory(sqlite):
class MyConnection(sqlite.Connection):
def __init__(self, *args, **kwargs):
super(MyConnection, self).__init__(*args, **kwargs)
self.create_aggregate("group_concat", 1, self.group_concat)
class group_concat:
def __init__(self):
self.acc = []
def step(self, value):
if isinstance(value, string_type):
self.acc.append(value)
else:
self.acc.append(str(value))
def finalize(self):
self.acc.sort()
return ", ".join(self.acc)
return MyConnection
setSQLiteConnectionFactory(SQLiteFactoryTest, SQLiteConnectionFactory)
SQLiteFactoryTest(name='sqlobject')
SQLiteFactoryTest(name='sqlbuilder')
assert SQLiteFactoryTest.select(orderBy="name").\
accumulateOne("group_concat", "name") == \
"sqlbuilder, sqlobject"
def do_select():
list(SOTestSO1.select())
def test_sqlite_threaded():
setupClass(SOTestSO1)
t = threading.Thread(target=do_select)
t.start()
t.join()
# This should reuse the same connection as the connection
# made above (at least will with most database drivers, but
# this will cause an error in SQLite):
do_select()
def test_empty_string():
setupClass(SOTestSO1)
test = SOTestSO1(name=None, passwd='')
assert test.name is None
assert test.passwd == ''
def test_memorydb():
if not supports("memorydb"):
pytest.skip("memorydb isn't supported")
if not connection._memory:
pytest.skip("The connection isn't memorydb")
setupClass(SOTestSO1)
connection.close() # create a new connection to an in-memory database
SOTestSO1.setConnection(connection)
SOTestSO1.createTable()
def test_list_databases():
assert 'main' in connection.listDatabases()
def test_list_tables():
setupClass(SOTestSO1)
assert SOTestSO1.sqlmeta.table in connection.listTables()
class SQLiteTruedivTest(SQLObject):
value = FloatCol()
def test_truediv():
setupClass(SQLiteTruedivTest)
if SQLiteTruedivTest._connection.dbName == "sqlite":
def SQLiteConnectionFactory(sqlite):
class MyConnection(sqlite.Connection):
def __init__(self, *args, **kwargs):
super(MyConnection, self).__init__(*args, **kwargs)
self.create_function("floor", 1, math.floor)
return MyConnection
setSQLiteConnectionFactory(SQLiteTruedivTest, SQLiteConnectionFactory)
SQLiteTruedivTest(value=-5.0)
assert SQLiteTruedivTest._connection.queryAll(
SQLiteTruedivTest._connection.sqlrepr(
Select(SQLiteTruedivTest.q.value // 4)))[0][0] == -2