Compare commits

..

2 Commits

Author SHA1 Message Date
dc819b1674 [REF] Refactor 2026-02-13 13:35:18 +01:00
b00c35f19b [WIP] Testing and reformuling 2026-02-08 18:55:16 +01:00
37 changed files with 215 additions and 80 deletions

View File

@@ -0,0 +1 @@
from .install import install

View File

@@ -0,0 +1,12 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from ...schema.library.base import Base
from ...db.config.config import get_engine_configuration
def install():
engine_string, echo = get_engine_configuration()
engine = create_engine(engine_string, echo=echo=="true", future=True) # TODO
metadata = Base.metadata
metadata.create_all(engine)
return engine

View File

@@ -1,11 +0,0 @@
from sqlalchemy import create_engine
from ....schema.config.base import Base
from ....db.config.config import get_engine_configuration
def install():
engine_string, echo = get_engine_configuration()
engine = create_engine(engine_string, echo=echo)
metadata = Base.metadata(engine)
metadata.create_all()
return engine

View File

@@ -1,6 +0,0 @@
from sqlalchemy.orm import Session
from .....schema.config.library import LibraryConfig
def create(session:Session, library:LibraryConfig):
session.add(library)
session.commit()

View File

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

View File

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

View File

@@ -1,8 +1,6 @@
import os
from sqlalchemy.orm import Session
from .....schema.config.env import Env
from ....schema.library.env import Env
def create(session:Session, env:Env):
session.add(env)
os.environ[env.key] = env.value
session.commit()

View File

@@ -1,9 +1,7 @@
import os
from sqlalchemy.orm import Session
from .....schema.config.env import Env
from ....schema.library.env import Env
def delete(session:Session, env:Env):
session.delete(env)
delete(os.environ[env.key])
session.commit()

View File

@@ -1,11 +1,9 @@
import os
from sqlalchemy.orm import Session
from sqlalchemy import select
from .....schema.config.env import Env
from ....schema.library.env import Env
def read(session:Session, _id:int):
stmt = select(Env).where(Env.id == _id)
env:Env = session.scalars(stmt).one()
os.environ[env.key] = env.value
return env

View File

@@ -1,8 +1,7 @@
import os
from sqlalchemy.orm import Session
from .....schema.config.env import Env
from ....schema.library.env import Env
def update(session:Session, env:Env):
os.environ[env.key] = env.value
session.commit()

View File

@@ -0,0 +1,4 @@
from .create import create
from .read import read
from .update import update
from .delete import delete

View File

@@ -0,0 +1,14 @@
from sqlalchemy.orm import Session
from ....schema.library.library import Library
import logging
logger = logging.getLogger(__name__)
def create(session:Session, library:Library):
logger.debug("Add Library")
session.add(library)
logger.debug("Added Library")
session.commit()
logger.debug("Committed Library")
session.refresh(library)
logger.debug("Rerfreshed Library")

View File

@@ -0,0 +1,7 @@
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 +1,8 @@
from sqlalchemy.orm import Session
from sqlalchemy import select
from .....schema.config.library import LibraryConfig
from ....schema.library.library import Library
def read(session:Session, _id:int):
stmt = select(LibraryConfig).where(LibraryConfig.id == _id)
stmt = select(Library).where(Library.id == _id)
return session.scalars(stmt).one()

View File

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

View File

@@ -0,0 +1,6 @@
from sqlalchemy.orm import Session
from ....schema.library.path import Path
def create(session:Session, path:Path):
session.add(path)
session.commit()

View File

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

View File

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

View File

@@ -0,0 +1,7 @@
import os
from sqlalchemy.orm import Session
from ....schema.library.path import Path
def update(session:Session, path:Path):
session.commit()

View File

@@ -1 +1 @@
from .engine import engine
from .config import get_engine_configuration

View File

@@ -1,6 +1,10 @@
import os
import logging
logger = logging.getLogger(__name__)
def get_engine_configuration():
engine_string = os.getenv("DEV_URIA_BIBLIOGAME_DEBUG", False)
echo = os.getenv("DEV_URIA_BIBLIOGAME_CONFIG_DB", "sqlite://")
echo = os.getenv("DEV_URIA_BIBLIOGAME_DEBUG", "false")
engine_string = os.getenv("DEV_URIA_BIBLIOGAME_CONFIG_DB", "sqlite://")
logger.debug(f"engine_string, {engine_string}")
return (engine_string, echo)

