Skip to content

Commit

Permalink
test: Fix psycopg2 tests to make database content predicatable
Browse files Browse the repository at this point in the history
Signed-off-by: Ferenc Géczi <ferenc.geczi@ibm.com>
  • Loading branch information
Ferenc- committed Sep 5, 2023
1 parent 501411f commit 9260e8d
Showing 1 changed file with 34 additions and 40 deletions.
74 changes: 34 additions & 40 deletions tests/clients/test_psycopg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,48 @@

logger = logging.getLogger(__name__)

create_table_query = """
CREATE TABLE IF NOT EXISTS users(
id serial PRIMARY KEY,
name VARCHAR (50),
password VARCHAR (50),
email VARCHAR (355),
created_on TIMESTAMP,
last_login TIMESTAMP
);
"""

create_proc_query = """\
CREATE OR REPLACE FUNCTION test_proc(candidate VARCHAR(70))
RETURNS text AS $$
BEGIN
RETURN(SELECT name FROM users where email = candidate);
END;
$$ LANGUAGE plpgsql;
"""

drop_proc_query = "DROP FUNCTION IF EXISTS test_proc(VARCHAR(70));"

db = psycopg2.connect(host=testenv['postgresql_host'], port=testenv['postgresql_port'],
user=testenv['postgresql_user'], password=testenv['postgresql_pw'],
database=testenv['postgresql_db'])

cursor = db.cursor()
cursor.execute(create_table_query)
cursor.execute(drop_proc_query)
cursor.execute(create_proc_query)
db.commit()
cursor.close()
db.close()


class TestPsycoPG2(unittest.TestCase):
def setUp(self):
self.db = psycopg2.connect(host=testenv['postgresql_host'], port=testenv['postgresql_port'],
user=testenv['postgresql_user'], password=testenv['postgresql_pw'],
database=testenv['postgresql_db'])

database_setup_query = """
DROP TABLE IF EXISTS users;
CREATE TABLE users(
id serial PRIMARY KEY,
name VARCHAR (50),
password VARCHAR (50),
email VARCHAR (355),
created_on TIMESTAMP,
last_login TIMESTAMP
);
INSERT INTO users(name, email) VALUES('kermit', 'kermit@muppets.com');
DROP FUNCTION IF EXISTS test_proc(VARCHAR(70));
CREATE FUNCTION test_proc(candidate VARCHAR(70))
RETURNS text AS $$
BEGIN
RETURN(SELECT name FROM users where email = candidate);
END;
$$ LANGUAGE plpgsql;
"""
cursor = self.db.cursor()
cursor.execute(database_setup_query)
self.db.commit()
cursor.close()


self.cursor = self.db.cursor()
self.recorder = tracer.recorder
self.recorder.clear_spans()
tracer.cur_ctx = None

def tearDown(self):
""" Do nothing for now """
return None
if self.cursor and not self.cursor.connection.closed:
self.cursor.close()
if self.db and not self.db.closed:
self.db.close()

def test_vanilla_query(self):
self.assertTrue(psycopg2.extras.register_uuid(None, self.db))
Expand Down Expand Up @@ -123,8 +117,8 @@ def test_basic_insert(self):

def test_executemany(self):
with tracer.start_active_span('test'):
result = self.cursor.executemany("INSERT INTO users(name, email) VALUES(%s, %s)",
[('beaker', 'beaker@muppets.com'), ('beaker', 'beaker@muppets.com')])
sult = self.cursor.executemany("INSERT INTO users(name, email) VALUES(%s, %s)",
[('beaker', 'beaker@muppets.com'), ('beaker', 'beaker@muppets.com')])
self.db.commit()

spans = self.recorder.queued_spans()
Expand All @@ -147,9 +141,9 @@ def test_executemany(self):

def test_call_proc(self):
with tracer.start_active_span('test'):
result = self.cursor.callproc('test_proc', ('beaker',))
callproc_result = self.cursor.callproc('test_proc', ('beaker',))

self.assertIsInstance(result, tuple)
self.assertIsInstance(callproc_result, tuple)

spans = self.recorder.queued_spans()
self.assertEqual(2, len(spans))
Expand Down

0 comments on commit 9260e8d

Please sign in to comment.