77
88import pytest
99from unittest .mock import Mock , patch
10- import pyarrow
10+
11+ try :
12+ import pyarrow
13+ except ImportError :
14+ pyarrow = None
1115
1216from databricks .sql .backend .sea .result_set import SeaResultSet , Row
1317from databricks .sql .backend .sea .queue import JsonQueue
@@ -106,10 +110,11 @@ def result_set_with_data(
106110 def mock_arrow_queue (self ):
107111 """Create a mock Arrow queue."""
108112 queue = Mock ()
109- queue .next_n_rows .return_value = Mock (spec = pyarrow .Table )
110- queue .next_n_rows .return_value .num_rows = 0
111- queue .remaining_rows .return_value = Mock (spec = pyarrow .Table )
112- queue .remaining_rows .return_value .num_rows = 0
113+ if pyarrow is not None :
114+ queue .next_n_rows .return_value = Mock (spec = pyarrow .Table )
115+ queue .next_n_rows .return_value .num_rows = 0
116+ queue .remaining_rows .return_value = Mock (spec = pyarrow .Table )
117+ queue .remaining_rows .return_value .num_rows = 0
113118 return queue
114119
115120 @pytest .fixture
@@ -313,6 +318,7 @@ def test_convert_json_types(self, result_set_with_data, sample_data):
313318 assert converted_row [1 ] == 1 # "1" converted to int
314319 assert converted_row [2 ] is True # "true" converted to boolean
315320
321+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
316322 def test_convert_json_to_arrow_table (self , result_set_with_data , sample_data ):
317323 """Test the _convert_json_to_arrow_table method."""
318324 # Call _convert_json_to_arrow_table
@@ -323,6 +329,7 @@ def test_convert_json_to_arrow_table(self, result_set_with_data, sample_data):
323329 assert result_table .num_rows == len (sample_data )
324330 assert result_table .num_columns == 3
325331
332+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
326333 def test_convert_json_to_arrow_table_empty (self , result_set_with_data ):
327334 """Test the _convert_json_to_arrow_table method with empty data."""
328335 # Call _convert_json_to_arrow_table with empty data
@@ -380,6 +387,7 @@ def test_fetchall_json(self, result_set_with_data, sample_data):
380387 assert result == []
381388 assert result_set_with_data ._next_row_index == len (sample_data )
382389
390+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
383391 def test_fetchmany_arrow (self , result_set_with_data , sample_data ):
384392 """Test the fetchmany_arrow method."""
385393 # Test with JSON queue (should convert to Arrow)
@@ -388,13 +396,15 @@ def test_fetchmany_arrow(self, result_set_with_data, sample_data):
388396 assert result .num_rows == 2
389397 assert result_set_with_data ._next_row_index == 2
390398
399+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
391400 def test_fetchmany_arrow_negative_size (self , result_set_with_data ):
392401 """Test the fetchmany_arrow method with negative size."""
393402 with pytest .raises (
394403 ValueError , match = "size argument for fetchmany is -1 but must be >= 0"
395404 ):
396405 result_set_with_data .fetchmany_arrow (- 1 )
397406
407+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
398408 def test_fetchall_arrow (self , result_set_with_data , sample_data ):
399409 """Test the fetchall_arrow method."""
400410 # Test with JSON queue (should convert to Arrow)
@@ -497,6 +507,7 @@ def test_is_staging_operation(
497507 assert result_set .is_staging_operation is True
498508
499509 # Edge case tests
510+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
500511 def test_fetchone_empty_arrow_queue (self , result_set_with_arrow_queue ):
501512 """Test fetchone with an empty Arrow queue."""
502513 # Setup _convert_arrow_table to return empty list
@@ -525,6 +536,7 @@ def test_fetchone_empty_json_queue(self, result_set_with_json_queue):
525536 # Verify _create_json_table was called
526537 result_set_with_json_queue ._create_json_table .assert_called_once ()
527538
539+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
528540 def test_fetchmany_empty_arrow_queue (self , result_set_with_arrow_queue ):
529541 """Test fetchmany with an empty Arrow queue."""
530542 # Setup _convert_arrow_table to return empty list
@@ -539,6 +551,7 @@ def test_fetchmany_empty_arrow_queue(self, result_set_with_arrow_queue):
539551 # Verify _convert_arrow_table was called
540552 result_set_with_arrow_queue ._convert_arrow_table .assert_called_once ()
541553
554+ @pytest .mark .skipif (pyarrow is None , reason = "PyArrow is not installed" )
542555 def test_fetchall_empty_arrow_queue (self , result_set_with_arrow_queue ):
543556 """Test fetchall with an empty Arrow queue."""
544557 # Setup _convert_arrow_table to return empty list
@@ -599,29 +612,3 @@ def test_convert_json_types_with_logging(
599612
600613 # Verify warnings were logged
601614 assert mock_logger .warning .call_count == 2
602-
603- def test_import_coverage (self ):
604- """Test that import statements are covered."""
605- # Test pyarrow import coverage
606- try :
607- import pyarrow
608-
609- assert pyarrow is not None
610- except ImportError :
611- # This branch should be covered by the import statement
612- pass
613-
614- # Test TYPE_CHECKING import coverage
615- from typing import TYPE_CHECKING
616-
617- assert TYPE_CHECKING is not None
618-
619- def test_pyarrow_not_available (self ):
620- """Test behavior when pyarrow is not available."""
621- # This test covers the case where pyarrow import fails
622- # The actual import is done at module level, but we can test the behavior
623- with patch .dict ("sys.modules" , {"pyarrow" : None }):
624- # The module should still load even if pyarrow is None
625- from databricks .sql .backend .sea .result_set import SeaResultSet
626-
627- assert SeaResultSet is not None
0 commit comments