add tests

This commit is contained in:
huangsong
2022-01-14 15:49:26 +08:00
parent 133ba6e1c1
commit 5b7d2d23cb
2 changed files with 89 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import pytest
import sqlalchemy
import ormar
from ormar import post_save, post_update, pre_save
from ormar.exceptions import ModelPersistenceError, QueryDefinitionError
from tests.settings import DATABASE_URL
@ -193,6 +194,35 @@ async def test_bulk_create():
assert len(completed) == 2
@pytest.mark.asyncio
async def test_bulk_create_send_signals():
async with database:
@pre_save(ToDo)
async def before_save(sender, instance, **kwargs):
instance.completed = False
@post_save(ToDo)
async def after_save(sender, instance, **kwargs):
assert not instance.completed
await ToDo.objects.bulk_create(
[
ToDo(text="Buy the groceries."),
ToDo(text="Call Mum.", completed=True),
ToDo(text="Send invoices.", completed=True),
], send_signals=True
)
todoes = await ToDo.objects.all()
assert len(todoes) == 3
for todo in todoes:
assert todo.pk is not None
count = await ToDo.objects.filter(completed=False).count()
assert count == 3
@pytest.mark.asyncio
async def test_bulk_create_with_relation():
async with database:
@ -240,6 +270,34 @@ async def test_bulk_update():
assert todo.text[-2:] == "_1"
@pytest.mark.asyncio
async def test_bulk_update_send_signals():
async with database:
@post_update(ToDo)
async def after_update(sender, instance, **kwargs):
await instance.delete()
await ToDo.objects.bulk_create(
[
ToDo(text="Buy the groceries."),
ToDo(text="Call Mum.", completed=True),
ToDo(text="Send invoices.", completed=True),
]
)
todoes = await ToDo.objects.all()
assert len(todoes) == 3
for todo in todoes:
todo.text = todo.text + "_1"
todo.completed = False
await ToDo.objects.bulk_update(todoes, send_signals=True)
count = await ToDo.objects.filter(completed=False).count()
assert count == 0
@pytest.mark.asyncio
async def test_bulk_update_with_only_selected_columns():
async with database: