Skip to content

Commit

Permalink
Merge pull request #1 from profcomff/backend-fixes
Browse files Browse the repository at this point in the history
Add simple logic, fix tests, linting
  • Loading branch information
grigoriev-semyon authored Mar 24, 2024
2 parents 2ed0c9c + b926be6 commit ed2b6eb
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 48 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- name: cd into folder
run: cd frontend
- uses: actions/setup-node@v3
with:
node-version: '18'
Expand Down
6 changes: 2 additions & 4 deletions backend/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from logging.config import fileConfig

from alembic import context
from sqlalchemy import engine_from_config, pool

from my_app_api.models.base import Base
from my_app_api.settings import get_settings

from sqlalchemy import engine_from_config, pool

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand Down Expand Up @@ -61,7 +59,7 @@ def run_migrations_online():
"""
configuration = config.get_section(config.config_ini_section)
configuration['sqlalchemy.url'] = str(settings.DB_DSN)
configuration["sqlalchemy.url"] = str(settings.DB_DSN)
connectable = engine_from_config(
configuration,
prefix="sqlalchemy.",
Expand Down
1 change: 0 additions & 1 deletion backend/my_app_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os


__version__ = os.getenv("APP_VERSION", "dev")
4 changes: 1 addition & 3 deletions backend/my_app_api/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import uvicorn

from my_app_api.routes import app


if __name__ == '__main__':
if __name__ == "__main__":
uvicorn.run(app)
5 changes: 5 additions & 0 deletions backend/my_app_api/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class APIError(Exception):
"""Base class for API errors"""

def __init__(self, message: str) -> None:
super().__init__(message)
24 changes: 14 additions & 10 deletions backend/my_app_api/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi_sqlalchemy import DBSessionMiddleware

from my_app_api import __version__
from my_app_api.settings import get_settings

from .click import router as click_router

from .touch import router as touch_router

settings = get_settings()
logger = logging.getLogger(__name__)
app = FastAPI(
title='Мое приложение',
description='Бэкэнд приложения-примера',
title="Мое приложение",
description="Бэкэнд приложения-примера",
version=__version__,
# Отключаем нелокальную документацию
root_path=settings.ROOT_PATH if __version__ != 'dev' else '',
docs_url='/swagger',
root_path=settings.ROOT_PATH if __version__ != "dev" else "",
docs_url="/swagger",
redoc_url=None,
)

Expand All @@ -39,10 +37,16 @@

if settings.UI_DIR_PATH:
logger.debug("Enabling UI")
app.mount("/ui", app=StaticFiles(directory=settings.UI_DIR_PATH, html=True), name="ui")
app.mount(
"/ui", app=StaticFiles(directory=settings.UI_DIR_PATH, html=True), name="ui"
)

if settings.DOCS_DIR_PATH:
logger.debug("Enabling Docs")
app.mount("/docs", app=StaticFiles(directory=settings.DOCS_DIR_PATH, html=True), name="docs")
app.mount(
"/docs",
app=StaticFiles(directory=settings.DOCS_DIR_PATH, html=True),
name="docs",
)

app.include_router(click_router)
app.include_router(touch_router)
25 changes: 0 additions & 25 deletions backend/my_app_api/routes/click.py

This file was deleted.

31 changes: 31 additions & 0 deletions backend/my_app_api/routes/touch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import logging

from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends
from pydantic import BaseModel

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/example", tags=["Example"])

CLICKS: dict[int, int] = {}


class WhoAmI(BaseModel):
id: int


class TouchGet(WhoAmI):
count: int


@router.get("/whoami", response_model=WhoAmI)
def whoami(auth=Depends(UnionAuth(allow_none=False))):
return {"id": auth["id"]}


@router.post("/touch", response_model=TouchGet)
def touch(auth=Depends(UnionAuth(allow_none=False))):
if auth["id"] not in CLICKS:
CLICKS[auth["id"]] = 0
CLICKS[auth["id"]] += 1
return {"id": auth["id"], "count": CLICKS[auth["id"]]}
2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.black]
line-length = 120
target-version = ['py312']
target-version = ['py311']
skip-string-normalization = true

[tool.isort]
Expand Down
1 change: 1 addition & 0 deletions backend/requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ isort
pytest
pytest-cov
pytest-mock
auth-lib-profcomff[testing]
9 changes: 9 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from fastapi.testclient import TestClient
from my_app_api.routes import app


@pytest.fixture
def client():
client = TestClient(app)
yield client
1 change: 0 additions & 1 deletion backend/tests/test_routes/.gitkeep

This file was deleted.

21 changes: 21 additions & 0 deletions backend/tests/test_routes/test_click.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest


@pytest.mark.authenticated()
def test_whoami(client):
resp = client.get("/example/whoami")
assert resp.status_code == 200
assert resp.json()["id"] == 0


@pytest.mark.authenticated()
def test_touch(client):
resp = client.post("/example/touch")
assert resp.status_code == 200
assert resp.json()["id"] == 0
assert resp.json()["count"] == 1

resp = client.post("/example/touch")
assert resp.status_code == 200
assert resp.json()["id"] == 0
assert resp.json()["count"] == 2
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint --ext ts,vue src/ --fix",
"format": "prettier src/ --write",
"stylelint": "stylelint 'src/**/*.{vue,css}' --fix",
"check": "vue-tsc && pnpm run format && pnpm run lint && pnpm run stylelint"
"check": "vue-tsc && npm run format && npm run lint && npm run stylelint"
},
"dependencies": {
"axios": "^1.6.5",
Expand Down

0 comments on commit ed2b6eb

Please sign in to comment.