1+ """
2+ Tests for the ResultSetFilter class.
3+ """
4+
5+ import unittest
6+ from unittest .mock import MagicMock , patch
7+ import sys
8+ from typing import List , Dict , Any
9+
10+ # Add the necessary path to import the filter module
11+ sys .path .append ("/home/varun.edachali/conn/databricks-sql-python/src" )
12+
13+ from databricks .sql .backend .filters import ResultSetFilter
14+
15+
16+ class TestResultSetFilter (unittest .TestCase ):
17+ """Tests for the ResultSetFilter class."""
18+
19+ def setUp (self ):
20+ """Set up test fixtures."""
21+ # Create a mock SeaResultSet
22+ self .mock_sea_result_set = MagicMock ()
23+ self .mock_sea_result_set ._response = {
24+ "result" : {
25+ "data_array" : [
26+ ["catalog1" , "schema1" , "table1" , "TABLE" , "" ],
27+ ["catalog1" , "schema1" , "table2" , "VIEW" , "" ],
28+ ["catalog1" , "schema1" , "table3" , "SYSTEM TABLE" , "" ],
29+ ["catalog1" , "schema1" , "table4" , "EXTERNAL TABLE" , "" ],
30+ ],
31+ "row_count" : 4
32+ }
33+ }
34+
35+ # Set up the connection and other required attributes
36+ self .mock_sea_result_set .connection = MagicMock ()
37+ self .mock_sea_result_set ._sea_client = MagicMock ()
38+ self .mock_sea_result_set ._buffer_size_bytes = 1000
39+ self .mock_sea_result_set ._arraysize = 100
40+
41+ @patch ("databricks.sql.backend.filters.SeaResultSet" )
42+ def test_filter_tables_by_type (self , mock_sea_result_set_class ):
43+ """Test filtering tables by type."""
44+ # Set up the mock to return a new mock when instantiated
45+ mock_instance = MagicMock ()
46+ mock_sea_result_set_class .return_value = mock_instance
47+
48+ # Test with specific table types
49+ table_types = ["TABLE" , "VIEW" ]
50+
51+ # Make the mock_sea_result_set appear to be a SeaResultSet
52+ with patch ("databricks.sql.backend.filters.isinstance" , return_value = True ):
53+ result = ResultSetFilter .filter_tables_by_type (self .mock_sea_result_set , table_types )
54+
55+ # Verify the filter was applied correctly
56+ mock_sea_result_set_class .assert_called_once ()
57+ call_kwargs = mock_sea_result_set_class .call_args [1 ]
58+
59+ # Check that the filtered response contains only TABLE and VIEW
60+ filtered_data = call_kwargs ["sea_response" ]["result" ]["data_array" ]
61+ self .assertEqual (len (filtered_data ), 2 )
62+ self .assertEqual (filtered_data [0 ][3 ], "TABLE" )
63+ self .assertEqual (filtered_data [1 ][3 ], "VIEW" )
64+
65+ # Check row count was updated
66+ self .assertEqual (call_kwargs ["sea_response" ]["result" ]["row_count" ], 2 )
67+
68+ @patch ("databricks.sql.backend.filters.SeaResultSet" )
69+ def test_filter_tables_by_type_case_insensitive (self , mock_sea_result_set_class ):
70+ """Test filtering tables by type with case insensitivity."""
71+ # Set up the mock to return a new mock when instantiated
72+ mock_instance = MagicMock ()
73+ mock_sea_result_set_class .return_value = mock_instance
74+
75+ # Test with lowercase table types
76+ table_types = ["table" , "view" ]
77+
78+ # Make the mock_sea_result_set appear to be a SeaResultSet
79+ with patch ("databricks.sql.backend.filters.isinstance" , return_value = True ):
80+ result = ResultSetFilter .filter_tables_by_type (self .mock_sea_result_set , table_types )
81+
82+ # Verify the filter was applied correctly
83+ mock_sea_result_set_class .assert_called_once ()
84+ call_kwargs = mock_sea_result_set_class .call_args [1 ]
85+
86+ # Check that the filtered response contains only TABLE and VIEW
87+ filtered_data = call_kwargs ["sea_response" ]["result" ]["data_array" ]
88+ self .assertEqual (len (filtered_data ), 2 )
89+ self .assertEqual (filtered_data [0 ][3 ], "TABLE" )
90+ self .assertEqual (filtered_data [1 ][3 ], "VIEW" )
91+
92+ @patch ("databricks.sql.backend.filters.SeaResultSet" )
93+ def test_filter_tables_by_type_default (self , mock_sea_result_set_class ):
94+ """Test filtering tables by type with default types."""
95+ # Set up the mock to return a new mock when instantiated
96+ mock_instance = MagicMock ()
97+ mock_sea_result_set_class .return_value = mock_instance
98+
99+ # Make the mock_sea_result_set appear to be a SeaResultSet
100+ with patch ("databricks.sql.backend.filters.isinstance" , return_value = True ):
101+ result = ResultSetFilter .filter_tables_by_type (self .mock_sea_result_set , None )
102+
103+ # Verify the filter was applied correctly
104+ mock_sea_result_set_class .assert_called_once ()
105+ call_kwargs = mock_sea_result_set_class .call_args [1 ]
106+
107+ # Check that the filtered response contains TABLE, VIEW, and SYSTEM TABLE
108+ filtered_data = call_kwargs ["sea_response" ]["result" ]["data_array" ]
109+ self .assertEqual (len (filtered_data ), 3 )
110+ self .assertEqual (filtered_data [0 ][3 ], "TABLE" )
111+ self .assertEqual (filtered_data [1 ][3 ], "VIEW" )
112+ self .assertEqual (filtered_data [2 ][3 ], "SYSTEM TABLE" )
113+
114+
115+ if __name__ == "__main__" :
116+ unittest .main ()
0 commit comments