add bulk_post_update: signals

This commit is contained in:
huangsong
2022-01-14 18:26:11 +08:00
parent 5b7d2d23cb
commit bca6b6eef0
10 changed files with 92 additions and 104 deletions

View File

@ -5,8 +5,10 @@ import pytest
import sqlalchemy
import ormar
from ormar import post_save, post_update, pre_save
from ormar.exceptions import ModelPersistenceError, QueryDefinitionError
from ormar.exceptions import (
ModelPersistenceError, QueryDefinitionError,
ModelListEmptyError
)
from tests.settings import DATABASE_URL
database = databases.Database(DATABASE_URL, force_rollback=True)
@ -194,35 +196,6 @@ 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:
@ -270,34 +243,6 @@ 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:
@ -367,3 +312,6 @@ async def test_bulk_update_not_saved_objts():
Note(text="Call Mum.", category=category),
]
)
with pytest.raises(ModelListEmptyError):
await Note.objects.bulk_update([])

View File

@ -6,7 +6,10 @@ import pytest
import sqlalchemy
import ormar
from ormar import post_delete, post_save, post_update, pre_delete, pre_save, pre_update
from ormar import (
post_bulk_update, post_delete, post_save, post_update,
pre_delete, pre_save, pre_update
)
from ormar.signals import SignalEmitter
from ormar.exceptions import SignalDefinitionError
from tests.settings import DATABASE_URL
@ -131,6 +134,14 @@ async def test_signal_functions(cleanup):
event_log=instance.json(),
).save()
@post_bulk_update(Album)
async def after_bulk_update(sender, instances, **kwargs):
for it in instances:
await AuditLog(
event_type=f"BULK_POST_UPDATE_{sender.get_name()}",
event_log=it.json(),
).save()
album = await Album.objects.create(name="Venice")
audits = await AuditLog.objects.all()
@ -183,6 +194,19 @@ async def test_signal_functions(cleanup):
album.signals.pre_save.disconnect(before_save)
album.signals.post_save.disconnect(after_save)
albums = await Album.objects.all()
assert len(albums)
for album in albums:
album.play_count = 1
await Album.objects.bulk_update(albums)
cnt = await AuditLog.objects.filter(event_type__contains="BULK_POST").count()
assert cnt == len(albums)
album.signals.bulk_post_update.disconnect(after_bulk_update)
@pytest.mark.asyncio
async def test_multiple_signals(cleanup):