Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Not passing parameters to cursor.execute if they are empty #137

Merged
merged 1 commit into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion momoko/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,10 @@ def execute(self,
"""
kwargs = {"cursor_factory": cursor_factory} if cursor_factory else {}
cursor = self.connection.cursor(**kwargs)
cursor.execute(operation, parameters)
if parameters:
cursor.execute(operation, parameters)
else:
cursor.execute(operation)

future = Future()
callback = partial(self._io_callback, future, cursor)
Expand Down
22 changes: 22 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def clean_db(self):
yield self.conn.execute("DROP TABLE IF EXISTS unit_test_transaction;")
yield self.conn.execute("DROP TABLE IF EXISTS unit_test_int_table;")
yield self.conn.execute("DROP FUNCTION IF EXISTS unit_test_callproc(integer);")
yield self.conn.execute("DROP FUNCTION IF EXISTS unit_test_callproc_noparams();")

def prepare_db(self):
yield self.conn.execute(
Expand All @@ -144,6 +145,12 @@ def prepare_db(self):
"RETURN n*n;\n"
"END;$BODY$ LANGUAGE plpgsql VOLATILE;"
)
yield self.conn.execute(
"CREATE OR REPLACE FUNCTION unit_test_callproc_noparams()\n"
"RETURNS integer AS $BODY$BEGIN\n"
"RETURN 42;\n"
"END;$BODY$ LANGUAGE plpgsql VOLATILE;"
)

def fill_int_data(self, amount=1000):
return self.conn.transaction([
Expand Down Expand Up @@ -193,6 +200,15 @@ def test_execute(self):
cursor = yield self.conn.execute("SELECT 1, 2, 3")
self.assertEqual(cursor.fetchall(), [(1, 2, 3)])

@gen_test
def test_execute_percent_sign(self):
"""
Testing that momoko does not complain on percent signs when there is no parameters.
Issue #136
"""
cursor = yield self.conn.execute("SELECT 'a%s'")
self.assertEqual(cursor.fetchall(), [('a%s',)])

@gen_test
def test_ping(self):
"""Testing ping"""
Expand Down Expand Up @@ -272,6 +288,12 @@ def test_callproc(self):
cursor = yield self.conn.callproc("unit_test_callproc", (64,))
self.assertEqual(cursor.fetchone(), (4096,))

@gen_test
def test_callproc_noparams(self):
"""Testing callproc without parameters"""
cursor = yield self.conn.callproc("unit_test_callproc_noparams")
self.assertEqual(cursor.fetchone(), (42,))

@gen_test
def test_query_error(self):
"""Testing that execute method propages exception properly"""
Expand Down