def test_remove(self): """remove method adds function to patch object""" patch = ApiPatch() def mock_function(): pass patch.remove('test-remove', mock_function) self.assertEqual(len(patch._actions), 1) self.assertEqual(patch._actions[0]['op'], 'remove') self.assertEqual(patch._actions[0]['path'], 'test-remove') self.assertEqual(patch._actions[0]['handler'], mock_function)
def test_dispatch_action(self): """dispatch_action calls specified actions""" patch = ApiPatch() mock_target = MockObject(13) def action_a(request, target, value): self.assertEqual(request, 'request') self.assertEqual(target, mock_target) return {'a': value * 2, 'b': 111} patch.replace('abc', action_a) def action_b(request, target, value): self.assertEqual(request, 'request') self.assertEqual(target, mock_target) return {'b': value * 10} patch.replace('abc', action_b) def action_fail(request, target, value): self.fail("unrequired action was called") patch.add('c', action_fail) patch.remove('c', action_fail) patch.replace('c', action_fail) patch_dict = {'id': 123} patch.dispatch_action( patch_dict, 'request', mock_target, { 'op': 'replace', 'path': 'abc', 'value': 5, } ) self.assertEqual(len(patch_dict), 3) self.assertEqual(patch_dict['id'], 123) self.assertEqual(patch_dict['a'], 10) self.assertEqual(patch_dict['b'], 50)
remove_participant(request, thread, participant.user) if len(thread.participants_list) == 1: return {'deleted': True} else: make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer(thread.participants_list, many=True) return { 'deleted': False, 'participants': participants.data, } thread_patch_dispatcher.remove('participants', patch_remove_participant) def patch_replace_owner(request, thread, value): try: user_id = int(value) except (ValueError, TypeError): user_id = 0 for participant in thread.participants_list: if participant.user_id == user_id: if participant.is_owner: raise ValidationError(_("This user already is thread owner.")) else: break else:
allow_unmark_best_answer(request.user, thread) thread.clear_best_answer() thread.save() return { 'best_answer': None, 'best_answer_is_protected': False, 'best_answer_marked_on': None, 'best_answer_marked_by': None, 'best_answer_marked_by_name': None, 'best_answer_marked_by_slug': None, } thread_patch_dispatcher.remove('best-answer', patch_unmark_best_answer) def patch_add_participant(request, thread, value): allow_add_participants(request.user, thread) try: username = six.text_type(value).strip().lower() if not username: raise ValidationError( _("You have to enter new participant's username.")) participant = UserModel.objects.get(slug=username) except UserModel.DoesNotExist: raise ValidationError(_("No user with such name exists.")) if participant in [p.user for p in thread.participants_list]: