def test_audit_run_was_called_if_documents_are_okay(self): # Mock an audit mock_job = self.mock_job_cls.objects.get.return_value mock_job.pk = 1 # Call the task process_job(mock_job.pk) # Check if run_audit was called with job.audit.pk self.mock_run_audit.assert_called_once_with(job_pk=mock_job.pk)
def test_document_validation_is_called(self, mock_validate): mock_job = self.mock_job_cls.objects.get.return_value mock_job.pk = 1 fake_documents_pks = list(range(3)) mock_job.documents.all.return_value = [ Mock(pk=i) for i in fake_documents_pks ] process_job(mock_job.pk) self.assertEqual(len(fake_documents_pks), mock_validate.call_count) for i in fake_documents_pks: mock_validate.assert_any_calls(i)
def test_run_audit_isnt_called_if_invalid_documents(self, mock_validate): mock_job = Mock(pk=1) mock_validate.return_value = { 'error': 'ValidationError', 'pk': mock_job.pk } # Call the task process_job(mock_job.pk) # Check if run_audit was not called assert not self.mock_run_audit.delay.called, ( "run_audit should not have been called" )
def test_jobs_are_updated_on_audit_success(self, mock_run_audit): """ Jobs must have their STATE and REPORT_FILE fields updated on audit success """ fake_job_pk = 1 # Call the task process_job(fake_job_pk) # magic number # Check if update_job was called with a success indicator self.mock_update_job.assert_called_once_with( fake_job_pk, success=True, report_path=mock_run_audit.return_value )
def test_updates_status_of_invalid_documents_and_job(self, mock_validate): """ On the face of invalid documents, each invalid document must have their status updated, and so must be the job instance's state. """ mock_job = Mock(pk=1) mock_job.documents.all.return_value = [Mock()] self.mock_job_cls.objects.get.return_value = mock_job error = {'error': 'ValidationError', 'pk': mock_job.pk} mock_validate.return_value = error # Call the task process_job(mock_job.pk) # Check if update_documents was called with the right collection # of errors self.mock_update_documents.assert_called_once_with(errors=[error]) # Check if update_job was called also. self.mock_update_job.assert_called_once_with( mock_job.pk, invalid_documents=True )