-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests for pyodbc * Move imports into tests to get import coverage * Fixup: remove time import * Trigger tests --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
363122a
commit 7103506
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from testing_support.fixtures import ( # noqa: F401; pylint: disable=W0611 | ||
collector_agent_registration_fixture, | ||
collector_available_fixture, | ||
) | ||
|
||
_default_settings = { | ||
"transaction_tracer.explain_threshold": 0.0, | ||
"transaction_tracer.transaction_threshold": 0.0, | ||
"transaction_tracer.stack_trace_threshold": 0.0, | ||
"debug.log_data_collector_payloads": True, | ||
"debug.record_transaction_failure": True, | ||
"debug.log_explain_plan_queries": True, | ||
} | ||
|
||
collector_agent_registration = collector_agent_registration_fixture( | ||
app_name="Python Agent Test (datastore_pyodbc)", | ||
default_settings=_default_settings, | ||
linked_applications=["Python Agent Test (datastore)"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pytest | ||
from testing_support.db_settings import postgresql_settings | ||
from testing_support.validators.validate_database_trace_inputs import ( | ||
validate_database_trace_inputs, | ||
) | ||
from testing_support.validators.validate_transaction_metrics import ( | ||
validate_transaction_metrics, | ||
) | ||
|
||
from newrelic.api.background_task import background_task | ||
|
||
DB_SETTINGS = postgresql_settings()[0] | ||
|
||
|
||
@validate_transaction_metrics( | ||
"test_pyodbc:test_execute_via_cursor", | ||
scoped_metrics=[ | ||
("Function/pyodbc:connect", 1), | ||
], | ||
rollup_metrics=[ | ||
("Datastore/all", 1), | ||
("Datastore/allOther", 1), | ||
("Datastore/ODBC/all", 1), | ||
("Datastore/ODBC/allOther", 1), | ||
], | ||
background_task=True, | ||
) | ||
@validate_database_trace_inputs(sql_parameters_type=tuple) | ||
@background_task() | ||
def test_execute_via_cursor(pyodbc_driver): | ||
import pyodbc | ||
|
||
with pyodbc.connect( | ||
"DRIVER={%s};SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s" | ||
% ( | ||
pyodbc_driver, | ||
DB_SETTINGS["host"], | ||
DB_SETTINGS["port"], | ||
DB_SETTINGS["name"], | ||
DB_SETTINGS["user"], | ||
DB_SETTINGS["password"], | ||
) | ||
) as connection: | ||
cursor = connection.cursor() | ||
cursor.execute("""drop table if exists %s""" % DB_SETTINGS["table_name"]) | ||
cursor.execute("""create table %s """ % DB_SETTINGS["table_name"] + """(a integer, b real, c text)""") | ||
cursor.executemany( | ||
"""insert into %s """ % DB_SETTINGS["table_name"] + """values (?, ?, ?)""", | ||
[(1, 1.0, "1.0"), (2, 2.2, "2.2"), (3, 3.3, "3.3")], | ||
) | ||
cursor.execute("""select * from %s""" % DB_SETTINGS["table_name"]) | ||
for row in cursor: | ||
pass | ||
cursor.execute( | ||
"""update %s """ % DB_SETTINGS["table_name"] + """set a=?, b=?, c=? where a=?""", | ||
(4, 4.0, "4.0", 1), | ||
) | ||
cursor.execute("""delete from %s where a=2""" % DB_SETTINGS["table_name"]) | ||
connection.commit() | ||
|
||
cursor.execute("SELECT now()") | ||
cursor.execute("SELECT pg_sleep(0.25)") | ||
|
||
connection.rollback() | ||
connection.commit() | ||
|
||
|
||
@validate_transaction_metrics( | ||
"test_pyodbc:test_rollback_on_exception", | ||
scoped_metrics=[ | ||
("Function/pyodbc:connect", 1), | ||
], | ||
rollup_metrics=[ | ||
("Datastore/all", 1), | ||
("Datastore/allOther", 1), | ||
("Datastore/ODBC/all", 1), | ||
("Datastore/ODBC/allOther", 1), | ||
], | ||
background_task=True, | ||
) | ||
@validate_database_trace_inputs(sql_parameters_type=tuple) | ||
@background_task() | ||
def test_rollback_on_exception(pyodbc_driver): | ||
import pyodbc | ||
|
||
with pytest.raises(RuntimeError): | ||
with pyodbc.connect( | ||
"DRIVER={%s};SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s" | ||
% ( | ||
pyodbc_driver, | ||
DB_SETTINGS["host"], | ||
DB_SETTINGS["port"], | ||
DB_SETTINGS["name"], | ||
DB_SETTINGS["user"], | ||
DB_SETTINGS["password"], | ||
) | ||
) as connection: | ||
raise RuntimeError("error") | ||
|
||
|
||
@pytest.fixture | ||
def pyodbc_driver(): | ||
import pyodbc | ||
|
||
driver_name = "PostgreSQL Unicode" | ||
assert driver_name in pyodbc.drivers() | ||
return driver_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters