def test_calls_survey(self, mock_get): """If source_type is supported calls the appropriate survey method.""" mock_get.side_effect = mocked_requests_get # Prevent a call being made to NCBI's API to determine # organism name/id. organism = Organism(name="H**O SAPIENS", taxonomy_id=9606, is_scientific_name=True) organism.save() survey_job = SurveyJob(source_type="ARRAY_EXPRESS") survey_job.save() key_value_pair = SurveyJobKeyValue(survey_job=survey_job, key="experiment_accession_code", value="E-GEOD-22166") key_value_pair.save() surveyor.run_job(survey_job) logger.info("Started Survey Job %d, waiting for it to complete.", survey_job.id) survey_job = wait_for_job(survey_job, SurveyJob) self.assertTrue(survey_job.success) batch = Batch.objects.all()[0] batch = Batch.objects.filter(survey_job=survey_job).get() downloader_job = batch.downloaderjob_set.get() logger.info("Survey Job finished, waiting for Downloader Job %d to complete.", downloader_job.id) downloader_job = wait_for_job(downloader_job, DownloaderJob) self.assertTrue(downloader_job.success) processor_job = batch.processorjob_set.get() logger.info("Downloader Job finished, waiting for processor Job %d to complete.", processor_job.id) processor_job = wait_for_job(processor_job, ProcessorJob) self.assertTrue(processor_job.success)
def test_run_unsupported_source(self): """If source_type is unsupported the job still is started and ended.""" job = SurveyJob(source_type="UNSUPPORTED") job.save() surveyor.run_job(job) self.assertIsInstance(job.start_time, datetime.datetime) self.assertIsInstance(job.end_time, datetime.datetime) self.assertFalse(job.success)
def test_calls_survey(self, survey_method): """If source_type is supported calls the appropriate survey method.""" survey_method.return_value = True job = SurveyJob(source_type="ARRAY_EXPRESS") job.save() surveyor.run_job(job) self.assertEqual(len(survey_method.mock_calls), 1) self.assertIsInstance(job.start_time, datetime.datetime) self.assertIsInstance(job.end_time, datetime.datetime) self.assertTrue(job.success)
def test_nonexistant_srp_survey(self): """Try surveying an accession that does not exist """ survey_job = SurveyJob(source_type="SRA") survey_job.save() key_value_pair = SurveyJobKeyValue( survey_job=survey_job, key="experiment_accession_code", value="ERP006216" ) key_value_pair.save() run_job(survey_job) survey_job.refresh_from_db() self.assertFalse(survey_job.success) self.assertEqual(survey_job.failure_reason, "No experiment found.")
def handle(self, *args, **options): if options["file"] is None and options[ "accession"] is None and options["job_id"] is None: logger.error("You must specify an accession or file or job ID.") return "1" if options["file"]: if "s3://" in options["file"]: bucket, key = parse_s3_url(options["file"]) s3 = boto3.resource("s3") try: filepath = "/tmp/input_" + str(uuid.uuid4()) + ".txt" s3.Bucket(bucket).download_file(key, filepath) except botocore.exceptions.ClientError as e: if e.response["Error"]["Code"] == "404": logger.error("The remote file does not exist.") raise else: raise else: filepath = options["file"] with open(filepath) as accession_file: for i, accession in enumerate(accession_file): if i < options["offset"]: continue accession = accession.strip() try: run_surveyor_for_accession(accession) except Exception as e: logger.exception(e) if options["accession"]: accession = options["accession"] try: run_surveyor_for_accession(accession) except Exception as e: logger.exception(e) if options["job_id"]: job_id = options["job_id"] try: survey_job = SurveyJob.objects.get(id=job_id) surveyor.run_job(survey_job) except Exception as e: logger.exception(e)