Skip to content

Commit 6d37dcf

Browse files
committed
tests: Add tests for 'patch-relation-changed' events
This event is rather odd. If you have two patches then the way a relation is created is by creating a 'PatchRelation' instance and then setting the 'related' attribute on the first patch followed by the second patch. Because the event uses the 'Patch' model's 'pre_save' signal, we'll only see events for the patch being currently saved. This means no event will be raised for the first patch and only one event, the one for the second patch, will be raised when the second patch is being added to the relationship. In hindsight, the structure of the event is off. We should have had something like a 'patch-added-to-relationship' and a 'patch-removed-from-relationship' event, both with the same fields: 'project', 'actor', 'patch' and 'related', the latter of which would have listed all of the _other_ patches in the relationship. Sadly, this is an API change which means we can't do it now. We may well wish to do so in the future though. Signed-off-by: Stephen Finucane <[email protected]> (cherry picked from commit 8092f8f)
1 parent dd19914 commit 6d37dcf

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

patchwork/tests/test_events.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,53 @@ def test_patch_delegated(self):
172172
Event.CATEGORY_PATCH_DELEGATED)
173173
self.assertEventFields(events[3], previous_delegate=delegate_b)
174174

175+
def test_patch_relations_changed(self):
176+
# purposefully setting series to None to minimize additional events
177+
relation = utils.create_relation()
178+
patches = utils.create_patches(3, series=None)
179+
180+
# mark the first two patches as related; the second patch should be the
181+
# one that the event is raised for
182+
183+
patches[0].related = relation
184+
patches[0].save()
185+
patches[1].related = relation
186+
patches[1].save()
187+
188+
events = _get_events(patch=patches[1])
189+
self.assertEqual(events.count(), 2)
190+
self.assertEqual(
191+
events[1].category, Event.CATEGORY_PATCH_RELATION_CHANGED)
192+
self.assertEqual(events[1].project, patches[1].project)
193+
self.assertEqual(events[1].previous_relation, None)
194+
self.assertEqual(events[1].current_relation, relation)
195+
196+
# add the third patch
197+
198+
patches[2].related = relation
199+
patches[2].save()
200+
201+
events = _get_events(patch=patches[2])
202+
self.assertEqual(events.count(), 2)
203+
self.assertEqual(
204+
events[1].category, Event.CATEGORY_PATCH_RELATION_CHANGED)
205+
self.assertEqual(events[1].project, patches[1].project)
206+
self.assertEqual(events[1].previous_relation, None)
207+
self.assertEqual(events[1].current_relation, relation)
208+
209+
# drop the third patch
210+
211+
patches[2].related = None
212+
patches[2].save()
213+
214+
events = _get_events(patch=patches[2])
215+
self.assertEqual(events.count(), 3)
216+
self.assertEqual(
217+
events[2].category, Event.CATEGORY_PATCH_RELATION_CHANGED)
218+
self.assertEqual(events[2].project, patches[1].project)
219+
self.assertEqual(events[2].previous_relation, relation)
220+
self.assertEqual(events[2].current_relation, None)
221+
175222

176223
class CheckCreatedTest(_BaseTestCase):
177224

0 commit comments

Comments
 (0)