mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-18 01:40:15 -04:00
add devcontainer setup + python setup
This commit is contained in:
parent
c6482e7c84
commit
eb1eb8dae4
72
.devcontainer/Dockerfile
Normal file
72
.devcontainer/Dockerfile
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
FROM ubuntu:22.04
|
||||||
|
|
||||||
|
# Setup workspace directory
|
||||||
|
RUN mkdir /workspace
|
||||||
|
WORKDIR /workspace
|
||||||
|
|
||||||
|
# Install useful system utilities
|
||||||
|
ENV TZ=America/New_York
|
||||||
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install --yes \
|
||||||
|
apt-transport-https \
|
||||||
|
ca-certificates \
|
||||||
|
curl \
|
||||||
|
debian-keyring \
|
||||||
|
debian-archive-keyring \
|
||||||
|
git \
|
||||||
|
gnupg \
|
||||||
|
locales \
|
||||||
|
postgresql-client \
|
||||||
|
software-properties-common \
|
||||||
|
sudo \
|
||||||
|
tzdata \
|
||||||
|
wget \
|
||||||
|
zsh \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Install Python 3.11
|
||||||
|
RUN add-apt-repository ppa:deadsnakes/ppa \
|
||||||
|
&& apt update \
|
||||||
|
&& apt install --yes \
|
||||||
|
python3.11 \
|
||||||
|
python3-pip \
|
||||||
|
libpq-dev \
|
||||||
|
python3.11-dev \
|
||||||
|
&& rm -rf /var/lib/apt/lists* \
|
||||||
|
&& unlink /usr/bin/python3 \
|
||||||
|
&& ln -s /usr/bin/python3.11 /usr/bin/python3
|
||||||
|
|
||||||
|
# Use a non-root user per https://code.visualstudio.com/remote/advancedcontainers/add-nonroot-user
|
||||||
|
ARG USERNAME=vscode
|
||||||
|
ARG USER_UID=1000
|
||||||
|
ARG USER_GID=$USER_UID
|
||||||
|
|
||||||
|
# Add non-root user and add to sudoers
|
||||||
|
RUN groupadd --gid $USER_GID $USERNAME \
|
||||||
|
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /usr/bin/zsh \
|
||||||
|
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
|
||||||
|
&& chmod 0440 /etc/sudoers.d/$USERNAME
|
||||||
|
|
||||||
|
# Set code to default git commit editor
|
||||||
|
RUN git config --system core.editor "code --wait"
|
||||||
|
# Set Safe Directory
|
||||||
|
RUN git config --system safe.directory '/workspace'
|
||||||
|
|
||||||
|
# Configure zsh
|
||||||
|
USER $USERNAME
|
||||||
|
ENV HOME /home/$USERNAME
|
||||||
|
|
||||||
|
# Add zsh theme with niceties
|
||||||
|
RUN curl https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | bash - \
|
||||||
|
&& sed -i 's/robbyrussell/kennethreitz/g' ~/.zshrc \
|
||||||
|
&& echo 'source <(ng completion script)' >>~/.zshrc \
|
||||||
|
&& echo 'export PATH=$PATH:$HOME/.local/bin' >>~/.zshrc
|
||||||
|
|
||||||
|
# Set Locale for Functional Autocompletion in zsh
|
||||||
|
RUN sudo locale-gen en_US.UTF-8
|
||||||
|
|
||||||
|
# Install Database Dependencies
|
||||||
|
COPY backend/requirements.txt /workspace/backend/requirements.txt
|
||||||
|
WORKDIR /workspace/backend
|
||||||
|
RUN python3 -m pip install -r requirements.txt
|
47
.devcontainer/devcontainer.json
Normal file
47
.devcontainer/devcontainer.json
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
{
|
||||||
|
"dockerComposeFile": "docker-compose.yml",
|
||||||
|
"workspaceFolder": "/workspace",
|
||||||
|
"service": "httpd",
|
||||||
|
"remoteUser": "vscode",
|
||||||
|
"forwardPorts": [
|
||||||
|
5432,
|
||||||
|
5050
|
||||||
|
],
|
||||||
|
"customizations": {
|
||||||
|
"vscode": {
|
||||||
|
"extensions": [
|
||||||
|
"dbaeumer.vscode-eslint",
|
||||||
|
"ecmel.vscode-html-css",
|
||||||
|
"ms-vscode.vscode-typescript-next",
|
||||||
|
"esbenp.prettier-vscode",
|
||||||
|
"bradlc.vscode-tailwindcss",
|
||||||
|
"vscode-icons-team.vscode-icons",
|
||||||
|
"tamasfe.even-better-toml",
|
||||||
|
"ckolkman.vscode-postgres",
|
||||||
|
"ms-python.python",
|
||||||
|
"ms-python.vscode-pylance",
|
||||||
|
"ms-python.black-formatter",
|
||||||
|
"gruntfuggly.todo-tree",
|
||||||
|
"ms-azuretools.vscode-docker"
|
||||||
|
],
|
||||||
|
"settings": {
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.formatOnSaveMode": "file",
|
||||||
|
"[python]": {
|
||||||
|
"editor.defaultFormatter": "ms-python.black-formatter"
|
||||||
|
},
|
||||||
|
"python.analysis.extraPaths": [
|
||||||
|
"/backend/"
|
||||||
|
],
|
||||||
|
"python.testing.pytestEnabled": true,
|
||||||
|
"python.testing.unittestEnabled": false,
|
||||||
|
"python.analysis.diagnosticSeverityOverrides": {
|
||||||
|
"reportMissingParameterType": "error",
|
||||||
|
"reportGeneralTypeIssues": "error",
|
||||||
|
"reportDeprecated": "error",
|
||||||
|
"reportImportCycles": "error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,22 @@
|
||||||
version: "3"
|
version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
httpd:
|
||||||
|
build:
|
||||||
|
context: ..
|
||||||
|
dockerfile: .devcontainer/Dockerfile
|
||||||
|
volumes:
|
||||||
|
- ..:/workspace
|
||||||
|
command: /bin/sh -c "while sleep 1000; do :; done"
|
||||||
|
environment:
|
||||||
|
- windir # Defined on Windows but not on other platforms
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: "postgres:latest"
|
image: "postgres:latest"
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
env_file:
|
env_file:
|
||||||
- ./backend/.env
|
- ../backend/.env
|
||||||
volumes:
|
volumes:
|
||||||
- compass-center-postgres:/var/lib/postgresql/data
|
- compass-center-postgres:/var/lib/postgresql/data
|
||||||
# - ./backend/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
|
# - ./backend/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
|
15
.vscode/extensions.json
vendored
15
.vscode/extensions.json
vendored
|
@ -1,15 +0,0 @@
|
||||||
{
|
|
||||||
"recommendations": [
|
|
||||||
"dbaeumer.vscode-eslint",
|
|
||||||
"ecmel.vscode-html-css",
|
|
||||||
"ms-vscode.vscode-typescript-next",
|
|
||||||
"esbenp.prettier-vscode",
|
|
||||||
"bradlc.vscode-tailwindcss",
|
|
||||||
"vscode-icons-team.vscode-icons",
|
|
||||||
"tamasfe.even-better-toml",
|
|
||||||
"ckolkman.vscode-postgres",
|
|
||||||
"ms-python.python",
|
|
||||||
"ms-python.vscode-pylance",
|
|
||||||
"ms-python.black-formatter"
|
|
||||||
]
|
|
||||||
}
|
|
24
.vscode/settings.json
vendored
24
.vscode/settings.json
vendored
|
@ -1,24 +0,0 @@
|
||||||
{
|
|
||||||
"editor.formatOnSave": true,
|
|
||||||
"editor.formatOnSaveMode": "file",
|
|
||||||
"[typescript]": {
|
|
||||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
||||||
},
|
|
||||||
"[javascript]": {
|
|
||||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
|
||||||
},
|
|
||||||
"[python]": {
|
|
||||||
"editor.defaultFormatter": "ms-python.autopep8"
|
|
||||||
},
|
|
||||||
"python.analysis.extraPaths": ["backend/"],
|
|
||||||
"python.testing.pytestEnabled": true,
|
|
||||||
"python.testing.unittestEnabled": false,
|
|
||||||
"python.analysis.diagnosticSeverityOverrides": {
|
|
||||||
"reportMissingParameterType": "error",
|
|
||||||
"reportGeneralTypeIssues": "error",
|
|
||||||
"reportDeprecated": "error",
|
|
||||||
"reportImportCycles": "error"
|
|
||||||
},
|
|
||||||
"python.analysis.autoImportCompletions": false,
|
|
||||||
"python.analysis.typeCheckingMode": "off"
|
|
||||||
}
|
|
|
@ -4,9 +4,9 @@ from .entity_base import EntityBase
|
||||||
|
|
||||||
|
|
||||||
class SampleEntity(EntityBase):
|
class SampleEntity(EntityBase):
|
||||||
__tablename__ = 'persons'
|
__tablename__ = "persons"
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||||
name: Mapped[str] = mapped_column(String, nullable=False)
|
name: Mapped[str] = mapped_column(String, nullable=False)
|
||||||
age: Mapped[int] = mapped_column(Integer)
|
age: Mapped[int] = mapped_column(Integer)
|
||||||
email: Mapped[str] = mapped_column(String, unique=True, nullable=False)
|
email: Mapped[str] = mapped_column(String, unique=True, nullable=False)
|
||||||
|
|
14
backend/script/create_database.py
Normal file
14
backend/script/create_database.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
from sqlalchemy import text, create_engine
|
||||||
|
from ..database import engine, _engine_str
|
||||||
|
from ..env import getenv
|
||||||
|
|
||||||
|
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"CREATE DATABASE {database}")
|
||||||
|
connection.execute(stmt)
|
14
backend/script/delete_database.py
Normal file
14
backend/script/delete_database.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
from sqlalchemy import text, create_engine
|
||||||
|
from ..database import engine, _engine_str
|
||||||
|
from ..env import getenv
|
||||||
|
|
||||||
|
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)
|
|
@ -1,38 +1,18 @@
|
||||||
from sqlalchemy import text, create_engine
|
from sqlalchemy import create_engine
|
||||||
from ..database import engine
|
import subprocess
|
||||||
|
|
||||||
|
from ..database import engine, _engine_str
|
||||||
from ..env import getenv
|
from ..env import getenv
|
||||||
from .. import entities
|
from .. import entities
|
||||||
|
|
||||||
database = getenv("POSTGRES_DATABASE")
|
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)
|
engine = create_engine(_engine_str(), echo=True)
|
||||||
"""Application-level SQLAlchemy database engine."""
|
"""Application-level SQLAlchemy database engine."""
|
||||||
|
|
||||||
|
# Run Delete and Create Database Scripts
|
||||||
with engine.connect() as connection:
|
subprocess.run(["python3", "-m", "backend.script.delete_database"])
|
||||||
connection.execute(
|
subprocess.run(["python3", "-m", "backend.script.create_database"])
|
||||||
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.drop_all(engine)
|
||||||
entities.EntityBase.metadata.create_all(engine)
|
entities.EntityBase.metadata.create_all(engine)
|
||||||
|
|
|
@ -1,22 +1,48 @@
|
||||||
"""Shared pytest fixtures for database dependent tests."""
|
"""Shared pytest fixtures for database dependent tests."""
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import Engine
|
from sqlalchemy import Engine, create_engine, text
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
import subprocess
|
from sqlalchemy.exc import OperationalError
|
||||||
|
|
||||||
from ...database import db_session
|
from ...database import _engine_str
|
||||||
|
from ...env import getenv
|
||||||
|
from ... import entities
|
||||||
|
|
||||||
|
POSTGRES_DATABASE = f'{getenv("POSTGRES_DATABASE")}_test'
|
||||||
|
POSTGRES_USER = getenv("POSTGRES_USER")
|
||||||
|
|
||||||
|
def reset_database():
|
||||||
|
engine = create_engine(_engine_str(database=""))
|
||||||
|
with engine.connect() as connection:
|
||||||
|
try:
|
||||||
|
conn = connection.execution_options(autocommit=False)
|
||||||
|
conn.execute(text("ROLLBACK")) # Get out of transactional mode...
|
||||||
|
conn.execute(text(f"DROP DATABASE IF EXISTS {POSTGRES_DATABASE}"))
|
||||||
|
except OperationalError:
|
||||||
|
print(
|
||||||
|
"Could not drop database because it's being accessed by others (psql open?)"
|
||||||
|
)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
conn.execute(text(f"CREATE DATABASE {POSTGRES_DATABASE}"))
|
||||||
|
conn.execute(
|
||||||
|
text(
|
||||||
|
f"GRANT ALL PRIVILEGES ON DATABASE {POSTGRES_DATABASE} TO {POSTGRES_USER}"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def test_engine() -> Engine:
|
def test_engine() -> Engine:
|
||||||
subprocess.run(["python3", "-m", "backend.script.create_database"])
|
reset_database()
|
||||||
session = db_session()
|
return create_engine(_engine_str(POSTGRES_DATABASE))
|
||||||
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
|
||||||
|
|
|
@ -1,13 +1,21 @@
|
||||||
"""Sample Test File"""
|
"""Sample Test File"""
|
||||||
|
|
||||||
import pytest
|
from sqlalchemy import Engine, select
|
||||||
from sqlalchemy import Engine
|
|
||||||
|
from ... import entities
|
||||||
|
from ...entities.sample_entity import SampleEntity
|
||||||
|
|
||||||
|
|
||||||
def test_sample(session: Engine):
|
def test_entity_count():
|
||||||
print(session)
|
"""Checks the number of entities to be inserted"""
|
||||||
assert session != None
|
print(entities.EntityBase.metadata.tables.keys())
|
||||||
|
assert len(entities.EntityBase.metadata.tables.keys()) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_tables(session: Engine):
|
def test_add_sample_data(session: Engine):
|
||||||
print()
|
"""Inserts a sample data point and verifies it is in the database"""
|
||||||
|
entity = SampleEntity(name="Praj", age=19, email="pmoha@unc.edu")
|
||||||
|
session.add(entity)
|
||||||
|
session.commit()
|
||||||
|
data = session.get(SampleEntity, 1)
|
||||||
|
assert data.name == "Praj"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user