def test_configuration_routine(self): """Ensure that the integration routines can be configured.""" routine_config = dict(patch={ 'vertica_python.vertica.connection.Connection': dict(routines=dict(cursor=dict( operation_name='get_cursor', trace_enabled=True, ), ), ), }, ) # Make a copy of the vertica config first before we merge our settings over # DEV: First argument gets merged into the second copy = deepmerge(config.vertica, dict()) overrides = deepmerge(routine_config, copy) with self.override_config('vertica', overrides): patch() import vertica_python test_tracer = get_dummy_tracer() conn = vertica_python.connect(**VERTICA_CONFIG) Pin.override(conn, service='mycustomservice', tracer=test_tracer) conn.cursor() # should be traced now conn.close() spans = test_tracer.writer.pop() assert len(spans) == 1 assert spans[0].name == 'get_cursor' assert spans[0].service == 'mycustomservice'
def test_patch_before_import(self): patch() import vertica_python # use a patched method from each class as indicators assert isinstance(vertica_python.Connection.cursor, wrapt.ObjectProxy) assert isinstance(vertica_python.vertica.cursor.Cursor.execute, wrapt.ObjectProxy)
def test_unpatch_after_import(self): patch() import vertica_python unpatch() assert not isinstance(vertica_python.Connection.cursor, wrapt.ObjectProxy) assert not isinstance(vertica_python.vertica.cursor.Cursor.execute, wrapt.ObjectProxy)
def test_idempotent_patch(self): patch() patch() import vertica_python assert not isinstance(vertica_python.Connection.cursor.__wrapped__, wrapt.ObjectProxy) assert not isinstance( vertica_python.vertica.cursor.Cursor.execute.__wrapped__, wrapt.ObjectProxy) assert isinstance(vertica_python.Connection.cursor, wrapt.ObjectProxy) assert isinstance(vertica_python.vertica.cursor.Cursor.execute, wrapt.ObjectProxy)
def test_configuration_service_name(self): """Ensure that the integration can be configured.""" with self.override_config('vertica', dict(service_name='test_svc_name')): patch() import vertica_python test_tracer = get_dummy_tracer() conn = vertica_python.connect(**VERTICA_CONFIG) cur = conn.cursor() Pin.override(cur, tracer=test_tracer) with conn: cur.execute('DROP TABLE IF EXISTS {}'.format(TEST_TABLE)) spans = test_tracer.writer.pop() assert len(spans) == 1 assert spans[0].service == 'test_svc_name'
def test_conn(request, test_tracer): oteltrace.tracer = test_tracer patch() import vertica_python # must happen AFTER installing with patch() conn = vertica_python.connect(**VERTICA_CONFIG) cur = conn.cursor() cur.execute('DROP TABLE IF EXISTS {}'.format(TEST_TABLE)) cur.execute("""CREATE TABLE {} ( a INT, b VARCHAR(32) ) """.format(TEST_TABLE)) test_tracer.writer.pop() request.cls.test_conn = (conn, cur) return conn, cur
def test_patch_after_import(self): """Patching _after_ the import will not work because we hook into the module import system. Vertica uses a local reference to `Cursor` which won't get patched if we call `patch` after the module has already been imported. """ import vertica_python assert not isinstance( vertica_python.vertica.connection.Connection.cursor, wrapt.ObjectProxy) assert not isinstance(vertica_python.vertica.cursor.Cursor.execute, wrapt.ObjectProxy) patch() conn = vertica_python.connect(**VERTICA_CONFIG) cursor = conn.cursor() assert not isinstance(cursor, wrapt.ObjectProxy)