Skip to content

Commit eda5663

Browse files
committed
Updated dbapi module to run on python2
implemented close method and activated corresponding unit test updated super() methods to python2 style wrote texts of raising errors
1 parent b118d62 commit eda5663

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

tarantool/dbapi.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"""
2-
https://www.python.org/dev/peps/pep-0249/
3-
"""
1+
from __future__ import unicode_literals
2+
43
import re
54

65
from tarantool.error import InterfaceError
76
from .connection import Connection as BaseConnection
87

8+
99
update_insert_pattern = re.compile(r'^UPDATE|^INSERT', re.IGNORECASE)
1010

1111

@@ -17,10 +17,10 @@ class Cursor:
1717
arraysize = 1
1818
autocommit = True
1919
closed = False
20+
rows = None
2021

21-
def __init__(self, connection):
22-
self._c = connection
23-
self.rows = None
22+
def __init__(self, conn):
23+
self._c = conn
2424

2525
def callproc(self, procname, *params): # TODO
2626
"""
@@ -34,14 +34,15 @@ def callproc(self, procname, *params): # TODO
3434
be made available through the standard .fetch*() methods.
3535
"""
3636

37-
def close(self): # TODO
37+
def close(self):
3838
"""
3939
Close the cursor now (rather than whenever __del__ is called).
4040
4141
The cursor will be unusable from this point forward; an Error (or
4242
subclass) exception will be raised if any operation is attempted with
4343
the cursor.
4444
"""
45+
self._c = None
4546

4647
@staticmethod
4748
def _convert_param(p):
@@ -90,13 +91,13 @@ def execute(self, query, params=None):
9091
"""
9192
if self.closed:
9293
raise self._c.ProgrammingError
94+
9395
if params:
9496
query = query % tuple(
9597
self._convert_param(param) for param in params)
9698

9799
response = self._c.execute(query)
98-
99-
self.rows = tuple(response.body.values())[1] if len(
100+
self.rows = tuple(response.data) if len(
100101
response.body) > 1 else None
101102

102103
if update_insert_pattern.match(query):
@@ -162,7 +163,7 @@ def fetchone(self):
162163
.execute*() did not produce any result set or no call was issued yet.
163164
"""
164165
if self.rows is None:
165-
raise self._c.Error
166+
raise self._c.ProgrammingError('Nothing to fetch')
166167
return self.fetchmany(1)[0] if len(self.rows) else None
167168

168169
def fetchmany(self, size=None):
@@ -189,7 +190,7 @@ def fetchmany(self, size=None):
189190
size = size or self.arraysize
190191

191192
if self.rows is None:
192-
raise self._c.ProgrammingError
193+
raise self._c.ProgrammingError('Nothing to fetch')
193194