View File

@@ -1,4 +0,0 @@
from sqlalchemy import create_engine
from .config import engine_string, echo
engine = create_engine(engine_string, echo=echo)

View File

@@ -1 +0,0 @@
from .engine import engine

View File

@@ -1,2 +0,0 @@
engine_string = "sqlite://"
echo = True

View File

@@ -1,4 +0,0 @@
from sqlalchemy import create_engine
from .config import engine_string, echo
engine = create_engine(engine_string, echo=echo)

View File

@@ -1,4 +0,0 @@
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass

View File

@@ -1,3 +1,6 @@
from .base import Base
from .book import Book
from .tag import Tag
from .env import Env
#from .tag import Tag
from .path import Path
from .library import Library
from .book import Book

View File

@@ -1,6 +1,6 @@
from typing import List
from typing import Optional
from sqlalchemy import String
from sqlalchemy import String, ForeignKey
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
@@ -10,14 +10,16 @@ from .base import Base
class Book(Base):
__tablename__ = "book"
id: Mapped[int] = mapped_column(primary_key=True)
hash: Mapped[str] = mapped_column(String(255))
file_name: Mapped[str] = mapped_column(String(255))
file_path: Mapped[str] = mapped_column(String(65656))
name: Mapped[str] = mapped_column(String(255))
publisher: Mapped[str] = mapped_column(String(255))
notes: Mapped[str] = mapped_column(String(65656))
classification: Mapped[int] = mapped_column(primary_key=True)
tags: Mapped[List["Tags"]] = relationship(
back_populates="books", cascade="all, delete-orphan"
)
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
library: Mapped[int] = relationship("Library", back_populates="books")
def __repr__(self) -> str:
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \

View File

@@ -1,6 +1,7 @@
from typing import List
from typing import Optional
from sqlalchemy import String
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
@@ -13,9 +14,8 @@ class Env(Base):
key: Mapped[str] = mapped_column(String(255))
value: Mapped[str] = mapped_column(String(65656))
libraries: Mapped[List["Library"]] = relationship(
back_populates="libraries", cascade="all, delete-orphan"
)
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
library: Mapped[int] = relationship("Library", back_populates="envs")
def __repr__(self) -> str:
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \

View File

@@ -0,0 +1,29 @@
from typing import List
from typing import Optional
from sqlalchemy import String
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
from .base import Base
class Library(Base):
__tablename__ = "library"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(255))
notes: Mapped[str] = mapped_column(String(65656))
envs: Mapped[List["Env"]] = relationship(
back_populates="library", cascade="all, delete-orphan"
)
paths: Mapped[List["Path"]] = relationship(
back_populates="library", cascade="all, delete-orphan"
)
books: Mapped[List["Book"]] = relationship(
back_populates="library", cascade="all, delete-orphan"
)
def __repr__(self) -> str:
return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r}"

View File

@@ -1,22 +1,20 @@
from typing import List
from typing import Optional
from sqlalchemy import String
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
from .base import Base
class LibraryConfig(Base):
__tablename__ = "library_config"
class Path(Base):
__tablename__ = "path"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(255))
notes: Mapped[str] = mapped_column(String(65656))
connection_string = Mapped[str] = mapped_column(String(65656))
env: Mapped[List["Env"]] = relationship(
back_populates="env", cascade="all, delete-orphan"
)
path: Mapped[str] = mapped_column(String(65656))
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
library: Mapped[int] = relationship("Library", back_populates="paths")
def __repr__(self) -> str:
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \

5
pyproject.toml Normal file
View File

@@ -0,0 +1,5 @@
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "DEBUG"
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"

View File

@@ -1,4 +1,5 @@
flask
sqlalchemy
requests
py-dotenv
py-dotenv
pytest

71
tests/test_db.py Normal file
View File

@@ -0,0 +1,71 @@
import os
import unittest
from sqlalchemy.orm import sessionmaker
from app.api.actions import install
from app.api.cruds.library import create, read
from app.schema.library import Library, Path, Env
import logging
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 = 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"
)
]
)
self.Session = sessionmaker(bind=self.engine)
self.session = self.Session()
create(self.session, self.library)
return super().setUp()
def test_install(self):
library_string = str(read(self.session, 1))
self.assertEqual(library_string, str(self.library))
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")
if __name__ == "__main__":
unittest.main()