forked from aws/aws-xray-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pymysql.py
78 lines (65 loc) · 2.21 KB
/
test_pymysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pymysql
import pytest
from aws_xray_sdk.core import patch
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.context import Context
from aws_xray_sdk.ext.pymysql import unpatch
MYSQL_USER = "root"
MYSQL_PASSWORD = "root"
MYSQL_HOST = "localhost"
MYSQL_PORT = 3306
MYSQL_DB_NAME = "test_db"
@pytest.fixture(scope='module', autouse=True)
def patch_module():
patch(('pymysql',))
yield
unpatch()
@pytest.fixture(autouse=True)
def construct_ctx():
"""
Clean up context storage on each test run and begin a segment
so that later subsegment can be attached. After each test run
it cleans up context storage again.
"""
xray_recorder.configure(service='test', sampling=False, context=Context())
xray_recorder.clear_trace_entities()
xray_recorder.begin_segment('name')
yield
xray_recorder.clear_trace_entities()
def test_execute_dsn_kwargs():
q = 'SELECT 1'
conn = pymysql.connect(database=MYSQL_DB_NAME,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
host=MYSQL_HOST,
port=MYSQL_PORT)
cur = conn.cursor()
cur.execute(q)
subsegment = xray_recorder.current_segment().subsegments[-1]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'MySQL'
assert sql['user'] == MYSQL_USER
assert sql['driver_version'] == 'PyMySQL'
assert sql['database_version']
def test_execute_bad_query():
q = "SELECT blarg"
conn = pymysql.connect(database=MYSQL_DB_NAME,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
host=MYSQL_HOST,
port=MYSQL_PORT)
cur = conn.cursor()
try:
cur.execute(q)
except Exception:
pass
subsegment = xray_recorder.current_segment().subsegments[-1]
assert subsegment.name == "execute"
sql = subsegment.sql
assert sql['database_type'] == 'MySQL'
assert sql['user'] == MYSQL_USER
assert sql['driver_version'] == 'PyMySQL'
assert sql['database_version']
exception = subsegment.cause['exceptions'][0]
assert exception.type is not None