Skip to content

Commit 644a135

Browse files
Add nested model and relationship test
1 parent c796dfa commit 644a135

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/test_fastapi_users_db_ormar.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,23 @@
2020
database = databases.Database(DATABASE_URL)
2121

2222

23+
class Role(ormar.Model):
24+
class Meta:
25+
tablename = "roles"
26+
metadata = metadata
27+
database = database
28+
29+
id = ormar.UUID(primary_key=True, uuid_format="string")
30+
name = ormar.String(nullable=False, max_length=255)
31+
32+
2333
class User(OrmarBaseUserModel):
2434
class Meta:
2535
metadata = metadata
2636
database = database
2737

2838
first_name = ormar.String(nullable=True, max_length=255)
39+
roles = ormar.ManyToMany(Role, skip_reverse=True)
2940

3041

3142
class OAuthAccount(OrmarBaseOAuthAccountModel):
@@ -196,3 +207,27 @@ async def test_queries_oauth(
196207
# Unknown OAuth account
197208
unknown_oauth_user = await ormar_user_db_oauth.get_by_oauth_account("foo", "bar")
198209
assert unknown_oauth_user is None
210+
211+
212+
@pytest.mark.asyncio
213+
@pytest.mark.db
214+
async def test_queries_custom_fields_relations(ormar_user_db: OrmarUserDatabase[UserDB]):
215+
# Create role to pair with
216+
role = await Role.objects.create(
217+
id=uuid.uuid4(),
218+
name='editor'
219+
)
220+
221+
assert role.id is not None
222+
223+
user = UserDB(
224+
email="lancelot@camelot.bt",
225+
hashed_password="guinevere",
226+
roles=[role]
227+
)
228+
229+
# Create with relationship
230+
user_db = await ormar_user_db.create(user)
231+
assert user_db.roles is not None
232+
assert len(user_db.roles) is not 0
233+
assert user_db.roles[0].id == role.id

0 commit comments

Comments
 (0)