[UPD] Added LibraryController

This commit is contained in:
2026-02-14 18:37:48 +01:00
parent 5fc1dc1b82
commit 5f7b4102bc
13 changed files with 257 additions and 36 deletions

143
tests/test_controller.py Normal file
View File

@@ -0,0 +1,143 @@
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"] = "true"
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 = read(self.session, 1)
logger.debug(f"Name: {library.name}")
self.assertEqual(library.name, self.library.name)
self.assertEqual(library.name, "Library Test")
def test_read_notes(self):
library = read(self.session, 1)
logger.debug(f"Notes: {library.notes}")
self.assertEqual(library.notes, self.library.notes)
self.assertEqual(library.notes, "My duckling library test")
def test_read_path(self):
library = read(self.session, 1)
logger.debug(f"PATH: {library.paths[0].path}")
self.assertEqual(library.paths[0].path, self.library.paths[0].path)
self.assertEqual(library.paths[0].path, "/home/ivan/Documentos/ttprpg")
def test_read_env(self):
library = read(self.session, 1)
logger.debug(f"ENV: {library.envs[0].key} - {library.envs[0].value}")
self.assertEqual(library.envs[0].key, self.library.envs[0].key)
self.assertEqual(library.envs[0].value, self.library.envs[0].value)
self.assertEqual(library.envs[0].key, "ENVIRONMENT_VARIABLE")
self.assertEqual(library.envs[0].value, "Clearly an environment variable")
def test_read_book(self):
library = read(self.session, 1)
book = library.books[0]
logger.debug(f"BOOK: {book}")
self.assertEqual(book.name, self.library.books[0].name)
self.assertEqual(book.name, "Test book")
def test_read_tags(self):
library = read(self.session, 1)
tags = library.tags
self.assertEqual(tags, self.library.tags)
self.assertEqual(tags, self.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 = read(self.session, 1)
book = library.books[0]
tags = library.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 = read(self.session, 1)
library.name = "Another Library"
update(self.session, library)
library1 = read(self.session, 1)
self.assertEqual(library1.name, self.library.name)
self.assertNotEqual(library1.name, "Library Test")
self.assertEqual(library1.name, "Another Library")
def test_update_name(self):
library = read(self.session, 1)
library.books[0].name = "Another Book on the shelf"
update(self.session, library)
library1 = read(self.session, 1)
book = library1.books[0]
self.assertEqual(book.name, self.library.books[0].name)
self.assertNotEqual(book.name, "Test book")
self.assertEqual(book.name, "Another Book on the shelf")
"""
if __name__ == "__main__":
unittest.main()

View File

@@ -4,7 +4,7 @@ import unittest
from sqlalchemy.orm import sessionmaker
from app.api.actions import install
from app.api.cruds.library import create, read, update
from app.api.cruds.base import create, read, update
from app.schema.library import Library, Path, Env, Book, Tag, BookTag
import logging
@@ -57,29 +57,29 @@ class TestDB(unittest.TestCase):
return super().setUp()
def test_install(self):
library_string = str(read(self.session, 1))
library_string = str(read(self.session, 1, Library))
self.assertEqual(library_string, str(self.library))
def test_read_name(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
logger.debug(f"Name: {library.name}")
self.assertEqual(library.name, self.library.name)
self.assertEqual(library.name, "Library Test")
def test_read_notes(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
logger.debug(f"Notes: {library.notes}")
self.assertEqual(library.notes, self.library.notes)
self.assertEqual(library.notes, "My duckling library test")
def test_read_path(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
logger.debug(f"PATH: {library.paths[0].path}")
self.assertEqual(library.paths[0].path, self.library.paths[0].path)
self.assertEqual(library.paths[0].path, "/home/ivan/Documentos/ttprpg")
def test_read_env(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
logger.debug(f"ENV: {library.envs[0].key} - {library.envs[0].value}")
self.assertEqual(library.envs[0].key, self.library.envs[0].key)
self.assertEqual(library.envs[0].value, self.library.envs[0].value)
@@ -87,14 +87,14 @@ class TestDB(unittest.TestCase):
self.assertEqual(library.envs[0].value, "Clearly an environment variable")
def test_read_book(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
book = library.books[0]
logger.debug(f"BOOK: {book}")
self.assertEqual(book.name, self.library.books[0].name)
self.assertEqual(book.name, "Test book")
def test_read_tags(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
tags = library.tags
self.assertEqual(tags, self.library.tags)
self.assertEqual(tags, self.tags)
@@ -106,7 +106,7 @@ class TestDB(unittest.TestCase):
self.assertEqual(tags[1].name, "Bar")
def test_read_book_tags(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
book = library.books[0]
tags = library.tags
logger.debug(f"BOOK TAGS: {book.tags}")
@@ -117,7 +117,7 @@ class TestDB(unittest.TestCase):
self.assertNotEqual(book.tags[0].tag.name, "Bar")
def test_update_name(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
library.name = "Another Library"
update(self.session, library)
library1 = read(self.session, 1)
@@ -126,11 +126,11 @@ class TestDB(unittest.TestCase):
self.assertEqual(library1.name, "Another Library")
def test_update_name(self):
library = read(self.session, 1)
library = read(self.session, 1, Library)
library.books[0].name = "Another Book on the shelf"
update(self.session, library)
library1 = read(self.session, 1)
library1 = read(self.session, 1, Library)
book = library1.books[0]
self.assertEqual(book.name, self.library.books[0].name)