Skip to content

Commit

Permalink
feat: add GET /group/{group_id} endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed May 15, 2024
1 parent da569a6 commit dbc0a64
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ def get_groups_by_owner_id(db: Session, owner_id: int):
.limit(100)
.all()
)


def get_group_by_id(db: Session, group_id: int):
return db.query(models.Group).filter(models.Group.id == group_id).first()
10 changes: 10 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ def create_group(group: schemas.GroupCreate, db: DbDependency, user: UserDepende
@app.get("/group")
def list_groups(db: DbDependency, user: UserDependency):
return crud.get_groups_by_owner_id(db, user.id)


@app.get("/group/{group_id}")
def list_groups(db: DbDependency, user: UserDependency, group_id: int):
group = crud.get_group_by_id(db, group_id)
if group is None or group.owner_id != user.id:
return HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Grupo inexistente"
)
return group
24 changes: 19 additions & 5 deletions src/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

from .database import Base, SQLALCHEMY_DATABASE_URL

from .main import app, get_db
from .database import Base, SQLALCHEMY_DATABASE_URL
from . import schemas


engine = create_engine(SQLALCHEMY_DATABASE_URL, poolclass=StaticPool)
Expand Down Expand Up @@ -151,14 +153,26 @@ def some_group(client: TestClient, some_user_id: int):
response_body = response.json()
assert "id" in response_body
assert response_body["owner_id"] == some_user_id
return response_body
return schemas.Group(**response_body)


def test_create_group(client: TestClient, some_group: int):
def test_create_group(client: TestClient, some_group: schemas.Group):
# NOTE: test is inside fixture
pass


def test_get_created_group(
client: TestClient, some_group: schemas.Group, some_user_id: int
):
response = client.get(
url=f"/group/{some_group.id}",
headers={"x-user": str(some_user_id)},
)

assert response.status_code == HTTPStatus.OK
assert response.json() == some_group.model_dump()


def test_create_group_for_invalid_user(client: TestClient):
first_response = client.post(
url="/group",
Expand All @@ -170,12 +184,12 @@ def test_create_group_for_invalid_user(client: TestClient):


def test_get_newly_created_group(
client: TestClient, some_user_id: int, some_group: int
client: TestClient, some_user_id: int, some_group: schemas.Group
):
response = client.get(
url="/group",
headers={"x-user": str(some_user_id)},
)

assert response.status_code == HTTPStatus.OK
assert response.json() == [some_group]
assert response.json() == [some_group.model_dump()]

0 comments on commit dbc0a64

Please sign in to comment.