File size: 1,033 Bytes
acf6310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()