Skip to content

Commit

Permalink
Add a internal method to create user
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Jul 30, 2024
1 parent e51b368 commit c4206ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
5 changes: 5 additions & 0 deletions fastagency/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
List,
Optional,
Protocol,
Union,
runtime_checkable,
)

Expand Down Expand Up @@ -81,6 +82,10 @@ class FrontendDBProtocol(Protocol):

async def get_user(self, user_uuid: str) -> Dict[str, Any]: ...

async def _create_user(
self, user_uuid: Union[int, str], email: str, username: str
) -> str: ...

@staticmethod
@contextmanager
def set_default(db: "FrontendDBProtocol") -> Generator[None, None, None]:
Expand Down
13 changes: 13 additions & 0 deletions fastagency/db/prisma.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,16 @@ async def get_user(self, user_uuid: Union[int, str]) -> Any:
status_code=404, detail=f"user_uuid {user_uuid} not found"
)
return user

async def _create_user(
self, user_uuid: Union[int, str], email: str, username: str
) -> str:
"""Only to create user in testing."""
async with self._get_db_connection() as db:
insert_query = (
'INSERT INTO "User" (email, username, uuid) VALUES (' # nosec: [B608]
+ f"'{email}', '{username}', '{user_uuid}')"
)
await db.execute_raw(insert_query)

return str(user_uuid)
16 changes: 7 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ async def user_uuid() -> AsyncIterator[str]:
try:
random_id = random.randint(1, 1_000_000)
generated_uuid = str(uuid.uuid4())
async with PrismaFrontendDB()._get_db_connection() as db:
insert_query = (
'INSERT INTO "User" (email, username, uuid) VALUES ('
+ f"'user{random_id}@airt.ai', 'user{random_id}', '{generated_uuid}')"
)
await db.execute_raw(insert_query)

select_query = 'SELECT * FROM "User" WHERE uuid=' + f"'{generated_uuid}'"
user = await db.query_first(select_query)
email = f"user{random_id}@airt.ai"
username = f"user{random_id}"

await FrontendDBProtocol.db()._create_user(
user_uuid=generated_uuid, email=email, username=username
)
user = await FrontendDBProtocol.db().get_user(user_uuid=generated_uuid)

yield user["uuid"]
finally:
Expand Down

0 comments on commit c4206ef

Please sign in to comment.