|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import pytest |
| 15 | +from testing_support.db_settings import postgresql_settings |
| 16 | +from testing_support.validators.validate_database_trace_inputs import ( |
| 17 | + validate_database_trace_inputs, |
| 18 | +) |
| 19 | +from testing_support.validators.validate_transaction_metrics import ( |
| 20 | + validate_transaction_metrics, |
| 21 | +) |
| 22 | + |
| 23 | +from newrelic.api.background_task import background_task |
| 24 | + |
| 25 | +DB_SETTINGS = postgresql_settings()[0] |
| 26 | + |
| 27 | + |
| 28 | +@validate_transaction_metrics( |
| 29 | + "test_pyodbc:test_execute_via_cursor", |
| 30 | + scoped_metrics=[ |
| 31 | + ("Function/pyodbc:connect", 1), |
| 32 | + ], |
| 33 | + rollup_metrics=[ |
| 34 | + ("Datastore/all", 1), |
| 35 | + ("Datastore/allOther", 1), |
| 36 | + ("Datastore/ODBC/all", 1), |
| 37 | + ("Datastore/ODBC/allOther", 1), |
| 38 | + ], |
| 39 | + background_task=True, |
| 40 | +) |
| 41 | +@validate_database_trace_inputs(sql_parameters_type=tuple) |
| 42 | +@background_task() |
| 43 | +def test_execute_via_cursor(pyodbc_driver): |
| 44 | + import pyodbc |
| 45 | + |
| 46 | + with pyodbc.connect( |
| 47 | + "DRIVER={%s};SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s" |
| 48 | + % ( |
| 49 | + pyodbc_driver, |
| 50 | + DB_SETTINGS["host"], |
| 51 | + DB_SETTINGS["port"], |
| 52 | + DB_SETTINGS["name"], |
| 53 | + DB_SETTINGS["user"], |
| 54 | + DB_SETTINGS["password"], |
| 55 | + ) |
| 56 | + ) as connection: |
| 57 | + cursor = connection.cursor() |
| 58 | + cursor.execute("""drop table if exists %s""" % DB_SETTINGS["table_name"]) |
| 59 | + cursor.execute("""create table %s """ % DB_SETTINGS["table_name"] + """(a integer, b real, c text)""") |
| 60 | + cursor.executemany( |
| 61 | + """insert into %s """ % DB_SETTINGS["table_name"] + """values (?, ?, ?)""", |
| 62 | + [(1, 1.0, "1.0"), (2, 2.2, "2.2"), (3, 3.3, "3.3")], |
| 63 | + ) |
| 64 | + cursor.execute("""select * from %s""" % DB_SETTINGS["table_name"]) |
| 65 | + for row in cursor: |
| 66 | + pass |
| 67 | + cursor.execute( |
| 68 | + """update %s """ % DB_SETTINGS["table_name"] + """set a=?, b=?, c=? where a=?""", |
| 69 | + (4, 4.0, "4.0", 1), |
| 70 | + ) |
| 71 | + cursor.execute("""delete from %s where a=2""" % DB_SETTINGS["table_name"]) |
| 72 | + connection.commit() |
| 73 | + |
| 74 | + cursor.execute("SELECT now()") |
| 75 | + cursor.execute("SELECT pg_sleep(0.25)") |
| 76 | + |
| 77 | + connection.rollback() |
| 78 | + connection.commit() |
| 79 | + |
| 80 | + |
| 81 | +@validate_transaction_metrics( |
| 82 | + "test_pyodbc:test_rollback_on_exception", |
| 83 | + scoped_metrics=[ |
| 84 | + ("Function/pyodbc:connect", 1), |
| 85 | + ], |
| 86 | + rollup_metrics=[ |
| 87 | + ("Datastore/all", 1), |
| 88 | + ("Datastore/allOther", 1), |
| 89 | + ("Datastore/ODBC/all", 1), |
| 90 | + ("Datastore/ODBC/allOther", 1), |
| 91 | + ], |
| 92 | + background_task=True, |
| 93 | +) |
| 94 | +@validate_database_trace_inputs(sql_parameters_type=tuple) |
| 95 | +@background_task() |
| 96 | +def test_rollback_on_exception(pyodbc_driver): |
| 97 | + import pyodbc |
| 98 | + |
| 99 | + with pytest.raises(RuntimeError): |
| 100 | + with pyodbc.connect( |
| 101 | + "DRIVER={%s};SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s" |
| 102 | + % ( |
| 103 | + pyodbc_driver, |
| 104 | + DB_SETTINGS["host"], |
| 105 | + DB_SETTINGS["port"], |
| 106 | + DB_SETTINGS["name"], |
| 107 | + DB_SETTINGS["user"], |
| 108 | + DB_SETTINGS["password"], |
| 109 | + ) |
| 110 | + ) as connection: |
| 111 | + raise RuntimeError("error") |
| 112 | + |
| 113 | + |
| 114 | +@pytest.fixture |
| 115 | +def pyodbc_driver(): |
| 116 | + import pyodbc |
| 117 | + |
| 118 | + driver_name = "PostgreSQL Unicode" |
| 119 | + assert driver_name in pyodbc.drivers() |
| 120 | + return driver_name |
0 commit comments