fix json fields in bulk operations

This commit is contained in:
collerek
2022-01-14 18:27:49 +01:00
parent 7f517c9bdb
commit 5677bda054
8 changed files with 90 additions and 27 deletions

View File

@ -1,13 +1,15 @@
from typing import Optional
from typing import List, Optional
import databases
import pydantic
import pytest
import sqlalchemy
import ormar
from ormar.exceptions import (
ModelPersistenceError, QueryDefinitionError,
ModelListEmptyError
ModelPersistenceError,
QueryDefinitionError,
ModelListEmptyError,
)
from tests.settings import DATABASE_URL
@ -63,6 +65,17 @@ class Note(ormar.Model):
category: Optional[Category] = ormar.ForeignKey(Category)
class ItemConfig(ormar.Model):
class Meta:
metadata = metadata
database = database
tablename = "item_config"
id: Optional[int] = ormar.Integer(primary_key=True)
item_id: str = ormar.String(max_length=32, index=True)
pairs: pydantic.Json = ormar.JSON(default=["2", "3"])
@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
@ -315,3 +328,23 @@ async def test_bulk_update_not_saved_objts():
with pytest.raises(ModelListEmptyError):
await Note.objects.bulk_update([])
@pytest.mark.asyncio
async def test_bulk_operations_with_json():
async with database:
items = [
ItemConfig(item_id="test1"),
ItemConfig(item_id="test2"),
ItemConfig(item_id="test3"),
]
await ItemConfig.objects.bulk_create(items)
items = await ItemConfig.objects.all()
assert all(x.pairs == ["2", "3"] for x in items)
for item in items:
item.pairs = ["1"]
await ItemConfig.objects.bulk_update(items)
items = await ItemConfig.objects.all()
assert all(x.pairs == ["1"] for x in items)

View File

@ -7,8 +7,13 @@ import sqlalchemy
import ormar
from ormar import (
post_bulk_update, post_delete, post_save, post_update,
pre_delete, pre_save, pre_update
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
@ -202,7 +207,9 @@ async def test_signal_functions(cleanup):
await Album.objects.bulk_update(albums)
cnt = await AuditLog.objects.filter(event_type__contains="BULK_POST").count()
cnt = await AuditLog.objects.filter(
event_type__contains="BULK_POST"
).count()
assert cnt == len(albums)
album.signals.bulk_post_update.disconnect(after_bulk_update)