Skip to content

Commit 1e0b55f

Browse files
committed
Improve tests
1 parent b66d38e commit 1e0b55f

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

fastapi_users_db_dynamodb/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ async def get_by_oauth_account(
182182
oauth_acc.user_id,
183183
consistent_read=instant_update,
184184
)
185-
except self.user_table.DoesNotExist: # type: ignore
185+
except self.user_table.DoesNotExist: # type: ignore # pragma: no cover
186186
return None
187187
return None
188188

@@ -231,10 +231,8 @@ async def delete(self, user: UP) -> None:
231231
try:
232232
await user.delete(condition=self.user_table.id.exists()) # type: ignore
233233
except DeleteError as e:
234-
raise ValueError( # pragma: no cover
235-
"User account could not be deleted."
236-
) from e
237-
except PutError as e:
234+
raise ValueError("User account could not be deleted.") from e
235+
except PutError as e: # pragma: no cover
238236
if e.cause_response_code == "ConditionalCheckFailedException":
239237
raise ValueError(
240238
"User account could not be deleted because it does not exist."
@@ -282,7 +280,7 @@ async def update_oauth_account(
282280
raise ValueError(
283281
"OAuth account could not be updated because it does not exist."
284282
) from e
285-
raise ValueError(
283+
raise ValueError( # pragma: no cover
286284
"OAuth account could not be updated because the table does not exist."
287285
) from e
288286

fastapi_users_db_dynamodb/access_token.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def create(self, create_dict: dict[str, Any] | AP) -> AP:
9797
raise ValueError(
9898
"Access token could not be created because it already exists."
9999
) from e
100-
raise ValueError(
100+
raise ValueError( # pragma: no cover
101101
"Access token could not be created because the table does not exist."
102102
) from e
103103
return token
@@ -116,7 +116,7 @@ async def update(self, access_token: AP, update_dict: dict[str, Any]) -> AP:
116116
raise ValueError(
117117
"Access token could not be updated because it does not exist."
118118
) from e
119-
raise ValueError(
119+
raise ValueError( # pragma: no cover
120120
"Access token could not be updated because the table does not exist."
121121
) from e
122122

@@ -127,10 +127,8 @@ async def delete(self, access_token: AP) -> None:
127127
try:
128128
await access_token.delete(condition=self.access_token_table.token.exists()) # type: ignore
129129
except DeleteError as e:
130-
raise ValueError( # pragma: no cover
131-
"Access token could not be deleted."
132-
) from e
133-
except PutError as e:
130+
raise ValueError("Access token could not be deleted.") from e
131+
except PutError as e: # pragma: no cover
134132
if e.cause_response_code == "ConditionalCheckFailedException":
135133
raise ValueError(
136134
"Access token could not be deleted because it does not exist."

tests/test_access_token.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ async def test_queries(
110110

111111
# Delete
112112
await dynamodb_access_token_db.delete(access_token)
113+
with pytest.raises(ValueError, match="Access token could not be deleted"):
114+
await dynamodb_access_token_db.delete(access_token)
115+
113116
deleted_token = await dynamodb_access_token_db.get_by_token(access_token.token)
114117
assert deleted_token is None
115118

tests/test_users.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ async def test_queries(dynamodb_user_db: DynamoDBUserDatabase[User, UUID_ID]):
107107

108108
# Delete user
109109
await dynamodb_user_db.delete(user)
110+
with pytest.raises(ValueError, match="User account could not be deleted"):
111+
await dynamodb_user_db.delete(user)
110112
deleted_user = await dynamodb_user_db.get(user.id)
111113
assert deleted_user is None
112114

@@ -160,6 +162,7 @@ async def test_queries_oauth(
160162
dynamodb_user_db_oauth: DynamoDBUserDatabase[UserOAuth, UUID_ID],
161163
oauth_account1: dict[str, Any],
162164
oauth_account2: dict[str, Any],
165+
user_id: UUID_ID,
163166
):
164167
# Test OAuth accounts
165168
user_create = {"email": "lancelot@camelot.bt", "hashed_password": "guinevere"}
@@ -216,3 +219,19 @@ def _get_account(_user: UserOAuth):
216219
# Unknown OAuth account
217220
unknown_oauth_user = await dynamodb_user_db_oauth.get_by_oauth_account("foo", "bar")
218221
assert unknown_oauth_user is None
222+
223+
with pytest.raises(
224+
ValueError,
225+
match="OAuth account could not be updated because it does not exist.",
226+
):
227+
user = UserOAuth()
228+
oauth_account = OAuthAccount()
229+
oauth_account.user_id = user_id
230+
oauth_account.oauth_name = "blabla_provider"
231+
oauth_account.account_id = "blabla_id"
232+
oauth_account.account_email = "blabla@gmail.com"
233+
await dynamodb_user_db_oauth.update_oauth_account(
234+
user,
235+
oauth_account,
236+
{"access_token": "NEW_TOKEN"},
237+
)

0 commit comments

Comments
 (0)