import os import unittest from sqlalchemy.orm import sessionmaker from app.api.actions import install from app.schema.library import Library, Tag, Book, BookTag, Path, Env from app.controller import LibraryController import logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) class TestDB(unittest.TestCase): def setUp(self): os.environ["DEV_URIA_BIBLIOGAME_CONFIG_DB"] = "sqlite:///" os.environ["DEV_URIA_BIBLIOGAME_DEBUG"] = "false" self.engine = install() self.library = LibraryController(engine = self.engine) self.tags = [ Tag( name="Foo" ), Tag( name="Bar" ) ] self.library.create(Library( name="Library Test", notes="My duckling library test", paths=[ Path( path="/home/ivan/Documentos/ttprpg" ) ], envs=[ Env( key="ENVIRONMENT_VARIABLE", value="Clearly an environment variable" ) ], books=[ Book( name="Test book", tags=[ BookTag( tag=self.tags[0] ) ] ), ], tags=self.tags )) return super().setUp() def test_install(self): library = LibraryController(1, engine=self.engine) library_string = str(library.data) self.assertEqual(library_string, str(self.library.data)) def test_read_name(self): library = LibraryController(1, engine=self.engine) logger.debug(f"Name: {library.data.name}") self.assertEqual(library.data.name, self.library.data.name) self.assertEqual(library.data.name, "Library Test") def test_read_notes(self): library = LibraryController(1, engine=self.engine) logger.debug(f"Notes: {library.data.notes}") self.assertEqual(library.data.notes, self.library.data.notes) self.assertEqual(library.data.notes, "My duckling library test") def test_read_path(self): library = LibraryController(1, engine=self.engine) logger.debug(f"PATH: {library.data.paths[0].path}") self.assertEqual(library.data.paths[0].path, self.library.data.paths[0].path) self.assertEqual(library.data.paths[0].path, "/home/ivan/Documentos/ttprpg") def test_read_env(self): library = LibraryController(1, engine=self.engine) logger.debug(f"ENV: {library.data.envs[0].key} - {library.data.envs[0].value}") self.assertEqual(library.data.envs[0].key, self.library.data.envs[0].key) self.assertEqual(library.data.envs[0].value, self.library.data.envs[0].value) self.assertEqual(library.data.envs[0].key, "ENVIRONMENT_VARIABLE") self.assertEqual(library.data.envs[0].value, "Clearly an environment variable") def test_read_book(self): library = LibraryController(1, engine=self.engine) book = library.data.books[0] logger.debug(f"BOOK: {book}") self.assertEqual(book.name, self.library.data.books[0].name) self.assertEqual(book.name, "Test book") def test_read_tags(self): library = LibraryController(1, engine=self.engine) tags = library.data.tags self.assertEqual(str(tags), str(self.library.data.tags)) self.assertEqual(str(tags[0]), str(self.tags[0])) self.assertEqual(tags[0].name, self.tags[0].name) self.assertEqual(tags[0].name, "Foo") self.assertEqual(str(tags[1]), str(self.tags[1])) self.assertEqual(tags[1].name, self.tags[1].name) self.assertEqual(tags[1].name, "Bar") def test_read_book_tags(self): library = LibraryController(1, engine=self.engine) book = library.data.books[0] tags = library.data.tags logger.debug(f"BOOK TAGS: {book.tags}") self.assertEqual(str(book.tags[0].tag), str(self.tags[0])) self.assertEqual(str(book.tags[0].tag), str(tags[0])) self.assertEqual(book.tags[0].tag.name, tags[0].name) self.assertEqual(book.tags[0].tag.name, "Foo") self.assertNotEqual(book.tags[0].tag.name, "Bar") def test_update_name(self): library = LibraryController(1, engine=self.engine) library.data.name = "Another Library" library.update() library1 = LibraryController(1, engine=self.engine) self.assertEqual(library1.data.name, self.library.data.name) self.assertNotEqual(library1.data.name, "Library Test") self.assertEqual(library1.data.name, "Another Library") def test_update_name(self): library = LibraryController(1, engine=self.engine) library.data.books[0].name = "Another Book on the shelf" library.update() library1 = LibraryController(1, engine=self.engine) book = library1.data.books[0] self.assertEqual(book.name, self.library.data.books[0].name) self.assertNotEqual(book.name, "Test book") self.assertEqual(book.name, "Another Book on the shelf") if __name__ == "__main__": unittest.main()