def test_downloadable_files_additional_metadata_default(clean_db): TrialMetadata.create(TRIAL_ID, METADATA) df = DownloadableFiles( trial_id=TRIAL_ID, upload_type="wes_bam", object_url="10021/Patient 1/sample 1/aliquot 1/wes_forward.fastq", file_size_bytes=1, md5_hash="hash1234", uploaded_timestamp=datetime.now(), ) # Check no value passed df.insert() assert df.additional_metadata == {} for nullish_value in [None, "null", {}]: df.additional_metadata = nullish_value df.update() assert df.additional_metadata == {} # Non-nullish value doesn't get overridden non_nullish_value = {"foo": "bar"} df.additional_metadata = non_nullish_value df.update() assert df.additional_metadata == non_nullish_value
def create_df(facet_group, additional_metadata={}) -> DownloadableFiles: df = DownloadableFiles( facet_group=facet_group, additional_metadata=additional_metadata, trial_id=TRIAL_ID, uploaded_timestamp=datetime.now(), file_size_bytes=0, object_url=facet_group, # just filler, not relevant to the test upload_type="", ) df.insert() clean_db.refresh(df) return df
def test_get_filelist(cidc_api, clean_db, monkeypatch): """Check that getting a filelist.tsv works as expected""" user_id = setup_user(cidc_api, monkeypatch) file_id_1, file_id_2 = setup_downloadable_files(cidc_api) client = cidc_api.test_client() url = "/downloadable_files/filelist" # A JSON body containing a file ID list must be provided res = client.post(url) assert res.status_code == 422 # User has no permissions, so no files should be found short_file_list = {"file_ids": [file_id_1, file_id_2]} res = client.post(url, json=short_file_list) assert res.status_code == 404 # Give the user one permission with cidc_api.app_context(): perm = Permissions( granted_to_user=user_id, trial_id=trial_id_1, upload_type=upload_types[0], granted_by_user=user_id, ) perm.insert() # User has one permission, so the filelist should contain a single file res = client.post(url, json=short_file_list) assert res.status_code == 200 assert "text/tsv" in res.headers["Content-Type"] assert "filename=filelist.tsv" in res.headers["Content-Disposition"] assert res.data.decode("utf-8") == ( f"gs://{GOOGLE_DATA_BUCKET}/{trial_id_1}/wes/.../reads_123.bam\t{trial_id_1}_wes_..._reads_123.bam\n" ) # Admins don't need permissions to get files make_admin(user_id, cidc_api) res = client.post(url, json=short_file_list) assert res.status_code == 200 assert res.data.decode("utf-8") == ( f"gs://{GOOGLE_DATA_BUCKET}/{trial_id_1}/wes/.../reads_123.bam\t{trial_id_1}_wes_..._reads_123.bam\n" f"gs://{GOOGLE_DATA_BUCKET}/{trial_id_2}/cytof/.../analysis.zip\t{trial_id_2}_cytof_..._analysis.zip\n" ) # Clear inserted file records with cidc_api.app_context(): clean_db.query(DownloadableFiles).delete() # Filelists don't get paginated ids = [] with cidc_api.app_context(): for id in range(1000): df = DownloadableFiles( trial_id=trial_id_1, object_url=str(id), upload_type="", file_size_bytes=0, uploaded_timestamp=datetime.now(), ) df.insert() ids.append(df.id) res = client.post(url, json={"file_ids": ids}) assert res.status_code == 200 # newly inserted files + EOF newline assert len(res.data.decode("utf-8").split("\n")) == len(ids) + 1