|
| 1 | +import zipfile |
1 | 2 | from unittest.mock import Mock, patch |
2 | 3 |
|
3 | 4 | import cloudpickle |
@@ -224,3 +225,32 @@ def test_write_yaml_append(self, mockparse_s3_path): |
224 | 225 | Bucket='bucket_name', |
225 | 226 | Key='key_prefix/test_data.yaml', |
226 | 227 | ) |
| 228 | + |
| 229 | + @patch('sdgym.result_writer.parse_s3_path') |
| 230 | + @patch('sdgym.result_writer.io.StringIO') |
| 231 | + def write_zipped_dataframes(self, mock_string_io, mockparse_s3_path): |
| 232 | + """Test the `write_zipped_dataframes` method.""" |
| 233 | + # Setup |
| 234 | + mock_s3_client = Mock() |
| 235 | + mockparse_s3_path.return_value = ('bucket_name', 'key_prefix/test_data.zip') |
| 236 | + result_writer = S3ResultsWriter(mock_s3_client) |
| 237 | + df1 = pd.DataFrame({'col1': [1, 2]}) |
| 238 | + df2 = pd.DataFrame({'colA': ['x', 'y']}) |
| 239 | + df1.to_csv = Mock(return_value='csv1') |
| 240 | + df2.to_csv = Mock(return_value='csv2') |
| 241 | + data = {'table1': df1, 'table2': df2} |
| 242 | + mock_string_io.side_effect = ['buffer1', 'buffer2'] |
| 243 | + |
| 244 | + # Run |
| 245 | + result_writer.write_zipped_dataframes(data, 'test_data.zip') |
| 246 | + |
| 247 | + # Assert |
| 248 | + mockparse_s3_path.assert_called_once_with('test_data.zip') |
| 249 | + mock_s3_client.upload_fileobj.assert_called_once() |
| 250 | + df1.to_csv.assert_called_once_with('buffer1', index=False) |
| 251 | + df2.to_csv.assert_called_once_with('buffer2', index=False) |
| 252 | + args, _ = mock_s3_client.upload_fileobj.call_args |
| 253 | + uploaded_buffer = args[0] |
| 254 | + uploaded_buffer.seek(0) |
| 255 | + with zipfile.ZipFile(uploaded_buffer, 'r') as zf: |
| 256 | + assert set(zf.namelist()) == {'table1.csv', 'table2.csv'} |
0 commit comments