Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): Clean up old PEP-249 tests #452

Merged
merged 5 commits into from
Sep 6, 2023
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
139 changes: 54 additions & 85 deletions tests/clients/test_mysqlclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,75 @@
import sys
import logging
import unittest
import MySQLdb
from ..helpers import testenv
from unittest import SkipTest
from instana.singletons import tracer


if sys.version_info[0] > 2:
import MySQLdb
else:
raise SkipTest("mysqlclient supported on Python 3 only")

logger = logging.getLogger(__name__)

create_table_query = 'CREATE TABLE IF NOT EXISTS users(id serial primary key, \
name varchar(40) NOT NULL, email varchar(40) NOT NULL)'

create_proc_query = """
CREATE PROCEDURE test_proc(IN t VARCHAR(255))
BEGIN
SELECT name FROM users WHERE name = t;
END
"""

db = MySQLdb.connect(host=testenv['mysql_host'], port=testenv['mysql_port'],
user=testenv['mysql_user'], passwd=testenv['mysql_pw'],
db=testenv['mysql_db'])

cursor = db.cursor()
cursor.execute(create_table_query)

while cursor.nextset() is not None:
pass

cursor.execute('DROP PROCEDURE IF EXISTS test_proc')

while cursor.nextset() is not None:
pass

cursor.execute(create_proc_query)

while cursor.nextset() is not None:
pass

cursor.close()
db.close()


class TestMySQLPython(unittest.TestCase):
def setUp(self):
self.db = MySQLdb.connect(host=testenv['mysql_host'], port=testenv['mysql_port'],
user=testenv['mysql_user'], passwd=testenv['mysql_pw'],
db=testenv['mysql_db'])
database_setup_query = """
DROP TABLE IF EXISTS users;
CREATE TABLE users(
id serial primary key,
name varchar(40) NOT NULL,
email varchar(40) NOT NULL
);
INSERT INTO users(name, email) VALUES('kermit', 'kermit@muppets.com');
DROP PROCEDURE IF EXISTS test_proc;
CREATE PROCEDURE test_proc(IN t VARCHAR(255))
BEGIN
SELECT name FROM users WHERE name = t;
END
"""
setup_cursor = self.db.cursor()
setup_cursor.execute(database_setup_query)
setup_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 self.cursor.connection.open:
self.cursor.close()
if self.db and self.db.open:
self.db.close()

def test_vanilla_query(self):
self.cursor.execute("""SELECT * from users""")
affected_rows = self.cursor.execute("""SELECT * from users""")
self.assertEqual(1, affected_rows)
result = self.cursor.fetchone()
self.assertEqual(3, len(result))

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

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

assert(result >= 0)
self.assertEqual(1, affected_rows)
self.assertEqual(3, len(result))

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

db_span = spans[0]
test_span = spans[1]
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.assertEqual(None, db_span.ec)
self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "mysql")
self.assertEqual(db_span.data["mysql"]["db"], testenv['mysql_db'])
Expand All @@ -102,25 +84,23 @@ def test_basic_query(self):
self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_basic_insert(self):
result = None
with tracer.start_active_span('test'):
result = self.cursor.execute(
affected_rows = self.cursor.execute(
"""INSERT INTO users(name, email) VALUES(%s, %s)""",
('beaker', 'beaker@muppets.com'))

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

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

db_span = spans[0]
test_span = spans[1]
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.assertEqual(None, db_span.ec)
self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "mysql")
self.assertEqual(db_span.data["mysql"]["db"], testenv['mysql_db'])
Expand All @@ -130,25 +110,23 @@ def test_basic_insert(self):
self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

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

self.assertEqual(2, result)
self.assertEqual(2, affected_rows)

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

db_span = spans[0]
test_span = spans[1]
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.assertEqual(None, db_span.ec)
self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "mysql")
self.assertEqual(db_span.data["mysql"]["db"], testenv['mysql_db'])
Expand All @@ -158,23 +136,21 @@ def test_executemany(self):
self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

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

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

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

db_span = spans[0]
test_span = spans[1]
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.assertEqual(None, db_span.ec)
self.assertIsNone(db_span.ec)

self.assertEqual(db_span.n, "mysql")
self.assertEqual(db_span.data["mysql"]["db"], testenv['mysql_db'])
Expand All @@ -184,25 +160,19 @@ def test_call_proc(self):
self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_error_capture(self):
result = None
span = None
affected_rows = None
try:
with tracer.start_active_span('test'):
result = self.cursor.execute("""SELECT * from blah""")
self.cursor.fetchone()
affected_rows = self.cursor.execute("""SELECT * from blah""")
except Exception:
pass
finally:
if span:
span.finish()

assert(result is None)
self.assertIsNone(affected_rows)

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

db_span = spans[0]
test_span = spans[1]
db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
Expand All @@ -222,13 +192,13 @@ def test_connect_cursor_ctx_mgr(self):
with tracer.start_active_span("test"):
with self.db as connection:
with connection.cursor() as cursor:
cursor.execute("""SELECT * from users""")
affected_rows = cursor.execute("""SELECT * from users""")

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

db_span = spans[0]
test_span = spans[1]
db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
Expand All @@ -253,8 +223,7 @@ def test_connect_ctx_mgr(self):
spans = self.recorder.queued_spans()
self.assertEqual(2, len(spans))

db_span = spans[0]
test_span = spans[1]
db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
Expand All @@ -273,14 +242,14 @@ def test_cursor_ctx_mgr(self):
with tracer.start_active_span("test"):
connection = self.db
with connection.cursor() as cursor:
cursor.execute("""SELECT * from users""")
affected_rows = cursor.execute("""SELECT * from users""")


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

db_span = spans[0]
test_span = spans[1]
db_span, test_span = spans

self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual(test_span.t, db_span.t)
Expand Down
Loading