| 1 |
|
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import jpype |
| 6 |
from jpype.types import * |
| 7 |
from jpype import java |
| 8 |
import jpype.dbapi2 as dbapi2 |
| 9 |
import common |
| 10 |
import datetime |
| 11 |
import decimal |
| 12 |
import threading |
| 13 |
|
| 14 |
java = jpype.java |
| 15 |
|
| 16 |
try: |
| 17 |
import zlib |
| 18 |
except ImportError: |
| 19 |
zlib = None |
| 20 |
|
| 21 |
|
| 22 |
db_name = "jdbc:h2:mem:testdb" |
| 23 |
|
| 24 |
def setUpModule(module): |
| 25 |
from common import java_version |
| 26 |
version = java_version() |
| 27 |
if version[0] == 1 and version[1] == 8: |
| 28 |
pytest.skip("jdk8 unsupported", allow_module_level=True) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ConnectTestCase(common.JPypeTestCase): |
| 33 |
def setUp(self): |
| 34 |
common.JPypeTestCase.setUp(self) |
| 35 |
if common.fast: |
| 36 |
raise common.unittest.SkipTest("fast") |
| 37 |
|
| 38 |
def testConnect(self): |
| 39 |
cx = dbapi2.connect(db_name) |
| 40 |
self.assertIsInstance(cx, dbapi2.Connection) |
| 41 |
|
| 42 |
def testClose(self): |
| 43 |
cx = dbapi2.connect(db_name) |
| 44 |
cx.close() |
| 45 |
|
| 46 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 47 |
cx.close() |
| 48 |
|
| 49 |
def testScope(self): |
| 50 |
with dbapi2.connect(db_name) as cx: |
| 51 |
pass |
| 52 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 53 |
cx.close() |
| 54 |
|
| 55 |
def testAdapters(self): |
| 56 |
cx = dbapi2.connect(db_name) |
| 57 |
self.assertEqual(cx.adapters, dbapi2._default_adapters) |
| 58 |
cx = dbapi2.connect(db_name, adapters=None) |
| 59 |
self.assertEqual(cx.adapters, {}) |
| 60 |
m = {} |
| 61 |
cx = dbapi2.connect(db_name, adapters=m) |
| 62 |
self.assertEqual(cx.adapters, m) |
| 63 |
|
| 64 |
def testConverters(self): |
| 65 |
cx = dbapi2.connect(db_name) |
| 66 |
self.assertEqual(cx.converters, dbapi2._default_converters) |
| 67 |
cx = dbapi2.connect(db_name, converters=None) |
| 68 |
self.assertEqual(cx.converters, {}) |
| 69 |
m = {} |
| 70 |
cx = dbapi2.connect(db_name, converters=m) |
| 71 |
self.assertEqual(cx.converters, m) |
| 72 |
|
| 73 |
|
| 74 |
class ConnectionTestCase(common.JPypeTestCase): |
| 75 |
def setUp(self): |
| 76 |
common.JPypeTestCase.setUp(self) |
| 77 |
if common.fast: |
| 78 |
raise common.unittest.SkipTest("fast") |
| 79 |
|
| 80 |
def test_commit(self): |
| 81 |
with dbapi2.connect(db_name) as cx: |
| 82 |
|
| 83 |
cx.commit() |
| 84 |
|
| 85 |
def test_rollback(self): |
| 86 |
with dbapi2.connect(db_name) as cx: |
| 87 |
|
| 88 |
cx.rollback() |
| 89 |
|
| 90 |
def test_cursor(self): |
| 91 |
with dbapi2.connect(db_name) as cx: |
| 92 |
self.assertIsInstance(cx.cursor(), dbapi2.Cursor) |
| 93 |
|
| 94 |
def testAdapters(self): |
| 95 |
with dbapi2.connect(db_name) as cx: |
| 96 |
m = {'foo': 1} |
| 97 |
cx.adapters = m |
| 98 |
self.assertEqual(cx._adapters, m) |
| 99 |
with self.assertRaises(TypeError): |
| 100 |
cx.adapters = object() |
| 101 |
|
| 102 |
def testConverters(self): |
| 103 |
with dbapi2.connect(db_name) as cx: |
| 104 |
m = {'foo': 1} |
| 105 |
cx.converters = m |
| 106 |
self.assertEqual(cx._converters, m) |
| 107 |
with self.assertRaises(TypeError): |
| 108 |
cx.converters = object() |
| 109 |
|
| 110 |
def testClosedClass(self): |
| 111 |
with dbapi2.connect(db_name) as cx: |
| 112 |
with self.assertRaises(AttributeError): |
| 113 |
cx.seters = object() |
| 114 |
|
| 115 |
def testAutocommitFail(self): |
| 116 |
with dbapi2.connect(db_name) as cx: |
| 117 |
cx.autocommit = True |
| 118 |
with self.assertRaises(dbapi2.NotSupportedError): |
| 119 |
cx.commit() |
| 120 |
with self.assertRaises(dbapi2.NotSupportedError): |
| 121 |
cx.rollback() |
| 122 |
|
| 123 |
def test_typeinfo(self): |
| 124 |
with dbapi2.connect(db_name) as cx: |
| 125 |
ti = cx.typeinfo |
| 126 |
self.assertIsInstance(ti, dict) |
| 127 |
for p, v in ti.items(): |
| 128 |
self.assertIsInstance(p, str) |
| 129 |
self.assertIsInstance(v, dbapi2.JDBCType) |
| 130 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 131 |
ti = cx.typeinfo |
| 132 |
|
| 133 |
def test_connection(self): |
| 134 |
with dbapi2.connect(db_name) as cx: |
| 135 |
c = cx.connection |
| 136 |
self.assertIsInstance(c, java.sql.Connection) |
| 137 |
|
| 138 |
def test_getters(self): |
| 139 |
with dbapi2.connect(db_name) as cx: |
| 140 |
cx.getters = dbapi2.GETTERS_BY_NAME |
| 141 |
self.assertEqual(cx._getters, dbapi2.GETTERS_BY_NAME) |
| 142 |
self.assertEqual(cx.getters, dbapi2.GETTERS_BY_NAME) |
| 143 |
cx.getters = dbapi2.GETTERS_BY_TYPE |
| 144 |
self.assertEqual(cx._getters, dbapi2.GETTERS_BY_TYPE) |
| 145 |
self.assertEqual(cx.getters, dbapi2.GETTERS_BY_TYPE) |
| 146 |
|
| 147 |
def test_setters(self): |
| 148 |
with dbapi2.connect(db_name) as cx: |
| 149 |
cx.setters = dbapi2.SETTERS_BY_META |
| 150 |
self.assertEqual(cx._setters, dbapi2.SETTERS_BY_META) |
| 151 |
self.assertEqual(cx.setters, dbapi2.SETTERS_BY_META) |
| 152 |
cx.setters = dbapi2.SETTERS_BY_TYPE |
| 153 |
self.assertEqual(cx._setters, dbapi2.SETTERS_BY_TYPE) |
| 154 |
self.assertEqual(cx.setters, dbapi2.SETTERS_BY_TYPE) |
| 155 |
|
| 156 |
def test_adapters(self): |
| 157 |
with dbapi2.connect(db_name) as cx: |
| 158 |
self.assertEqual(cx.adapters, dbapi2._default_adapters) |
| 159 |
cx.adapters = None |
| 160 |
self.assertEqual(cx.adapters, {}) |
| 161 |
m = {} |
| 162 |
cx.adapters = m |
| 163 |
self.assertEqual(cx.adapters, m) |
| 164 |
with self.assertRaises(dbapi2.InterfaceError): |
| 165 |
cx.adapters = object() |
| 166 |
|
| 167 |
def test_converters(self): |
| 168 |
with dbapi2.connect(db_name) as cx: |
| 169 |
self.assertEqual(cx.converters, dbapi2._default_converters) |
| 170 |
cx.converters = None |
| 171 |
self.assertEqual(cx.converters, {}) |
| 172 |
m = {} |
| 173 |
cx.converters = m |
| 174 |
self.assertEqual(cx.converters, m) |
| 175 |
with self.assertRaises(dbapi2.InterfaceError): |
| 176 |
cx.converters = object() |
| 177 |
|
| 178 |
|
| 179 |
class CursorTestCase(common.JPypeTestCase): |
| 180 |
def setUp(self): |
| 181 |
common.JPypeTestCase.setUp(self) |
| 182 |
if common.fast: |
| 183 |
raise common.unittest.SkipTest("fast") |
| 184 |
|
| 185 |
def tearDown(self): |
| 186 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 187 |
try: |
| 188 |
cur.execute("drop table booze") |
| 189 |
except dbapi2.Error: |
| 190 |
pass |
| 191 |
try: |
| 192 |
cur.execute("drop table barflys") |
| 193 |
except dbapi2.Error: |
| 194 |
pass |
| 195 |
|
| 196 |
def testCursorIsolation(self): |
| 197 |
with dbapi2.connect(db_name) as cx: |
| 198 |
|
| 199 |
|
| 200 |
cur1 = cx.cursor() |
| 201 |
cur2 = cx.cursor() |
| 202 |
cur1.execute("create table booze (name varchar(20))") |
| 203 |
cur1.execute("insert into booze values ('Victoria Bitter')") |
| 204 |
cur2.execute("select name from booze") |
| 205 |
booze = cur2.fetchall() |
| 206 |
self.assertEqual(len(booze), 1) |
| 207 |
self.assertEqual(len(booze[0]), 1) |
| 208 |
self.assertEqual(booze[0][0], "Victoria Bitter") |
| 209 |
|
| 210 |
def testDescription(self): |
| 211 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 212 |
cur.execute("create table booze (name varchar(20))") |
| 213 |
self.assertEqual( |
| 214 |
cur.description, |
| 215 |
None, |
| 216 |
"cursor.description should be none after executing a " |
| 217 |
"statement that can return no rows (such as create table)", |
| 218 |
) |
| 219 |
cur.execute("select name from booze") |
| 220 |
self.assertEqual( |
| 221 |
len(cur.description), 1, "cursor.description describes too many columns" |
| 222 |
) |
| 223 |
self.assertEqual( |
| 224 |
len(cur.description[0]), |
| 225 |
7, |
| 226 |
"cursor.description[x] tuples must have 7 elements", |
| 227 |
) |
| 228 |
self.assertEqual( |
| 229 |
cur.description[0][0].lower(), |
| 230 |
"name", |
| 231 |
"cursor.description[x][0] must return column name", |
| 232 |
) |
| 233 |
self.assertEqual( |
| 234 |
cur.description[0][1], |
| 235 |
dbapi2.STRING, |
| 236 |
"cursor.description[x][1] must return column type. Got %r" |
| 237 |
% cur.description[0][1], |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
cur.execute("create table barflys (name varchar(20))") |
| 242 |
self.assertEqual( |
| 243 |
cur.description, |
| 244 |
None, |
| 245 |
"cursor.description not being set to None when executing " |
| 246 |
"no-result statements (eg. create table)", |
| 247 |
) |
| 248 |
|
| 249 |
def testRowcount(self): |
| 250 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 251 |
cur.execute("create table booze (name varchar(20))") |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
cur.execute("insert into booze values ('Victoria Bitter')") |
| 259 |
self.assertEqual( |
| 260 |
cur.rowcount, 1, |
| 261 |
"cursor.rowcount should == number or rows inserted, or " |
| 262 |
"set to -1 after executing an insert statement", |
| 263 |
) |
| 264 |
cur.execute("select name from booze") |
| 265 |
self.assertEqual( |
| 266 |
cur.rowcount, -1, |
| 267 |
"cursor.rowcount should == number of rows returned, or " |
| 268 |
"set to -1 after executing a select statement", |
| 269 |
) |
| 270 |
cur.execute("create table barflys (name varchar(20))") |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
def testClose(self): |
| 280 |
cx = dbapi2.connect(db_name) |
| 281 |
try: |
| 282 |
cur = cx.cursor() |
| 283 |
finally: |
| 284 |
cx.close() |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
with self.assertRaises(dbapi2.Error): |
| 289 |
cur.execute("create table booze (name varchar(20))") |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
self.assertRaises(dbapi2.Error, cx.commit) |
| 294 |
|
| 295 |
|
| 296 |
self.assertRaises(dbapi2.Error, cx.close) |
| 297 |
|
| 298 |
def test_None(self): |
| 299 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 300 |
cur.execute("create table booze (name varchar(20))") |
| 301 |
cur.execute("insert into booze values (NULL)") |
| 302 |
cur.execute("select name from booze") |
| 303 |
r = cur.fetchall() |
| 304 |
self.assertEqual(len(r), 1) |
| 305 |
self.assertEqual(len(r[0]), 1) |
| 306 |
self.assertEqual(r[0][0], None, "NULL value not returned as None") |
| 307 |
|
| 308 |
def testArraysize(self): |
| 309 |
|
| 310 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 311 |
self.assertTrue( |
| 312 |
hasattr(cur, "arraysize"), "cursor.arraysize must be defined" |
| 313 |
) |
| 314 |
|
| 315 |
def _paraminsert(self, cur): |
| 316 |
cur.execute("create table booze (name varchar(20))") |
| 317 |
cur.execute("insert into booze values ('Victoria Bitter')") |
| 318 |
self.assertTrue(cur.rowcount in (-1, 1)) |
| 319 |
cur.execute("insert into booze values (?)", ("Cooper's",)) |
| 320 |
self.assertTrue(cur.rowcount in (-1, 1)) |
| 321 |
cur.execute("select name from booze") |
| 322 |
res = cur.fetchall() |
| 323 |
self.assertEqual(len(res), 2, "cursor.fetchall returned too few rows") |
| 324 |
beers = [res[0][0], res[1][0]] |
| 325 |
beers.sort() |
| 326 |
self.assertEqual( |
| 327 |
beers[0], |
| 328 |
"Cooper's", |
| 329 |
"cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly", |
| 330 |
) |
| 331 |
self.assertEqual( |
| 332 |
beers[1], |
| 333 |
"Victoria Bitter", |
| 334 |
"cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly", |
| 335 |
) |
| 336 |
|
| 337 |
def test_execute(self): |
| 338 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 339 |
self._paraminsert(cur) |
| 340 |
|
| 341 |
def test_executeBad(self): |
| 342 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 343 |
with self.assertRaises(dbapi2.InterfaceError): |
| 344 |
cur.execute("insert into booze values (?)", object()) |
| 345 |
with self.assertRaises(dbapi2.InterfaceError): |
| 346 |
cur.execute(object()) |
| 347 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 348 |
cur.execute("inert into booze values (?)", [["?"]]) |
| 349 |
|
| 350 |
def test_setinputsizes(self): |
| 351 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 352 |
cur.setinputsizes((25,)) |
| 353 |
self._paraminsert(cur) |
| 354 |
|
| 355 |
def test_setoutputsize_basic(self): |
| 356 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 357 |
cur.setoutputsize(1000) |
| 358 |
cur.setoutputsize(2000, 0) |
| 359 |
self._paraminsert(cur) |
| 360 |
|
| 361 |
def test_executemany(self): |
| 362 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 363 |
cur.execute("create table booze (name varchar(20))") |
| 364 |
largs = [("Cooper's",), ("Boag's",)] |
| 365 |
margs = [{"beer": "Cooper's"}, {"beer": "Boag's"}] |
| 366 |
cur.executemany("insert into booze values (?)", largs) |
| 367 |
self.assertEqual( |
| 368 |
cur.rowcount, 2, |
| 369 |
"insert using cursor.executemany set cursor.rowcount to " |
| 370 |
"incorrect value %r" % cur.rowcount, |
| 371 |
) |
| 372 |
cur.execute("select name from booze") |
| 373 |
res = cur.fetchall() |
| 374 |
self.assertEqual( |
| 375 |
len(res), 2, "cursor.fetchall retrieved incorrect number of rows" |
| 376 |
) |
| 377 |
beers = [res[0][0], res[1][0]] |
| 378 |
beers.sort() |
| 379 |
self.assertEqual(beers[0], "Boag's", "incorrect data retrieved") |
| 380 |
self.assertEqual(beers[1], "Cooper's", "incorrect data retrieved") |
| 381 |
|
| 382 |
def test_executemanyGenerator(self): |
| 383 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 384 |
def mygen(ls): |
| 385 |
for i in ls: |
| 386 |
yield [i] |
| 387 |
cu.execute("create table booze (name varchar(20))") |
| 388 |
cu.executemany("insert into booze values (?)", mygen(self.samples)) |
| 389 |
cu.execute("select * from booze") |
| 390 |
f = cu.fetchall() |
| 391 |
for i, v in enumerate(self.samples): |
| 392 |
self.assertEqual(f[i][0], v) |
| 393 |
|
| 394 |
def test_executemanyIterator(self): |
| 395 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 396 |
class myiter: |
| 397 |
def __init__(self, ls): |
| 398 |
self.ls = ls |
| 399 |
self.i = 0 |
| 400 |
|
| 401 |
def __iter__(self): |
| 402 |
return self |
| 403 |
|
| 404 |
def __next__(self): |
| 405 |
if self.i == len(self.ls): |
| 406 |
raise StopIteration |
| 407 |
rc = [self.ls[self.i]] |
| 408 |
self.i += 1 |
| 409 |
return rc |
| 410 |
cu.execute("create table booze (name varchar(20))") |
| 411 |
cu.executemany("insert into booze values (?)", myiter(self.samples)) |
| 412 |
cu.execute("select * from booze") |
| 413 |
f = cu.fetchall() |
| 414 |
for i, v in enumerate(self.samples): |
| 415 |
self.assertEqual(f[i][0], v) |
| 416 |
|
| 417 |
def test_executemanyBad(self): |
| 418 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 419 |
cu.execute("create table booze (name varchar(20))") |
| 420 |
with self.assertRaises(TypeError): |
| 421 |
cu.executemany("insert into booze values (?)", object()) |
| 422 |
with self.assertRaises(dbapi2.InterfaceError): |
| 423 |
cu.executemany("insert into booze values (?)", object()) |
| 424 |
with self.assertRaises(dbapi2.InterfaceError): |
| 425 |
cu.executemany(object(), []) |
| 426 |
cu.executemany("insert into booze values (?)", None) |
| 427 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 428 |
cu.executemany("inert into booze values (?)", [['?']]) |
| 429 |
|
| 430 |
def test_fetchone(self): |
| 431 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 432 |
|
| 433 |
|
| 434 |
self.assertRaises(dbapi2.ProgrammingError, cur.fetchone) |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
cur.execute("create table booze (name varchar(20))") |
| 439 |
self.assertRaises(dbapi2.Error, cur.fetchone) |
| 440 |
|
| 441 |
cur.execute("select name from booze") |
| 442 |
self.assertEqual( |
| 443 |
cur.fetchone(), |
| 444 |
None, |
| 445 |
"cursor.fetchone should return None if a query retrieves " "no rows", |
| 446 |
) |
| 447 |
self.assertEqual(cur.rowcount, -1) |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
cur.execute("insert into booze values ('Victoria Bitter')") |
| 452 |
self.assertRaises(dbapi2.Error, cur.fetchone) |
| 453 |
|
| 454 |
cur.execute("select name from booze") |
| 455 |
r = cur.fetchone() |
| 456 |
self.assertEqual( |
| 457 |
len(r), 1, "cursor.fetchone should have retrieved a single row" |
| 458 |
) |
| 459 |
self.assertEqual( |
| 460 |
r[0], "Victoria Bitter", "cursor.fetchone retrieved incorrect data" |
| 461 |
) |
| 462 |
self.assertEqual( |
| 463 |
cur.fetchone(), |
| 464 |
None, |
| 465 |
"cursor.fetchone should return None if no more rows available", |
| 466 |
) |
| 467 |
self.assertEqual(cur.rowcount, -1) |
| 468 |
|
| 469 |
samples = [ |
| 470 |
"Carlton Cold", |
| 471 |
"Carlton Draft", |
| 472 |
"Mountain Goat", |
| 473 |
"Redback", |
| 474 |
"Victoria Bitter", |
| 475 |
"XXXX", |
| 476 |
] |
| 477 |
|
| 478 |
def _populate(self): |
| 479 |
""" Return a list of sql commands to setup the DB for the fetch |
| 480 |
tests. |
| 481 |
""" |
| 482 |
populate = [ |
| 483 |
"insert into booze values ('{}')".format(s) |
| 484 |
for s in self.samples |
| 485 |
] |
| 486 |
return populate |
| 487 |
|
| 488 |
def test_fetchmany(self): |
| 489 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
self.assertRaises(dbapi2.ProgrammingError, cur.fetchmany, 4) |
| 494 |
|
| 495 |
cur.execute("create table booze (name varchar(20))") |
| 496 |
for sql in self._populate(): |
| 497 |
cur.execute(sql) |
| 498 |
|
| 499 |
cur.execute("select name from booze") |
| 500 |
r = cur.fetchmany() |
| 501 |
self.assertEqual( |
| 502 |
len(r), |
| 503 |
1, |
| 504 |
"cursor.fetchmany retrieved incorrect number of rows, " |
| 505 |
"default of arraysize is one.", |
| 506 |
) |
| 507 |
cur.arraysize = 10 |
| 508 |
r = cur.fetchmany(3) |
| 509 |
self.assertEqual( |
| 510 |
len(r), 3, "cursor.fetchmany retrieved incorrect number of rows" |
| 511 |
) |
| 512 |
r = cur.fetchmany(4) |
| 513 |
self.assertEqual( |
| 514 |
len(r), 2, "cursor.fetchmany retrieved incorrect number of rows" |
| 515 |
) |
| 516 |
r = cur.fetchmany(4) |
| 517 |
self.assertEqual( |
| 518 |
len(r), |
| 519 |
0, |
| 520 |
"cursor.fetchmany should return an empty sequence after " |
| 521 |
"results are exhausted", |
| 522 |
) |
| 523 |
self.assertEqual(cur.rowcount, -1) |
| 524 |
|
| 525 |
|
| 526 |
cur.arraysize = 4 |
| 527 |
cur.execute("select name from booze") |
| 528 |
r = cur.fetchmany() |
| 529 |
self.assertEqual( |
| 530 |
len(r), 4, "cursor.arraysize not being honoured by fetchmany" |
| 531 |
) |
| 532 |
r = cur.fetchmany() |
| 533 |
self.assertEqual(len(r), 2) |
| 534 |
r = cur.fetchmany() |
| 535 |
self.assertEqual(len(r), 0) |
| 536 |
self.assertEqual(cur.rowcount, -1) |
| 537 |
|
| 538 |
cur.arraysize = 6 |
| 539 |
cur.execute("select name from booze") |
| 540 |
rows = cur.fetchmany() |
| 541 |
self.assertTrue(cur.rowcount in (-1, 6)) |
| 542 |
self.assertEqual(len(rows), 6) |
| 543 |
self.assertEqual(len(rows), 6) |
| 544 |
rows = [r[0] for r in rows] |
| 545 |
rows.sort() |
| 546 |
|
| 547 |
|
| 548 |
for i in range(0, 6): |
| 549 |
self.assertEqual( |
| 550 |
rows[i], |
| 551 |
self.samples[i], |
| 552 |
"incorrect data retrieved by cursor.fetchmany", |
| 553 |
) |
| 554 |
|
| 555 |
rows = cur.fetchmany() |
| 556 |
self.assertEqual( |
| 557 |
len(rows), |
| 558 |
0, |
| 559 |
"cursor.fetchmany should return an empty sequence if " |
| 560 |
"called after the whole result set has been fetched", |
| 561 |
) |
| 562 |
self.assertEqual(cur.rowcount, -1) |
| 563 |
|
| 564 |
cur.execute("create table barflys (name varchar(20))") |
| 565 |
cur.execute("select name from barflys") |
| 566 |
r = cur.fetchmany() |
| 567 |
self.assertEqual( |
| 568 |
len(r), |
| 569 |
0, |
| 570 |
"cursor.fetchmany should return an empty sequence if " |
| 571 |
"query retrieved no rows", |
| 572 |
) |
| 573 |
|
| 574 |
def test_fetchall(self): |
| 575 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
self.assertRaises(dbapi2.Error, cur.fetchall) |
| 580 |
|
| 581 |
cur.execute("create table booze (name varchar(20))") |
| 582 |
for sql in self._populate(): |
| 583 |
cur.execute(sql) |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
self.assertRaises(dbapi2.ProgrammingError, cur.fetchall) |
| 588 |
|
| 589 |
cur.execute("select name from booze") |
| 590 |
rows = cur.fetchall() |
| 591 |
self.assertEqual(cur.rowcount, -1) |
| 592 |
self.assertEqual( |
| 593 |
len(rows), |
| 594 |
len(self.samples), |
| 595 |
"cursor.fetchall did not retrieve all rows", |
| 596 |
) |
| 597 |
rows = [r[0] for r in rows] |
| 598 |
rows.sort() |
| 599 |
for i in range(0, len(self.samples)): |
| 600 |
self.assertEqual( |
| 601 |
rows[i], self.samples[i], "cursor.fetchall retrieved incorrect rows" |
| 602 |
) |
| 603 |
rows = cur.fetchall() |
| 604 |
self.assertEqual( |
| 605 |
len(rows), |
| 606 |
0, |
| 607 |
"cursor.fetchall should return an empty list if called " |
| 608 |
"after the whole result set has been fetched", |
| 609 |
) |
| 610 |
self.assertEqual(cur.rowcount, -1) |
| 611 |
|
| 612 |
cur.execute("create table barflys (name varchar(20))") |
| 613 |
cur.execute("select name from barflys") |
| 614 |
rows = cur.fetchall() |
| 615 |
self.assertEqual(cur.rowcount, -1) |
| 616 |
self.assertEqual( |
| 617 |
len(rows), |
| 618 |
0, |
| 619 |
"cursor.fetchall should return an empty list if " |
| 620 |
"a select query returns no rows", |
| 621 |
) |
| 622 |
|
| 623 |
def test_mixedfetch(self): |
| 624 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 625 |
cur.execute("create table booze (name varchar(20))") |
| 626 |
for sql in self._populate(): |
| 627 |
cur.execute(sql) |
| 628 |
|
| 629 |
cur.execute("select name from booze") |
| 630 |
self.assertEqual(cur.rowcount, -1) |
| 631 |
rows1 = cur.fetchone() |
| 632 |
rows23 = cur.fetchmany(2) |
| 633 |
rows4 = cur.fetchone() |
| 634 |
rows56 = cur.fetchall() |
| 635 |
self.assertEqual(cur.rowcount, -1) |
| 636 |
self.assertEqual( |
| 637 |
len(rows23), 2, "fetchmany returned incorrect number of rows" |
| 638 |
) |
| 639 |
self.assertEqual( |
| 640 |
len(rows56), 2, "fetchall returned incorrect number of rows" |
| 641 |
) |
| 642 |
|
| 643 |
rows = [rows1[0]] |
| 644 |
rows.extend([rows23[0][0], rows23[1][0]]) |
| 645 |
rows.append(rows4[0]) |
| 646 |
rows.extend([rows56[0][0], rows56[1][0]]) |
| 647 |
rows.sort() |
| 648 |
for i in range(0, len(self.samples)): |
| 649 |
self.assertEqual( |
| 650 |
rows[i], self.samples[i], "incorrect data retrieved or inserted" |
| 651 |
) |
| 652 |
|
| 653 |
def test_nextset(self): |
| 654 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 655 |
cu.execute("create table booze (name varchar(20))") |
| 656 |
booze = ["Wiskey", ] |
| 657 |
cu.execute("insert into booze(name) values (?)", booze) |
| 658 |
self.assertEqual(cu.rowcount, 1) |
| 659 |
cu.execute("select * from booze; select * from booze") |
| 660 |
self.assertEqual(cu.fetchone(), booze) |
| 661 |
nxt = cu.nextset() |
| 662 |
self.assertTrue(nxt in (True, None)) |
| 663 |
|
| 664 |
if nxt: |
| 665 |
self.assertEqual(cu.fetchone(), booze) |
| 666 |
|
| 667 |
def test_lastrowid(self): |
| 668 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 669 |
cu.execute("create table booze(id identity auto_increment not null, name varchar(255), PRIMARY KEY (id))") |
| 670 |
cu.execute("insert into booze(name) values('hello')", keys=True) |
| 671 |
id0 = cu.lastrowid |
| 672 |
self.assertIsInstance(id0, int) |
| 673 |
|
| 674 |
id1 = cu.lastrowid |
| 675 |
self.assertIsInstance(id1, int) |
| 676 |
cx.commit() |
| 677 |
cu.execute("insert into booze(name) values('there')", keys=True) |
| 678 |
id2 = cu.lastrowid |
| 679 |
self.assertIsInstance(id2, int) |
| 680 |
cu.execute("select * from booze") |
| 681 |
f = cu.fetchall() |
| 682 |
self.assertEqual(cu.lastrowid, None) |
| 683 |
self.assertEqual(f[0][0], id0) |
| 684 |
self.assertEqual(f[1][0], id2) |
| 685 |
|
| 686 |
def test_lastrowidMany(self): |
| 687 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 688 |
cu.execute("create table booze(id identity auto_increment not null, name varchar(255), PRIMARY KEY (id))") |
| 689 |
cu.executemany("insert into booze(name) values(?)", [['Redback'], ['Fat Yak']], keys=True) |
| 690 |
ids = cu.lastrowid |
| 691 |
self.assertIsInstance(ids, list) |
| 692 |
self.assertEqual(len(ids), 2) |
| 693 |
cu.execute("select * from booze") |
| 694 |
f = cu.fetchall() |
| 695 |
self.assertEqual(f[0][0], ids[0]) |
| 696 |
self.assertEqual(f[1][0], ids[1]) |
| 697 |
|
| 698 |
def test_iter(self): |
| 699 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 700 |
ls = [['Tooheys Old'], ['Nine Tales Original Amber Ale'], ['Dogbolter']] |
| 701 |
cu.execute("create table booze(name varchar(255))") |
| 702 |
cu.executemany("insert into booze(name) values(?)", ls, keys=True) |
| 703 |
|
| 704 |
|
| 705 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 706 |
for booze in cu: |
| 707 |
pass |
| 708 |
cu.execute("select * from booze") |
| 709 |
for i, booze in enumerate(cu): |
| 710 |
self.assertEqual(ls[i], booze) |
| 711 |
|
| 712 |
def testNoAdapter(self): |
| 713 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 714 |
cu.execute("create table booze(name varchar(50))") |
| 715 |
with self.assertRaises(dbapi2.InterfaceError): |
| 716 |
cu.execute("insert into booze(name) values(?)", [object()]) |
| 717 |
|
| 718 |
def testBadParameters(self): |
| 719 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 720 |
cu.execute("create table booze(name varchar(50))") |
| 721 |
with self.assertRaises(dbapi2.InterfaceError): |
| 722 |
cu.execute("insert into booze(name) values(?)", object()) |
| 723 |
|
| 724 |
def test_callproc(self): |
| 725 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 726 |
r = cu.callproc("lower", ("FOO",)) |
| 727 |
self.assertEqual(len(r), 1) |
| 728 |
self.assertEqual(r[0], "FOO") |
| 729 |
r = cu.fetchall() |
| 730 |
self.assertEqual(len(r), 1) |
| 731 |
self.assertEqual(len(r[0]), 1) |
| 732 |
self.assertEqual(r[0][0], "foo") |
| 733 |
|
| 734 |
def test_callprocBad(self): |
| 735 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 736 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 737 |
r = cu.callproc("not_there", ("FOO",)) |
| 738 |
with self.assertRaises(dbapi2.InterfaceError): |
| 739 |
r = cu.callproc(object(), ("FOO",)) |
| 740 |
with self.assertRaises(dbapi2.InterfaceError): |
| 741 |
r = cu.callproc("lower", (object(),)) |
| 742 |
with self.assertRaises(dbapi2.InterfaceError): |
| 743 |
r = cu.callproc("lower", object()) |
| 744 |
|
| 745 |
def test_close(self): |
| 746 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 747 |
cu.close() |
| 748 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 749 |
cu.close() |
| 750 |
|
| 751 |
def test_parameters(self): |
| 752 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 753 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 754 |
p = cu.parameters |
| 755 |
cu.execute("create table booze(name varchar(50))") |
| 756 |
cu.execute("insert into booze(name) values(?)", ('Hahn Super Dry',)) |
| 757 |
p = cu.parameters |
| 758 |
self.assertEqual(p[0][0:2], ('VARCHAR', dbapi2.VARCHAR)) |
| 759 |
|
| 760 |
def test_resultSet(self): |
| 761 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 762 |
cu.execute("create table booze(name varchar(50))") |
| 763 |
cu.execute("insert into booze(name) values(?)", ('Hahn Super Dry',)) |
| 764 |
cu.execute("select * from booze") |
| 765 |
rs = cu.resultSet |
| 766 |
self.assertIsInstance(rs, java.sql.ResultSet) |
| 767 |
|
| 768 |
def test_fetchClosed(self): |
| 769 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 770 |
cu.execute("create table booze(name varchar(50))") |
| 771 |
cu.execute("insert into booze(name) values(?)", ('Hahn Super Dry',)) |
| 772 |
cu.execute("select * from booze") |
| 773 |
cu.close() |
| 774 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 775 |
cu.fetchone() |
| 776 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 777 |
cu.fetchmany() |
| 778 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 779 |
cu.fetchall() |
| 780 |
self.assertEqual(cu.resultSet, None) |
| 781 |
|
| 782 |
def test_executeBadParameters(self): |
| 783 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 784 |
def mygen(ls): |
| 785 |
for i in ls: |
| 786 |
yield i |
| 787 |
cu.execute("create table booze(name varchar(50), price integer)") |
| 788 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 789 |
cu.executemany("insert into booze(name,price) values(?,?)", [('Hahn Super Dry', 2), ('Coors',)]) |
| 790 |
with self.assertRaises(dbapi2.InterfaceError): |
| 791 |
cu.execute("insert into booze(name,price) values(?,?)", {'name': 'Budweiser', 'price': 10}) |
| 792 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 793 |
cu.executemany("insert into booze(name,price) values(?,?)", mygen([['Budweiser']])) |
| 794 |
cu.execute("insert into booze(name,price) values(?,?)", mygen(['Pabst Blue Robot', 20000])) |
| 795 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 796 |
cu.execute("insert into booze(name,price) values(?,?)", mygen(['Budweiser'])) |
| 797 |
with self.assertRaises(dbapi2.InterfaceError): |
| 798 |
cu.execute("insert into booze(name,price) values(?,?)", 'Budweiser') |
| 799 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 800 |
cu.execute("insert into booze(name,price) values(?,?)", mygen(['Budweiser', 2, 'no'])) |
| 801 |
with self.assertRaises(dbapi2.InterfaceError): |
| 802 |
cu.execute("insert into booze(name,price) values(?,?)", object()) |
| 803 |
|
| 804 |
|
| 805 |
@common.unittest.skipUnless(zlib, "requires zlib") |
| 806 |
class AdapterTestCase(common.JPypeTestCase): |
| 807 |
def setUp(self): |
| 808 |
common.JPypeTestCase.setUp(self) |
| 809 |
if common.fast: |
| 810 |
raise common.unittest.SkipTest("fast") |
| 811 |
self.testdata = b"abcdefg" * 10 |
| 812 |
self.params = memoryview(zlib.compress(self.testdata)) |
| 813 |
|
| 814 |
def tearDown(self): |
| 815 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 816 |
try: |
| 817 |
cur.execute("drop table test") |
| 818 |
except dbapi2.Error: |
| 819 |
pass |
| 820 |
|
| 821 |
def testOnConnector(self): |
| 822 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 823 |
cu.execute("create table test(name varchar(255))") |
| 824 |
cx.adapters[object] = lambda x: "none" |
| 825 |
cu.execute("insert into test(name) values(?)", [object()]) |
| 826 |
cu.execute("select * from test") |
| 827 |
f = cu.fetchall() |
| 828 |
self.assertEqual(f[0][0], "none") |
| 829 |
|
| 830 |
|
| 831 |
@common.unittest.skipUnless(zlib, "requires zlib") |
| 832 |
class ConverterTestCase(common.JPypeTestCase): |
| 833 |
def setUp(self): |
| 834 |
common.JPypeTestCase.setUp(self) |
| 835 |
if common.fast: |
| 836 |
raise common.unittest.SkipTest("fast") |
| 837 |
self.testdata = b"abcdefg" * 10 |
| 838 |
self.params = memoryview(zlib.compress(self.testdata)) |
| 839 |
|
| 840 |
def tearDown(self): |
| 841 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 842 |
try: |
| 843 |
cur.execute("drop table test") |
| 844 |
except dbapi2.Error: |
| 845 |
pass |
| 846 |
|
| 847 |
@staticmethod |
| 848 |
def convert(s): |
| 849 |
return zlib.decompress(s) |
| 850 |
|
| 851 |
def testConvertersBad(self): |
| 852 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 853 |
cu.execute("create table test(name varchar(20))") |
| 854 |
cu.executemany("insert into test(name) values(?)", [['alice'], ['bob'], ['charlie']]) |
| 855 |
cu.execute("select name from test") |
| 856 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 857 |
cu.fetchone(converters=[]) |
| 858 |
cu.execute("select name from test") |
| 859 |
with self.assertRaises(dbapi2.InterfaceError): |
| 860 |
cu.fetchone(converters=[int]) |
| 861 |
cu.execute("select name from test") |
| 862 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 863 |
cu.fetchone(converters=[str, str]) |
| 864 |
|
| 865 |
def testConvertersNone(self): |
| 866 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 867 |
cu.execute("create table test(name varchar(20))") |
| 868 |
cu.executemany("insert into test(name) values(?)", [['alice'], ['bob'], ['charlie']]) |
| 869 |
cu.execute("select name from test") |
| 870 |
f = cu.fetchone(converters=None) |
| 871 |
self.assertIsInstance(f[0], java.lang.String) |
| 872 |
cu.execute("select name from test") |
| 873 |
|
| 874 |
def testConvertersPositional(self): |
| 875 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 876 |
cu.execute("create table test(name varchar(20))") |
| 877 |
cu.executemany("insert into test(name) values(?)", [['alice'], ['bob'], ['charlie']]) |
| 878 |
cu.execute("select name from test") |
| 879 |
f = cu.fetchone(converters=[str]) |
| 880 |
self.assertIsInstance(f[0], str) |
| 881 |
cu.execute("select name from test") |
| 882 |
|
| 883 |
def test_fetchoneTypesPositional(self): |
| 884 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 885 |
cu.execute("create table test(name varchar(20))") |
| 886 |
cu.executemany("insert into test(name) values(?)", [['alice'], ['bob'], ['charlie']]) |
| 887 |
cu.execute("select name from test") |
| 888 |
f = cu.fetchone(types=[dbapi2.STRING]) |
| 889 |
self.assertIsInstance(f[0], str) |
| 890 |
|
| 891 |
def test_fetchmanyTypesPositional(self): |
| 892 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 893 |
cu.execute("create table test(name varchar(20))") |
| 894 |
cu.executemany("insert into test(name) values(?)", [['alice'], ['bob'], ['charlie']]) |
| 895 |
cu.execute("select name from test") |
| 896 |
f = cu.fetchmany(types=[dbapi2.STRING]) |
| 897 |
self.assertIsInstance(f[0][0], str) |
| 898 |
|
| 899 |
def test_fetchallTypesPositional(self): |
| 900 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 901 |
cu.execute("create table test(name varchar(20))") |
| 902 |
cu.executemany("insert into test(name) values(?)", [['alice'], ['bob'], ['charlie']]) |
| 903 |
cu.execute("select name from test") |
| 904 |
f = cu.fetchall(types=[dbapi2.STRING]) |
| 905 |
self.assertIsInstance(f[0][0], str) |
| 906 |
|
| 907 |
def testTypesPositionalBAD(self): |
| 908 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 909 |
cu.execute("create table test(name varchar(20))") |
| 910 |
cu.executemany("insert into test(name) values(?)", [['alice'], ['bob'], ['charlie']]) |
| 911 |
cu.execute("select name from test") |
| 912 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 913 |
f = cu.fetchone(types=[dbapi2.STRING, dbapi2.STRING]) |
| 914 |
with self.assertRaises(dbapi2.ProgrammingError): |
| 915 |
f = cu.fetchone(types=[]) |
| 916 |
|
| 917 |
|
| 918 |
@common.unittest.skipUnless(zlib, "requires zlib") |
| 919 |
class GettersTestCase(common.JPypeTestCase): |
| 920 |
def setUp(self): |
| 921 |
common.JPypeTestCase.setUp(self) |
| 922 |
if common.fast: |
| 923 |
raise common.unittest.SkipTest("fast") |
| 924 |
self.testdata = b"abcdefg" * 10 |
| 925 |
self.params = memoryview(zlib.compress(self.testdata)) |
| 926 |
|
| 927 |
def tearDown(self): |
| 928 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 929 |
try: |
| 930 |
cur.execute("drop table test") |
| 931 |
except dbapi2.Error: |
| 932 |
pass |
| 933 |
|
| 934 |
|
| 935 |
class TransactionsTestCase(common.JPypeTestCase): |
| 936 |
def setUp(self): |
| 937 |
common.JPypeTestCase.setUp(self) |
| 938 |
if common.fast: |
| 939 |
raise common.unittest.SkipTest("fast") |
| 940 |
|
| 941 |
def tearDown(self): |
| 942 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 943 |
try: |
| 944 |
cur.execute("drop table test") |
| 945 |
except dbapi2.Error: |
| 946 |
pass |
| 947 |
|
| 948 |
def testTransactionRollbackCreate(self): |
| 949 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 950 |
cx.commit() |
| 951 |
cu.execute("begin") |
| 952 |
cu.execute("create table test(name VARCHAR(10))") |
| 953 |
cx.rollback() |
| 954 |
|
| 955 |
cu.execute("select * from test") |
| 956 |
|
| 957 |
def testTransactionRollbackInsert(self): |
| 958 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 959 |
cx.commit() |
| 960 |
cu.execute("create table test(name VARCHAR(10))") |
| 961 |
cx.commit() |
| 962 |
cu.execute("begin") |
| 963 |
cu.execute("insert into test(name) values('alice')") |
| 964 |
cx.rollback() |
| 965 |
|
| 966 |
result = cu.execute("select * from test").fetchall() |
| 967 |
self.assertEqual(result, []) |
| 968 |
|
| 969 |
def testTransactionRollbackClose(self): |
| 970 |
with dbapi2.connect(db_name) as cx2: |
| 971 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 972 |
cx.commit() |
| 973 |
cu.execute("create table test(name VARCHAR(10))") |
| 974 |
cu.execute("insert into test(name) values('alice')") |
| 975 |
cx.commit() |
| 976 |
cu.execute("begin") |
| 977 |
cu.execute("insert into test(name) values('bob')") |
| 978 |
result = cu.execute("select * from test").fetchall() |
| 979 |
self.assertEqual(result, [['alice'], ['bob']]) |
| 980 |
|
| 981 |
with cx2.cursor() as cu: |
| 982 |
result = cu.execute("select * from test").fetchall() |
| 983 |
self.assertEqual(result, [['alice']]) |
| 984 |
|
| 985 |
|
| 986 |
class TypeTestCase(common.JPypeTestCase): |
| 987 |
""" This test is db dependent, but needed to check all |
| 988 |
code paths. """ |
| 989 |
|
| 990 |
def setUp(self): |
| 991 |
common.JPypeTestCase.setUp(self) |
| 992 |
if common.fast: |
| 993 |
raise common.unittest.SkipTest("fast") |
| 994 |
|
| 995 |
def tearDown(self): |
| 996 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 997 |
try: |
| 998 |
cur.execute("drop table test") |
| 999 |
except dbapi2.Error: |
| 1000 |
pass |
| 1001 |
|
| 1002 |
def testTime(self): |
| 1003 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1004 |
cu.execute("create table test(NAME TIME)") |
| 1005 |
cu.execute("select * from test") |
| 1006 |
cu.execute("insert into test(NAME) values('12:05:10')") |
| 1007 |
f = cu.execute('select * from test').fetchone() |
| 1008 |
self.assertIsInstance(f[0], datetime.time) |
| 1009 |
cu.execute("delete from test") |
| 1010 |
cu.execute("insert into test(NAME) values(?)", f) |
| 1011 |
f2 = cu.execute('select * from test').fetchone() |
| 1012 |
self.assertEqual(f2, f) |
| 1013 |
cu.execute("delete from test") |
| 1014 |
|
| 1015 |
|
| 1016 |
t1 = jpype.java.sql.Time(1, 2, 3) |
| 1017 |
cu.execute("insert into test(NAME) values(?)", [t1]) |
| 1018 |
f3 = cu.execute('select * from test').fetchone() |
| 1019 |
self.assertEqual(f3[0], datetime.time(1, 2, 3)) |
| 1020 |
f4 = cu.execute('select * from test').fetchone(converters=None) |
| 1021 |
self.assertEqual(f4[0], t1) |
| 1022 |
self.assertIsInstance(f4[0], jpype.java.sql.Time) |
| 1023 |
cu.execute("delete from test") |
| 1024 |
|
| 1025 |
|
| 1026 |
t1 = dbapi2.Time(6, 21, 32) |
| 1027 |
cu.execute("insert into test(NAME) values(?)", [t1]) |
| 1028 |
f3 = cu.execute('select * from test').fetchone() |
| 1029 |
self.assertEqual(f3[0], datetime.time(6, 21, 32)) |
| 1030 |
|
| 1031 |
def testDate(self): |
| 1032 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1033 |
cu.execute("create table test(NAME DATE)") |
| 1034 |
cu.execute("select * from test") |
| 1035 |
cu.execute("insert into test(NAME) values('2012-02-05')") |
| 1036 |
f = cu.execute('select * from test').fetchone() |
| 1037 |
self.assertIsInstance(f[0], datetime.date) |
| 1038 |
cu.execute("delete from test") |
| 1039 |
cu.execute("insert into test(NAME) values(?)", f) |
| 1040 |
f2 = cu.execute('select * from test').fetchone() |
| 1041 |
self.assertEqual(f2, f) |
| 1042 |
cu.execute("delete from test") |
| 1043 |
|
| 1044 |
|
| 1045 |
t1 = java.sql.Date(1972 - 1900, 4 - 1, 1) |
| 1046 |
cu.execute("insert into test(NAME) values(?)", [t1]) |
| 1047 |
f3 = cu.execute('select * from test').fetchone() |
| 1048 |
self.assertEqual(f3[0], datetime.date(1972, 4, 1)) |
| 1049 |
f4 = cu.execute('select * from test').fetchone(converters=None) |
| 1050 |
self.assertEqual(f4[0], t1) |
| 1051 |
self.assertIsInstance(f4[0], java.sql.Date) |
| 1052 |
cu.execute("delete from test") |
| 1053 |
|
| 1054 |
|
| 1055 |
t1 = dbapi2.Date(2020, 5, 21) |
| 1056 |
cu.execute("insert into test(NAME) values(?)", [t1]) |
| 1057 |
f3 = cu.execute('select * from test').fetchone() |
| 1058 |
self.assertEqual(f3[0], datetime.date(2020, 5, 21)) |
| 1059 |
|
| 1060 |
def testTimestamp(self): |
| 1061 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1062 |
cu.execute("create table test(NAME TIMESTAMP)") |
| 1063 |
cu.execute("select * from test") |
| 1064 |
cu.execute("insert into test(NAME) values('2012-02-05 12:02:45.123')") |
| 1065 |
f = cu.execute('select * from test').fetchone() |
| 1066 |
self.assertIsInstance(f[0], datetime.date) |
| 1067 |
cu.execute("delete from test") |
| 1068 |
cu.execute("insert into test(NAME) values(?)", f) |
| 1069 |
f2 = cu.execute('select * from test').fetchone() |
| 1070 |
self.assertEqual(f2, f) |
| 1071 |
cu.execute("delete from test") |
| 1072 |
|
| 1073 |
|
| 1074 |
t1 = java.sql.Timestamp(1972 - 1900, 4 - 1, 1, 12, 2, 45, 123456000) |
| 1075 |
cu.execute("insert into test(NAME) values(?)", [t1]) |
| 1076 |
f3 = cu.execute('select * from test').fetchone() |
| 1077 |
self.assertEqual(f3[0], datetime.datetime(1972, 4, 1, 12, 2, 45, 123456)) |
| 1078 |
f4 = cu.execute('select * from test').fetchone(converters=None) |
| 1079 |
self.assertEqual(f4[0], t1) |
| 1080 |
self.assertIsInstance(f4[0], java.sql.Timestamp) |
| 1081 |
cu.execute("delete from test") |
| 1082 |
|
| 1083 |
|
| 1084 |
t1 = dbapi2.Timestamp(2020, 5, 21, 3, 4, 5, 123122000) |
| 1085 |
cu.execute("insert into test(NAME) values(?)", [t1]) |
| 1086 |
f3 = cu.execute('select * from test').fetchone() |
| 1087 |
self.assertEqual(f3[0], datetime.datetime(2020, 5, 21, 3, 4, 5, 123122)) |
| 1088 |
|
| 1089 |
def _testInt(self, tp, desc, jtype, null=True): |
| 1090 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1091 |
cu.execute("create table test(NAME %s)" % tp) |
| 1092 |
cu.execute("insert into test(NAME) values(?)", [123]) |
| 1093 |
f = cu.execute("select * from test").fetchone() |
| 1094 |
self.assertEqual(f[0], 123) |
| 1095 |
self.assertIsInstance(f[0], int) |
| 1096 |
self.assertEqual(cu.description[0][0:2], desc) |
| 1097 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1098 |
self.assertEqual(f[0], 123) |
| 1099 |
self.assertIsInstance(f[0], jtype) |
| 1100 |
cu.execute("delete from test") |
| 1101 |
if null: |
| 1102 |
cu.execute("insert into test(NAME) values(?)", [None]) |
| 1103 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1104 |
self.assertEqual(f[0], None) |
| 1105 |
|
| 1106 |
def testTinyInt(self): |
| 1107 |
self._testInt('TINYINT', ('NAME', 'TINYINT'), jpype.JShort) |
| 1108 |
|
| 1109 |
def testBigInt(self): |
| 1110 |
self._testInt('BIGINT', ('NAME', 'BIGINT'), jpype.JLong) |
| 1111 |
|
| 1112 |
def testIdentity(self): |
| 1113 |
self._testInt('IDENTITY', ('NAME', 'BIGINT'), jpype.JLong, False) |
| 1114 |
|
| 1115 |
def testInteger(self): |
| 1116 |
self._testInt('INTEGER', ('NAME', 'INTEGER'), jpype.JInt) |
| 1117 |
|
| 1118 |
def testSmallInt(self): |
| 1119 |
self._testInt('SMALLINT', ('NAME', 'SMALLINT'), jpype.JShort) |
| 1120 |
|
| 1121 |
def _testFloat(self, tp, desc, jtype): |
| 1122 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1123 |
cu.execute("create table test(NAME %s)" % tp) |
| 1124 |
cu.execute("insert into test(NAME) values(?)", [1.25]) |
| 1125 |
f = cu.execute("select * from test").fetchone() |
| 1126 |
self.assertEqual(f[0], 1.25) |
| 1127 |
self.assertIsInstance(f[0], float) |
| 1128 |
self.assertEqual(cu.description[0][0:2], desc) |
| 1129 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1130 |
self.assertEqual(f[0], 1.25) |
| 1131 |
self.assertIsInstance(f[0], jtype) |
| 1132 |
cu.execute("delete from test") |
| 1133 |
cu.execute("insert into test(NAME) values(?)", [None]) |
| 1134 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1135 |
self.assertEqual(f[0], None) |
| 1136 |
|
| 1137 |
def testFloat(self): |
| 1138 |
self._testFloat('FLOAT', ('NAME', 'DOUBLE'), jpype.JDouble) |
| 1139 |
|
| 1140 |
def testDouble(self): |
| 1141 |
self._testFloat('DOUBLE', ('NAME', 'DOUBLE'), jpype.JDouble) |
| 1142 |
|
| 1143 |
def testReal(self): |
| 1144 |
self._testFloat('REAL', ('NAME', 'REAL'), jpype.JFloat) |
| 1145 |
|
| 1146 |
def _testNumeric(self, tp, desc, jtype): |
| 1147 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1148 |
cu.execute("create table test(NAME %s(5,2))" % tp) |
| 1149 |
cu.execute("insert into test(NAME) values(?)", [1.25]) |
| 1150 |
f = cu.execute("select * from test").fetchone() |
| 1151 |
self.assertEqual(f[0], 1.25) |
| 1152 |
self.assertIsInstance(f[0], decimal.Decimal) |
| 1153 |
self.assertEqual(cu.description[0][0:2], desc) |
| 1154 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1155 |
self.assertEqual(f[0], decimal.Decimal(1.25)) |
| 1156 |
self.assertIsInstance(f[0], jtype) |
| 1157 |
cu.execute("delete from test") |
| 1158 |
cu.execute("insert into test(NAME) values(?)", [None]) |
| 1159 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1160 |
self.assertEqual(f[0], None) |
| 1161 |
|
| 1162 |
def testNumeric(self): |
| 1163 |
self._testNumeric('NUMERIC', ('NAME', 'DECIMAL'), java.math.BigDecimal) |
| 1164 |
|
| 1165 |
def testDecimal(self): |
| 1166 |
self._testNumeric('DECIMAL', ('NAME', 'DECIMAL'), java.math.BigDecimal) |
| 1167 |
|
| 1168 |
def _testBinary(self, tp, desc, jtype): |
| 1169 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1170 |
cu.execute("create table test(NAME %s)" % tp) |
| 1171 |
v = bytes([1, 2, 3]) |
| 1172 |
cu.execute("insert into test(NAME) values(?)", [v]) |
| 1173 |
f = cu.execute("select * from test").fetchone() |
| 1174 |
self.assertEqual(f[0], v) |
| 1175 |
self.assertIsInstance(f[0], bytes) |
| 1176 |
self.assertEqual(cu.description[0][0:2], desc) |
| 1177 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1178 |
self.assertEqual(bytes(f[0]), v) |
| 1179 |
self.assertIsInstance(f[0], jtype) |
| 1180 |
cu.execute("delete from test") |
| 1181 |
cu.execute("insert into test(NAME) values(?)", [None]) |
| 1182 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1183 |
self.assertEqual(f[0], None) |
| 1184 |
|
| 1185 |
def testBinary(self): |
| 1186 |
self._testBinary('BINARY(3)', ('NAME', 'VARBINARY'), JArray(JByte)) |
| 1187 |
|
| 1188 |
def testBlob(self): |
| 1189 |
self._testBinary('BLOB', ('NAME', 'BLOB'), JArray(JByte)) |
| 1190 |
|
| 1191 |
def testLongVarBinary(self): |
| 1192 |
self._testBinary('LONGVARBINARY', ('NAME', 'VARBINARY'), JArray(JByte)) |
| 1193 |
|
| 1194 |
def testVarBinary(self): |
| 1195 |
self._testBinary('VARBINARY(10)', ('NAME', 'VARBINARY'), JArray(JByte)) |
| 1196 |
|
| 1197 |
def _testChars(self, tp, desc, jtype, v="hello"): |
| 1198 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1199 |
cu.execute("create table test(NAME %s)" % tp) |
| 1200 |
cu.execute("insert into test(NAME) values(?)", [v]) |
| 1201 |
f = cu.execute("select * from test").fetchone() |
| 1202 |
self.assertEqual(f[0], v) |
| 1203 |
self.assertIsInstance(f[0], str) |
| 1204 |
self.assertEqual(cu.description[0][0:2], desc) |
| 1205 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1206 |
self.assertEqual(f[0], v) |
| 1207 |
self.assertIsInstance(f[0], jtype) |
| 1208 |
cu.execute("delete from test") |
| 1209 |
cu.execute("insert into test(NAME) values(?)", [None]) |
| 1210 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1211 |
self.assertEqual(f[0], None) |
| 1212 |
|
| 1213 |
def testChar(self): |
| 1214 |
self._testChars('CHAR', ('NAME', 'CHAR'), java.lang.String, 'a') |
| 1215 |
|
| 1216 |
def testVarChar(self): |
| 1217 |
self._testChars('VARCHAR', ('NAME', 'VARCHAR'), java.lang.String) |
| 1218 |
|
| 1219 |
def testLongVarChar(self): |
| 1220 |
self._testChars('LONGVARCHAR', ('NAME', 'VARCHAR'), java.lang.String) |
| 1221 |
|
| 1222 |
def testClob(self): |
| 1223 |
self._testChars('CLOB', ('NAME', 'CLOB'), java.lang.String) |
| 1224 |
|
| 1225 |
def testBoolean(self): |
| 1226 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1227 |
cu.execute("create table test(NAME BOOLEAN)") |
| 1228 |
cu.execute("insert into test(NAME) values(?)", [True]) |
| 1229 |
f = cu.execute("select * from test").fetchone() |
| 1230 |
self.assertEqual(f[0], True) |
| 1231 |
self.assertIsInstance(f[0], int) |
| 1232 |
self.assertEqual(cu.description[0][0:2], ('NAME', 'BOOLEAN')) |
| 1233 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1234 |
self.assertEqual(f[0], True) |
| 1235 |
self.assertIsInstance(f[0], type(True)) |
| 1236 |
cu.execute("delete from test") |
| 1237 |
cu.execute("insert into test(NAME) values(?)", [None]) |
| 1238 |
f = cu.execute("select * from test").fetchone(converters=None) |
| 1239 |
self.assertEqual(f[0], None) |
| 1240 |
|
| 1241 |
|
| 1242 |
class OtherTestCase(common.JPypeTestCase): |
| 1243 |
""" This test is db dependent, but needed to check all |
| 1244 |
code paths. """ |
| 1245 |
|
| 1246 |
def setUp(self): |
| 1247 |
common.JPypeTestCase.setUp(self) |
| 1248 |
if common.fast: |
| 1249 |
raise common.unittest.SkipTest("fast") |
| 1250 |
|
| 1251 |
def tearDown(self): |
| 1252 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 1253 |
try: |
| 1254 |
cur.execute("drop table test") |
| 1255 |
except dbapi2.Error: |
| 1256 |
pass |
| 1257 |
|
| 1258 |
def testJSON(self): |
| 1259 |
got = [] |
| 1260 |
|
| 1261 |
class MyType(dbapi2.JDBCType): |
| 1262 |
def get(self, rs, column, st): |
| 1263 |
got.append(column) |
| 1264 |
return dbapi2.JDBCType.get(self, rs, column, st) |
| 1265 |
|
| 1266 |
JSON = MyType("JSON", None) |
| 1267 |
with dbapi2.connect(db_name, getters=dbapi2.GETTERS_BY_TYPE) as cx, cx.cursor() as cu: |
| 1268 |
cu.execute("create table test(name JSON)") |
| 1269 |
cu.execute("insert into test(name) values('{age: 31}')") |
| 1270 |
f = cu.execute("select * from test").fetchone() |
| 1271 |
self.assertEqual(got, [1]) |
| 1272 |
with dbapi2.connect(db_name, getters=dbapi2.GETTERS_BY_NAME) as cx, cx.cursor() as cu: |
| 1273 |
cu.execute("create table test(name JSON)") |
| 1274 |
cu.execute("insert into test(name) values('{age: 31}')") |
| 1275 |
f = cu.execute("select * from test").fetchone() |
| 1276 |
self.assertEqual(got, [1, 1]) |
| 1277 |
del dbapi2._registry[JSON] |
| 1278 |
|
| 1279 |
|
| 1280 |
class ThreadingTestCase(common.JPypeTestCase): |
| 1281 |
def setUp(self): |
| 1282 |
common.JPypeTestCase.setUp(self) |
| 1283 |
if common.fast: |
| 1284 |
raise common.unittest.SkipTest("fast") |
| 1285 |
|
| 1286 |
def tearDown(self): |
| 1287 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cur: |
| 1288 |
try: |
| 1289 |
cur.execute("drop table test") |
| 1290 |
except dbapi2.Error: |
| 1291 |
pass |
| 1292 |
|
| 1293 |
def _testThread(self, cu, cmd, args, exc): |
| 1294 |
error = [] |
| 1295 |
|
| 1296 |
def run(): |
| 1297 |
try: |
| 1298 |
cmd(*args) |
| 1299 |
except Exception as ex: |
| 1300 |
error.append(ex) |
| 1301 |
t = threading.Thread(target=run) |
| 1302 |
t.start() |
| 1303 |
t.join() |
| 1304 |
self.assertEqual(len(error), 1) |
| 1305 |
with self.assertRaises(exc): |
| 1306 |
raise error[0] |
| 1307 |
|
| 1308 |
def test_execute(self): |
| 1309 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1310 |
cu.execute("create table test(name VARCHAR(10))") |
| 1311 |
self._testThread(cu, cu.execute, ("insert into test(name) values ('a')",), dbapi2.ProgrammingError) |
| 1312 |
|
| 1313 |
def test_executemany(self): |
| 1314 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1315 |
cu.execute("create table test(name VARCHAR(10))") |
| 1316 |
self._testThread(cu, cu.executemany, ("insert into test(name) values ('?')", [[1]]), dbapi2.ProgrammingError) |
| 1317 |
|
| 1318 |
def test_fetchone(self): |
| 1319 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1320 |
cu.execute("create table test(name VARCHAR(10))") |
| 1321 |
cu.execute("insert into test(name) values('a')") |
| 1322 |
self._testThread(cu, cu.fetchone, (), dbapi2.ProgrammingError) |
| 1323 |
|
| 1324 |
def test_fetchmany(self): |
| 1325 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1326 |
cu.execute("create table test(name VARCHAR(10))") |
| 1327 |
cu.execute("insert into test(name) values('a')") |
| 1328 |
self._testThread(cu, cu.fetchmany, (), dbapi2.ProgrammingError) |
| 1329 |
|
| 1330 |
def test_fetchall(self): |
| 1331 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1332 |
cu.execute("create table test(name VARCHAR(10))") |
| 1333 |
cu.execute("insert into test(name) values('a')") |
| 1334 |
self._testThread(cu, cu.fetchall, (), dbapi2.ProgrammingError) |
| 1335 |
|
| 1336 |
def test_close(self): |
| 1337 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1338 |
cu.execute("create table test(name VARCHAR(10))") |
| 1339 |
cu.execute("insert into test(name) values('a')") |
| 1340 |
self._testThread(cu, cu.close, (), dbapi2.ProgrammingError) |
| 1341 |
|
| 1342 |
def test_callproc(self): |
| 1343 |
with dbapi2.connect(db_name) as cx, cx.cursor() as cu: |
| 1344 |
cu.execute("create table test(name VARCHAR(10))") |
| 1345 |
cu.execute("insert into test(name) values('a')") |
| 1346 |
self._testThread(cu, cu.callproc, ("lower", ["FOO"]), dbapi2.ProgrammingError) |