[REF] Refactor
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....schema.config.base import Base
|
||||
from ....db.config.config import get_engine_configuration
|
||||
from ...schema.library.base import Base
|
||||
from ...db.config.config import get_engine_configuration
|
||||
|
||||
def install():
|
||||
engine_string, echo = get_engine_configuration()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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
|
||||
@@ -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()
|
||||
@@ -1,5 +1,5 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from .....schema.config.library import Library
|
||||
from ....schema.library.library import Library
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -1,6 +1,6 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.library import Library
|
||||
from ....schema.library.library import Library
|
||||
|
||||
def delete(session:Session, library:Library):
|
||||
session.delete(library)
|
||||
@@ -1,7 +1,7 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select
|
||||
|
||||
from .....schema.config.library import Library
|
||||
from ....schema.library.library import Library
|
||||
|
||||
def read(session:Session, _id:int):
|
||||
stmt = select(Library).where(Library.id == _id)
|
||||
@@ -1,6 +1,6 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.library import Library
|
||||
from ....schema.library.library import Library
|
||||
|
||||
def update(session:Session, library:Library):
|
||||
session.commit()
|
||||
@@ -1,5 +1,5 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from .....schema.config.path import Path
|
||||
from ....schema.library.path import Path
|
||||
|
||||
def create(session:Session, path:Path):
|
||||
session.add(path)
|
||||
@@ -1,6 +1,6 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.path import Path
|
||||
from ....schema.library.path import Path
|
||||
|
||||
def delete(session:Session, path:Path):
|
||||
session.delete(path)
|
||||
@@ -2,7 +2,7 @@ import os
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select
|
||||
|
||||
from .....schema.config.path import Path
|
||||
from ....schema.library.path import Path
|
||||
|
||||
def read(session:Session, _id:int):
|
||||
stmt = select(Path).where(Path.id == _id)
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.path import Path
|
||||
from ....schema.library.path import Path
|
||||
|
||||
def update(session:Session, path:Path):
|
||||
session.commit()
|
||||
@@ -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 .base import Base
|
||||
from .env import Env
|
||||
from .library import Library
|
||||
from .path import Path
|
||||
@@ -1,4 +0,0 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
@@ -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
|
||||
@@ -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}," \
|
||||
|
||||
@@ -12,7 +12,6 @@ class Library(Base):
|
||||
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))
|
||||
|
||||
envs: Mapped[List["Env"]] = relationship(
|
||||
back_populates="library", cascade="all, delete-orphan"
|
||||
@@ -22,6 +21,9 @@ class Library(Base):
|
||||
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}," \
|
||||
f" connection_string={self.connection_string!r})"
|
||||
return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r}"
|
||||
@@ -1,35 +0,0 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.api.config.actions import install
|
||||
from app.api.config.cruds.library import create, read
|
||||
from app.schema.config import Library
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class TestConfig(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",
|
||||
connection_string="sqlite:///"
|
||||
)
|
||||
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))
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
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