|
| 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 | + |
| 15 | +import pyodbc |
| 16 | +from testing_support.db_settings import postgresql_settings |
| 17 | +from testing_support.validators.validate_database_trace_inputs import ( |
| 18 | + validate_database_trace_inputs, |
| 19 | +) |
| 20 | +from testing_support.validators.validate_transaction_metrics import ( |
| 21 | + validate_transaction_metrics, |
| 22 | +) |
| 23 | + |
| 24 | +from newrelic.api.background_task import background_task |
| 25 | + |
| 26 | +DB_SETTINGS = postgresql_settings()[0] |
| 27 | + |
| 28 | +_test_execute_via_cursor_scoped_metrics = [ |
| 29 | + ("Function/postgresql.driver.dbapi20:connect", 1), |
| 30 | + ("Function/postgresql.driver.dbapi20:Connection.__enter__", 1), |
| 31 | + ("Function/postgresql.driver.dbapi20:Connection.__exit__", 1), |
| 32 | + ("Datastore/statement/Postgres/%s/select" % DB_SETTINGS["table_name"], 1), |
| 33 | + ("Datastore/statement/Postgres/%s/insert" % DB_SETTINGS["table_name"], 1), |
| 34 | + ("Datastore/statement/Postgres/%s/update" % DB_SETTINGS["table_name"], 1), |
| 35 | + ("Datastore/statement/Postgres/%s/delete" % DB_SETTINGS["table_name"], 1), |
| 36 | + ("Datastore/statement/Postgres/now/call", 1), |
| 37 | + ("Datastore/statement/Postgres/pg_sleep/call", 1), |
| 38 | + ("Datastore/operation/Postgres/drop", 1), |
| 39 | + ("Datastore/operation/Postgres/create", 1), |
| 40 | + ("Datastore/operation/Postgres/commit", 3), |
| 41 | + ("Datastore/operation/Postgres/rollback", 1), |
| 42 | +] |
| 43 | + |
| 44 | +_test_execute_via_cursor_rollup_metrics = [ |
| 45 | + ("Datastore/all", 13), |
| 46 | + ("Datastore/allOther", 13), |
| 47 | + ("Datastore/Postgres/all", 13), |
| 48 | + ("Datastore/Postgres/allOther", 13), |
| 49 | + ("Datastore/operation/Postgres/select", 1), |
| 50 | + ("Datastore/statement/Postgres/%s/select" % DB_SETTINGS["table_name"], 1), |
| 51 | + ("Datastore/operation/Postgres/insert", 1), |
| 52 | + ("Datastore/statement/Postgres/%s/insert" % DB_SETTINGS["table_name"], 1), |
| 53 | + ("Datastore/operation/Postgres/update", 1), |
| 54 | + ("Datastore/statement/Postgres/%s/update" % DB_SETTINGS["table_name"], 1), |
| 55 | + ("Datastore/operation/Postgres/delete", 1), |
| 56 | + ("Datastore/statement/Postgres/%s/delete" % DB_SETTINGS["table_name"], 1), |
| 57 | + ("Datastore/operation/Postgres/drop", 1), |
| 58 | + ("Datastore/operation/Postgres/create", 1), |
| 59 | + ("Datastore/statement/Postgres/now/call", 1), |
| 60 | + ("Datastore/statement/Postgres/pg_sleep/call", 1), |
| 61 | + ("Datastore/operation/Postgres/call", 2), |
| 62 | + ("Datastore/operation/Postgres/commit", 3), |
| 63 | + ("Datastore/operation/Postgres/rollback", 1), |
| 64 | +] |
| 65 | + |
| 66 | + |
| 67 | +@validate_transaction_metrics( |
| 68 | + "test_database:test_execute_via_cursor", |
| 69 | + scoped_metrics=_test_execute_via_cursor_scoped_metrics, |
| 70 | + rollup_metrics=_test_execute_via_cursor_rollup_metrics, |
| 71 | + background_task=True, |
| 72 | +) |
| 73 | +@validate_database_trace_inputs(sql_parameters_type=tuple) |
| 74 | +@background_task() |
| 75 | +def test_execute_via_cursor(): |
| 76 | + with pyodbc.connect( |
| 77 | + "DRIVER={Devart ODBC Driver for PostgreSQL};SERVER=%s;Port=%s;DATABASE=%s;UID=%s;PWD=%s" |
| 78 | + % ( |
| 79 | + DB_SETTINGS["host"], |
| 80 | + DB_SETTINGS["port"], |
| 81 | + DB_SETTINGS["name"], |
| 82 | + DB_SETTINGS["user"], |
| 83 | + DB_SETTINGS["password"], |
| 84 | + ) |
| 85 | + ) as connection: |
| 86 | + cursor = connection.cursor() |
| 87 | + cursor.execute("""drop table if exists %s""" % DB_SETTINGS["table_name"]) |
| 88 | + cursor.execute("""create table %s """ % DB_SETTINGS["table_name"] + """(a integer, b real, c text)""") |
| 89 | + cursor.executemany( |
| 90 | + """insert into %s """ % DB_SETTINGS["table_name"] + """values (%s, %s, %s)""", |
| 91 | + [(1, 1.0, "1.0"), (2, 2.2, "2.2"), (3, 3.3, "3.3")], |
| 92 | + ) |
| 93 | + cursor.execute("""select * from %s""" % DB_SETTINGS["table_name"]) |
| 94 | + for row in cursor: |
| 95 | + pass |
| 96 | + cursor.execute( |
| 97 | + """update %s """ % DB_SETTINGS["table_name"] + """set a=%s, b=%s, c=%s where a=%s""", |
| 98 | + (4, 4.0, "4.0", 1), |
| 99 | + ) |
| 100 | + cursor.execute("""delete from %s where a=2""" % DB_SETTINGS["table_name"]) |
| 101 | + connection.commit() |
| 102 | + |
| 103 | + cursor.callproc("now", ()) |
| 104 | + cursor.callproc("pg_sleep", (0.25,)) |
| 105 | + |
| 106 | + connection.rollback() |
| 107 | + connection.commit() |
| 108 | + |
| 109 | + |
| 110 | +_test_rollback_on_exception_scoped_metrics = [ |
| 111 | + ("Function/postgresql.driver.dbapi20:connect", 1), |
| 112 | + ("Function/postgresql.driver.dbapi20:Connection.__enter__", 1), |
| 113 | + ("Function/postgresql.driver.dbapi20:Connection.__exit__", 1), |
| 114 | + ("Datastore/operation/Postgres/rollback", 1), |
| 115 | +] |
| 116 | + |
| 117 | +_test_rollback_on_exception_rollup_metrics = [ |
| 118 | + ("Datastore/all", 2), |
| 119 | + ("Datastore/allOther", 2), |
| 120 | + ("Datastore/Postgres/all", 2), |
| 121 | + ("Datastore/Postgres/allOther", 2), |
| 122 | +] |
| 123 | + |
| 124 | + |
| 125 | +@validate_transaction_metrics( |
| 126 | + "test_database:test_rollback_on_exception", |
| 127 | + scoped_metrics=_test_rollback_on_exception_scoped_metrics, |
| 128 | + rollup_metrics=_test_rollback_on_exception_rollup_metrics, |
| 129 | + background_task=True, |
| 130 | +) |
| 131 | +@validate_database_trace_inputs(sql_parameters_type=tuple) |
| 132 | +@background_task() |
| 133 | +def test_rollback_on_exception(): |
| 134 | + try: |
| 135 | + with pyodbc.connect( |
| 136 | + "DRIVER={Devart ODBC Driver for PostgreSQL};SERVER=%s;Port=%s;DATABASE=%s;UID=%s;PWD=%s" |
| 137 | + % ( |
| 138 | + DB_SETTINGS["host"], |
| 139 | + DB_SETTINGS["port"], |
| 140 | + DB_SETTINGS["name"], |
| 141 | + DB_SETTINGS["user"], |
| 142 | + DB_SETTINGS["password"], |
| 143 | + ) |
| 144 | + ) as connection: |
| 145 | + raise RuntimeError("error") |
| 146 | + except RuntimeError: |
| 147 | + pass |
0 commit comments