[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

View File

@@ -0,0 +1,18 @@
from ...db.config.config import get_engine_configuration
from sqlalchemy import create_engine as sqlalchemy_create_engine
from functools import cache
from sqlalchemy.orm import sessionmaker
import logging
logger = logging.getLogger(__name__)
@cache
def create_engine(*, engine_string=None, echo=None):
logger.debug(f"create_engine {engine_string}")
if engine_string is None:
engine_string, echo = get_engine_configuration()
engine = sqlalchemy_create_engine(engine_string, echo=echo=="true", future=True) # TODO
Session = sessionmaker(engine)
session = Session()
return engine, session

View File

@@ -1,10 +1,10 @@
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from ....schema.library.library import Library from ....schema.library.base import Base
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def create(session:Session, library:Library): def create(session:Session, library:Base):
logger.debug("Add Library") logger.debug("Add Library")
session.add(library) session.add(library)
logger.debug("Added Library") logger.debug("Added Library")

View File

@@ -0,0 +1,7 @@
from sqlalchemy.orm import Session
from ....schema.library.base import Base
def delete(session:Session, library:Base):
session.delete(library)
session.commit()

View File

@@ -0,0 +1,8 @@
from sqlalchemy.orm import Session
from sqlalchemy import select
from ....schema.library.base import Base
def read(session:Session, _id:int, obj:Base):
stmt = select(obj).where(obj.id == _id)
return session.scalars(stmt).one()

View File

@@ -0,0 +1,6 @@
from sqlalchemy.orm import Session
from ....schema.library.base import Base
def update(session:Session, library:Base):
session.commit()

View File

@@ -1,7 +0,0 @@
from sqlalchemy.orm import Session
from ....schema.library.library import Library
def delete(session:Session, library:Library):
session.delete(library)
session.commit()

View File

@@ -1,8 +0,0 @@
from sqlalchemy.orm import Session
from sqlalchemy import select
from ....schema.library.library import Library
def read(session:Session, _id:int):
stmt = select(Library).where(Library.id == _id)
return session.scalars(stmt).one()

View File

@@ -1,6 +0,0 @@
from sqlalchemy.orm import Session
from ....schema.library.library import Library
def update(session:Session, library:Library):
session.commit()

View File

@@ -0,0 +1,60 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.api.cruds.base import create, read, update, delete
from app.schema.library import Library
from ..db.config.config import get_engine_configuration
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class LibraryController:
def __init__(self, library_id=None, *, engine_string=None, echo=False, engine=None):
if engine_string is None:
engine_string, echo = get_engine_configuration()
if engine is None:
self._engine = create_engine(engine_string, echo=echo=="true", future=True)
else:
self._engine = engine
self._Session = sessionmaker(bind=self.engine)
self._library = None
if library_id is not None:
self.read(library_id)
@property
def data(self):
return self._library
@property
def engine(self):
return self._engine
@property
def Session(self):
return self._Session
@property
def session(self):
return self.Session()
def create(self, library:Library):
with self.session as session:
self._library = create(session, library)
return self
def read(self, _id):
with self.session as session:
self._library = read(session, _id, Library)
return self
def update(self):
with self.session as session:
session.commit()
def delete(self):
with self.session as session:
delete(session, self)
del(self)

View File

@@ -30,4 +30,4 @@ class Library(Base):
) )
def __repr__(self) -> str: def __repr__(self) -> str:
return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r}" return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r})"

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