Skip to content

Commit

Permalink
test(pep0249): Update database name parameters for test PEP0249 support.
Browse files Browse the repository at this point in the history
Guarantees that PEP0249 can test all options for database parameter name
during the connection.

Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
  • Loading branch information
pvital committed Oct 25, 2023
1 parent 3e360a9 commit 47b1c4b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
38 changes: 35 additions & 3 deletions tests/clients/test_psycopg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@

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'])
deprecated_param_name = self.shortDescription() == 'test_deprecated_parameter_database'
kwargs = {
'host': testenv['postgresql_host'],
'port': testenv['postgresql_port'],
'user': testenv['postgresql_user'],
'password': testenv['postgresql_pw'],
'dbname' if not deprecated_param_name else 'database': testenv['postgresql_db'],
}
self.db = psycopg2.connect(**kwargs)

database_setup_query = """
DROP TABLE IF EXISTS users;
Expand Down Expand Up @@ -330,3 +336,29 @@ def test_cursor_ctx_mgr(self):
self.assertEqual(db_span.data["pg"]["stmt"], "SELECT * from users")
self.assertEqual(db_span.data["pg"]["host"], testenv["postgresql_host"])
self.assertEqual(db_span.data["pg"]["port"], testenv["postgresql_port"])

def test_deprecated_parameter_database(self):
"""test_deprecated_parameter_database"""

with tracer.start_active_span('test'):
self.cursor.execute("""SELECT * from users""")
affected_rows = self.cursor.rowcount
result = self.cursor.fetchone()
self.db.commit()

self.assertEqual(1, affected_rows)
self.assertEqual(6, len(result))

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

db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "postgres")
self.assertEqual(db_span.data["pg"]["db"], testenv['postgresql_db'])
37 changes: 34 additions & 3 deletions tests/clients/test_pymysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@

class TestPyMySQL(unittest.TestCase):
def setUp(self):
self.db = pymysql.connect(host=testenv['mysql_host'], port=testenv['mysql_port'],
user=testenv['mysql_user'], passwd=testenv['mysql_pw'],
db=testenv['mysql_db'])
deprecated_param_name = self.shortDescription() == 'test_deprecated_parameter_db'
kwargs = {
'host': testenv['mysql_host'],
'port': testenv['mysql_port'],
'user': testenv['mysql_user'],
'passwd': testenv['mysql_pw'],
'database' if not deprecated_param_name else 'db': testenv['mysql_db'],
}
self.db = pymysql.connect(**kwargs)

database_setup_query = """
DROP TABLE IF EXISTS users; |
CREATE TABLE users(
Expand Down Expand Up @@ -286,3 +293,27 @@ def test_cursor_ctx_mgr(self):
self.assertEqual(db_span.data["mysql"]["stmt"], "SELECT * from users")
self.assertEqual(db_span.data["mysql"]["host"], testenv["mysql_host"])
self.assertEqual(db_span.data["mysql"]["port"], testenv["mysql_port"])

def test_deprecated_parameter_db(self):
"""test_deprecated_parameter_db"""

with tracer.start_active_span('test'):
affected_rows = self.cursor.execute("""SELECT * from users""")
result = self.cursor.fetchone()

self.assertEqual(1, affected_rows)
self.assertEqual(3, len(result))

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

db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "mysql")
self.assertEqual(db_span.data["mysql"]["db"], testenv['mysql_db'])

0 comments on commit 47b1c4b

Please sign in to comment.