Skip to content

Commit 46a7ccd

Browse files
committed
LITE-23027 unit tests for cqrs.bulk_update with support of previous_data
1 parent 62c480b commit 46a7ccd

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

dj_cqrs/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
1+
# Copyright © 2022 Ingram Micro Inc. All rights reserved.
22

33
import logging
44

tests/test_master/test_signals.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
1+
# Copyright © 2022 Ingram Micro Inc. All rights reserved.
22

33
from datetime import datetime, timezone
44

@@ -10,7 +10,7 @@
1010
import pytest
1111

1212
from tests.dj_master import models
13-
from tests.utils import assert_publisher_once_called_with_args
13+
from tests.utils import assert_is_sub_dict, assert_publisher_once_called_with_args
1414

1515

1616
@pytest.mark.parametrize('model', (models.AllFieldsModel, models.BasicFieldsModel))
@@ -136,7 +136,7 @@ def test_automatic_post_bulk_create(mocker):
136136

137137

138138
@pytest.mark.django_db(transaction=True)
139-
def test_post_bulk_update(mocker):
139+
def test_post_bulk_update_wout_prev_data(mocker):
140140
for i in range(3):
141141
models.SimplestModel.objects.create(id=i)
142142
cqrs_updated = models.SimplestModel.objects.get(id=1).cqrs_updated
@@ -155,3 +155,55 @@ def test_post_bulk_update(mocker):
155155
m = models.SimplestModel.objects.get(id=1)
156156
assert m.cqrs_updated > cqrs_updated
157157
assert m.cqrs_revision == 1
158+
159+
160+
@pytest.mark.django_db(transaction=True)
161+
def test_post_bulk_update_with_prev_data(mocker):
162+
for i in range(3):
163+
models.SimplestTrackedModel.objects.create(id=i, description='old')
164+
165+
m = models.SimplestTrackedModel.objects.get(id=1)
166+
m.status = 'x'
167+
m.save()
168+
169+
publisher_mock = mocker.patch('dj_cqrs.controller.producer.produce')
170+
models.SimplestTrackedModel.cqrs.bulk_update(
171+
queryset=models.SimplestTrackedModel.objects.filter(id__in={0, 1}).order_by('id'),
172+
description='new',
173+
status=None,
174+
)
175+
176+
m = models.SimplestTrackedModel.objects.get(id=2)
177+
assert m.cqrs_revision == 0
178+
179+
assert publisher_mock.call_count == 2
180+
for pk, prev_data in (
181+
(0, {'description': 'old', 'status': None}),
182+
(1, {'description': 'old', 'status': 'x'}),
183+
):
184+
t0_payload = publisher_mock.call_args_list[pk][0][0]
185+
assert t0_payload.signal_type == SignalType.SAVE
186+
assert t0_payload.cqrs_id == models.SimplestTrackedModel.CQRS_ID
187+
assert t0_payload.pk == pk
188+
189+
assert_is_sub_dict(
190+
{'id': pk, 'description': 'new', 'status': None},
191+
t0_payload.instance_data,
192+
)
193+
assert t0_payload.previous_data == prev_data
194+
195+
m = models.SimplestTrackedModel.objects.get(id=pk)
196+
assert m.cqrs_revision == pk + 1
197+
assert m.description == 'new'
198+
assert m.status is None
199+
200+
201+
@pytest.mark.django_db
202+
def test_post_bulk_update_nothing_to_update(mocker):
203+
publisher_mock = mocker.patch('dj_cqrs.controller.producer.produce')
204+
models.SimplestTrackedModel.cqrs.bulk_update(
205+
queryset=models.SimplestTrackedModel.objects.all(),
206+
description='something',
207+
)
208+
209+
publisher_mock.assert_not_called()

0 commit comments

Comments
 (0)