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

Commit

Permalink
Merge pull request #137 from haizaar/master
Browse files Browse the repository at this point in the history
Not passing parameters to `cursor.execute` if they are empty.
  • Loading branch information
haizaar committed Mar 10, 2016
2 parents ff4b218 + 1a52d88 commit 0d93a25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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

0 comments on commit 0d93a25

Please sign in to comment.