Skip to content

Commit f43a05b

Browse files
authored
Merge pull request #1508 from newrelic/fix-psycopg-as-string
Fix psycopg as_string()
1 parent 66348e1 commit f43a05b

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

newrelic/hooks/database_psycopg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def _add_defaults(parsed_host, parsed_hostaddr, parsed_port, parsed_database):
418418

419419

420420
def wrapper_psycopg_as_string(wrapped, instance, args, kwargs):
421-
def _bind_params(context, *args, **kwargs):
421+
def _bind_params(context=None, *args, **kwargs):
422422
return context, args, kwargs
423423

424424
context, _args, _kwargs = _bind_params(*args, **kwargs)

tests/datastore_psycopg/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from testing_support.fixture.event_loop import event_loop as loop
1818
from testing_support.fixtures import collector_agent_registration_fixture, collector_available_fixture
1919

20+
from newrelic.common.package_version_utils import get_package_version_tuple
21+
2022
_default_settings = {
2123
"package_reporting.enabled": False, # Turn off package reporting for testing as it causes slow downs.
2224
"transaction_tracer.explain_threshold": 0.0,
@@ -36,6 +38,7 @@
3638

3739
DB_MULTIPLE_SETTINGS = postgresql_settings()
3840
DB_SETTINGS = DB_MULTIPLE_SETTINGS[0]
41+
PSYCOPG_VERSION = get_package_version_tuple("psycopg")
3942

4043

4144
@pytest.fixture(scope="session", params=["sync", "async"])

tests/datastore_psycopg/test_as_string.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
from conftest import PSYCOPG_VERSION
17+
1518
try:
1619
from psycopg import sql
1720
except ImportError:
@@ -109,3 +112,12 @@ def test_as_string_10(connection):
109112
)
110113
result = q2.as_string(connection)
111114
assert result == 'insert into table ("foo", "bar", "baz") values (%(foo)s, %(bar)s, %(baz)s)'
115+
116+
117+
@pytest.mark.skipif(PSYCOPG_VERSION < (3, 2, 0), reason="This signature was changed in psycopg 3.2.0")
118+
@background_task()
119+
def test_as_string_11(connection):
120+
ident = sql.Identifier("foo")
121+
# No context provided to as_string(), should not raise an error
122+
result = ident.as_string()
123+
assert result == '"foo"'

0 commit comments

Comments
 (0)