Update get_or_create method

This commit is contained in:
Mojix Coder
2022-02-01 09:44:07 +03:30
parent 4ed267e5c3
commit fc32001fe7
9 changed files with 92 additions and 33 deletions

View File

@ -166,17 +166,18 @@ async def test_delete_and_update():
@pytest.mark.asyncio
async def test_get_or_create():
async with database:
tom = await Book.objects.get_or_create(
tom, created = await Book.objects.get_or_create(
title="Volume I", author="Anonymous", genre="Fiction"
)
assert await Book.objects.count() == 1
assert created is True
assert (
await Book.objects.get_or_create(
title="Volume I", author="Anonymous", genre="Fiction"
)
== tom
second_tom, created = await Book.objects.get_or_create(
title="Volume I", author="Anonymous", genre="Fiction"
)
assert second_tom.pk == tom.pk
assert created is False
assert await Book.objects.count() == 1
assert await Book.objects.create(
@ -188,6 +189,28 @@ async def test_get_or_create():
)
@pytest.mark.asyncio
async def test_get_or_create_with_defaults():
async with database:
book, created = await Book.objects.get_or_create(
title="Nice book", _defaults={"author": "Mojix", "genre": "Historic"}
)
assert created is True
assert book.author == "Mojix"
assert book.title == "Nice book"
assert book.genre == "Historic"
book2, created = await Book.objects.get_or_create(
author="Mojix", _defaults={"title": "Book2"}
)
assert created is False
assert book2 == book
assert book2.title == "Nice book"
assert book2.author == "Mojix"
assert book2.genre == "Historic"
assert await Book.objects.count() == 1
@pytest.mark.asyncio
async def test_update_or_create():
async with database: