sau / tests /test_db_init.py
skatef's picture
fix: 自动初始化 HF 数据库表
acf6310
import sqlite3
import tempfile
import unittest
from pathlib import Path
from db.init_db import ensure_database
class EnsureDatabaseTests(unittest.TestCase):
def test_creates_app_database_with_required_tables(self):
with tempfile.TemporaryDirectory() as tmp_dir:
base_dir = Path(tmp_dir)
db_path = ensure_database(base_dir)
self.assertEqual(db_path, base_dir / 'db' / 'database.db')
self.assertTrue(db_path.exists())
conn = sqlite3.connect(db_path)
try:
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user_info'")
self.assertEqual(cursor.fetchone(), ('user_info',))
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='file_records'")
self.assertEqual(cursor.fetchone(), ('file_records',))
finally:
conn.close()
if __name__ == '__main__':
unittest.main()