def test_custom_celery_task(settings): settings.CELERY_TASK_ALWAYS_EAGER = True instance = UserFactory.create() successful_task.delay(instance.pk) assert User.objects.get(pk=instance.pk).username == 'test'
def test_exception(self, mock_requests_post, mock_task_add_subscribe_retry): mock_task_add_subscribe_retry.side_effect = Retry() mock_requests_post.side_effect = Exception() instance = UserFactory.create() with pytest.raises(Retry): task_add_subscribe(instance.pk)
def test_post_succeed(self, mock_requests_post): instance = UserFactory.create() task_add_subscribe(instance.pk) mock_requests_post.assert_called_with( 'https://httpbin.org/delay/5', data={'email': instance.email} )
def test_db_transaction(settings): settings.CELERY_TASK_ALWAYS_EAGER = True settings.CELERY_TASK_EAGER_PROPAGATES = True instance = UserFactory.create() with pytest.raises(TypeError): throwing_task.delay(instance.pk) assert User.objects.get(pk=instance.pk).username != 'test'
def test_subscribe3_post_succeed(db, monkeypatch, client): user = UserFactory.create() mock_task_add_subscribe_delay = mock.MagicMock( name="task_add_subscribe_delay") monkeypatch.setattr(polls.tasks.task_add_subscribe, 'delay', mock_task_add_subscribe_delay) from django_capture_on_commit_callbacks import capture_on_commit_callbacks with capture_on_commit_callbacks(execute=True) as callbacks: response = client.post(reverse('subscribe3'), { 'name': user.username, 'email': user.email, }) assert response.status_code == 302 assert User.objects.filter(username=user.username).exists() is True user = User.objects.filter(username=user.username).first() mock_task_add_subscribe_delay.assert_called_with(user.pk)
def test_model_creation(self): user = UserFactory() question = QuestionFactory(created_by=user) choice = ChoiceFactory(question=question) self.assertEqual(question.created_by, user) self.assertEqual(choice.question, question)