66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
import sqlite3
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from scripts.import_excel import DEFAULT_DATABASE, import_data, inspect_database, verify_import
|
|
|
|
|
|
def database_file():
|
|
directory = tempfile.TemporaryDirectory()
|
|
path = Path(directory.name) / "calendar.sqlite"
|
|
with sqlite3.connect(path) as database:
|
|
database.execute(
|
|
"CREATE TABLE date_choices (choice_date TEXT PRIMARY KEY, amount_cents INTEGER, updated_at TEXT DEFAULT CURRENT_TIMESTAMP)"
|
|
)
|
|
database.execute(
|
|
"CREATE TABLE date_statuses (status_date TEXT PRIMARY KEY, day_status TEXT, updated_at TEXT DEFAULT CURRENT_TIMESTAMP)"
|
|
)
|
|
return directory, path
|
|
|
|
|
|
def test_default_database_is_anchored_to_project_directory():
|
|
assert DEFAULT_DATABASE.is_absolute()
|
|
assert DEFAULT_DATABASE.name == "calendar.sqlite"
|
|
assert DEFAULT_DATABASE.parent.name == "instance"
|
|
|
|
|
|
def test_database_inspection_rejects_missing_or_wrong_database():
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
missing = Path(directory) / "missing.sqlite"
|
|
with pytest.raises(RuntimeError, match="does not exist"):
|
|
inspect_database(missing)
|
|
|
|
wrong = Path(directory) / "wrong.sqlite"
|
|
with sqlite3.connect(wrong) as database:
|
|
database.execute("CREATE TABLE something_else (value TEXT)")
|
|
with pytest.raises(RuntimeError, match="wrong schema"):
|
|
inspect_database(wrong)
|
|
|
|
|
|
def test_import_creates_backup_and_verifies_committed_values():
|
|
directory, path = database_file()
|
|
try:
|
|
backup = import_data(
|
|
path,
|
|
{"2026-07-15": 2750},
|
|
{"2026-07-16": "vacation"},
|
|
)
|
|
|
|
assert backup.is_file()
|
|
assert inspect_database(path) == (1, 1, "2026-07-15", "2026-07-16")
|
|
verify_import(path, {"2026-07-15": 2750}, {"2026-07-16": "vacation"})
|
|
assert inspect_database(backup) == (0, 0, None, None)
|
|
finally:
|
|
directory.cleanup()
|
|
|
|
|
|
def test_verification_detects_values_that_do_not_match():
|
|
directory, path = database_file()
|
|
try:
|
|
with pytest.raises(RuntimeError, match="verification failed"):
|
|
verify_import(path, {"2026-07-15": 5500}, {})
|
|
finally:
|
|
directory.cleanup()
|