194195
if len(self.rows) < size:
195196
items = self.rows
@@ -208,7 +209,7 @@ def fetchall(self):
208209
.execute*() did not produce any result set or no call was issued yet.
209210
"""
210211
if self.rows is None:
211-
raise self._c.ProgrammingError
212+
raise self._c.ProgrammingError('Nothing to fetch')
212213

213214
items = self.rows[:]
214215
self.rows = []
@@ -217,8 +218,7 @@ def fetchall(self):
217218
def setinputsizes(self, sizes):
218219
"""This can be used before a call to .execute*() to predefine memory
219220
areas for the operation's parameters.
220-
221-
sizes is specified as a sequence — one item for each input parameter.
221+
sizes is specified as a sequence - one item for each input parameter.
222222
The item should be a Type Object that corresponds to the input that
223223
will be used, or it should be an integer specifying the maximum
224224
length of a string parameter. If the item is None, then no predefined
@@ -231,7 +231,13 @@ def setinputsizes(self, sizes):
231231
free to not use it."""
232232

233233
def setoutputsize(self, size, column=None):
234-
pass
234+
"""Set a column buffer size for fetches of large columns (e.g. LONGs,
235+
BLOBs, etc.). The column is specified as an index into the result
236+
sequence. Not specifying the column will set the default size for all
237+
large columns in the cursor.
238+
This method would be used before the .execute*() method is invoked.
239+
Implementations are free to have this method do nothing and users are
240+
free to not use it."""
235241

236242

237243
class Connection(BaseConnection):
@@ -243,7 +249,7 @@ class Connection(BaseConnection):
243249
server_version = 2
244250

245251
def connect(self):
246-
super().connect()
252+
super(Connection, self).connect()
247253
return self
248254

249255
def commit(self):
@@ -272,8 +278,8 @@ def rollback(self):
272278

273279
def execute(self, query, params=None):
274280
if self._socket is None:
275-
raise self.ProgrammingError
276-
return super().execute(query, params)
281+
raise self.ProgrammingError('Can not execute on closed connection')
282+
return super(Connection, self).execute(query, params)
277283

278284
def close(self):
279285
"""
@@ -289,11 +295,7 @@ def close(self):
289295
self._socket.close()
290296
self._socket = None
291297
else:
292-
raise self.ProgrammingError
293-
294-
def _set_cursor(self):
295-
self._cursor = Cursor(self)
296-
return self._cursor
298+
raise self.ProgrammingError('Connection already closed')
297299

298300
def cursor(self, params=None):
299301
"""
@@ -303,4 +305,4 @@ def cursor(self, params=None):
303305
will have to emulate cursors using other means to the extent needed
304306
by this specification.
305307
"""
306-
return self._cursor or self._set_cursor()
308+
return Cursor(self)

unit/suites/test_dbapi.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,43 @@
55
import sys
66
import unittest
77

8-
from dbapi20 import DatabaseAPI20Test
8+
import dbapi20
99

10+
import tarantool
1011
from tarantool.dbapi import Connection
1112
from .lib.tarantool_server import TarantoolServer
1213

1314

14-
class TestSuite_DBAPI(DatabaseAPI20Test):
15+
class TestSuite_DBAPI(dbapi20.DatabaseAPI20Test):
1516
table_prefix = 'dbapi20test_' # If you need to specify a prefix for tables
1617

1718
ddl1 = 'create table %sbooze (name varchar(20) primary key)' % table_prefix
1819
ddl2 = 'create table %sbarflys (name varchar(20) primary key, ' \
1920
'drink varchar(30))' % table_prefix
2021

2122
@classmethod
22-
def setUpClass(cls):
23+
def setUpClass(self):
2324
print(' DBAPI '.center(70, '='), file=sys.stderr)
2425
print('-' * 70, file=sys.stderr)
25-
26-
def setUp(self):
2726
self.srv = TarantoolServer()
2827
self.srv.script = 'unit/suites/box.lua'
2928
self.srv.start()
29+
self.con = tarantool.Connection(self.srv.host, self.srv.args['primary'])
3030
self.driver = Connection(self.srv.host, self.srv.args['primary'])
31+
32+
def setUp(self):
3133
# prevent a remote tarantool from clean our session
3234
if self.srv.is_started():
3335
self.srv.touch_lock()
36+
self.con.flush_schema()
37+
3438
# grant full access to guest
3539
self.srv.admin("box.schema.user.grant('guest', 'create,read,write,"
3640
"execute', 'universe')")
3741

38-
def tearDown(self):
39-
# self.driver.close()
42+
@classmethod
43+
def tearDownClass(self):
44+
self.con.close()
4045
self.srv.stop()
4146
self.srv.clean()
4247

@@ -84,14 +89,9 @@ def test_nextset(self):
8489
def test_callproc(self):
8590
pass
8691

87-
@unittest.skip('To do')
88-
def test_setoutputsize(self):
92+
def test_setoutputsize(self): # Do nothing
8993
pass
9094

9195
@unittest.skip('To do')
9296
def test_description(self):
9397
pass
94-
95-
@unittest.skip('To do')
96-
def test_close(self):
97-
pass

0 commit comments

Comments
 (0)