Skip to content

Commit

Permalink
fix(agents-api): Fixed tests
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Tomer <[email protected]>
  • Loading branch information
Diwank Tomer committed Aug 9, 2024
1 parent dd52045 commit d0b3e6b
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 17 deletions.
6 changes: 5 additions & 1 deletion agents-api/agents_api/models/agent/get_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@

@rewrap_exceptions(
{
lambda e: isinstance(e, QueryException)
and "Developer not found" in str(e): lambda *_: HTTPException(
detail="developer does not exist", status_code=403
),
lambda e: isinstance(e, QueryException)
and "asserted to return some results, but returned none"
in str(e): lambda *_: HTTPException(
detail="developer not found or doesnt own resource", status_code=404
detail="developer doesnt own resource", status_code=404
),
QueryException: partialclass(HTTPException, status_code=400),
ValidationError: partialclass(HTTPException, status_code=400),
Expand Down
5 changes: 5 additions & 0 deletions agents-api/agents_api/models/user/create_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

@rewrap_exceptions(
{
lambda e: isinstance(e, QueryException)
and "asserted to return some results, but returned none"
in str(e): lambda *_: HTTPException(
detail="developer not found", status_code=403
),
QueryException: partialclass(HTTPException, status_code=400),
ValidationError: partialclass(HTTPException, status_code=400),
TypeError: partialclass(HTTPException, status_code=400),
Expand Down
13 changes: 8 additions & 5 deletions agents-api/agents_api/models/user/delete_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ def delete_user(*, developer_id: UUID, user_id: UUID) -> tuple[list[str], dict]:
verify_developer_owns_resource_query(developer_id, "users", user_id=user_id),
"""
# Delete docs
?[user_id, doc_id] :=
?[owner_type, owner_id, doc_id] :=
*docs{
owner_id: user_id,
owner_type: "user",
owner_id,
owner_type,
doc_id,
}, user_id = to_uuid($user_id)
},
owner_id = to_uuid($user_id),
owner_type = "user"
:delete docs {
user_id,
owner_type,
owner_id,
doc_id
}
:returning
Expand Down
9 changes: 9 additions & 0 deletions agents-api/agents_api/models/user/get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

@rewrap_exceptions(
{
lambda e: isinstance(e, QueryException)
and "Developer not found" in str(e): lambda *_: HTTPException(
detail="developer does not exist", status_code=403
),
lambda e: isinstance(e, QueryException)
and "asserted to return some results, but returned none"
in str(e): lambda *_: HTTPException(
detail="developer doesnt own resource", status_code=404
),
QueryException: partialclass(HTTPException, status_code=400),
ValidationError: partialclass(HTTPException, status_code=400),
TypeError: partialclass(HTTPException, status_code=400),
Expand Down
7 changes: 5 additions & 2 deletions agents-api/agents_api/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ class NewCls(cls):

def verify_developer_id_query(developer_id: UUID | str) -> str:
return f"""
?[developer_id] :=
matched[count(developer_id)] :=
*developers{{
developer_id,
}}, developer_id = to_uuid("{str(developer_id)}")
:assert some
?[exists] :=
matched[num],
exists = num > 0,
assert(exists, "Developer does not exist")
"""


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ async def create_or_update_agent(
data=data,
)

return ResourceCreatedResponse(id=agent.id, created_at=agent.created_at)
return ResourceCreatedResponse(id=agent.id, created_at=agent.created_at, jobs=[])
2 changes: 1 addition & 1 deletion agents-api/agents_api/routers/users/create_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ async def create_user(
data=data,
)

return ResourceCreatedResponse(id=user.id, created_at=user.created_at)
return ResourceCreatedResponse(id=user.id, created_at=user.created_at, jobs=[])
1 change: 1 addition & 0 deletions agents-api/agents_api/routers/users/delete_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
async def delete_user(
user_id: UUID4, x_developer_id: Annotated[UUID4, Depends(get_developer_id)]
) -> ResourceDeletedResponse:
print(user_id)
return delete_user_query(developer_id=x_developer_id, user_id=user_id)
7 changes: 4 additions & 3 deletions agents-api/agents_api/routers/users/list_users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from json import JSONDecodeError
from typing import Annotated, List, Literal
from typing import Annotated, Literal

from fastapi import Depends, HTTPException, status
from pydantic import UUID4
Expand All @@ -19,7 +19,7 @@ async def list_users(
sort_by: Literal["created_at", "updated_at"] = "created_at",
direction: Literal["asc", "desc"] = "desc",
metadata_filter: str = "{}",
) -> List[User]:
) -> ListResponse[User]:
try:
metadata_filter = json.loads(metadata_filter)
except JSONDecodeError:
Expand All @@ -37,4 +37,5 @@ async def list_users(
metadata_filter=metadata_filter,
)

return ListResponse[User](items=users)
result = ListResponse[User](items=users)
return result
3 changes: 2 additions & 1 deletion agents-api/agents_api/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from agents_api.routers import (
agents,
tasks,
users,
)

if not sentry_dsn:
Expand Down Expand Up @@ -88,7 +89,7 @@ def register_exceptions(app: FastAPI):

app.include_router(agents.router)
# app.include_router(sessions.router)
# app.include_router(users.router)
app.include_router(users.router)
# app.include_router(jobs.router)
app.include_router(tasks.router)

Expand Down
8 changes: 5 additions & 3 deletions agents-api/tests/test_agent_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def _(make_request=make_request):

@test("route: create or update agent")
def _(make_request=make_request):
agent_id = str(uuid4())

data = dict(
name="test agent",
about="test agent about",
Expand All @@ -69,7 +71,7 @@ def _(make_request=make_request):

response = make_request(
method="POST",
url="/agents",
url=f"/agents/{agent_id}",
json=data,
)

Expand Down Expand Up @@ -162,7 +164,7 @@ def _(make_request=make_request, agent=test_agent):
assert "test" not in agent["metadata"]


@test("model: patch agent")
@test("route: patch agent")
def _(make_request=make_request, agent=test_agent):
agent_id = str(agent.id)

Expand Down Expand Up @@ -194,7 +196,7 @@ def _(make_request=make_request, agent=test_agent):
assert "hello" in agent["metadata"]


@test("model: list agents")
@test("route: list agents")
def _(make_request=make_request):
response = make_request(
method="GET",
Expand Down

0 comments on commit d0b3e6b

Please sign in to comment.