diff --git a/tests/test_database.py b/tests/test_database.py new file mode 100644 index 0000000..93b31bd --- /dev/null +++ b/tests/test_database.py @@ -0,0 +1,90 @@ +"""Tests for SQLite schema initialization and constraints.""" + +import sqlite3 +import tempfile +import unittest +from pathlib import Path + +from database import SCHEMA_VERSION, initialize_database + + +class DatabaseInitializationTests(unittest.TestCase): + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + self.database_path = Path(self.temp_dir.name) / "nested" / "test.sqlite3" + + def tearDown(self): + self.temp_dir.cleanup() + + def connect(self): + connection = sqlite3.connect(self.database_path) + connection.execute("PRAGMA foreign_keys = ON") + return connection + + def test_initialization_creates_versioned_schema_and_is_idempotent(self): + initialize_database(self.database_path) + initialize_database(self.database_path) + + with self.connect() as connection: + version = connection.execute("PRAGMA user_version").fetchone()[0] + tables = { + row[0] + for row in connection.execute( + "SELECT name FROM sqlite_master WHERE type = 'table'" + ) + } + + self.assertEqual(version, SCHEMA_VERSION) + self.assertTrue( + {"keys", "health_checks", "key_checks", "tool_checks", "ai_security_checks"} + <= tables + ) + + def test_invalid_status_and_duplicate_key_numbers_are_rejected(self): + initialize_database(self.database_path) + with self.connect() as connection: + with self.assertRaises(sqlite3.IntegrityError): + connection.execute( + "INSERT INTO health_checks (checked_at, next_check_due, software_compatibility_status) " + "VALUES ('2026-01-01', '2027-01-01', 'UNKNOWN')" + ) + + connection.execute( + "INSERT INTO keys (key_number, name, type, device) " + "VALUES (1, 'SeedSigner', 'hardware', 'SeedSigner')" + ) + with self.assertRaises(sqlite3.IntegrityError): + connection.execute( + "INSERT INTO keys (key_number, name, type, device) " + "VALUES (1, 'Duplicate', 'hardware', 'SeedSigner')" + ) + + def test_ai_security_status_is_limited_to_required_values(self): + initialize_database(self.database_path) + with self.connect() as connection: + connection.execute( + "INSERT INTO health_checks (checked_at, next_check_due) VALUES (?, ?)", + ("2026-01-01", "2027-01-01"), + ) + health_check_id = connection.execute("SELECT last_insert_rowid()").fetchone()[0] + connection.execute( + "INSERT INTO ai_security_checks (health_check_id, provider, status) VALUES (?, ?, ?)", + (health_check_id, "SeedSigner", "TRYGG"), + ) + with self.assertRaises(sqlite3.IntegrityError): + connection.execute( + "INSERT INTO ai_security_checks (health_check_id, provider, status) VALUES (?, ?, ?)", + (health_check_id, "Nunchuk", "UNKNOWN"), + ) + + def test_unknown_future_schema_version_is_rejected(self): + self.database_path.parent.mkdir(parents=True, exist_ok=True) + with sqlite3.connect(self.database_path) as connection: + connection.execute(f"PRAGMA user_version = {SCHEMA_VERSION + 1}") + + with self.assertRaises(RuntimeError): + initialize_database(self.database_path) + + +if __name__ == "__main__": + unittest.main()