34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
import zipfile
|
|
import datetime
|
|
from utils import backup
|
|
|
|
|
|
def test_backup_creates_zip(tmp_path, monkeypatch):
|
|
# Setup fake web folder
|
|
web_folder = tmp_path / 'plots'
|
|
web_folder.mkdir()
|
|
(web_folder / 'file1.txt').write_text('abc')
|
|
(web_folder / 'file2.txt').write_text('def')
|
|
backup_folder = tmp_path / 'backups'
|
|
backup_folder.mkdir()
|
|
|
|
# Patch the module variables
|
|
monkeypatch.setattr(backup, 'WEB_FOLDER', web_folder)
|
|
monkeypatch.setattr(backup, 'BACKUP_FOLDER', backup_folder)
|
|
|
|
# Call the backup function
|
|
backup.create_backup()
|
|
|
|
# Check that backup was created
|
|
today = datetime.date.today().strftime('%Y%m%d')
|
|
backup_name = f'backup-{today}.zip'
|
|
backup_path = backup_folder / backup_name
|
|
|
|
assert backup_path.exists()
|
|
with zipfile.ZipFile(backup_path, 'r') as z:
|
|
names = z.namelist()
|
|
assert any('file1.txt' in n for n in names)
|
|
assert any('file2.txt' in n for n in names)
|
|
|
|
# Cleanup: remove the backup file after test
|
|
backup_path.unlink() |