-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathtest_query.py
97 lines (77 loc) · 3.62 KB
/
test_query.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pytest
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.context import Context
from aws_xray_sdk.ext.sqlalchemy.query import XRaySessionMaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String
from ...util import find_subsegment_by_annotation
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
@pytest.fixture()
def engine():
return create_engine('sqlite:///:memory:')
@pytest.fixture()
def session(engine):
"""Test Fixture to Create DataBase Tables and start a trace segment"""
engine = create_engine('sqlite:///:memory:')
xray_recorder.configure(service='test', sampling=False, context=Context())
xray_recorder.clear_trace_entities()
xray_recorder.begin_segment('SQLAlchemyTest')
Session = XRaySessionMaker(bind=engine)
Base.metadata.create_all(engine)
session = Session()
yield session
xray_recorder.end_segment()
xray_recorder.clear_trace_entities()
@pytest.fixture()
def connection(engine):
conn = engine.connect()
xray_recorder.configure(service='test', sampling=False, context=Context())
xray_recorder.clear_trace_entities()
xray_recorder.begin_segment('SQLAlchemyTest')
Session = XRaySessionMaker(bind=conn)
Base.metadata.create_all(engine)
session = Session()
yield session
xray_recorder.end_segment()
xray_recorder.clear_trace_entities()
def test_all(capsys, session):
""" Test calling all() on get all records.
Verify we run the query and return the SQL as metdata"""
# with capsys.disabled():
session.query(User).all()
subsegment = find_subsegment_by_annotation(xray_recorder.current_segment(), 'sqlalchemy', 'sqlalchemy.orm.query.all')
assert subsegment['annotations']['sqlalchemy'] == 'sqlalchemy.orm.query.all'
assert subsegment['sql']['sanitized_query']
assert subsegment['sql']['url']
def test_supports_connection(capsys, connection):
""" Test that XRaySessionMaker supports connection as well as engine"""
connection.query(User).all()
subsegment = find_subsegment_by_annotation(xray_recorder.current_segment(), 'sqlalchemy',
'sqlalchemy.orm.query.all')
assert subsegment['annotations']['sqlalchemy'] == 'sqlalchemy.orm.query.all'
def test_add(capsys, session):
""" Test calling add() on insert a row.
Verify we that we capture trace for the add"""
# with capsys.disabled():
john = User(name='John', fullname="John Doe", password="password")
session.add(john)
subsegment = find_subsegment_by_annotation(xray_recorder.current_segment(), 'sqlalchemy', 'sqlalchemy.orm.session.add')
assert subsegment['annotations']['sqlalchemy'] == 'sqlalchemy.orm.session.add'
assert subsegment['sql']['url']
def test_filter_first(capsys, session):
""" Test calling filter().first() on get first filtered records.
Verify we run the query and return the SQL as metdata"""
# with capsys.disabled():
session.query(User).filter(User.password=="mypassword!").first()
subsegment = find_subsegment_by_annotation(xray_recorder.current_segment(), 'sqlalchemy', 'sqlalchemy.orm.query.first')
assert subsegment['annotations']['sqlalchemy'] == 'sqlalchemy.orm.query.first'
assert subsegment['sql']['sanitized_query']
assert "mypassword!" not in subsegment['sql']['sanitized_query']
assert "users.password = ?" in subsegment['sql']['sanitized_query']
assert subsegment['sql']['url']