26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
import importlib.util
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
|
|
def test_backup_and_restore_snapshot(tmp_path):
|
|
source = Path(__file__).resolve().parents[1] / 'tools' / 'backup_db.py'
|
|
spec = importlib.util.spec_from_file_location('backup_tool', source)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
instance = tmp_path / 'instance'
|
|
instance.mkdir()
|
|
path = instance / 'po.db'
|
|
with sqlite3.connect(path) as db:
|
|
db.execute('CREATE TABLE orders(id INTEGER PRIMARY KEY, amount INTEGER)')
|
|
db.execute('INSERT INTO orders(amount) VALUES(66)')
|
|
snapshot = module.backup_database(tmp_path)
|
|
with sqlite3.connect(path) as db:
|
|
db.execute('UPDATE orders SET amount=100')
|
|
with sqlite3.connect(snapshot) as db:
|
|
assert db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok'
|
|
assert db.execute('SELECT amount FROM orders').fetchone()[0] == 66
|
|
with sqlite3.connect(tmp_path / 'restored.db') as restored:
|
|
db.backup(restored)
|
|
assert restored.execute('SELECT amount FROM orders').fetchone()[0] == 66
|