Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions auto_backup/models/db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ def action_backup(self):
_logger.exception(f"Action backup - OSError: {exc}")

with open(os.path.join(rec.folder, filename), "wb") as destiny:
# Copy the cached backup
if backup:
# Copy the cached backup, ensuring that it is in the same format
if backup and backup.endswith(rec.backup_format):
with open(backup) as cached:
shutil.copyfileobj(cached, destiny)
# Generate new backup
Expand Down
28 changes: 28 additions & 0 deletions auto_backup/tests/test_db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,34 @@ def test_sftp_connection_return(self, pysftp):
res,
)

def test_action_backup_local_format_mismatch(self):
"""Ensure backup is not copied if the format does not match."""
# This test ensures that when formats differ, dump_db is called again
rec1 = self.env["db.backup"].create(
{
"name": "Backup 1",
"method": "local",
"backup_format": "zip",
"folder": "/tmp/backup",
}
)
rec2 = self.env["db.backup"].create(
{
"name": "Backup 2",
"method": "local",
"backup_format": "dump",
"folder": "/tmp/backup",
}
)
recs = rec1 | rec2
with self.mock_assets() as assets, patch("builtins.open") as open_mock:
assets["os"].path.join.return_value = "/tmp/backup/file"
# destiny.name is used to set backup variable
open_mock.return_value.__enter__.return_value.name = "/tmp/backup/file"
recs.action_backup()
self.assertEqual(assets["db"].dump_db.call_count, 2)
assets["shutil"].copyfileobj.assert_not_called()

def test_filename_default(self):
"""It should not error and should return a .dump.zip file str"""
now = datetime.now()
Expand Down
Loading