def test_delete_then_flush(self): pk = 1 model = MagicMock() # The utility ought to call model.objects.filter(pk__in=[pk]).delete(). # Use side_effect to get mock to call our code when it # tries to call objects.filter, so that we can check that # it passed the expected args, pk__in=[pk]. # Then return another mock, which the caller should try to # call delete() on, so we can check that too. filter_return = MagicMock() def our_callback(*args, **kwargs): assert not args assert kwargs == {'pk__in': [pk]} return filter_return model = MagicMock(side_effect=our_callback) batch = BatchOperations(model) batch.delete(pk) batch.flush() filter_return.delete.assert_called()
def test_delete_one_record(self): batch = BatchOperations(None) pk = 1 batch.delete(pk) self.assertEqual(1, batch.num_pending_deletes)