Debian Codegraph @

test/test_integration.py

raw
1 #-*- coding: utf-8 -*-
2
3 # Copyright 2010 Bastian Bowe
4 #
5 # This file is part of JayDeBeApi.
6 # JayDeBeApi is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # JayDeBeApi is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with JayDeBeApi. If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 import jaydebeapi
21
22 import os
23 import sys
24 import threading
25
26 try:
27 import unittest2 as unittest
28 except ImportError:
29 import unittest
30
31 _THIS_DIR = os.path.dirname(os.path.abspath(__file__))
32
33 PY26 = not sys.version_info >= (2, 7)
34
35 def is_jython():
36 return sys.platform.lower().startswith('java')
37
38 if PY26 and not is_jython:
39 memoryview = buffer
40
41 class IntegrationTestBase(object):
42
43 def sql_file(self, filename):
44 f = open(filename, 'r')
45 try:
46 lines = f.readlines()
47 finally:
48 f.close()
49 stmt = []
50 stmts = []
51 for i in lines:
52 stmt.append(i)
53 if ";" in i:
54 stmts.append(" ".join(stmt))
55 stmt = []
56 with self.conn.cursor() as cursor:
57 for i in stmts:
58 cursor.execute(i)
59
60 def setUp(self):
61 (self.dbapi, self.conn) = self.connect()
62 self.setUpSql()
63
64 def setUpSql(self):
65 raise NotImplementedError
66
67 def connect(self):
68 raise NotImplementedError
69
70 def tearDown(self):
71 with self.conn.cursor() as cursor:
72 cursor.execute("drop table ACCOUNT")
73 self.conn.close()
74
75 def test_execute_and_fetch_no_data(self):
76 with self.conn.cursor() as cursor:
77 stmt = "select * from ACCOUNT where ACCOUNT_ID is null"
78 cursor.execute(stmt)
79 result = cursor.fetchall()
80 self.assertEqual(result, [])
81
82 def test_execute_and_fetch(self):
83 with self.conn.cursor() as cursor:
84 cursor.execute("select ACCOUNT_ID, ACCOUNT_NO, BALANCE, BLOCKING " \
85 "from ACCOUNT")
86 result = cursor.fetchall()
87 self.assertEqual(result, [(u'2009-09-10 14:15:22.123456', 18, 12.4, None),
88 (u'2009-09-11 14:15:22.123456', 19, 12.9, 1)])
89
90 def test_execute_and_fetch_parameter(self):
91 with self.conn.cursor() as cursor:
92 cursor.execute("select ACCOUNT_ID, ACCOUNT_NO, BALANCE, BLOCKING " \
93 "from ACCOUNT where ACCOUNT_NO = ?", (18,))
94 result = cursor.fetchall()
95 self.assertEqual(result, [(u'2009-09-10 14:15:22.123456', 18, 12.4, None)])
96
97 def test_execute_and_fetchone(self):
98 with self.conn.cursor() as cursor:
99 cursor.execute("select ACCOUNT_ID, ACCOUNT_NO, BALANCE, BLOCKING " \
100 "from ACCOUNT order by ACCOUNT_NO")
101 result = cursor.fetchone()
102 self.assertEqual(result, (u'2009-09-10 14:15:22.123456', 18, 12.4, None))
103 cursor.close()
104
105 def test_execute_reset_description_without_execute_result(self):
106 """Expect the descriptions property being reset when no query
107 has been made via execute method.
108 """
109 with self.conn.cursor() as cursor:
110 cursor.execute("select * from ACCOUNT")
111 self.assertIsNotNone(cursor.description)
112 cursor.fetchone()
113 cursor.execute("delete from ACCOUNT")
114 self.assertIsNone(cursor.description)
115
116 def test_execute_and_fetchone_after_end(self):
117 with self.conn.cursor() as cursor:
118 cursor.execute("select * from ACCOUNT where ACCOUNT_NO = ?", (18,))
119 cursor.fetchone()
120 result = cursor.fetchone()
121 self.assertIsNone(result)
122
123 def test_execute_and_fetchmany(self):
124 with self.conn.cursor() as cursor:
125 cursor.execute("select ACCOUNT_ID, ACCOUNT_NO, BALANCE, BLOCKING " \
126 "from ACCOUNT order by ACCOUNT_NO")
127 result = cursor.fetchmany()
128 self.assertEqual(result, [(u'2009-09-10 14:15:22.123456', 18, 12.4, None)])
129 # TODO: find out why this cursor has to be closed in order to
130 # let this test work with sqlite if __del__ is not overridden
131 # in cursor
132 # cursor.close()
133
134 def test_executemany(self):
135 stmt = "insert into ACCOUNT (ACCOUNT_ID, ACCOUNT_NO, BALANCE) " \
136 "values (?, ?, ?)"
137 parms = (
138 ( '2009-09-11 14:15:22.123450', 20, 13.1 ),
139 ( '2009-09-11 14:15:22.123451', 21, 13.2 ),
140 ( '2009-09-11 14:15:22.123452', 22, 13.3 ),
141 )
142 with self.conn.cursor() as cursor:
143 cursor.executemany(stmt, parms)
144 self.assertEqual(cursor.rowcount, 3)
145
146 def test_execute_types(self):
147 stmt = "insert into ACCOUNT (ACCOUNT_ID, ACCOUNT_NO, BALANCE, " \
148 "BLOCKING, DBL_COL, OPENED_AT, VALID, PRODUCT_NAME) " \
149 "values (?, ?, ?, ?, ?, ?, ?, ?)"
150 d = self.dbapi
151 account_id = d.Timestamp(2010, 1, 26, 14, 31, 59)
152 account_no = 20
153 balance = 1.2
154 blocking = 10.0
155 dbl_col = 3.5
156 opened_at = d.Date(2008, 2, 27)
157 valid = 1
158 product_name = u'Savings account'
159 parms = (account_id, account_no, balance, blocking, dbl_col,
160 opened_at, valid, product_name)
161 with self.conn.cursor() as cursor:
162 cursor.execute(stmt, parms)
163 stmt = "select ACCOUNT_ID, ACCOUNT_NO, BALANCE, BLOCKING, " \
164 "DBL_COL, OPENED_AT, VALID, PRODUCT_NAME " \
165 "from ACCOUNT where ACCOUNT_NO = ?"
166 parms = (20, )
167 cursor.execute(stmt, parms)
168 result = cursor.fetchone()
169 exp = ( '2010-01-26 14:31:59', account_no, balance, blocking,
170 dbl_col, '2008-02-27', valid, product_name )
171 self.assertEqual(result, exp)
172
173 def test_execute_type_time(self):
174 stmt = "insert into ACCOUNT (ACCOUNT_ID, ACCOUNT_NO, BALANCE, " \
175 "OPENED_AT_TIME) " \
176 "values (?, ?, ?, ?)"
177 d = self.dbapi
178 account_id = d.Timestamp(2010, 1, 26, 14, 31, 59)
179 account_no = 20
180 balance = 1.2
181 opened_at_time = d.Time(13, 59, 59)
182 parms = (account_id, account_no, balance, opened_at_time)
183 with self.conn.cursor() as cursor:
184 cursor.execute(stmt, parms)
185 stmt = "select ACCOUNT_ID, ACCOUNT_NO, BALANCE, OPENED_AT_TIME " \
186 "from ACCOUNT where ACCOUNT_NO = ?"
187 parms = (20, )
188 cursor.execute(stmt, parms)
189 result = cursor.fetchone()
190 exp = ( '2010-01-26 14:31:59', account_no, balance, '13:59:59' )
191 self.assertEqual(result, exp)
192
193 def test_execute_different_rowcounts(self):
194 stmt = "insert into ACCOUNT (ACCOUNT_ID, ACCOUNT_NO, BALANCE) " \
195 "values (?, ?, ?)"
196 parms = (
197 ( '2009-09-11 14:15:22.123450', 20, 13.1 ),
198 ( '2009-09-11 14:15:22.123452', 22, 13.3 ),
199 )
200 with self.conn.cursor() as cursor:
201 cursor.executemany(stmt, parms)
202 self.assertEqual(cursor.rowcount, 2)
203 parms = ( '2009-09-11 14:15:22.123451', 21, 13.2 )
204 cursor.execute(stmt, parms)
205 self.assertEqual(cursor.rowcount, 1)
206 cursor.execute("select * from ACCOUNT")
207 self.assertEqual(cursor.rowcount, -1)
208
209 class SqliteTestBase(IntegrationTestBase):
210
211 def setUpSql(self):
212 self.sql_file(os.path.join(_THIS_DIR, 'data', 'create.sql'))
213 self.sql_file(os.path.join(_THIS_DIR, 'data', 'insert.sql'))
214
215 def test_execute_type_blob(self):
216 stmt = "insert into ACCOUNT (ACCOUNT_ID, ACCOUNT_NO, BALANCE, " \
217 "STUFF) values (?, ?, ?, ?)"
218 binary_stuff = 'abcdef'.encode('UTF-8')
219 stuff = self.dbapi.Binary(binary_stuff)
220 parms = ('2009-09-11 14:15:22.123450', 20, 13.1, stuff)
221 with self.conn.cursor() as cursor:
222 cursor.execute(stmt, parms)
223 stmt = "select STUFF from ACCOUNT where ACCOUNT_NO = ?"
224 parms = (20, )
225 cursor.execute(stmt, parms)
226 result = cursor.fetchone()
227 value = result[0]
228 self.assertEqual(value, memoryview(binary_stuff))
229
230 @unittest.skipIf(is_jython(), "requires python")
231 class SqlitePyTest(SqliteTestBase, unittest.TestCase):
232
233 def connect(self):
234 import sqlite3
235 return sqlite3, sqlite3.connect(':memory:')
236
237 def test_execute_type_time(self):
238 """Time type not supported by PySqlite"""
239
240 class SqliteXerialTest(SqliteTestBase, unittest.TestCase):
241
242 def connect(self):
243 #http://bitbucket.org/xerial/sqlite-jdbc
244 # sqlite-jdbc-3.7.2.jar
245 driver, url = 'org.sqlite.JDBC', 'jdbc:sqlite::memory:'
246 # db2jcc
247 # driver, driver_args = 'com.ibm.db2.jcc.DB2Driver', \
248 # ['jdbc:db2://4.100.73.81:50000/db2t', 'user', 'passwd']
249 # driver from http://www.ch-werner.de/javasqlite/ seems to be
250 # crap as it returns decimal values as VARCHAR type
251 # sqlite.jar
252 # driver, driver_args = 'SQLite.JDBCDriver', 'jdbc:sqlite:/:memory:'
253 # Oracle Thin Driver
254 # driver, driver_args = 'oracle.jdbc.OracleDriver', \
255 # ['jdbc:oracle:thin:@//hh-cluster-scan:1521/HH_TPP',
256 # 'user', 'passwd']
257 return jaydebeapi, jaydebeapi.connect(driver, url)
258
259 @unittest.skipUnless(is_jython(), "don't know how to support blob")
260 def test_execute_type_blob(self):
261 return super(SqliteXerialTest, self).test_execute_type_blob()
262
263 class HsqldbTest(IntegrationTestBase, unittest.TestCase):
264
265 def connect(self):
266 # http://hsqldb.org/
267 # hsqldb.jar
268 driver, url, driver_args = ( 'org.hsqldb.jdbcDriver',
269 'jdbc:hsqldb:mem:.',
270 ['SA', ''] )
271 return jaydebeapi, jaydebeapi.connect(driver, url, driver_args)
272
273 def setUpSql(self):
274 self.sql_file(os.path.join(_THIS_DIR, 'data', 'create_hsqldb.sql'))
275 self.sql_file(os.path.join(_THIS_DIR, 'data', 'insert.sql'))
276
277 class PropertiesDriverArgsPassingTest(unittest.TestCase):
278
279 def test_connect_with_sequence(self):
280 driver, url, driver_args = ( 'org.hsqldb.jdbcDriver',
281 'jdbc:hsqldb:mem:.',
282 ['SA', ''] )
283 c = jaydebeapi.connect(driver, url, driver_args)
284 c.close()
285
286 def test_connect_with_properties(self):
287 driver, url, driver_args = ( 'org.hsqldb.jdbcDriver',
288 'jdbc:hsqldb:mem:.',
289 {'user': 'SA', 'password': '' } )
290 c = jaydebeapi.connect(driver, url, driver_args)
291 c.close()