Skip to content

Commit 206a1e5

Browse files
committed
Fix incompatibility with Beanie 0.18
1 parent f835660 commit 206a1e5

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

fastapi_users_db_beanie/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ async def get_by_oauth_account(
8989
async def create(self, create_dict: Dict[str, Any]) -> UP_BEANIE:
9090
"""Create a user."""
9191
user = self.user_model(**create_dict)
92-
return await user.insert()
92+
await user.create()
93+
return user
9394

9495
async def update(self, user: UP_BEANIE, update_dict: Dict[str, Any]) -> UP_BEANIE:
9596
"""Update a user."""
9697
for key, value in update_dict.items():
9798
setattr(user, key, value)
98-
return await user.save()
99+
await user.save()
100+
return user
99101

100102
async def delete(self, user: UP_BEANIE) -> None:
101103
"""Delete a user."""
@@ -111,7 +113,8 @@ async def add_oauth_account(
111113
oauth_account = self.oauth_account_model(**create_dict)
112114
user.oauth_accounts.append(oauth_account) # type: ignore
113115

114-
return await user.save()
116+
await user.save()
117+
return user
115118

116119
async def update_oauth_account(
117120
self, user: UP_BEANIE, oauth_account: OAP, update_dict: Dict[str, Any]
@@ -128,7 +131,8 @@ async def update_oauth_account(
128131
for key, value in update_dict.items():
129132
setattr(user.oauth_accounts[i], key, value) # type: ignore
130133

131-
return await user.save()
134+
await user.save()
135+
return user
132136

133137

134138
class ObjectIDIDMixin:

fastapi_users_db_beanie/access_token.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ async def get_by_token(
4040

4141
async def create(self, create_dict: Dict[str, Any]) -> AP_BEANIE:
4242
access_token = self.access_token_model(**create_dict)
43-
return await access_token.save()
43+
await access_token.create()
44+
return access_token
4445

4546
async def update(
4647
self, access_token: AP_BEANIE, update_dict: Dict[str, Any]
4748
) -> AP_BEANIE:
4849
for key, value in update_dict.items():
4950
setattr(access_token, key, value)
50-
return await access_token.save()
51+
await access_token.save()
52+
return access_token
5153

5254
async def delete(self, access_token: AP_BEANIE) -> None:
5355
await access_token.delete()

tests/conftest.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,9 @@
66

77
@pytest.fixture(scope="session")
88
def event_loop():
9-
"""
10-
Force the pytest-asyncio loop to be the main one.
11-
If there is no running event loop, create one and
12-
set as the current one.
13-
"""
14-
try:
15-
loop = asyncio.get_running_loop()
16-
except RuntimeError:
17-
loop = asyncio.new_event_loop()
18-
asyncio.set_event_loop(loop)
9+
loop = asyncio.get_event_loop()
1910
yield loop
11+
loop.close()
2012

2113

2214
@pytest.fixture

tests/test_access_token.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,14 @@ async def test_queries(
6363
assert access_token.user_id == user_id
6464

6565
# Update
66-
update_dict = {"created_at": datetime.now(timezone.utc)}
66+
updated_created_at = datetime.now(timezone.utc)
67+
update_dict = {"created_at": updated_created_at}
6768
updated_access_token = await beanie_access_token_db.update(
6869
access_token, update_dict
6970
)
70-
assert updated_access_token.created_at == update_dict["created_at"]
71+
assert updated_access_token.created_at == update_dict["created_at"].replace(
72+
microsecond=int(updated_created_at.microsecond / 1000) * 1000, tzinfo=None
73+
)
7174

7275
# Get by token
7376
access_token_by_token = await beanie_access_token_db.get_by_token(

0 commit comments

Comments
 (0)