Skip to content

Commit

Permalink
Fix append
Browse files Browse the repository at this point in the history
  • Loading branch information
pvk-developer committed May 2, 2024
1 parent 96abf0e commit 0e22101
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sdv/io/local/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,14 @@ def write(self, synthetic_data, file_name, sheet_name_suffix=None, mode='w'):
sheet_name = f'{table_name}{sheet_name_suffix}'
suffix_added = True

temp_data[sheet_name] = table
if temp_data.get(sheet_name):
temp_data[sheet_name] = pd.concat([
temp_data[sheet_name],
synthetic_data[sheet_name]
])

else:
temp_data[sheet_name] = table

writer = pd.ExcelWriter(file_name)
for table_name, table_data in temp_data.items():
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/io/local/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,35 @@ def test_write_mode_append(self, mock_pd):
index=False
)
mock_pd.ExcelWriter.return_value.close.assert_called_once_with()

@patch('sdv.io.local.local.pd')
def test_write_mode_append_no_suffix(self, mock_pd):
"""Test the write functionality of the ExcelHandler when mode is `a`` and no suffix."""
# Setup
sheet_one = Mock()
sheet_two = Mock()
synth_sheet_one = Mock()
synthetic_data = {'Sheet1': synth_sheet_one}
file_name = 'output_file.xlsx'
instance = ExcelHandler()
instance._read_excel = Mock(return_value={'Sheet1': sheet_one, 'Sheet2': sheet_two})

# Run
instance.write(synthetic_data, file_name, mode='a')

# Assert
mock_pd.concat.assert_called_once_with([sheet_one, synth_sheet_one])
mock_pd.concat.return_value.to_excel.assert_called_once_with(
mock_pd.ExcelWriter.return_value,
sheet_name='Sheet1',
float_format=None,
index=False
)

sheet_two.to_excel.assert_called_once_with(
mock_pd.ExcelWriter.return_value,
sheet_name='Sheet2',
float_format=None,
index=False
)
mock_pd.ExcelWriter.return_value.close.assert_called_once_with()

0 comments on commit 0e22101

Please sign in to comment.