Skip to content
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
16 changes: 6 additions & 10 deletions src/connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# coding: utf-8
import logging

# we support both pg8000 and psycopg2
try:
import psycopg2 as pglib
Expand Down Expand Up @@ -106,16 +104,14 @@ def rollback(self):

def execute(self, query, *args):
self.cursor.execute(query, args)
try:
# pg8000 might return tuples
res = [tuple(t) for t in self.cursor.fetchall()]
return res
except ProgrammingError:
return None
except Exception as e:
logging.error("Error executing query: {}\n {}".format(repr(e), query))

if self.cursor.description is None:
return None

# pg8000 might return tuples
res = [tuple(t) for t in self.cursor.fetchall()]
return res

def close(self):
self.cursor.close()
self.connection.close()
24 changes: 24 additions & 0 deletions tests/test_testgres_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3343,6 +3343,30 @@ def test_node_app__make_empty_and_pgconf(self, node_svc: PostgresNodeService):
assert node.status() == NodeStatus.Running
return

def test_node_connection__execute(
self,
node_svc: PostgresNodeService,
):
assert isinstance(node_svc, PostgresNodeService)

with __class__.helper__get_node(node_svc) as node:
node.init()
node.start()

with node.connect("postgres") as cn:
assert cn.execute("create table ttt (id integer);") is None
assert cn.execute("insert into ttt values(1);") is None
assert cn.execute("update ttt set id=2;") is None
assert cn.execute("select id from ttt;") == [(2, )]
assert cn.execute("select id from ttt where id=%s;", '2') == [(2, )]
assert cn.execute("select id from ttt where id=%s and (id + 1) = %s;", '2', 3) == [(2, )]
assert cn.execute("delete from ttt where id=2;") is None
assert cn.execute("select id from ttt;") == []
assert cn.execute("drop table ttt;") is None
pass
pass
return

@staticmethod
def helper__get_node(
node_svc: PostgresNodeService,
Expand Down
Loading