@@ -68,7 +68,8 @@ def test_subnet():
6868
6969@pytest .fixture
7070def test_subnetwork_uri (test_subnet ):
71- # Make DATAPROC_SPARK_CONNECT_SUBNET the full URI to align with how user would specify it in the project
71+ # Make DATAPROC_SPARK_CONNECT_SUBNET the full URI
72+ # to align with how user would specify it in the project
7273 return test_subnet
7374
7475
@@ -106,11 +107,9 @@ def connect_session(test_project, test_region, os_environment):
106107 pass
107108
108109
109- # Tests for magics.py
110110@pytest .fixture
111111def ipython_shell (connect_session ):
112112 """Provides an IPython shell with a DataprocSparkSession in user_ns."""
113- pytest .importorskip ("IPython" , reason = "IPython not available" )
114113 try :
115114 from IPython .terminal .interactiveshell import TerminalInteractiveShell
116115 from google .cloud import dataproc_magics
@@ -128,71 +127,79 @@ def ipython_shell(connect_session):
128127 TerminalInteractiveShell .clear_instance ()
129128
130129
130+ # Tests for magics.py
131131def test_dpip_magic_loads (ipython_shell ):
132132 """Test that %dpip magic is registered."""
133133 assert "dpip" in ipython_shell .magics_manager .magics ["line" ]
134134
135135
136- @mock .patch .object (DataprocSparkSession , "addArtifacts" )
137- def test_dpip_install_single_package (mock_add_artifacts , ipython_shell , capsys ):
136+ def test_dpip_install_success (connect_session , ipython_shell , capsys ):
138137 """Test installing a single package with %dpip."""
139- ipython_shell .run_line_magic ("dpip" , "install pandas" )
140- mock_add_artifacts .assert_called_once_with ("pandas" , pypi = True )
138+ ipython_shell .run_line_magic ("dpip" , "install roman" )
141139 captured = capsys .readouterr ()
142- assert "Installing packages: " in captured .out
143- assert "Packages successfully added as artifacts." in captured .out
140+ assert "Active sessions found:" in captured .out
141+ assert "Installing packages:" in captured .out
142+ assert (
143+ "Successfully installed packages in Dataproc session(s)."
144+ in captured .out
145+ )
144146
147+ from pyspark .sql .connect .functions import udf
148+ from pyspark .sql .types import StringType
145149
146- @mock .patch .object (DataprocSparkSession , "addArtifacts" )
147- def test_dpip_install_multiple_packages_with_flags (
148- mock_add_artifacts , ipython_shell , capsys
149- ):
150- """Test installing multiple packages with flags like -U."""
151- ipython_shell .run_line_magic ("dpip" , "install -U numpy scikit-learn" )
152- calls = [
153- mock .call ("numpy" , pypi = True ),
154- mock .call ("scikit-learn" , pypi = True ),
155- ]
156- mock_add_artifacts .assert_has_calls (calls , any_order = True )
157- assert mock_add_artifacts .call_count == 2
158- captured = capsys .readouterr ()
159- assert "Installing packages: " in captured .out
160- assert "Packages successfully added as artifacts." in captured .out
150+ df = connect_session .createDataFrame (
151+ [(1 ,), (4 ,), (16 ,), (51 ,), (1666 ,)], ["number" ]
152+ )
153+
154+ def to_roman (number ):
155+ import roman
156+
157+ return roman .toRoman (number ) if number else None
158+
159+ df_result = df .withColumn (
160+ "roman" , udf (to_roman , StringType ())("number" )
161+ ).collect ()
162+
163+ assert df_result [0 ]["roman" ] == "I"
164+ assert df_result [1 ]["roman" ] == "IV"
165+ assert df_result [2 ]["roman" ] == "XVI"
166+ assert df_result [3 ]["roman" ] == "LI"
167+ assert df_result [4 ]["roman" ] == "MDCLXVI"
168+
169+ connect_session .stop ()
161170
162171
163172def test_dpip_no_install_command (ipython_shell , capsys ):
164173 """Test usage message when 'install' is missing."""
165174 ipython_shell .run_line_magic ("dpip" , "pandas" )
166175 captured = capsys .readouterr ()
167176 assert "Usage: %dpip install <package1> <package2> ..." in captured .out
168- assert "No packages specified." in captured .out
169177
170178
171179def test_dpip_no_packages (ipython_shell , capsys ):
172180 """Test message when no packages are specified."""
173181 ipython_shell .run_line_magic ("dpip" , "install" )
174182 captured = capsys .readouterr ()
175- assert "No packages specified." in captured .out
183+ assert "Error: No packages specified." in captured .out
184+
185+
186+ def test_dpip_with_flags (ipython_shell , capsys ):
187+ """Test installing multiple packages with flags like -U."""
188+ ipython_shell .run_line_magic ("dpip" , "install -U numpy scikit-learn" )
189+ captured = capsys .readouterr ()
190+ assert "Error: Flags are not currently supported." in captured .out
176191
177192
178- @mock .patch .object (DataprocSparkSession , "addArtifacts" )
179- def test_dpip_no_session (mock_add_artifacts , ipython_shell , capsys ):
193+ def test_dpip_no_session (ipython_shell , capsys ):
180194 """Test message when no Spark session is active."""
181195 ipython_shell .user_ns = {} # Remove spark session from namespace
182196 ipython_shell .run_line_magic ("dpip" , "install pandas" )
183197 captured = capsys .readouterr ()
184- assert "No active Spark Sessions found." in captured .out
185- mock_add_artifacts .assert_not_called ()
198+ assert "No active Dataproc Spark Sessions found." in captured .out
186199
187200
188- @mock .patch .object (
189- DataprocSparkSession ,
190- "addArtifacts" ,
191- side_effect = Exception ("Install failed" ),
192- )
193- def test_dpip_install_failure (mock_add_artifacts , ipython_shell , capsys ):
201+ def test_dpip_install_failure (ipython_shell , capsys ):
194202 """Test error message on installation failure."""
195- ipython_shell .run_line_magic ("dpip" , "install bad-package" )
196- mock_add_artifacts .assert_called_once_with ("bad-package" , pypi = True )
203+ ipython_shell .run_line_magic ("dpip" , "install dp-non-existent-package" )
197204 captured = capsys .readouterr ()
198- assert "Failed to add artifacts: Install failed " in captured .out
205+ assert "No matching distribution found " in captured .out
0 commit comments