105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
"""
|
|
tests/test_backup.py
|
|
---------------------
|
|
Tests for db.database backup_db() and restore_db().
|
|
"""
|
|
|
|
import os
|
|
import pytest
|
|
from db.database import backup_db, restore_db, DB_PATH, get_connection
|
|
from db.models import insert_draw, get_draws_with_game, get_game_by_name
|
|
|
|
|
|
# ── backup_db ─────────────────────────────────────────────────────────────────
|
|
|
|
def test_backup_creates_file(tmp_db, tmp_path):
|
|
dest = backup_db(dest_dir=str(tmp_path))
|
|
assert os.path.isfile(dest)
|
|
|
|
|
|
def test_backup_filename_contains_timestamp(tmp_db, tmp_path):
|
|
dest = backup_db(dest_dir=str(tmp_path))
|
|
basename = os.path.basename(dest)
|
|
assert basename.startswith("lottosight_backup_")
|
|
assert basename.endswith(".db")
|
|
|
|
|
|
def test_backup_returns_path_string(tmp_db, tmp_path):
|
|
result = backup_db(dest_dir=str(tmp_path))
|
|
assert isinstance(result, str)
|
|
assert len(result) > 0
|
|
|
|
|
|
def test_backup_file_is_valid_sqlite(tmp_db, tmp_path):
|
|
dest = backup_db(dest_dir=str(tmp_path))
|
|
import sqlite3
|
|
conn = sqlite3.connect(dest)
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
|
tables = {r[0] for r in cursor.fetchall()}
|
|
conn.close()
|
|
assert "games" in tables
|
|
assert "draws" in tables
|
|
|
|
|
|
def test_backup_preserves_draw_data(tmp_db, tmp_path):
|
|
game = get_game_by_name("Powerball")
|
|
insert_draw(game["id"], "2024-06-01", [5, 14, 22, 36, 69], bonus=7)
|
|
dest = backup_db(dest_dir=str(tmp_path))
|
|
import sqlite3
|
|
conn = sqlite3.connect(dest)
|
|
conn.row_factory = sqlite3.Row
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT numbers FROM draws WHERE draw_date='2024-06-01'")
|
|
row = cursor.fetchone()
|
|
conn.close()
|
|
assert row is not None
|
|
assert "5" in row["numbers"]
|
|
|
|
|
|
def test_backup_creates_dest_dir_if_missing(tmp_db, tmp_path):
|
|
nested = str(tmp_path / "a" / "b" / "c")
|
|
dest = backup_db(dest_dir=nested)
|
|
assert os.path.isfile(dest)
|
|
|
|
|
|
def test_backup_default_dest_dir(tmp_db, monkeypatch, tmp_path):
|
|
from core.paths import user_data_dir as _udd
|
|
monkeypatch.setattr("db.database.user_data_dir", lambda: str(tmp_path))
|
|
dest = backup_db() # no dest_dir — should use exports/ sub-dir
|
|
assert os.path.isfile(dest)
|
|
assert "exports" in dest
|
|
|
|
|
|
# ── restore_db ────────────────────────────────────────────────────────────────
|
|
|
|
def test_restore_missing_file_raises(tmp_db):
|
|
with pytest.raises(FileNotFoundError):
|
|
restore_db("/nonexistent/backup.db")
|
|
|
|
|
|
def test_restore_replaces_live_db(tmp_db, tmp_path):
|
|
game = get_game_by_name("Powerball")
|
|
insert_draw(game["id"], "2024-07-04", [3, 17, 28, 45, 62], bonus=12)
|
|
backup_path = backup_db(dest_dir=str(tmp_path))
|
|
|
|
# Delete the draw from the live DB
|
|
conn = get_connection()
|
|
conn.execute("DELETE FROM draws WHERE draw_date='2024-07-04'")
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
draws_before = get_draws_with_game(game_id=game["id"])
|
|
assert not any(d["draw_date"] == "2024-07-04" for d in draws_before)
|
|
|
|
restore_db(backup_path)
|
|
|
|
draws_after = get_draws_with_game(game_id=game["id"])
|
|
assert any(d["draw_date"] == "2024-07-04" for d in draws_after)
|
|
|
|
|
|
def test_restore_returns_none(tmp_db, tmp_path):
|
|
backup_path = backup_db(dest_dir=str(tmp_path))
|
|
result = restore_db(backup_path)
|
|
assert result is None
|