Skip to content

♻ Refactor items and services endpoints to return count and data, and add CI tests #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Pytest Backend

on:
push:
branches:
- master

jobs:

pytest:
runs-on: ubuntu-latest
defaults:
run:
working-directory: src
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup python 3.10
uses: actions/setup-python@v2
with:
python-version: '3.10'

- name: docker-compose setup
run: |
docker-compose up -d

- name: docker-compose run pytest
run: |
docker exec src_backend_1 pytest --cov=app --cov-report=term-missing app/tests
15 changes: 10 additions & 5 deletions src/backend/app/app/api/api_v1/endpoints/items.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
from typing import Any

from fastapi import APIRouter, HTTPException
from sqlmodel import select
from sqlmodel import select, func

from app.api.deps import CurrentUser, SessionDep
from app.models import Item, ItemCreate, ItemOut, ItemUpdate, Message
from app.models import Item, ItemCreate, ItemOut, ItemUpdate, Message, ItemsOut

router = APIRouter()


@router.get("/", response_model=list[ItemOut])
@router.get("/", response_model=ItemsOut)
def read_items(
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
) -> Any:
"""
Retrieve items.
"""

statment = select(func.count()).select_from(Item)
count = session.exec(statment).one()

if current_user.is_superuser:
statement = select(Item).offset(skip).limit(limit)
return session.exec(statement).all()
items = session.exec(statement).all()
else:
statement = (
select(Item)
.where(Item.owner_id == current_user.id)
.offset(skip)
.limit(limit)
)
return session.exec(statement).all()
items = session.exec(statement).all()

return ItemsOut(data=items, count=count)


@router.get("/{id}", response_model=ItemOut)
Expand Down
12 changes: 9 additions & 3 deletions src/backend/app/app/api/api_v1/endpoints/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, List

from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import select
from sqlmodel import select, func

from app import crud
from app.api.deps import (
Expand All @@ -16,6 +16,7 @@
UserCreate,
UserCreateOpen,
UserOut,
UsersOut,
UserUpdate,
UserUpdateMe,
)
Expand All @@ -27,15 +28,20 @@
@router.get(
"/",
dependencies=[Depends(get_current_active_superuser)],
response_model=List[UserOut],
response_model=UsersOut
)
def read_users(session: SessionDep, skip: int = 0, limit: int = 100) -> Any:
"""
Retrieve users.
"""

statment = select(func.count()).select_from(User)
count = session.exec(statment).one()

statement = select(User).offset(skip).limit(limit)
users = session.exec(statement).all()
return users

return UsersOut(data=users, count=count)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Front end needs to know how many items are in database to be able to create a pagination



@router.post(
Expand Down
11 changes: 11 additions & 0 deletions src/backend/app/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class UserOut(UserBase):
id: int


class UsersOut(BaseModel):
data: list[UserOut]
count: int


# Shared properties
class ItemBase(SQLModel):
title: str
Expand Down Expand Up @@ -75,6 +80,12 @@ class Item(ItemBase, table=True):
# Properties to return via API, id is always required
class ItemOut(ItemBase):
id: int
owner_id: int


class ItemsOut(BaseModel):
data: list[ItemOut]
count: int


# Generic message
Expand Down
5 changes: 3 additions & 2 deletions src/backend/app/app/tests/api/api_v1/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def test_retrieve_users(
r = client.get(f"{settings.API_V1_STR}/users/", headers=superuser_token_headers)
all_users = r.json()

assert len(all_users) > 1
for item in all_users:
assert len(all_users["data"]) > 1
assert "count" in all_users
for item in all_users["data"]:
assert "email" in item
20 changes: 12 additions & 8 deletions src/docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ services:
- traefik.http.routers.${STACK_NAME?Variable not set}-traefik-public-http.rule=Host(`${DOMAIN?Variable not set}`)
- traefik.http.services.${STACK_NAME?Variable not set}-traefik-public.loadbalancer.server.port=80

db:
ports:
- "5432:5432"

pgadmin:
ports:
- "5050:5050"
Expand Down Expand Up @@ -84,14 +88,14 @@ services:
- traefik.http.routers.${STACK_NAME?Variable not set}-frontend-http.rule=Host(`old-frontend.localhost.tiangolo.com`)
- traefik.http.services.${STACK_NAME?Variable not set}-frontend.loadbalancer.server.port=80

new-frontend:
build:
context: ./new-frontend
labels:
- traefik.enable=true
- traefik.constraint-label-stack=${TRAEFIK_TAG?Variable not set}
- traefik.http.routers.${STACK_NAME?Variable not set}-new-frontend-http.rule=PathPrefix(`/`)
- traefik.http.services.${STACK_NAME?Variable not set}-new-frontend.loadbalancer.server.port=80
# new-frontend:
# build:
# context: ./new-frontend
# labels:
# - traefik.enable=true
# - traefik.constraint-label-stack=${TRAEFIK_TAG?Variable not set}
# - traefik.http.routers.${STACK_NAME?Variable not set}-new-frontend-http.rule=PathPrefix(`/`)
# - traefik.http.services.${STACK_NAME?Variable not set}-new-frontend.loadbalancer.server.port=80

networks:
traefik-public:
Expand Down
20 changes: 10 additions & 10 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,16 @@ services:
- traefik.http.routers.${STACK_NAME?Variable not set}-frontend-http.rule=PathPrefix(`/`)
- traefik.http.services.${STACK_NAME?Variable not set}-frontend.loadbalancer.server.port=80

new-frontend:
image: '${DOCKER_IMAGE_NEW_FRONTEND?Variable not set}:${TAG-latest}'
build:
context: ./new-frontend
deploy:
labels:
- traefik.enable=true
- traefik.constraint-label-stack=${TRAEFIK_TAG?Variable not set}
- traefik.http.routers.${STACK_NAME?Variable not set}-new-frontend-http.rule=PathPrefix(`/`)
- traefik.http.services.${STACK_NAME?Variable not set}-new-frontend.loadbalancer.server.port=80
# new-frontend:
# image: '${DOCKER_IMAGE_NEW_FRONTEND?Variable not set}:${TAG-latest}'
# build:
# context: ./new-frontend
# deploy:
# labels:
# - traefik.enable=true
# - traefik.constraint-label-stack=${TRAEFIK_TAG?Variable not set}
# - traefik.http.routers.${STACK_NAME?Variable not set}-new-frontend-http.rule=PathPrefix(`/`)
# - traefik.http.services.${STACK_NAME?Variable not set}-new-frontend.loadbalancer.server.port=80


volumes:
Expand Down