|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +pyodbc = pytest.importorskip("pyodbc") |
| 6 | + |
| 7 | +pytestmark = pytest.mark.pyodbc |
| 8 | + |
| 9 | + |
| 10 | +@pytest.yield_fixture(scope='function') |
| 11 | +def pyodbc_postgres_connection(request): |
| 12 | + conn_str = ( |
| 13 | + "DRIVER={PostgreSQL Unicode};" |
| 14 | + "DATABASE=%s;" |
| 15 | + "UID=%s;" |
| 16 | + "SERVER=%s;" |
| 17 | + "PORT=%s;" |
| 18 | + ) % ( |
| 19 | + os.environ.get('POSTGRES_DB', 'elasticapm_test'), |
| 20 | + os.environ.get('POSTGRES_USER', 'postgres'), |
| 21 | + os.environ.get('POSTGRES_HOST', None), |
| 22 | + os.environ.get('POSTGRES_PORT', None), |
| 23 | + ) |
| 24 | + conn = pyodbc.connect(conn_str) |
| 25 | + cursor = conn.cursor() |
| 26 | + cursor.execute( |
| 27 | + "CREATE TABLE test(id int, name VARCHAR(5) NOT NULL);" |
| 28 | + "INSERT INTO test VALUES (1, 'one'), (2, 'two'), (3, 'three');" |
| 29 | + ) |
| 30 | + |
| 31 | + yield conn |
| 32 | + |
| 33 | + # cleanup |
| 34 | + cursor.execute('ROLLBACK') |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.integrationtest |
| 38 | +def test_pyodbc_select(instrument, pyodbc_postgres_connection, elasticapm_client): |
| 39 | + cursor = pyodbc_postgres_connection.cursor() |
| 40 | + query = "SELECT * FROM test WHERE name LIKE 't%'" |
| 41 | + |
| 42 | + try: |
| 43 | + elasticapm_client.begin_transaction("web.django") |
| 44 | + cursor.execute(query) |
| 45 | + cursor.fetchall() |
| 46 | + elasticapm_client.end_transaction(None, "test-transaction") |
| 47 | + finally: |
| 48 | + transactions = elasticapm_client.instrumentation_store.get_all() |
| 49 | + spans = transactions[0]['spans'] |
| 50 | + span = spans[0] |
| 51 | + assert span['name'] == 'SELECT FROM test' |
| 52 | + assert 'db' in span['context'] |
| 53 | + assert span['context']['db']['type'] == 'sql' |
| 54 | + assert span['context']['db']['statement'] == query |
0 commit comments