Skip to content
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

feat: add GET /group/{group_id} endpoint #9

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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()]
Loading