@@ -86,18 +86,7 @@ def test_autocomplete_operation_functions(self, client_mock):
8686 mock_client_instance .create_session .return_value = {'sessionHandle' : self .TEST_SESSION_HANDLE }
8787 mock_client_instance .info .return_value = {'version' : '2.0.0' }
8888 mock_client_instance .execute_statement .return_value = {'operationHandle' : self .TEST_OPERATION_HANDLE }
89- mock_client_instance .fetch_results .return_value = {
90- 'resultType' : 'PAYLOAD' ,
91- 'resultKind' : 'SUCCESS_WITH_CONTENT' ,
92- 'results' : {
93- 'columns' : [{'name' : 'function name' , 'logicalType' : {'type' : 'VARCHAR' , 'nullable' : True , 'length' : 1000 }}],
94- 'rowFormat' : 'JSON' ,
95- 'data' : [
96- {'kind' : 'INSERT' , 'fields' : ['lower' ]},
97- {'kind' : 'INSERT' , 'fields' : ['upper' ]}
98- ]},
99- 'nextResultUri' : f'/v3/sessions/{ self .TEST_SESSION_HANDLE } /operations/{ self .TEST_OPERATION_HANDLE } /result/1?rowFormat=JSON'
100- }
89+ mock_client_instance .fetch_results .return_value = self ._list_function_payload (['lower' , 'upper' ])
10190
10291 # and: FlinkSqlApi instance with configuration
10392 flink_api = FlinkSqlApi (self .user , interpreter = self .interpreter )
@@ -111,7 +100,54 @@ def test_autocomplete_operation_functions(self, client_mock):
111100
112101 # then
113102 mock_client_instance .execute_statement .assert_called_once_with (self .TEST_SESSION_HANDLE , 'SHOW FUNCTIONS' )
114- assert autocomplete_result == {'functions' : [{'name' : 'lower' }, {'name' : 'upper' }]}
103+ self ._assert_autocomplete_functions (autocomplete_result , ['lower' , 'upper' ])
104+
105+ @patch ('notebook.connectors.flink_sql.FlinkSqlClient' )
106+ def test_autocomplete_operation_functions_list_all (self , client_mock ):
107+ # given: mock interactions
108+ def mock_execute_statement (session_handle , statement ):
109+ responses = {
110+ 'SHOW CATALOGS' : {'operationHandle' : 'show-catalogs' },
111+ 'SHOW DATABASES IN `test_catalog`' : {'operationHandle' : 'show-databases' },
112+ 'SHOW FUNCTIONS IN `test_catalog`.`db_a`' : {'operationHandle' : 'show-fns-dba' },
113+ 'SHOW USER FUNCTIONS IN `test_catalog`.`db_a`' : {'operationHandle' : 'show-user-fns-dba' },
114+ 'SHOW USER FUNCTIONS IN `test_catalog`.`db_b`' : {'operationHandle' : 'show-user-fns-dbb' },
115+ }
116+ return responses .get (statement )
117+
118+ def mock_fetch_results (session_handle , operation_handle , token ):
119+ responses = {
120+ 'show-catalogs' : self ._list_function_payload (['test_catalog' ]),
121+ 'show-databases' : self ._list_function_payload (['db_a' , 'db_b' ]),
122+ 'show-fns-dba' : self ._list_function_payload (['test_fun_a' , 'lower' , 'upper' ]),
123+ 'show-user-fns-dba' : self ._list_function_payload (['test_fun_a' ]),
124+ 'show-user-fns-dbb' : self ._list_function_payload (['test_fun_b' ]),
125+ }
126+ return responses .get (operation_handle )
127+
128+ mock_client_instance = MagicMock ()
129+ client_mock .return_value = mock_client_instance
130+ mock_client_instance .create_session .return_value = {'sessionHandle' : self .TEST_SESSION_HANDLE }
131+ mock_client_instance .info .return_value = {'version' : '2.0.0' }
132+ mock_client_instance .execute_statement .side_effect = mock_execute_statement
133+ mock_client_instance .fetch_results .side_effect = mock_fetch_results
134+
135+ # and: FlinkSqlApi instance with configuration
136+ self .interpreter ['options' ]['list_all_functions' ] = True
137+ self .interpreter ['options' ]['default_catalog' ] = 'test_catalog'
138+ self .interpreter ['options' ]['default_database' ] = 'db_a'
139+ flink_api = FlinkSqlApi (self .user , interpreter = self .interpreter )
140+
141+ # and: session is created
142+ flink_api .create_session (lang = 'flink' , properties = None )
143+
144+ # when
145+ autocomplete_result = flink_api .autocomplete (snippet = 'dummy' , database = None , table = None , column = None ,
146+ nested = None , operation = 'functions' )
147+
148+ # then
149+ self ._assert_autocomplete_functions (autocomplete_result , ['lower' , 'upper' , 'test_catalog.db_a.test_fun_a' ,
150+ 'test_catalog.db_b.test_fun_b' ])
115151
116152 @patch ('notebook.connectors.flink_sql.FlinkSqlClient' )
117153 def test_autocomplete_operation_function_flink_1_x (self , client_mock ):
@@ -164,7 +200,8 @@ def test_autocomplete_operation_function_flink_2_x(self, client_mock):
164200 {'kind' : 'INSERT' , 'fields' : ['signature' , 'default_catalog.default_db.test_function(values <ANY>...)' ]},
165201 ]
166202 },
167- 'nextResultUri' : f'/v3/sessions/{ self .TEST_SESSION_HANDLE } /operations/{ self .TEST_OPERATION_HANDLE } /result/1?rowFormat=JSON'
203+ 'nextResultUri' :
204+ f'/v3/sessions/{ self .TEST_SESSION_HANDLE } /operations/{ self .TEST_OPERATION_HANDLE } /result/1?rowFormat=JSON'
168205 }
169206
170207 # and: FlinkSqlApi instance with configuration
@@ -185,3 +222,24 @@ def test_autocomplete_operation_function_flink_2_x(self, client_mock):
185222 assert autocomplete_result == {
186223 'function' : {'name' : 'test_function' , 'signature' : 'default_catalog.default_db.test_function(values <ANY>...)' }
187224 }
225+
226+ def _list_function_payload (self , expected_functions , session_handle = None , operation_handle = None , token = 0 ):
227+ session_handle = session_handle if session_handle else self .TEST_SESSION_HANDLE
228+ operation_handle = operation_handle if operation_handle else self .TEST_OPERATION_HANDLE
229+
230+ return {
231+ 'resultType' : 'PAYLOAD' ,
232+ 'resultKind' : 'SUCCESS_WITH_CONTENT' ,
233+ 'results' : {
234+ 'columns' : [
235+ {'name' : 'function name' , 'logicalType' : {'type' : 'VARCHAR' , 'nullable' : True , 'length' : 1000 }}],
236+ 'rowFormat' : 'JSON' ,
237+ 'data' : [
238+ {'kind' : 'INSERT' , 'fields' : [fname ]} for fname in expected_functions
239+ ]},
240+ 'nextResultUri' : f'/v3/sessions/{ session_handle } /operations/{ operation_handle } /result/1?rowFormat=JSON'
241+ }
242+
243+ def _assert_autocomplete_functions (self , autocomplete_result , expected_fun_names ):
244+ actual_fun_names = set ([f ['name' ] for f in autocomplete_result ['functions' ]])
245+ assert set (expected_fun_names ) == actual_fun_names
0 commit comments