|
1 |
| -from sqlalchemy import Column, ForeignKey, Integer, String |
| 1 | +import sys |
| 2 | +import pytest |
| 3 | + |
| 4 | +from sqlalchemy import Column, ForeignKey, Integer, String, create_engine |
| 5 | +from sqlalchemy.exc import IntegrityError |
2 | 6 | from sqlalchemy.ext.declarative import declarative_base
|
3 | 7 | from sqlalchemy.orm import relationship, sessionmaker
|
4 |
| -from sqlalchemy import create_engine |
5 | 8 |
|
| 9 | +import sentry_sdk |
6 | 10 | from sentry_sdk import capture_message
|
7 | 11 | from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
|
8 | 12 |
|
@@ -63,3 +67,70 @@ class Address(Base):
|
63 | 67 | "type": "default",
|
64 | 68 | },
|
65 | 69 | ]
|
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.skipif( |
| 73 | + sys.version_info < (3,), reason="This sqla usage seems to be broken on Py2" |
| 74 | +) |
| 75 | +def test_transactions(sentry_init, capture_events, render_span_tree): |
| 76 | + |
| 77 | + sentry_init( |
| 78 | + integrations=[SqlalchemyIntegration()], _experiments={"record_sql_params": True} |
| 79 | + ) |
| 80 | + events = capture_events() |
| 81 | + |
| 82 | + Base = declarative_base() # noqa: N806 |
| 83 | + |
| 84 | + class Person(Base): |
| 85 | + __tablename__ = "person" |
| 86 | + id = Column(Integer, primary_key=True) |
| 87 | + name = Column(String(250), nullable=False) |
| 88 | + |
| 89 | + class Address(Base): |
| 90 | + __tablename__ = "address" |
| 91 | + id = Column(Integer, primary_key=True) |
| 92 | + street_name = Column(String(250)) |
| 93 | + street_number = Column(String(250)) |
| 94 | + post_code = Column(String(250), nullable=False) |
| 95 | + person_id = Column(Integer, ForeignKey("person.id")) |
| 96 | + person = relationship(Person) |
| 97 | + |
| 98 | + engine = create_engine("sqlite:///:memory:") |
| 99 | + Base.metadata.create_all(engine) |
| 100 | + |
| 101 | + Session = sessionmaker(bind=engine) # noqa: N806 |
| 102 | + session = Session() |
| 103 | + |
| 104 | + with sentry_sdk.start_span(transaction="test_transaction", sampled=True): |
| 105 | + with session.begin_nested(): |
| 106 | + session.query(Person).first() |
| 107 | + |
| 108 | + for _ in range(2): |
| 109 | + with pytest.raises(IntegrityError): |
| 110 | + with session.begin_nested(): |
| 111 | + session.add(Person(id=1, name="bob")) |
| 112 | + session.add(Person(id=1, name="bob")) |
| 113 | + |
| 114 | + with session.begin_nested(): |
| 115 | + session.query(Person).first() |
| 116 | + |
| 117 | + (event,) = events |
| 118 | + |
| 119 | + assert ( |
| 120 | + render_span_tree(event) |
| 121 | + == """\ |
| 122 | +- op=None: description=None |
| 123 | + - op='db': description='SAVEPOINT sa_savepoint_1' |
| 124 | + - op='db': description='SELECT person.id AS person_id, person.name AS person_name \\nFROM person\\n LIMIT ? OFFSET ?' |
| 125 | + - op='db': description='RELEASE SAVEPOINT sa_savepoint_1' |
| 126 | + - op='db': description='SAVEPOINT sa_savepoint_2' |
| 127 | + - op='db': description='INSERT INTO person (id, name) VALUES (?, ?)' |
| 128 | + - op='db': description='ROLLBACK TO SAVEPOINT sa_savepoint_2' |
| 129 | + - op='db': description='SAVEPOINT sa_savepoint_3' |
| 130 | + - op='db': description='INSERT INTO person (id, name) VALUES (?, ?)' |
| 131 | + - op='db': description='ROLLBACK TO SAVEPOINT sa_savepoint_3' |
| 132 | + - op='db': description='SAVEPOINT sa_savepoint_4' |
| 133 | + - op='db': description='SELECT person.id AS person_id, person.name AS person_name \\nFROM person\\n LIMIT ? OFFSET ?' |
| 134 | + - op='db': description='RELEASE SAVEPOINT sa_savepoint_4'\ |
| 135 | +""" |
| 136 | + ) |
0 commit comments