[WIP] Testing and reformuling
This commit is contained in:
1
app/api/config/actions/__init__.py
Normal file
1
app/api/config/actions/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .install import install
|
||||
@@ -1,11 +1,12 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
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()
|
||||
engine = create_engine(engine_string, echo=echo=="true", future=True) # TODO
|
||||
metadata = Base.metadata
|
||||
metadata.create_all(engine)
|
||||
return engine
|
||||
4
app/api/config/cruds/library/__init__.py
Normal file
4
app/api/config/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
|
||||
@@ -1,6 +1,14 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from .....schema.config.library import LibraryConfig
|
||||
from .....schema.config.library import Library
|
||||
|
||||
def create(session:Session, library:LibraryConfig):
|
||||
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")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.library import LibraryConfig
|
||||
from .....schema.config.library import Library
|
||||
|
||||
def delete(session:Session, library:LibraryConfig):
|
||||
def delete(session:Session, library:Library):
|
||||
session.delete(library)
|
||||
session.commit()
|
||||
@@ -1,8 +1,8 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select
|
||||
|
||||
from .....schema.config.library import LibraryConfig
|
||||
from .....schema.config.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()
|
||||
@@ -1,6 +1,6 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.library import LibraryConfig
|
||||
from .....schema.config.library import Library
|
||||
|
||||
def update(session:Session, library:LibraryConfig):
|
||||
def update(session:Session, library:Library):
|
||||
session.commit()
|
||||
6
app/api/config/cruds/path/create.py
Normal file
6
app/api/config/cruds/path/create.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from .....schema.config.path import Path
|
||||
|
||||
def create(session:Session, path:Path):
|
||||
session.add(path)
|
||||
session.commit()
|
||||
7
app/api/config/cruds/path/delete.py
Normal file
7
app/api/config/cruds/path/delete.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.path import Path
|
||||
|
||||
def delete(session:Session, path:Path):
|
||||
session.delete(path)
|
||||
session.commit()
|
||||
10
app/api/config/cruds/path/read.py
Normal file
10
app/api/config/cruds/path/read.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import os
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select
|
||||
|
||||
from .....schema.config.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/config/cruds/path/update.py
Normal file
7
app/api/config/cruds/path/update.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import os
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .....schema.config.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 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)
|
||||
@@ -1,4 +0,0 @@
|
||||
from sqlalchemy import create_engine
|
||||
from .config import engine_string, echo
|
||||
|
||||
engine = create_engine(engine_string, echo=echo)
|
||||
@@ -0,0 +1,4 @@
|
||||
from .base import Base
|
||||
from .env import Env
|
||||
from .library import Library
|
||||
from .path import Path
|
||||
@@ -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}," \
|
||||
|
||||
@@ -7,17 +7,21 @@ from sqlalchemy.orm import relationship
|
||||
|
||||
from .base import Base
|
||||
|
||||
class LibraryConfig(Base):
|
||||
__tablename__ = "library_config"
|
||||
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))
|
||||
connection_string = 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"
|
||||
envs: Mapped[List["Env"]] = relationship(
|
||||
back_populates="library", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
paths: Mapped[List["Path"]] = relationship(
|
||||
back_populates="library", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Book(id={self.id!r}, name={self.name!r}, publisher={self.publisher!r}," \
|
||||
" notes={self.notes!r}, classification={self.classification!r})"
|
||||
return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r}," \
|
||||
f" connection_string={self.connection_string!r})"
|
||||
21
app/schema/config/path.py
Normal file
21
app/schema/config/path.py
Normal file
@@ -0,0 +1,21 @@
|
||||
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 Path(Base):
|
||||
__tablename__ = "path"
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
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}," \
|
||||
" notes={self.notes!r}, classification={self.classification!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"
|
||||
@@ -2,3 +2,4 @@ flask
|
||||
sqlalchemy
|
||||
requests
|
||||
py-dotenv
|
||||
pytest
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
35
tests/test_config.py
Normal file
35
tests/test_config.py
Normal file
@@ -0,0 +1,35 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user