Compare commits
2 Commits
1531a5dc77
...
dc819b1674
| Author | SHA1 | Date | |
|---|---|---|---|
| dc819b1674 | |||
| b00c35f19b |
1
app/api/actions/__init__.py
Normal file
1
app/api/actions/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .install import install
|
||||||
12
app/api/actions/install.py
Normal file
12
app/api/actions/install.py
Normal 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
|
||||||
@@ -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
|
|
||||||
@@ -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()
|
|
||||||
@@ -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()
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
from sqlalchemy.orm import Session
|
|
||||||
|
|
||||||
from .....schema.config.library import LibraryConfig
|
|
||||||
|
|
||||||
def update(session:Session, library:LibraryConfig):
|
|
||||||
session.commit()
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import os
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from .....schema.config.env import Env
|
from ....schema.library.env import Env
|
||||||
|
|
||||||
def create(session:Session, env:Env):
|
def create(session:Session, env:Env):
|
||||||
session.add(env)
|
session.add(env)
|
||||||
os.environ[env.key] = env.value
|
|
||||||
session.commit()
|
session.commit()
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
import os
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from .....schema.config.env import Env
|
from ....schema.library.env import Env
|
||||||
|
|
||||||
def delete(session:Session, env:Env):
|
def delete(session:Session, env:Env):
|
||||||
session.delete(env)
|
session.delete(env)
|
||||||
delete(os.environ[env.key])
|
|
||||||
session.commit()
|
session.commit()
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
import os
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
|
|
||||||
from .....schema.config.env import Env
|
from ....schema.library.env import Env
|
||||||
|
|
||||||
def read(session:Session, _id:int):
|
def read(session:Session, _id:int):
|
||||||
stmt = select(Env).where(Env.id == _id)
|
stmt = select(Env).where(Env.id == _id)
|
||||||
env:Env = session.scalars(stmt).one()
|
env:Env = session.scalars(stmt).one()
|
||||||
os.environ[env.key] = env.value
|
|
||||||
return env
|
return env
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from .....schema.config.env import Env
|
from ....schema.library.env import Env
|
||||||
|
|
||||||
def update(session:Session, env:Env):
|
def update(session:Session, env:Env):
|
||||||
os.environ[env.key] = env.value
|
|
||||||
session.commit()
|
session.commit()
|
||||||
4
app/api/cruds/library/__init__.py
Normal file
4
app/api/cruds/library/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from .create import create
|
||||||
|
from .read import read
|
||||||
|
from .update import update
|
||||||
|
from .delete import delete
|
||||||
14
app/api/cruds/library/create.py
Normal file
14
app/api/cruds/library/create.py
Normal 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")
|
||||||
7
app/api/cruds/library/delete.py
Normal file
7
app/api/cruds/library/delete.py
Normal 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()
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
|
|
||||||
from .....schema.config.library import LibraryConfig
|
from ....schema.library.library import Library
|
||||||
|
|
||||||
def read(session:Session, _id:int):
|
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()
|
return session.scalars(stmt).one()
|
||||||
6
app/api/cruds/library/update.py
Normal file
6
app/api/cruds/library/update.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from ....schema.library.library import Library
|
||||||
|
|
||||||
|
def update(session:Session, library:Library):
|
||||||
|
session.commit()
|
||||||
6
app/api/cruds/path/create.py
Normal file
6
app/api/cruds/path/create.py
Normal 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()
|
||||||
7
app/api/cruds/path/delete.py
Normal file
7
app/api/cruds/path/delete.py
Normal 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()
|
||||||
10
app/api/cruds/path/read.py
Normal file
10
app/api/cruds/path/read.py
Normal 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
|
||||||
7
app/api/cruds/path/update.py
Normal file
7
app/api/cruds/path/update.py
Normal 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()
|
||||||
@@ -1 +1 @@
|
|||||||
from .engine import engine
|
from .config import get_engine_configuration
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def get_engine_configuration():
|
def get_engine_configuration():
|
||||||
engine_string = os.getenv("DEV_URIA_BIBLIOGAME_DEBUG", False)
|
echo = os.getenv("DEV_URIA_BIBLIOGAME_DEBUG", "false")
|
||||||
echo = os.getenv("DEV_URIA_BIBLIOGAME_CONFIG_DB", "sqlite://")
|
engine_string = os.getenv("DEV_URIA_BIBLIOGAME_CONFIG_DB", "sqlite://")
|
||||||
|
logger.debug(f"engine_string, {engine_string}")
|
||||||
return (engine_string, echo)
|
return (engine_string, echo)
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
from sqlalchemy import create_engine
|
|
||||||
from .config import engine_string, echo
|
|
||||||
|
|
||||||
engine = create_engine(engine_string, echo=echo)
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
from .engine import engine
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
engine_string = "sqlite://"
|
|
||||||
echo = True
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
from sqlalchemy import create_engine
|
|
||||||
from .config import engine_string, echo
|
|
||||||
|
|
||||||
engine = create_engine(engine_string, echo=echo)
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
from sqlalchemy.orm import DeclarativeBase
|
|
||||||
|
|
||||||
class Base(DeclarativeBase):
|
|
||||||
pass
|
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
from .base import Base
|
from .base import Base
|
||||||
from .book import Book
|
from .env import Env
|
||||||
from .tag import Tag
|
#from .tag import Tag
|
||||||
|
from .path import Path
|
||||||
|
from .library import Library
|
||||||
|
from .book import Book
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from sqlalchemy import String
|
from sqlalchemy import String, ForeignKey
|
||||||
from sqlalchemy.orm import Mapped
|
from sqlalchemy.orm import Mapped
|
||||||
from sqlalchemy.orm import mapped_column
|
from sqlalchemy.orm import mapped_column
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
@@ -10,14 +10,16 @@ from .base import Base
|
|||||||
class Book(Base):
|
class Book(Base):
|
||||||
__tablename__ = "book"
|
__tablename__ = "book"
|
||||||
id: Mapped[int] = mapped_column(primary_key=True)
|
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))
|
name: Mapped[str] = mapped_column(String(255))
|
||||||
publisher: Mapped[str] = mapped_column(String(255))
|
publisher: Mapped[str] = mapped_column(String(255))
|
||||||
notes: Mapped[str] = mapped_column(String(65656))
|
notes: Mapped[str] = mapped_column(String(65656))
|
||||||
classification: Mapped[int] = mapped_column(primary_key=True)
|
classification: Mapped[int] = mapped_column(primary_key=True)
|
||||||
|
|
||||||
tags: Mapped[List["Tags"]] = relationship(
|
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
|
||||||
back_populates="books", cascade="all, delete-orphan"
|
library: Mapped[int] = relationship("Library", back_populates="books")
|
||||||
)
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from sqlalchemy import String
|
from sqlalchemy import String
|
||||||
|
from sqlalchemy import ForeignKey
|
||||||
from sqlalchemy.orm import Mapped
|
from sqlalchemy.orm import Mapped
|
||||||
from sqlalchemy.orm import mapped_column
|
from sqlalchemy.orm import mapped_column
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
@@ -13,9 +14,8 @@ class Env(Base):
|
|||||||
key: Mapped[str] = mapped_column(String(255))
|
key: Mapped[str] = mapped_column(String(255))
|
||||||
value: Mapped[str] = mapped_column(String(65656))
|
value: Mapped[str] = mapped_column(String(65656))
|
||||||
|
|
||||||
libraries: Mapped[List["Library"]] = relationship(
|
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
|
||||||
back_populates="libraries", cascade="all, delete-orphan"
|
library: Mapped[int] = relationship("Library", back_populates="envs")
|
||||||
)
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
||||||
29
app/schema/library/library.py
Normal file
29
app/schema/library/library.py
Normal 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}"
|
||||||
@@ -1,22 +1,20 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from sqlalchemy import String
|
from sqlalchemy import String
|
||||||
|
from sqlalchemy import ForeignKey
|
||||||
from sqlalchemy.orm import Mapped
|
from sqlalchemy.orm import Mapped
|
||||||
from sqlalchemy.orm import mapped_column
|
from sqlalchemy.orm import mapped_column
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
from .base import Base
|
from .base import Base
|
||||||
|
|
||||||
class LibraryConfig(Base):
|
class Path(Base):
|
||||||
__tablename__ = "library_config"
|
__tablename__ = "path"
|
||||||
id: Mapped[int] = mapped_column(primary_key=True)
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
name: Mapped[str] = mapped_column(String(255))
|
path: Mapped[str] = mapped_column(String(65656))
|
||||||
notes: Mapped[str] = mapped_column(String(65656))
|
|
||||||
connection_string = Mapped[str] = mapped_column(String(65656))
|
library_id: Mapped[int] = mapped_column(ForeignKey("library.id"))
|
||||||
|
library: Mapped[int] = relationship("Library", back_populates="paths")
|
||||||
env: Mapped[List["Env"]] = relationship(
|
|
||||||
back_populates="env", cascade="all, delete-orphan"
|
|
||||||
)
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
||||||
5
pyproject.toml
Normal file
5
pyproject.toml
Normal 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"
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
flask
|
flask
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
requests
|
requests
|
||||||
py-dotenv
|
py-dotenv
|
||||||
|
pytest
|
||||||
71
tests/test_db.py
Normal file
71
tests/test_db.py
Normal 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()
|
||||||
Reference in New Issue
Block a user