@@ -29,12 +29,18 @@ def print_writer(self, mock_spark_session):
2929
3030 def test_write_to_dlo (self , print_writer , mock_dataframe ):
3131 """Test write_to_dlo method calls dataframe.show()."""
32+ # Mock the validate_dataframe_columns_against_dlo method
33+ print_writer .validate_dataframe_columns_against_dlo = MagicMock ()
34+
3235 # Call the method
3336 print_writer .write_to_dlo ("test_dlo" , mock_dataframe , WriteMode .OVERWRITE )
3437
3538 # Verify show() was called
3639 mock_dataframe .show .assert_called_once ()
3740
41+ # Verify validate_dataframe_columns_against_dlo was called
42+ print_writer .validate_dataframe_columns_against_dlo .assert_called_once ()
43+
3844 def test_write_to_dmo (self , print_writer , mock_dataframe ):
3945 """Test write_to_dmo method calls dataframe.show()."""
4046 # Call the method
@@ -59,9 +65,37 @@ def test_ignores_name_and_write_mode(self, print_writer, mock_dataframe):
5965 for name , write_mode in test_cases :
6066 # Reset mock before each call
6167 mock_dataframe .show .reset_mock ()
62-
68+ # Mock the validate_dataframe_columns_against_dlo method
69+ print_writer .validate_dataframe_columns_against_dlo = MagicMock ()
6370 # Call method
6471 print_writer .write_to_dlo (name , mock_dataframe , write_mode )
6572
6673 # Verify show() was called with no arguments
6774 mock_dataframe .show .assert_called_once_with ()
75+
76+ print_writer .validate_dataframe_columns_against_dlo .assert_called_once ()
77+
78+ def test_validate_dataframe_columns_against_dlo (self , print_writer , mock_dataframe ):
79+ """Test validate_dataframe_columns_against_dlo method."""
80+ # Mock the QueryAPIDataCloudReader
81+ mock_reader = MagicMock ()
82+ mock_dlo_df = MagicMock ()
83+ mock_dlo_df .columns = ["col1" , "col2" ]
84+ mock_reader .read_dlo .return_value = mock_dlo_df
85+
86+ # Set up mock dataframe columns
87+ mock_dataframe .columns = ["col1" , "col2" , "col3" ]
88+
89+ # Test that validation raises ValueError for extra columns
90+ with pytest .raises (ValueError ) as exc_info :
91+ print_writer .validate_dataframe_columns_against_dlo (
92+ mock_dataframe , "test_dlo" , mock_reader
93+ )
94+
95+ assert "col3" in str (exc_info .value )
96+
97+ # Test successful validation with matching columns
98+ mock_dataframe .columns = ["col1" , "col2" ]
99+ print_writer .validate_dataframe_columns_against_dlo (
100+ mock_dataframe , "test_dlo" , mock_reader
101+ )
0 commit comments