11import pytest
2+ import sys
23from unittest .mock import patch , MagicMock , Mock , PropertyMock
34import gc
45
1415import databricks .sql
1516
1617
18+ def _forget_kernel_client_module ():
19+ sys .modules .pop ("databricks.sql.backend.kernel.client" , None )
20+ import databricks .sql .backend .kernel as kernel_pkg
21+
22+ if hasattr (kernel_pkg , "client" ):
23+ delattr (kernel_pkg , "client" )
24+
25+
1726class TestSession :
1827 """
1928 Unit tests for Session functionality
@@ -427,7 +436,6 @@ class TestKernelRetryOptionsThreading:
427436 PACKAGE = "databricks.sql"
428437
429438 def test_retry_kwargs_threaded_into_kernel_client (self ):
430- import sys
431439 import types
432440
433441 pytest .importorskip (
@@ -442,6 +450,7 @@ def test_retry_kwargs_threaded_into_kernel_client(self):
442450 fake = types .ModuleType ("databricks_sql_kernel" )
443451 fake .KernelError = type ("KernelError" , (Exception ,), {})
444452 fake .Session = MagicMock ()
453+ _forget_kernel_client_module ()
445454
446455 # Patch the kernel client class (imported lazily inside
447456 # _create_backend) and the provider builder; capture the kwargs
@@ -493,6 +502,7 @@ def test_azure_sp_m2m_kwargs_threaded_into_kernel_auth_options(self):
493502 fake = types .ModuleType ("databricks_sql_kernel" )
494503 fake .KernelError = type ("KernelError" , (Exception ,), {})
495504 fake .Session = MagicMock ()
505+ _forget_kernel_client_module ()
496506
497507 with patch .dict (sys .modules , {"databricks_sql_kernel" : fake }), patch (
498508 "databricks.sql.backend.kernel.client.KernelDatabricksClient"
@@ -527,6 +537,54 @@ def test_azure_sp_m2m_kwargs_threaded_into_kernel_auth_options(self):
527537 conn .close ()
528538
529539
540+ class TestKernelTelemetryOptionsThreading :
541+ """The kernel path must forward telemetry options from connect()
542+ into ``KernelDatabricksClient`` so phase-7 PyO3 Session kwargs can
543+ be populated before the kernel opens its session."""
544+
545+ PACKAGE = "databricks.sql"
546+
547+ def test_telemetry_kwargs_threaded_into_kernel_client (self ):
548+ import types
549+
550+ pytest .importorskip (
551+ "pyarrow" ,
552+ reason = "kernel client module imports pyarrow at load" ,
553+ )
554+
555+ fake = types .ModuleType ("databricks_sql_kernel" )
556+ fake .KernelError = type ("KernelError" , (Exception ,), {})
557+ fake .Session = MagicMock ()
558+ _forget_kernel_client_module ()
559+
560+ with patch .dict (sys .modules , {"databricks_sql_kernel" : fake }), patch (
561+ "databricks.sql.backend.kernel.client.KernelDatabricksClient"
562+ ) as mock_kernel_client , patch (
563+ "%s.session.get_python_sql_connector_auth_provider" % self .PACKAGE
564+ ):
565+ instance = mock_kernel_client .return_value
566+ instance .open_session .return_value = SessionId (
567+ BackendType .SEA , "sess-id" , None
568+ )
569+
570+ conn = databricks .sql .connect (
571+ server_hostname = "foo" ,
572+ http_path = "/sql/1.0/warehouses/abc" ,
573+ use_kernel = True ,
574+ access_token = "dapi-xyz" ,
575+ enable_telemetry = True ,
576+ force_enable_telemetry = False ,
577+ telemetry_batch_size = 17 ,
578+ )
579+ try :
580+ _ , kwargs = mock_kernel_client .call_args
581+ opts = kwargs ["telemetry_options" ]
582+ assert opts ["enable_telemetry" ] is True
583+ assert opts ["telemetry_batch_size" ] == 17
584+ finally :
585+ conn .close ()
586+
587+
530588class TestKernelUserAgentForwarding :
531589 """user_agent_entry must reach the kernel on the use_kernel path —
532590 session.py folds it into the composed User-Agent and includes it in
@@ -537,7 +595,6 @@ class TestKernelUserAgentForwarding:
537595 PACKAGE = "databricks.sql"
538596
539597 def test_user_agent_entry_reaches_kernel_client_http_headers (self ):
540- import sys
541598 import types
542599
543600 pytest .importorskip (
@@ -547,6 +604,7 @@ def test_user_agent_entry_reaches_kernel_client_http_headers(self):
547604 fake = types .ModuleType ("databricks_sql_kernel" )
548605 fake .KernelError = type ("KernelError" , (Exception ,), {})
549606 fake .Session = MagicMock ()
607+ _forget_kernel_client_module ()
550608
551609 with patch .dict (sys .modules , {"databricks_sql_kernel" : fake }), patch (
552610 "databricks.sql.backend.kernel.client.KernelDatabricksClient"
0 commit comments