80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
import os
|
|||
|
|
import shutil
|
||
|
|
import sqlite3
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from audiobook_library import SqlDatabase
|
||
|
|
|
||
|
|
|
||
|
|
class SqlDatabaseTests(unittest.TestCase):
|
||
|
|
def setUp(self):
|
||
|
|
self.tempdir = tempfile.TemporaryDirectory()
|
||
|
|
self.root = Path(self.tempdir.name)
|
||
|
|
|
||
|
|
def tearDown(self):
|
||
|
|
self.tempdir.cleanup()
|
||
|
|
|
||
|
|
def test_database_methods(self):
|
||
|
|
db_path = self.root / "books.sqlite"
|
||
|
|
connection = sqlite3.connect(db_path)
|
||
|
|
connection.execute(
|
||
|
|
"CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT, genres TEXT, audioFiles TEXT)"
|
||
|
|
)
|
||
|
|
connection.execute(
|
||
|
|
"INSERT INTO books VALUES (1, 'Book', '[\"Sci-Fi\"]', '[]')"
|
||
|
|
)
|
||
|
|
connection.commit()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
database = SqlDatabase(db_path)
|
||
|
|
self.assertTrue(database.connect())
|
||
|
|
self.assertIsNotNone(database.cursor())
|
||
|
|
self.assertTrue(database.load_database_data())
|
||
|
|
self.assertEqual(database.books[0]["genres"], ["Sci-Fi"])
|
||
|
|
database.books[0]["title"] = "Updated"
|
||
|
|
database._save_book(database.books[0])
|
||
|
|
database.update_books()
|
||
|
|
database.fix_genres()
|
||
|
|
database.fix_comments()
|
||
|
|
database.commit()
|
||
|
|
database.close()
|
||
|
|
|
||
|
|
with self.assertRaises(ValueError):
|
||
|
|
SqlDatabase(db_path).cursor()
|
||
|
|
with self.assertRaises(FileExistsError):
|
||
|
|
SqlDatabase(self.root / "missing.sqlite").connect()
|
||
|
|
|
||
|
|
def test_asset_fixture(self):
|
||
|
|
assets = Path(os.environ.get("AUDIOBOOK_TEST_ASSETS", Path(__file__).resolve().parents[2] / "testassets" / "audiobook_library"))
|
||
|
|
database_fixture = os.environ.get("AUDIOBOOK_TEST_DATABASE")
|
||
|
|
fixture = (
|
||
|
|
Path(database_fixture)
|
||
|
|
if database_fixture
|
||
|
|
else assets / "absdatabase.sqlite_test"
|
||
|
|
)
|
||
|
|
with sqlite3.connect(fixture.resolve().as_uri() + "?mode=ro", uri=True) as source:
|
||
|
|
expected_books = source.execute("SELECT COUNT(*) FROM books").fetchone()[0]
|
||
|
|
self.assertGreater(expected_books, 0)
|
||
|
|
database_path = self.root / "absdatabase.sqlite"
|
||
|
|
shutil.copyfile(fixture, database_path)
|
||
|
|
|
||
|
|
database = SqlDatabase(database_path)
|
||
|
|
self.assertTrue(database.init())
|
||
|
|
self.assertEqual(len(database.books), expected_books)
|
||
|
|
self.assertTrue(database.data["libraries"])
|
||
|
|
database.fix_genres()
|
||
|
|
database.fix_comments()
|
||
|
|
database.update_books()
|
||
|
|
database.commit()
|
||
|
|
database.close()
|
||
|
|
|
||
|
|
reopened = SqlDatabase(database_path)
|
||
|
|
self.assertTrue(reopened.init())
|
||
|
|
self.assertEqual(len(reopened.books), expected_books)
|
||
|
|
reopened.close()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|