second commit

This commit is contained in:
pmoharana-cmd 2024-03-02 15:16:28 -05:00
parent b012fb98ab
commit 7e1e31244c
11 changed files with 76 additions and 6 deletions

View File

@ -10,6 +10,6 @@
"ckolkman.vscode-postgres",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.autopep8"
"ms-python.black-formatter"
]
}

View File

@ -10,7 +10,7 @@
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.analysis.extraPaths": ["/backend/"],
"python.analysis.extraPaths": ["backend/"],
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"python.analysis.diagnosticSeverityOverrides": {
@ -18,5 +18,7 @@
"reportGeneralTypeIssues": "error",
"reportDeprecated": "error",
"reportImportCycles": "error"
}
},
"python.analysis.autoImportCompletions": false,
"python.analysis.typeCheckingMode": "off"
}

View File

@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
from .env import getenv
def _engine_str(database: str = getenv("POSTGRES_DB")) -> str:
def _engine_str(database: str = getenv("POSTGRES_DATABASE")) -> str:
"""Helper function for reading settings from environment variables to produce connection string."""
dialect = "postgresql+psycopg2"
user = getenv("POSTGRES_USER")

View File

@ -0,0 +1,2 @@
from .entity_base import EntityBase
from .sample_entity import SampleEntity

View File

@ -0,0 +1,12 @@
"""Abstract superclass of all entities in the application.
There is no reason to instantiate this class directly. Instead, look toward the child classes.
Additionally, import from the top-level entities file which indexes all entity implementations.
"""
from sqlalchemy.orm import DeclarativeBase
class EntityBase(DeclarativeBase):
pass

View File

@ -0,0 +1,12 @@
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .entity_base import EntityBase
class SampleEntity(EntityBase):
__tablename__ = 'persons'
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String, nullable=False)
age: Mapped[int] = mapped_column(Integer)
email: Mapped[str] = mapped_column(String, unique=True, nullable=False)

View File

View File

@ -0,0 +1,38 @@
from sqlalchemy import text, create_engine
from ..database import engine
from ..env import getenv
from .. import entities
database = getenv("POSTGRES_DATABASE")
def _engine_str() -> str:
"""Helper function for reading settings from environment variables to produce connection string."""
dialect = "postgresql+psycopg2"
user = getenv("POSTGRES_USER")
password = getenv("POSTGRES_PASSWORD")
host = getenv("POSTGRES_HOST")
port = getenv("POSTGRES_PORT")
return f"{dialect}://{user}:{password}@{host}:{port}"
engine = create_engine(_engine_str(), echo=True)
"""Application-level SQLAlchemy database engine."""
with engine.connect() as connection:
connection.execute(
text("COMMIT")
)
database = getenv("POSTGRES_DATABASE")
stmt = text(f"DROP DATABASE IF EXISTS {database}")
connection.execute(stmt)
connection.execute(
text("COMMIT")
)
database = getenv("POSTGRES_DATABASE")
stmt = text(f"CREATE DATABASE {database}")
connection.execute(stmt)
entities.EntityBase.metadata.drop_all(engine)
entities.EntityBase.metadata.create_all(engine)

View File

View File

@ -3,20 +3,20 @@
import pytest
from sqlalchemy import Engine
from sqlalchemy.orm import Session
import subprocess
from ...database import db_session
@pytest.fixture(scope="session")
def test_engine() -> Engine:
subprocess.run(["python3", "-m", "backend.script.create_database"])
session = db_session()
return session
@pytest.fixture(scope="function")
def session(test_engine: Engine):
# entities.EntityBase.metadata.drop_all(test_engine)
# entities.EntityBase.metadata.create_all(test_engine)
session = Session(test_engine)
try:
yield session

View File

@ -7,3 +7,7 @@ from sqlalchemy import Engine
def test_sample(session: Engine):
print(session)
assert session != None
def test_tables(session: Engine):
print()