Add QuerySet.first_or_none (#1137)

* Add QuerySet.first_or_none

* restore docstrings for exceptions

---------

Co-authored-by: collerek <collerek@gmail.com>
This commit is contained in:
Dominik Kozaczko
2024-05-26 20:13:41 +02:00
committed by GitHub
parent 1a22cf3a31
commit a6462a0ab9
2 changed files with 27 additions and 1 deletions

View File

@ -451,6 +451,11 @@ async def test_offset():
async def test_model_first():
async with base_ormar_config.database:
async with base_ormar_config.database.transaction(force_rollback=True):
with pytest.raises(ormar.NoMatch):
await User.objects.first()
assert await User.objects.first_or_none() is None
tom = await User.objects.create(name="Tom")
jane = await User.objects.create(name="Jane")
@ -460,6 +465,9 @@ async def test_model_first():
with pytest.raises(NoMatch):
await User.objects.filter(name="Lucy").first()
assert await User.objects.first_or_none(name="Lucy") is None
assert await User.objects.filter(name="Lucy").first_or_none() is None
assert await User.objects.order_by("name").first() == jane