mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-18 01:40:15 -04:00
second commit
This commit is contained in:
parent
b012fb98ab
commit
7e1e31244c
2
.vscode/extensions.json
vendored
2
.vscode/extensions.json
vendored
|
@ -10,6 +10,6 @@
|
||||||
"ckolkman.vscode-postgres",
|
"ckolkman.vscode-postgres",
|
||||||
"ms-python.python",
|
"ms-python.python",
|
||||||
"ms-python.vscode-pylance",
|
"ms-python.vscode-pylance",
|
||||||
"ms-python.autopep8"
|
"ms-python.black-formatter"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
|
@ -10,7 +10,7 @@
|
||||||
"[python]": {
|
"[python]": {
|
||||||
"editor.defaultFormatter": "ms-python.autopep8"
|
"editor.defaultFormatter": "ms-python.autopep8"
|
||||||
},
|
},
|
||||||
"python.analysis.extraPaths": ["/backend/"],
|
"python.analysis.extraPaths": ["backend/"],
|
||||||
"python.testing.pytestEnabled": true,
|
"python.testing.pytestEnabled": true,
|
||||||
"python.testing.unittestEnabled": false,
|
"python.testing.unittestEnabled": false,
|
||||||
"python.analysis.diagnosticSeverityOverrides": {
|
"python.analysis.diagnosticSeverityOverrides": {
|
||||||
|
@ -18,5 +18,7 @@
|
||||||
"reportGeneralTypeIssues": "error",
|
"reportGeneralTypeIssues": "error",
|
||||||
"reportDeprecated": "error",
|
"reportDeprecated": "error",
|
||||||
"reportImportCycles": "error"
|
"reportImportCycles": "error"
|
||||||
}
|
},
|
||||||
|
"python.analysis.autoImportCompletions": false,
|
||||||
|
"python.analysis.typeCheckingMode": "off"
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
|
||||||
from .env import getenv
|
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."""
|
"""Helper function for reading settings from environment variables to produce connection string."""
|
||||||
dialect = "postgresql+psycopg2"
|
dialect = "postgresql+psycopg2"
|
||||||
user = getenv("POSTGRES_USER")
|
user = getenv("POSTGRES_USER")
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
from .entity_base import EntityBase
|
||||||
|
from .sample_entity import SampleEntity
|
12
backend/entities/entity_base.py
Normal file
12
backend/entities/entity_base.py
Normal 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
|
12
backend/entities/sample_entity.py
Normal file
12
backend/entities/sample_entity.py
Normal 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)
|
0
backend/script/__init__.py
Normal file
0
backend/script/__init__.py
Normal file
38
backend/script/reset_demo.py
Normal file
38
backend/script/reset_demo.py
Normal 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)
|
0
backend/test/models/__init__.py
Normal file
0
backend/test/models/__init__.py
Normal file
|
@ -3,20 +3,20 @@
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import Engine
|
from sqlalchemy import Engine
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
import subprocess
|
||||||
|
|
||||||
from ...database import db_session
|
from ...database import db_session
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def test_engine() -> Engine:
|
def test_engine() -> Engine:
|
||||||
|
subprocess.run(["python3", "-m", "backend.script.create_database"])
|
||||||
session = db_session()
|
session = db_session()
|
||||||
return session
|
return session
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
def session(test_engine: Engine):
|
def session(test_engine: Engine):
|
||||||
# entities.EntityBase.metadata.drop_all(test_engine)
|
|
||||||
# entities.EntityBase.metadata.create_all(test_engine)
|
|
||||||
session = Session(test_engine)
|
session = Session(test_engine)
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
|
|
|
@ -7,3 +7,7 @@ from sqlalchemy import Engine
|
||||||
def test_sample(session: Engine):
|
def test_sample(session: Engine):
|
||||||
print(session)
|
print(session)
|
||||||
assert session != None
|
assert session != None
|
||||||
|
|
||||||
|
|
||||||
|
def test_tables(session: Engine):
|
||||||
|
print()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user