@@ -33,6 +33,14 @@ def setUp(self):
3333 self .shell .config = Config ()
3434 self .magics = DataprocMagics (shell = self .shell )
3535
36+ def _create_mock_arrow_binary (self , lines : list [str ]) -> bytes :
37+ schema = pa .schema ([pa .field ("output" , pa .string ())])
38+ table = pa .Table .from_arrays ([lines ], schema = schema )
39+ sink = pa .BufferOutputStream ()
40+ with pa .ipc .RecordBatchStreamWriter (sink , table .schema ) as writer :
41+ writer .write_table (table )
42+ return sink .getvalue ()
43+
3644 def test_dpip_with_flags (self ):
3745 with self .assertRaisesRegex (
3846 RuntimeError , "Error: Flags are not currently supported."
@@ -82,13 +90,9 @@ def test_dpip_install_packages_success(self):
8290 properties = mock .Mock ()
8391
8492 # Create a pyarrow table and serialize it
85- schema = pa .schema ([pa .field ("output" , pa .string ())])
86- data = [["Collecting pandas" , "Successfully installed pandas" ]]
87- table = pa .Table .from_arrays (data , schema = schema )
88- sink = pa .BufferOutputStream ()
89- with pa .ipc .RecordBatchStreamWriter (sink , table .schema ) as writer :
90- writer .write_table (table )
91- binary_data = sink .getvalue ()
93+ binary_data = self ._create_mock_arrow_binary (
94+ ["Collecting pandas" , "Successfully installed pandas" ]
95+ )
9296
9397 # Set up the mock response structure
9498 properties .sql_command_result .local_relation .data = binary_data
@@ -109,7 +113,8 @@ def test_dpip_install_packages_success(self):
109113 call_args = mock_session .client .execute_command .call_args [0 ][0 ]
110114 self .assertIsInstance (call_args , pb2 .Command )
111115 self .assertEqual (
112- call_args .execute_external_command .command , "PipInstallPackages"
116+ call_args .execute_external_command .command ,
117+ DataprocMagics .PIP_INSTALL_COMMAND ,
113118 )
114119 self .assertEqual (
115120 call_args .execute_external_command .options ["0" ], "pandas"
@@ -130,18 +135,12 @@ def test_dpip_install_failure(self):
130135
131136 # Create a mock for the properties object with failure message
132137 properties = mock .Mock ()
133- schema = pa .schema ([pa .field ("output" , pa .string ())])
134- data = [
138+ binary_data = self ._create_mock_arrow_binary (
135139 [
136- "Pip install failed with non-zero exit code" ,
140+ DataprocMagics . PIP_INSTALL_FAILURE_MSG ,
137141 "ERROR: some pip error" ,
138142 ]
139- ]
140- table = pa .Table .from_arrays (data , schema = schema )
141- sink = pa .BufferOutputStream ()
142- with pa .ipc .RecordBatchStreamWriter (sink , table .schema ) as writer :
143- writer .write_table (table )
144- binary_data = sink .getvalue ()
143+ )
145144
146145 properties .sql_command_result .local_relation .data = binary_data
147146 mock_session .client .execute_command .return_value = (
0 commit comments