def test_contribute_to_event__reverse_propagates(self): reverse = Mock(name='reverse') reverse2 = Mock(name='reverse2') on_create = ModelEvent('x.create', reverse=reverse) on_change = ModelEvent('x.change') self.Model = webhook_model( on_create=on_create, on_change=on_change, reverse=reverse2, )(self.Model) self.assertIs(on_create.reverse, reverse) self.assertIs(on_change.reverse, reverse2)
def test_on_delete(self): on_delete = ModelEvent('x.delete') Model = webhook_model(on_delete=on_delete)(self.Model) self.assertIs(Model.webhook_events.events['on_delete'], on_delete) on_delete.send = Mock(name='event.send') self.obj.delete() on_delete.send.assert_called_with( instance=self.obj, data=self.obj.webhook_payload(), sender=None, )
def test_on_create(self): on_create = ModelEvent('x.create') Model = webhook_model(on_create=on_create)(self.Model) self.assertIs(Model.webhook_events.events['on_create'], on_create) on_create.send = Mock(name='event.send') self.Model.objects.filter(username='******').delete() obj, _ = self.Model.objects.get_or_create(username='******') on_create.send.assert_called_with( instance=obj, data=obj.webhook_payload(), sender=None, )
def test_on_create(self): on_create = ModelEvent('x.create') Model = webhook_model(on_create=on_create)(self.Model) assert Model.webhooks.events['on_create'] is on_create on_create.send = Mock(name='event.send') self.Model.objects.filter(username='******').delete() obj, _ = self.Model.objects.get_or_create(username='******') on_create.send.assert_called_with( instance=obj, data=obj.webhooks.payload(obj), headers=None, sender=None, context={'instance': obj.pk}, )
def test_on_delete(self): on_delete = ModelEvent('x.delete') Model = webhook_model(on_delete=on_delete)(self.Model) assert Model.webhooks.events['on_delete'] is on_delete on_delete.send = Mock(name='event.send') obj_pk = self.obj.pk self.obj.delete() on_delete.send.assert_called_with( instance=self.obj, data=self.obj.webhooks.payload(self.obj), headers=None, sender=None, context={'instance': obj_pk}, )
def test_on_change(self): on_change = ModelEvent('x.change') Model = webhook_model( on_change=on_change, sender_field='username', )(self.Model) self.assertIs(Model.webhook_events.events['on_change'], on_change) on_change.send = Mock(name='event.send') self.obj.username = '******' self.obj.save() on_change.send.assert_called_with( instance=self.obj, data=self.obj.webhook_payload(), sender=self.obj.username, )
def mock_modelevent(self, name, *args, **kwargs): return ModelEvent(name, *args, dispatcher=kwargs.pop('dispatcher', None) or self.dispatcher, app=kwargs.pop('app', None) or self.app, **kwargs)
def test_with_on_custom_create__connects(self): Model = webhook_model( on_custom=ModelEvent('x.create').dispatches_on_create(), )(self.Model) self.post_save.connect.assert_called_with( ANY, sender=Model, weak=False, )
def test_on_change(self): on_change = ModelEvent('x.change') Model = webhook_model( on_change=on_change, sender_field='username', )(self.Model) assert Model.webhooks.events['on_change'] is on_change on_change.send = Mock(name='event.send') self.obj.username = '******' self.obj.save() on_change.send.assert_called_with( instance=self.obj, data=self.obj.webhooks.payload(self.obj), headers=None, sender=self.obj.username, context={'instance': self.obj.pk}, )
def test_dict_compat(self): on_create = ModelEvent('x.create') self.Model = webhook_model(on_create=on_create)(self.Model) assert self.Model.webhooks['on_create'] is on_create self.Model.webhooks['on_create'] = 42 assert self.Model.webhooks['on_create'] == 42 del (self.Model.webhooks['on_create']) with pytest.raises(KeyError): self.Model.webhooks['on_create']
def mock_event(self, name, *args, **kwargs): event = ModelEvent( name, dispatcher=kwargs.get('dispatcher') or self.dispatcher, reverse=model_reverser('something-view', uuid='uuid'), *args, **kwargs ) event.reverse._reverse = Mock(name='event.reverse._reverse') return event
def test_on_custom_with_filter_dispatching_on_delete(self): jerry, _ = self.Model.objects.get_or_create(username='******') on_jerry_delete = ModelEvent( 'x.delete', username__eq='jerry').dispatches_on_delete() Model = webhook_model(on_jerry_delete=on_jerry_delete)(self.Model) self.assertIs( Model.webhook_events.events['on_jerry_delete'], on_jerry_delete, ) on_jerry_delete.send = Mock(name='event.send') self.obj.delete() on_jerry_delete.send.assert_not_called() jerry.delete() on_jerry_delete.send.assert_called_with( instance=jerry, data=jerry.webhook_payload(), sender=None, )
def test_on_custom_with_filter_dispatching_on_delete(self): jerry, _ = self.Model.objects.get_or_create(username='******') on_jerry_delete = ModelEvent( 'x.delete', username__eq='jerry').dispatches_on_delete() Model = webhook_model(on_jerry_delete=on_jerry_delete)(self.Model) assert (Model.webhooks.events['on_jerry_delete'] is on_jerry_delete) on_jerry_delete.send = Mock(name='event.send') self.obj.delete() on_jerry_delete.send.assert_not_called() jerry_pk = jerry.pk jerry.delete() on_jerry_delete.send.assert_called_with( instance=jerry, data=jerry.webhooks.payload(jerry), headers=None, sender=None, context={'instance': jerry_pk}, )
def test_on_change__does_not_dispatch_on_create(self): on_create = ModelEvent('x.create') on_change = ModelEvent('x.change') on_create.send = Mock(name='on_create.send') on_change.send = Mock(name='on_change.send') self.Model = webhook_model( on_create=on_create, on_change=on_change)(self.Model) obj2, _ = self.Model.objects.get_or_create(username='******') on_create.send.assert_called_with( instance=obj2, data=obj2.webhook_payload(), sender=None, ) on_change.send.assert_not_called()
class webhooks: on_create = ModelEvent('x.create')
def test_is_dict(self): Model = webhook_model(on_change=ModelEvent('x.y'))(self.Model) assert (Model.webhooks['on_change'] is Model.webhooks.events['on_change'])
class webhooks: on_custom = ModelEvent('x.create').dispatches_on_create()
class webhooks: on_custom = ModelEvent('x.change').dispatches_on_change()
class webhooks: on_custom = ModelEvent('x.delete').dispatches_on_delete()
class webhooks: on_delete = ModelEvent('x.delete')
class webhooks: on_change = ModelEvent('x.change')
def test_with_on_delete__connects(self): Model = webhook_model(on_delete=ModelEvent('x.delete'))(self.Model) self.post_delete.connect.assert_called_with( ANY, sender=Model, weak=False, )