Skip to content

Commit f0c519f

Browse files
committed
Improve tests
1 parent 1e0b55f commit f0c519f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

fastapi_users_db_dynamodb/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,16 @@ async def create(self, create_dict: dict[str, Any] | UP) -> UP:
195195
else:
196196
user = create_dict
197197
try:
198-
await user.save(condition=self.user_table.id.does_not_exist()) # type: ignore
198+
await user.save( # type: ignore
199+
condition=self.user_table.id.does_not_exist()
200+
& self.user_table.email.does_not_exist() # type: ignore
201+
)
199202
except PutError as e:
200203
if e.cause_response_code == "ConditionalCheckFailedException":
201204
raise ValueError(
202205
"User account could not be created because it already exists."
203206
) from e
204-
raise ValueError(
207+
raise ValueError( # pragma: no cover
205208
"User account could not be created because the table does not exist."
206209
) from e
207210
return user
@@ -220,7 +223,7 @@ async def update(self, user: UP, update_dict: dict[str, Any]) -> UP:
220223
raise ValueError(
221224
"User account could not be updated because it does not exist."
222225
) from e
223-
raise ValueError(
226+
raise ValueError( # pragma: no cover
224227
"User account could not be updated because the table does not exist."
225228
) from e
226229

@@ -254,7 +257,7 @@ async def add_oauth_account(self, user: UP, create_dict: dict[str, Any]) -> UP:
254257
raise ValueError(
255258
"OAuth account could not be added because it already exists."
256259
) from e
257-
raise ValueError(
260+
raise ValueError( # pragma: no cover
258261
"OAuth account could not be added because the table does not exist."
259262
) from e
260263

tests/test_users.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ async def test_queries(dynamodb_user_db: DynamoDBUserDatabase[User, UUID_ID]):
8484
# Update user
8585
updated_user = await dynamodb_user_db.update(user, {"is_superuser": True})
8686
assert updated_user.is_superuser is True
87+
with pytest.raises(
88+
ValueError,
89+
match="User account could not be updated because it does not exist.",
90+
):
91+
fake_user = User()
92+
fake_user.email = "blabla@gmail.com"
93+
fake_user.hashed_password = "crypticpassword"
94+
await dynamodb_user_db.update(fake_user, {"is_superuser": True})
8795

8896
# Get by id
8997
id_user = await dynamodb_user_db.get(user.id)
@@ -130,7 +138,13 @@ async def test_insert_existing_email(
130138
"email": "lancelot@camelot.bt",
131139
"hashed_password": "guinevere",
132140
}
133-
await dynamodb_user_db.create(user_create)
141+
user = await dynamodb_user_db.create(user_create)
142+
with pytest.raises(
143+
ValueError,
144+
match="User account could not be created because it already exists.",
145+
):
146+
user_create["id"] = str(user.id)
147+
await dynamodb_user_db.create(user_create)
134148

135149
with pytest.raises(ValueError): # oder eigene Exception
136150
existing = await dynamodb_user_db.get_by_email(user_create["email"])

0 commit comments

Comments
 (0)