29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
|
|
"""Offline SQLite backup + restore smoke in an isolated directory."""
|
||
|
|
import sqlite3
|
||
|
|
import zipfile
|
||
|
|
from pathlib import Path
|
||
|
|
from app import db, backup
|
||
|
|
from scripts.restore_backup import restore
|
||
|
|
|
||
|
|
|
||
|
|
def test_sqlite_backup_and_restore_with_media(tmp_path,monkeypatch):
|
||
|
|
storage=tmp_path/'original'
|
||
|
|
storage.mkdir()
|
||
|
|
monkeypatch.setattr(db,'DATA',storage)
|
||
|
|
monkeypatch.setattr(db,'DB_FILE',storage/'siteforge.sqlite3')
|
||
|
|
db.init()
|
||
|
|
db.execute('INSERT INTO sites(id,name,created_at,updated_at) VALUES (?,?,?,?)',
|
||
|
|
('restorecheck','Recovered Site',db.now(),db.now()))
|
||
|
|
media=storage/'media'/'restorecheck'/'logo.svg'
|
||
|
|
media.parent.mkdir(parents=True)
|
||
|
|
media.write_text('<svg/>',encoding='utf-8')
|
||
|
|
dest=backup.backup()
|
||
|
|
with zipfile.ZipFile(dest) as z:
|
||
|
|
assert 'siteforge.sqlite3' in z.namelist()
|
||
|
|
assert 'media/restorecheck/logo.svg' in z.namelist()
|
||
|
|
recovered=tmp_path/'restored'
|
||
|
|
restore(dest,recovered,confirmed=True)
|
||
|
|
with sqlite3.connect(recovered/'siteforge.sqlite3') as c:
|
||
|
|
assert c.execute("SELECT name FROM sites WHERE id='restorecheck'").fetchone()[0]=='Recovered Site'
|
||
|
|
assert (recovered/'media'/'restorecheck'/'logo.svg').read_text(encoding='utf-8')=='<svg/>'
|