forked from nakagami/pydrda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_derby.py
executable file
·255 lines (234 loc) · 8.13 KB
/
test_derby.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
#!/usr/bin/env python3
##############################################################################
# The MIT License (MIT)
#
# Copyright (c) 2016-2019 Hajime Nakagami<[email protected]>
#
# 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.
##############################################################################
"""Tests for derby"""
import unittest
import io
import decimal
import datetime
import decimal
import drda
HOST = 'localhost'
DATABASE = 'testdb;create=true'
PORT = 1527
class TestBasic(unittest.TestCase):
def setUp(self):
self.connection = drda.connect(
host=HOST,
database=DATABASE,
port=PORT,
)
cur = self.connection.cursor()
try:
cur.execute("""
CREATE TABLE test_basic (
s VARCHAR(20),
i int,
d1 decimal(2, 1),
d2 decimal(11, 2)
)
""")
except drda.OperationalError:
pass
cur.execute("DELETE FROM test_basic")
def tearDown(self):
self.connection.close()
def test_basic(self):
cur = self.connection.cursor()
cur.execute("SELECT * FROM test_basic")
self.assertEqual(cur.description, [
('S', 449, 20, 20, 20, 0, None),
('I', 497, 4, 4, 10, 0, None),
('D1', 485, 513, 513, 2, 1, None),
('D2', 485, 2818, 2818, 11, 2, None)
])
self.assertEqual(cur.fetchall(), [])
cur.execute("""
INSERT INTO test_basic (s, i, d1, d2) VALUES
('abcdefghijklmnopq', 1, 1.1, 123456789.12),
('B', 2, 1.2, 2),
('C', 3, null, null)
""")
cur.execute("SELECT * FROM test_basic")
self.assertEqual(cur.description, [
('S', 449, 20, 20, 20, 0, None),
('I', 497, 4, 4, 10, 0, None),
('D1', 485, 513, 513, 2, 1, None),
('D2', 485, 2818, 2818, 11, 2, None)
])
self.assertEqual(cur.fetchall(), [
('abcdefghijklmnopq', 1, decimal.Decimal('1.1'), decimal.Decimal('123456789.12')),
('B', 2, decimal.Decimal('1.2'), decimal.Decimal('2')),
('C', 3, None, None)
])
with self.assertRaises(NotImplementedError):
cur.execute(
"SELECT * FROM test_basic where s=?",
["abcdefghijklmnopq"]
)
def test_error(self):
cur = self.connection.cursor()
with self.assertRaises(drda.OperationalError):
cur.execute("invalid query")
class TestDataType(unittest.TestCase):
def setUp(self):
self.connection = drda.connect(
host=HOST,
database=DATABASE,
port=PORT
)
def test_datetime(self):
cur = self.connection.cursor()
try:
cur.execute("""
CREATE TABLE test_datetime (
d date,
t time,
dt timestamp
)
""")
except drda.OperationalError:
pass
cur.execute("DELETE FROM test_datetime")
cur.execute("""
INSERT INTO test_datetime (d, t, dt) VALUES
('2019-04-30', '12:34:56', '2019-04-30 12:34:56.123456789')
""")
cur.execute("SELECT * FROM test_datetime")
self.assertEqual(cur.fetchall(), [(
datetime.date(2019, 4, 30),
datetime.time(12, 34, 56),
datetime.datetime(2019, 4, 30, 12, 34, 56, 123456)
)])
def test_not_null(self):
cur = self.connection.cursor()
# VARCHAR
try:
cur.execute("""
CREATE TABLE test_varchar_not_null (
s varchar(20) not null
)
""")
except drda.OperationalError:
pass
cur.execute("DELETE FROM test_varchar_not_null")
cur.execute("""
INSERT INTO test_varchar_not_null (s) VALUES
('abcdefghijklmnopq'), ('B'), ('C')
""")
cur.execute("SELECT * FROM test_varchar_not_null")
self.assertEqual(cur.fetchall(), [
('abcdefghijklmnopq', ), ('B', ), ('C', )
])
# LONG VARCHAR
try:
cur.execute("""
CREATE TABLE test_long_varchar_not_null (
s long varchar not null
)
""")
except drda.OperationalError as e:
pass
cur.execute("DELETE FROM test_long_varchar_not_null")
cur.execute("""
INSERT INTO test_long_varchar_not_null (s) VALUES
('abcdefghijklmnopq')
""")
cur.execute("SELECT * FROM test_long_varchar_not_null")
self.assertEqual(cur.fetchall(), [
('abcdefghijklmnopq', )
])
# INTEGER8
try:
cur.execute("""
CREATE TABLE test_bigint_not_null (
bi bigint not null
)
""")
except drda.OperationalError:
pass
cur.execute("DELETE FROM test_bigint_not_null")
cur.execute("""
INSERT INTO test_bigint_not_null (bi) VALUES (-1)
""")
cur.execute("SELECT * FROM test_bigint_not_null")
self.assertEqual(cur.fetchall(), [(-1, )])
# FLOAT8
try:
cur.execute("""
CREATE TABLE test_float8_not_null (
d double not null
)
""")
except drda.OperationalError:
pass
cur.execute("DELETE FROM test_float8_not_null")
cur.execute("""
INSERT INTO test_float8_not_null (d) VALUES (-1)
""")
cur.execute("SELECT * FROM test_float8_not_null")
self.assertEqual(cur.fetchall(), [(-1.0, )])
def test_bool(self):
cur = self.connection.cursor()
try:
cur.execute("""
CREATE TABLE test_bool (
b1 boolean,
b2 boolean not null,
b3 boolean
)
""")
except drda.OperationalError:
pass
cur.execute("DELETE FROM test_bool")
cur.execute("""
INSERT INTO test_bool (b1, b2, b3) VALUES (TRUE, FALSE, NULL)
""")
cur.execute("SELECT * FROM test_bool")
self.assertEqual(cur.fetchall(), [(True, False, None)])
def test_double(self):
cur = self.connection.cursor()
try:
cur.execute("""
CREATE TABLE test_double (
bi bigint,
si smallint,
r real,
d double
)
""")
except drda.OperationalError:
pass
cur.execute("DELETE FROM test_double")
cur.execute("""
INSERT INTO test_double (bi, si, r, d) VALUES
(-1, -1, -1, -1)
""")
cur.execute("SELECT * FROM test_double")
self.assertEqual(cur.fetchall(), [(-1, -1, -1.0, -1.0)])
def tearDown(self):
self.connection.close()
if __name__ == "__main__":
import unittest
unittest.main()