4848 # as of Jan 2018, psycopg2cffi doesn't have this module
4949 has_sql_module = False
5050
51+ try :
52+ import psycopg2 .extensions
53+ except ImportError :
54+ psycopg2 .extensions = None
55+
5156
5257pytestmark = pytest .mark .psycopg2
5358
@@ -259,7 +264,7 @@ def test_fully_qualified_table_name():
259264
260265@pytest .mark .integrationtest
261266@pytest .mark .skipif (not has_postgres_configured , reason = "PostgresSQL not configured" )
262- def test_psycopg2_register_type (postgres_connection , elasticapm_client ):
267+ def test_psycopg2_register_type (instrument , postgres_connection , elasticapm_client ):
263268 import psycopg2 .extras
264269
265270 elasticapm_client .begin_transaction ("web.django" )
@@ -271,7 +276,7 @@ def test_psycopg2_register_type(postgres_connection, elasticapm_client):
271276
272277@pytest .mark .integrationtest
273278@pytest .mark .skipif (not has_postgres_configured , reason = "PostgresSQL not configured" )
274- def test_psycopg2_register_json (postgres_connection , elasticapm_client ):
279+ def test_psycopg2_register_json (instrument , postgres_connection , elasticapm_client ):
275280 # register_json bypasses register_type, so we have to test unwrapping
276281 # separately
277282 import psycopg2 .extras
@@ -286,6 +291,40 @@ def test_psycopg2_register_json(postgres_connection, elasticapm_client):
286291 elasticapm_client .end_transaction (None , "test-transaction" )
287292
288293
294+ @pytest .mark .integrationtest
295+ @pytest .mark .skipif (not has_postgres_configured , reason = "PostgresSQL not configured" )
296+ @pytest .mark .skipif (
297+ not hasattr (psycopg2 .extensions , "quote_ident" ), reason = "psycopg2 driver doesn't have quote_ident extension"
298+ )
299+ def test_psycopg2_quote_ident (instrument , postgres_connection , elasticapm_client ):
300+ elasticapm_client .begin_transaction ("web.django" )
301+ ident = psycopg2 .extensions .quote_ident ("x'x" , postgres_connection )
302+ elasticapm_client .end_transaction (None , "test-transaction" )
303+
304+ assert ident == '"x\' x"'
305+
306+
307+ @pytest .mark .integrationtest
308+ @pytest .mark .skipif (not has_postgres_configured , reason = "PostgresSQL not configured" )
309+ @pytest .mark .skipif (
310+ not hasattr (psycopg2 .extensions , "encrypt_password" ),
311+ reason = "psycopg2 driver doesn't have encrypt_password extension" ,
312+ )
313+ @pytest .mark .skipif (
314+ hasattr (psycopg2 , "__libpq_version__" ) and psycopg2 .__libpq_version__ < 100000 ,
315+ reason = "test code requires libpq >= 10" ,
316+ )
317+ def test_psycopg2_encrypt_password (instrument , postgres_connection , elasticapm_client ):
318+ elasticapm_client .begin_transaction ("web.django" )
319+ pw1 = psycopg2 .extensions .encrypt_password ("user" , "password" , postgres_connection )
320+ pw2 = psycopg2 .extensions .encrypt_password ("user" , "password" , postgres_connection , None )
321+ pw3 = psycopg2 .extensions .encrypt_password ("user" , "password" , postgres_connection , algorithm = None )
322+ pw4 = psycopg2 .extensions .encrypt_password ("user" , "password" , scope = postgres_connection , algorithm = None )
323+ elasticapm_client .end_transaction (None , "test-transaction" )
324+
325+ assert pw1 .startswith ("md5" ) and (pw1 == pw2 == pw3 == pw4 )
326+
327+
289328@pytest .mark .integrationtest
290329@pytest .mark .skipif (not has_postgres_configured , reason = "PostgresSQL not configured" )
291330def test_psycopg2_tracing_outside_of_elasticapm_transaction (instrument , postgres_connection , elasticapm_client ):
0 commit comments