Fixed DB-API violation of execute-Method. Free resources after executemany. Tests against SQLite. Improved type handling.

This commit is contained in:
baztian
2011-01-26 13:28:47 +01:00
parent cd8b93ca27
commit 4884cc60d1
4 changed files with 131 additions and 60 deletions
+48 -24
View File
@@ -17,19 +17,19 @@
# License along with JayDeBeApi. If not, see
# <http://www.gnu.org/licenses/>.
import os
from os import path
from unittest import TestCase
import jaydebeapi
import sys
this_dir = os.path.dirname(os.path.abspath(__file__))
jar_dir = os.path.abspath(os.path.join(this_dir, '..', '..',
this_dir = path.dirname(path.abspath(__file__))
jar_dir = path.abspath(path.join(this_dir, '..', '..',
'build', 'lib'))
create_sql = os.path.join(this_dir, 'data', 'create.sql')
insert_sql = os.path.join(this_dir, 'data', 'insert.sql')
create_sql = path.join(this_dir, 'data', 'create.sql')
insert_sql = path.join(this_dir, 'data', 'insert.sql')
def is_jython():
return sys.platform.lower().startswith('java')
return sys.platform.lower().startswith('java')
class IntegrationTest(TestCase):
@@ -50,31 +50,52 @@ class IntegrationTest(TestCase):
for i in stmts:
cursor.execute(i)
def setup_jpype(self, jars):
def setup_jpype(self, jars, libs=None):
import jpype
if not jpype.isJVMStarted():
jvm_path = jpype.getDefaultJVMPath()
#jvm_path = ('/usr/lib/jvm/java-6-openjdk'
# '/jre/lib/i386/client/libjvm.so')
args='-Djava.class.path=%s' % jars
jpype.startJVM(jvm_path, args)
# path to shared libraries
args = []
if libs:
libs_path = path.pathsep.join(libs)
args.append('-Djava.library.path=%s' % libs_path)
class_path = path.pathsep.join(jars)
args.append('-Djava.class.path=%s' % class_path)
jpype.startJVM(jvm_path, *args)
if not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
def setUp(self):
# TODO support more than one jar
#jars='/usr/share/java/hsqldb.jar'
jars=os.path.join(jar_dir, 'hsqldb.jar')
#jars=jars_path(r'C:\Programme\DbVisualizer-4.3.5\jdbc')
def connect(self):
import sqlite3
return sqlite3.connect(':memory:')
def connect_(self):
jar_names = [ 'hsqldb.jar', 'sqlitejdbc-v056.jar', 'sqlite.jar' ]
jars = [ path.join(jar_dir, i) for i in jar_names ]
if is_jython():
sys.path.append(jars)
sys.path.extend(jars)
else:
self.setup_jpype(jars)
self.conn = jaydebeapi.connect('org.hsqldb.jdbcDriver',
'jdbc:hsqldb:mem', 'SA', '')
#conn = jaydebeapi.connect('com.ibm.db2.jcc.DB2Driver',
# 'jdbc:db2://4.100.73.81:50000/db2t',
# getpass.getuser(), getpass.getpass())
self.setup_jpype(jars, [jar_dir])
# http://www.zentus.com/sqlitejdbc/
conn = jaydebeapi.connect('org.sqlite.JDBC',
'jdbc:sqlite::memory:')
# http://hsqldb.org/
# conn = jaydebeapi.connect('org.hsqldb.jdbcDriver',
# 'jdbc:hsqldb:mem', 'SA', '')
# conn = jaydebeapi.connect('com.ibm.db2.jcc.DB2Driver',
# 'jdbc:db2://4.100.73.81:50000/db2t',
# getpass.getuser(),
# getpass.getpass())
# driver from http://www.ch-werner.de/javasqlite/ seems to be
# crap as it returns decimal values as VARCHAR type
# conn = jaydebeapi.connect('SQLite.JDBCDriver',
# 'jdbc:sqlite:/:memory:')
return conn
def setUp(self):
self.conn = self.connect()
self.sql_file(create_sql)
self.sql_file(insert_sql)
@@ -98,7 +119,7 @@ class IntegrationTest(TestCase):
def test_execute_and_fetch_parameter(self):
cursor = self.conn.cursor()
cursor.execute("select * from konto where konto_nr = ?", 18)
cursor.execute("select * from konto where konto_nr = ?", (18,))
result = cursor.fetchall()
assert [(u'2009-09-10 14:15:22.123456', 18, 12.4, None)] == result
@@ -107,6 +128,7 @@ class IntegrationTest(TestCase):
cursor.execute("select * from konto order by konto_nr")
result = cursor.fetchone()
assert (u'2009-09-10 14:15:22.123456', 18, 12.4, None) == result
cursor.close()
def test_execute_reset_description_without_execute_result(self):
"""Excpect the descriptions property being reset when no query
@@ -121,7 +143,7 @@ class IntegrationTest(TestCase):
def test_execute_and_fetchone_after_end(self):
cursor = self.conn.cursor()
cursor.execute("select * from konto where konto_nr = ?", 18)
cursor.execute("select * from konto where konto_nr = ?", (18,))
cursor.fetchone()
result = cursor.fetchone()
assert None is result
@@ -131,6 +153,9 @@ class IntegrationTest(TestCase):
cursor.execute("select * from konto order by konto_nr")
result = cursor.fetchmany()
assert [(u'2009-09-10 14:15:22.123456', 18, 12.4, None)] == result
# TODO: find out why this cursor has to be closed in order to
# let this test work with sqlite
# cursor.close()
def test_executemany(self):
cursor = self.conn.cursor()
@@ -142,4 +167,3 @@ class IntegrationTest(TestCase):
)
cursor.executemany(stmt, parms)
assert cursor.rowcount == 3