Skip to content
This repository has been archived by the owner on Feb 15, 2025. It is now read-only.

Commit

Permalink
chore(api): raise exception on CRUD base fails (#748)
Browse files Browse the repository at this point in the history
Fix for api key retrieval
Fixes to CRUD error handling for no results
Edits to unit testing for CRUD base:
- Add mocks for `api_key` table
- Add mocks for user_id on `create`
  • Loading branch information
alekst23 authored Jul 12, 2024
1 parent 57ddfb6 commit 4d8ff03
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 99 deletions.
21 changes: 12 additions & 9 deletions src/leapfrogai_api/data/crud_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ async def create(self, object_: ModelType) -> ModelType | None:
if "user_id" in response[0]:
del response[0]["user_id"]
return self.model(**response[0])
except Exception:
return None
except Exception as e:
raise e

async def get(self, filters: dict | None = None) -> ModelType | None:
"""Get row by filters."""
Expand All @@ -56,8 +56,10 @@ async def get(self, filters: dict | None = None) -> ModelType | None:
if "user_id" in response[0]:
del response[0]["user_id"]
return self.model(**response[0])
except Exception:
except IndexError:
return None
except Exception as e:
raise e

async def list(self, filters: dict | None = None) -> list[ModelType]:
"""List all rows."""
Expand All @@ -75,8 +77,8 @@ async def list(self, filters: dict | None = None) -> list[ModelType]:
if "user_id" in item:
del item["user_id"]
return [self.model(**item) for item in response]
except Exception as exc:
raise Exception from exc
except Exception as e:
raise e

async def update(self, id_: str, object_: ModelType) -> ModelType | None:
"""Update a row by its ID."""
Expand All @@ -93,8 +95,10 @@ async def update(self, id_: str, object_: ModelType) -> ModelType | None:
if "user_id" in response[0]:
del response[0]["user_id"]
return self.model(**response[0])
except Exception:
except IndexError:
return None
except Exception as e:
raise e

async def delete(self, filters: dict | None = None) -> bool:
"""Delete a row by filters."""
Expand All @@ -115,9 +119,8 @@ async def _get_user_id(self) -> str:
"""Get the user_id from the API key."""

if self.db.options.headers.get("x-custom-api-key"):
data, _count = await self.db.table("api_keys").select("user_id").execute()
_, tmp = data
user_id: str = tmp[0]["user_id"]
result = await self.db.table("api_keys").select("user_id").execute()
user_id: str = result.data[0]["user_id"]
else:
user_id = (await self.db.auth.get_user()).user.id

Expand Down
2 changes: 2 additions & 0 deletions tests/mocks/mock_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mock_run,
mock_message,
mock_data_model,
mock_api_key,
)

_mocks_cache = {}
Expand Down Expand Up @@ -67,6 +68,7 @@ def mock_execute_data(table_name):
assistant=mock_assistant,
message=mock_message,
dummy_table=mock_data_model,
api_keys=mock_api_key,
)
if table_name:
return mock_map[table_name]
Expand Down
7 changes: 7 additions & 0 deletions tests/mocks/mock_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ class MockModel(BaseModel):
TextContentBlock(text=Text(value="mock-data", annotations=[]), type="text")
],
)


class MockApiKey(BaseModel):
user_id: str


mock_api_key = MockApiKey(user_id="mock-api-key")
Loading

0 comments on commit 4d8ff03

Please sign in to comment.