From 7e1e31244c3697ff18083533c857ea6f6c45cb2c Mon Sep 17 00:00:00 2001 From: pmoharana-cmd Date: Sat, 2 Mar 2024 15:16:28 -0500 Subject: [PATCH] second commit --- .vscode/extensions.json | 2 +- .vscode/settings.json | 6 +++-- backend/database.py | 2 +- backend/entities/__init__.py | 2 ++ backend/entities/entity_base.py | 12 +++++++++ backend/entities/sample_entity.py | 12 +++++++++ backend/script/__init__.py | 0 backend/script/reset_demo.py | 38 ++++++++++++++++++++++++++++ backend/test/models/__init__.py | 0 backend/test/services/conftest.py | 4 +-- backend/test/services/sample_test.py | 4 +++ 11 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 backend/entities/entity_base.py create mode 100644 backend/entities/sample_entity.py create mode 100644 backend/script/__init__.py create mode 100644 backend/script/reset_demo.py create mode 100644 backend/test/models/__init__.py diff --git a/.vscode/extensions.json b/.vscode/extensions.json index ec0d050..11dae3a 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -10,6 +10,6 @@ "ckolkman.vscode-postgres", "ms-python.python", "ms-python.vscode-pylance", - "ms-python.autopep8" + "ms-python.black-formatter" ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index c85959b..3cf96ac 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } diff --git a/backend/database.py b/backend/database.py index 4c5ced7..b53d444 100644 --- a/backend/database.py +++ b/backend/database.py @@ -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") diff --git a/backend/entities/__init__.py b/backend/entities/__init__.py index e69de29..39abb6f 100644 --- a/backend/entities/__init__.py +++ b/backend/entities/__init__.py @@ -0,0 +1,2 @@ +from .entity_base import EntityBase +from .sample_entity import SampleEntity diff --git a/backend/entities/entity_base.py b/backend/entities/entity_base.py new file mode 100644 index 0000000..5e34685 --- /dev/null +++ b/backend/entities/entity_base.py @@ -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 diff --git a/backend/entities/sample_entity.py b/backend/entities/sample_entity.py new file mode 100644 index 0000000..28948ff --- /dev/null +++ b/backend/entities/sample_entity.py @@ -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) diff --git a/backend/script/__init__.py b/backend/script/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/script/reset_demo.py b/backend/script/reset_demo.py new file mode 100644 index 0000000..65de426 --- /dev/null +++ b/backend/script/reset_demo.py @@ -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) diff --git a/backend/test/models/__init__.py b/backend/test/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/test/services/conftest.py b/backend/test/services/conftest.py index c06a731..42b2fdd 100644 --- a/backend/test/services/conftest.py +++ b/backend/test/services/conftest.py @@ -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 diff --git a/backend/test/services/sample_test.py b/backend/test/services/sample_test.py index 7d4cfbd..ad2167e 100644 --- a/backend/test/services/sample_test.py +++ b/backend/test/services/sample_test.py @@ -7,3 +7,7 @@ from sqlalchemy import Engine def test_sample(session: Engine): print(session) assert session != None + + +def test_tables(session: Engine): + print()