forked from nakagami/pydrda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_db2.py
executable file
·346 lines (316 loc) · 11.1 KB
/
test_db2.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
##############################################################################
# The MIT License (MIT)
#
# Copyright (c) 2016-2019 Hajime Nakagami<nakagami@gmail.com>
#
# 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 db2"""
import unittest
import os
import decimal
import datetime
import decimal
import drda
HOST = os.environ.get("DB2_HOST", "localhost")
DATABASE = os.environ.get("DB2_DATABASE", "testdb")
USER = os.environ.get("DB2_USER", "db2inst1")
PASSWORD = os.environ.get("DB2_PASSWORD", "password")
PORT = int(os.environ.get("DB2_PORT", 50000))
SSL_CA_CERTS = os.environ.get("DB2_SSL_CA_CERTS")
class TestBasic(unittest.TestCase):
def setUp(self):
self.connection = drda.connect(
host=HOST,
database=DATABASE,
user=USER,
password=PASSWORD,
port=PORT,
use_ssl=bool(SSL_CA_CERTS),
ssl_ca_certs=SSL_CA_CERTS,
)
cur = self.connection.cursor()
try:
cur.execute("DROP TABLE test_basic")
except drda.OperationalError:
pass
cur.execute("""
CREATE TABLE test_basic (
s varchar(20),
i int,
d1 decimal(2, 1),
d2 decimal(11, 2)
)
""")
self.connection.commit()
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, 0, 0, None),
('I', 497, 4, 4, 0, 0, None),
('D1', 485, 0, 0, 2, 1, None),
('D2', 485, 0, 0, 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, 0, 0, None),
('I', 497, 4, 4, 0, 0, None),
('D1', 485, 0, 0, 2, 1, None),
('D2', 485, 0, 0, 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.00')),
('C', 3, None, None)
])
cur.execute(
"SELECT * FROM test_basic where s=?",
["abcdefghijklmnopq"]
)
self.assertEqual(cur.description, [
('S', 449, 20, 20, 0, 0, None),
('I', 497, 4, 4, 0, 0, None),
('D1', 485, 0, 0, 2, 1, None),
('D2', 485, 0, 0, 11, 2, None)
])
self.assertEqual(cur.fetchall(), [
('abcdefghijklmnopq', 1, decimal.Decimal('1.1'), decimal.Decimal('123456789.12')),
])
cur.execute(
"SELECT * FROM test_basic where s=? and i=?",
["abcdefghijklmnopq", 1]
)
self.assertEqual(cur.fetchall(), [
('abcdefghijklmnopq', 1, decimal.Decimal('1.1'), decimal.Decimal('123456789.12')),
])
cur.execute(
"SELECT * FROM test_basic where i=? and d1=? and d2=?",
[1, decimal.Decimal('1.1'), decimal.Decimal('123456789.12')]
)
self.assertEqual(cur.fetchall(), [
('abcdefghijklmnopq', 1, decimal.Decimal('1.1'), decimal.Decimal('123456789.12')),
])
cur.execute("UPDATE test_basic SET s='abc' WHERE i=?", [1])
cur.execute(
"SELECT * FROM test_basic where i=?",
[1]
)
self.assertEqual(cur.fetchall(), [
('abc', 1, decimal.Decimal('1.1'), decimal.Decimal('123456789.12')),
])
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,
user=USER,
password=PASSWORD,
port=PORT,
use_ssl=bool(SSL_CA_CERTS),
ssl_ca_certs=SSL_CA_CERTS,
)
def test_datetime(self):
cur = self.connection.cursor()
try:
cur.execute("DROP TABLE test_datetime")
except drda.OperationalError:
pass
cur.execute("""
CREATE TABLE test_datetime (
d date,
t time,
dt timestamp
)
""")
cur.execute("""
INSERT INTO test_datetime (d, t, dt) VALUES
('2019-04-30', '12:34:56', '2019-04-30 12:34:56.123456')
""")
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)
)])
cur.execute("SELECT * FROM test_datetime where d=? and t=? and dt=?", [
datetime.date(2019, 4, 30),
datetime.time(12, 34, 56),
datetime.datetime(2019, 4, 30, 12, 34, 56, 123456)
])
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
(-2, -3, -4, -5)
""")
cur.execute("SELECT * FROM test_double")
self.assertEqual(cur.fetchall(), [(-2, -3, -4.0, -5.0)])
cur.execute(
"SELECT * FROM test_double where bi=? and si=? and r=? and d=?",
[-2, -3, -4.0, -5.0]
)
self.assertEqual(cur.fetchall(), [(-2, -3, -4.0, -5.0)])
def test_string(self):
cur = self.connection.cursor()
try:
cur.execute("""
CREATE TABLE test_string (
a character(20),
b varchar(20),
c clob(20),
d graphic(20),
e vargraphic(20)
)
""")
except drda.OperationalError:
pass
def tearDown(self):
self.connection.close()
class TestSecmec(unittest.TestCase):
def test_secmec9(self):
from drda import secmec9
a = secmec9.get_private()
b = secmec9.get_private()
A = secmec9.calc_public(a)
B = secmec9.calc_public(b)
self.assertEqual(
secmec9.calc_session_key(A, b),
secmec9.calc_session_key(B, a)
)
if __name__ == "__main__":
import unittest
unittest.main()