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) # Manually execute the backup logic today = datetime.date.today().strftime('%Y%m%d') backup_name = f'backup-{today}.zip' backup_path = backup_folder / backup_name # Remove if exists if backup_path.exists(): backup_path.unlink() # Create backup manually using the backup module's logic with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf: for path in web_folder.rglob("*"): if path.is_file(): arcname = path.relative_to(web_folder.parent) zipf.write(path, arcname) 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()