Skip to content

Commit f0eed1e

Browse files
committed
test: Fix mysqlclient to make database content predicatable
Signed-off-by: Ferenc Géczi <[email protected]>
1 parent 9c45787 commit f0eed1e

File tree

1 file changed

+41
-52
lines changed

1 file changed

+41
-52
lines changed

tests/clients/test_mysqlclient.py

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,46 @@
1111
from unittest import SkipTest
1212
from instana.singletons import tracer
1313

14-
1514
logger = logging.getLogger(__name__)
1615

17-
create_table_query = 'CREATE TABLE IF NOT EXISTS users(id serial primary key, \
18-
name varchar(40) NOT NULL, email varchar(40) NOT NULL)'
19-
20-
create_proc_query = """
21-
CREATE PROCEDURE test_proc(IN t VARCHAR(255))
22-
BEGIN
23-
SELECT name FROM users WHERE name = t;
24-
END
25-
"""
26-
27-
db = MySQLdb.connect(host=testenv['mysql_host'], port=testenv['mysql_port'],
28-
user=testenv['mysql_user'], passwd=testenv['mysql_pw'],
29-
db=testenv['mysql_db'])
30-
31-
cursor = db.cursor()
32-
cursor.execute(create_table_query)
33-
34-
while cursor.nextset() is not None:
35-
pass
36-
37-
cursor.execute('DROP PROCEDURE IF EXISTS test_proc')
38-
39-
while cursor.nextset() is not None:
40-
pass
41-
42-
cursor.execute(create_proc_query)
43-
44-
while cursor.nextset() is not None:
45-
pass
46-
47-
cursor.close()
48-
db.close()
49-
5016

5117
class TestMySQLPython(unittest.TestCase):
5218
def setUp(self):
5319
self.db = MySQLdb.connect(host=testenv['mysql_host'], port=testenv['mysql_port'],
5420
user=testenv['mysql_user'], passwd=testenv['mysql_pw'],
5521
db=testenv['mysql_db'])
22+
database_setup_query = """
23+
DROP TABLE IF EXISTS users;
24+
CREATE TABLE users(
25+
id serial primary key,
26+
name varchar(40) NOT NULL,
27+
email varchar(40) NOT NULL
28+
);
29+
INSERT INTO users(name, email) VALUES('kermit', '[email protected]');
30+
DROP PROCEDURE IF EXISTS test_proc;
31+
CREATE PROCEDURE test_proc(IN t VARCHAR(255))
32+
BEGIN
33+
SELECT name FROM users WHERE name = t;
34+
END
35+
"""
36+
setup_cursor = self.db.cursor()
37+
setup_cursor.execute(database_setup_query)
38+
setup_cursor.close()
39+
5640
self.cursor = self.db.cursor()
5741
self.recorder = tracer.recorder
5842
self.recorder.clear_spans()
5943
tracer.cur_ctx = None
6044

6145
def tearDown(self):
62-
""" Do nothing for now """
63-
return None
46+
if self.cursor and self.cursor.connection.open:
47+
self.cursor.close()
48+
if self.db and self.db.open:
49+
self.db.close()
6450

6551
def test_vanilla_query(self):
66-
self.cursor.execute("""SELECT * from users""")
52+
affected_rows = self.cursor.execute("""SELECT * from users""")
53+
self.assertEqual(1, affected_rows)
6754
result = self.cursor.fetchone()
6855
self.assertEqual(3, len(result))
6956

@@ -72,10 +59,11 @@ def test_vanilla_query(self):
7259

7360
def test_basic_query(self):
7461
with tracer.start_active_span('test'):
75-
result = self.cursor.execute("""SELECT * from users""")
76-
self.cursor.fetchone()
62+
affected_rows = self.cursor.execute("""SELECT * from users""")
63+
result = self.cursor.fetchone()
7764

78-
self.assertTrue(result >= 0)
65+
self.assertEqual(1, affected_rows)
66+
self.assertEqual(3, len(result))
7967

8068
spans = self.recorder.queued_spans()
8169
self.assertEqual(2, len(spans))
@@ -97,11 +85,11 @@ def test_basic_query(self):
9785

9886
def test_basic_insert(self):
9987
with tracer.start_active_span('test'):
100-
result = self.cursor.execute(
88+
affected_rows = self.cursor.execute(
10189
"""INSERT INTO users(name, email) VALUES(%s, %s)""",
10290
('beaker', '[email protected]'))
10391

104-
self.assertEqual(1, result)
92+
self.assertEqual(1, affected_rows)
10593

10694
spans = self.recorder.queued_spans()
10795
self.assertEqual(2, len(spans))
@@ -123,11 +111,11 @@ def test_basic_insert(self):
123111

124112
def test_executemany(self):
125113
with tracer.start_active_span('test'):
126-
result = self.cursor.executemany("INSERT INTO users(name, email) VALUES(%s, %s)",
114+
affected_rows = self.cursor.executemany("INSERT INTO users(name, email) VALUES(%s, %s)",
127115
[('beaker', '[email protected]'), ('beaker', '[email protected]')])
128116
self.db.commit()
129117

130-
self.assertEqual(2, result)
118+
self.assertEqual(2, affected_rows)
131119

132120
spans = self.recorder.queued_spans()
133121
self.assertEqual(2, len(spans))
@@ -149,9 +137,9 @@ def test_executemany(self):
149137

150138
def test_call_proc(self):
151139
with tracer.start_active_span('test'):
152-
result = self.cursor.callproc('test_proc', ('beaker',))
140+
callproc_result = self.cursor.callproc('test_proc', ('beaker',))
153141

154-
self.assertTrue(result)
142+
self.assertIsInstance(callproc_result, tuple)
155143

156144
spans = self.recorder.queued_spans()
157145
self.assertEqual(2, len(spans))
@@ -172,15 +160,14 @@ def test_call_proc(self):
172160
self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])
173161

174162
def test_error_capture(self):
175-
result = None
163+
affected_rows = None
176164
try:
177165
with tracer.start_active_span('test'):
178-
result = self.cursor.execute("""SELECT * from blah""")
179-
self.cursor.fetchone()
166+
affected_rows = self.cursor.execute("""SELECT * from blah""")
180167
except Exception:
181168
pass
182169

183-
self.assertIsNone(result)
170+
self.assertIsNone(affected_rows)
184171

185172
spans = self.recorder.queued_spans()
186173
self.assertEqual(2, len(spans))
@@ -205,8 +192,9 @@ def test_connect_cursor_ctx_mgr(self):
205192
with tracer.start_active_span("test"):
206193
with self.db as connection:
207194
with connection.cursor() as cursor:
208-
cursor.execute("""SELECT * from users""")
195+
affected_rows = cursor.execute("""SELECT * from users""")
209196

197+
self.assertEqual(1, affected_rows)
210198
spans = self.recorder.queued_spans()
211199
self.assertEqual(2, len(spans))
212200

@@ -254,9 +242,10 @@ def test_cursor_ctx_mgr(self):
254242
with tracer.start_active_span("test"):
255243
connection = self.db
256244
with connection.cursor() as cursor:
257-
cursor.execute("""SELECT * from users""")
245+
affected_rows = cursor.execute("""SELECT * from users""")
258246

259247

248+
self.assertEqual(1, affected_rows)
260249
spans = self.recorder.queued_spans()
261250
self.assertEqual(2, len(spans))
262251

0 commit comments

Comments
 (0)