diff --git a/src/mds/agg_mds/adapters.py b/src/mds/agg_mds/adapters.py index 069221d7..d36f5502 100644 --- a/src/mds/agg_mds/adapters.py +++ b/src/mds/agg_mds/adapters.py @@ -574,7 +574,7 @@ def getRemoteDataAsJson(self, **kwargs) -> Dict: raise except httpx.HTTPError as exc: logger.error( - f"An HTTP error {exc.response.status_code if exc.response is not None else ''} occurred while requesting {exc.request.url}. Returning {len(results['results'])} results" + f"An HTTP error occurred while requesting {mds_url} {exc}. Returning {len(results['results'])} results." ) break # need to break here as cannot be assured of leaving while loop except ValueError as exc: @@ -1581,6 +1581,696 @@ def normalizeToGen3MDSFields(self, data, **kwargs) -> Dict[str, Any]: return results +class PDCSubjectAdapter(RemoteMetadataAdapter): + """ + Simple adapter for Proteomic Data Commons + Expected Parameters: + size: Number of studies to pull in a single call, default=5 and therefore optional + #Note - The API doesn't seem to do very well with large requests, + hence confining it to a smaller number + """ + + @retry( + stop=stop_after_attempt(5), + retry=retry_if_exception_type(httpx.TimeoutException), + wait=wait_random_exponential(multiplier=1, max=10), + ) + def getRemoteDataAsJson(self, **kwargs) -> Dict: + results = {"results": []} + + mds_url = kwargs.get("mds_url", None) + if mds_url is None: + return results + batchSize = kwargs["filters"].get("size", 5) + + query = """ +query FilteredClinicalDataPaginated($offset_value: Int, $limit_value: Int, $sort_value: String, $program_name_filter: String!, $project_name_filter: String!, $study_name_filter: String!, $disease_filter: String!, $filterValue: String!, $analytical_frac_filter: String!, $exp_type_filter: String!, $ethnicity_filter: String!, $race_filter: String!, $gender_filter: String!, $tumor_grade_filter: String!, $sample_type_filter: String!, $acquisition_type_filter: String!, $data_category_filter: String!, $file_type_filter: String!, $access_filter: String!, $downloadable_filter: String!, $case_status_filter: String!, $biospecimen_status_filter: String!, $getAll: Boolean!) { + getPaginatedUIClinical(offset: $offset_value, limit: $limit_value, sort: $sort_value, program_name: $program_name_filter, project_name: $project_name_filter, study_name: $study_name_filter, disease_type: $disease_filter, primary_site: $filterValue, analytical_fraction: $analytical_frac_filter, experiment_type: $exp_type_filter, ethnicity: $ethnicity_filter, race: $race_filter, gender: $gender_filter, tumor_grade: $tumor_grade_filter, sample_type: $sample_type_filter, acquisition_type: $acquisition_type_filter, data_category: $data_category_filter, file_type: $file_type_filter, access: $access_filter, downloadable: $downloadable_filter, case_status: $case_status_filter, biospecimen_status: $biospecimen_status_filter, getAll: $getAll) { + total + uiClinical { + case_submitter_id + external_case_id + ethnicity + gender + race + morphology + primary_diagnosis + site_of_resection_or_biopsy + tissue_or_organ_of_origin + tumor_grade + tumor_stage + age_at_diagnosis + classification_of_tumor + days_to_recurrence + case_id + disease_type + primary_site + program_name + project_name + status + cause_of_death + days_to_birth + days_to_death + vital_status + year_of_birth + year_of_death + age_at_index + premature_at_birth + weeks_gestation_at_birth + age_is_obfuscated + cause_of_death_source + occupation_duration_years + country_of_residence_at_enrollment + days_to_last_follow_up + days_to_last_known_disease_status + last_known_disease_status + progression_or_recurrence + prior_malignancy + ajcc_clinical_m + ajcc_clinical_n + ajcc_clinical_stage + ajcc_clinical_t + ajcc_pathologic_m + ajcc_pathologic_n + ajcc_pathologic_stage + ajcc_pathologic_t + ajcc_staging_system_edition + ann_arbor_b_symptoms + ann_arbor_clinical_stage + ann_arbor_extranodal_involvement + ann_arbor_pathologic_stage + best_overall_response + burkitt_lymphoma_clinical_variant + circumferential_resection_margin + colon_polyps_history + days_to_best_overall_response + days_to_diagnosis + days_to_hiv_diagnosis + days_to_new_event + figo_stage + hiv_positive + hpv_positive_type + hpv_status + iss_stage + laterality + ldh_level_at_diagnosis + ldh_normal_range_upper + lymph_nodes_positive + lymphatic_invasion_present + method_of_diagnosis + peripancreatic_lymph_nodes_positive + peripancreatic_lymph_nodes_tested + supratentorial_localization + tumor_confined_to_organ_of_origin + tumor_focality + tumor_regression_grade + vascular_invasion_type + wilms_tumor_histologic_subtype + breslow_thickness + gleason_grade_group + igcccg_stage + international_prognostic_index + largest_extrapelvic_peritoneal_focus + masaoka_stage + new_event_anatomic_site + new_event_type + overall_survival + perineural_invasion_present + prior_treatment + progression_free_survival + progression_free_survival_event + residual_disease + vascular_invasion_present + year_of_diagnosis + icd_10_code + synchronous_malignancy + metastasis_at_diagnosis + metastasis_at_diagnosis_site + mitosis_karyorrhexis_index + non_nodal_regional_disease + non_nodal_tumor_deposits + ovarian_specimen_status + ovarian_surface_involvement + percent_tumor_invasion + peritoneal_fluid_cytological_status + primary_gleason_grade + secondary_gleason_grade + weiss_assessment_score + adrenal_hormone + ann_arbor_b_symptoms_described + diagnosis_is_primary_disease + eln_risk_classification + figo_staging_edition_year + gleason_grade_tertiary + gleason_patterns_percent + margin_distance + margins_involved_site + pregnant_at_diagnosis + satellite_nodule_present + sites_of_involvement + tumor_depth + who_cns_grade + who_nte_grade + diagnosis_uuid + anaplasia_present + anaplasia_present_type + child_pugh_classification + cog_liver_stage + cog_neuroblastoma_risk_group + cog_renal_stage + cog_rhabdomyosarcoma_risk_group + enneking_msts_grade + enneking_msts_metastasis + enneking_msts_stage + enneking_msts_tumor_site + esophageal_columnar_dysplasia_degree + esophageal_columnar_metaplasia_present + first_symptom_prior_to_diagnosis + gastric_esophageal_junction_involvement + goblet_cells_columnar_mucosa_present + gross_tumor_weight + inpc_grade + inpc_histologic_group + inrg_stage + inss_stage + irs_group + irs_stage + ishak_fibrosis_score + lymph_nodes_tested + medulloblastoma_molecular_classification + externalReferences { + reference_resource_shortname + reference_entity_location + __typename + } + exposures { + exposure_id + exposure_submitter_id + alcohol_days_per_week + alcohol_drinks_per_day + alcohol_history + alcohol_intensity + asbestos_exposure + cigarettes_per_day + coal_dust_exposure + environmental_tobacco_smoke_exposure + pack_years_smoked + radon_exposure + respirable_crystalline_silica_exposure + smoking_frequency + time_between_waking_and_first_smoke + tobacco_smoking_onset_year + tobacco_smoking_quit_year + tobacco_smoking_status + type_of_smoke_exposure + type_of_tobacco_used + years_smoked + age_at_onset + alcohol_type + exposure_duration + exposure_duration_years + exposure_type + marijuana_use_per_week + parent_with_radiation_exposure + secondhand_smoke_as_child + smokeless_tobacco_quit_age + tobacco_use_per_day + __typename + } + follow_ups { + follow_up_id + follow_up_submitter_id + adverse_event + adverse_event_grade + aids_risk_factors + barretts_esophagus_goblet_cells_present + bmi + body_surface_area + cause_of_response + cd4_count + cdc_hiv_risk_factors + comorbidity + comorbidity_method_of_diagnosis + days_to_adverse_event + days_to_comorbidity + days_to_follow_up + days_to_imaging + days_to_progression + days_to_progression_free + days_to_recurrence + diabetes_treatment_type + disease_response + dlco_ref_predictive_percent + ecog_performance_status + evidence_of_recurrence_type + eye_color + fev1_ref_post_bronch_percent + fev1_ref_pre_bronch_percent + fev1_fvc_pre_bronch_percent + fev1_fvc_post_bronch_percent + haart_treatment_indicator + height + hepatitis_sustained_virological_response + history_of_tumor + history_of_tumor_type + hiv_viral_load + hormonal_contraceptive_type + hormonal_contraceptive_use + hormone_replacement_therapy_type + hpv_positive_type + hysterectomy_margins_involved + hysterectomy_type + imaging_result + imaging_type + immunosuppressive_treatment_type + karnofsky_performance_status + menopause_status + nadir_cd4_count + pancreatitis_onset_year + pregnancy_outcome + procedures_performed + progression_or_recurrence + progression_or_recurrence_anatomic_site + progression_or_recurrence_type + recist_targeted_regions_number + recist_targeted_regions_sum + reflux_treatment_type + risk_factor + risk_factor_treatment + scan_tracer_used + undescended_testis_corrected + undescended_testis_corrected_age + undescended_testis_corrected_laterality + undescended_testis_corrected_method + undescended_testis_history + undescended_testis_history_laterality + viral_hepatitis_serologies + weight + __typename + } + treatments { + treatment_id + treatment_submitter_id + days_to_treatment_end + days_to_treatment_start + initial_disease_status + regimen_or_line_of_therapy + therapeutic_agents + treatment_anatomic_site + treatment_effect + treatment_intent_type + treatment_or_therapy + treatment_outcome + treatment_type + chemo_concurrent_to_radiation + number_of_cycles + reason_treatment_ended + route_of_administration + treatment_arm + treatment_dose + treatment_dose_units + treatment_effect_indicator + treatment_frequency + __typename + } + samples { + sample_id + sample_submitter_id + annotation + __typename + } + __typename + } + pagination { + count + sort + from + page + total + pages + size + __typename + } + __typename + } +} + """ + + variables = { + "offset_value": 0, + "limit_value": 250, + "sort_value": "", + "program_name_filter": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name_filter": "", + "study_name_filter": "", + "disease_filter": "", + "filterValue": "", + "analytical_frac_filter": "", + "exp_type_filter": "", + "ethnicity_filter": "", + "race_filter": "", + "gender_filter": "", + "tumor_grade_filter": "", + "sample_type_filter": "", + "acquisition_type_filter": "", + "data_category_filter": "", + "file_type_filter": "", + "access_filter": "", + "downloadable_filter": "", + "case_status_filter": "", + "biospecimen_status_filter": "", + "getAll": False, + } + + try: + response = httpx.post( + mds_url, json={"query": query, "variables": variables} + ) + response.raise_for_status() + response_data = response.json() + results["results"] = response_data["data"]["getPaginatedUIClinical"]["uiClinical"] + logger.info( + f"Fetched {response_data['data']['getPaginatedUICase']['total']} records from PDC)" + ) + + except httpx.TimeoutException as exc: + logger.error(f"An timeout error occurred while requesting {mds_url}.") + raise + except httpx.HTTPError as exc: + logger.error( + f"An HTTP error occurred while requesting {mds_url} {exc}. Returning {len(results['results'])} results." + ) + except Exception as exc: + logger.error( + f"An error occurred while requesting {mds_url} {exc}. Returning {len(results['results'])} results." + ) + return results + + @staticmethod + def addGen3ExpectedFields( + item, mappings, keepOriginalFields, globalFieldFilters, schema + ): + """ + Map item fields to gen3 normalized fields + using the mapping and adding the location + """ + results = item + if mappings is not None: + mapped_fields = RemoteMetadataAdapter.mapFields( + item, mappings, globalFieldFilters, schema + ) + if keepOriginalFields: + results.update(mapped_fields) + else: + results = mapped_fields + + return results + + def normalizeToGen3MDSFields(self, data, **kwargs) -> Dict[str, Any]: + """ + Iterates over the response. + :param data: + :return: + """ + mappings = kwargs.get("mappings", None) + keepOriginalFields = kwargs.get("keepOriginalFields", False) + globalFieldFilters = kwargs.get("globalFieldFilters", []) + schema = kwargs.get("schema", {}) + + results = {} + for item in data["results"]: + normalized_item = PDCSubjectAdapter.addGen3ExpectedFields( + item, + mappings, + keepOriginalFields, + globalFieldFilters, + schema, + ) + normalized_item["tags"] = [ + { + "name": normalized_item[tag] if normalized_item[tag] else "", + "category": tag, + } + for tag in ["disease_type", "primary_site"] + ] + results[normalized_item["_unique_id"]] = { + "_guid_type": "discovery_metadata", + "gen3_discovery": normalized_item, + } + + return results + + +class PDCStudyAdapter(RemoteMetadataAdapter): + """ + Simple adapter for Proteomic Data Commons + Expected Parameters: + size: Number of studies to pull in a single call, default=5 and therefore optional + #Note - The API doesn't seem to do very well with large requests, + hence confining it to a smaller number + """ + + @retry( + stop=stop_after_attempt(5), + retry=retry_if_exception_type(httpx.TimeoutException), + wait=wait_random_exponential(multiplier=1, max=10), + ) + def getRemoteDataAsJson(self, **kwargs) -> Dict: + results = {"results": []} + + mds_url = kwargs.get("mds_url", None) + if mds_url is None: + return results + batchSize = kwargs["filters"].get("size", 5) + + query = """ + query FilteredStudiesDataPaginated( + $offset_value: Int + $limit_value: Int + $sort_value: String + $program_name_filter: String! + $project_name_filter: String! + $study_name_filter: String! + $disease_filter: String! + $filterValue: String! + $analytical_frac_filter: String! + $exp_type_filter: String! + $ethnicity_filter: String! + $race_filter: String! + $gender_filter: String! + $tumor_grade_filter: String! + $sample_type_filter: String! + $acquisition_type_filter: String! + $data_category_filter: String! + $file_type_filter: String! + $access_filter: String! + $downloadable_filter: String! + $biospecimen_status_filter: String! + $case_status_filter: String! + $getAll: Boolean! + ) { + getPaginatedUIStudy( + offset: $offset_value + limit: $limit_value + sort: $sort_value + program_name: $program_name_filter + project_name: $project_name_filter + study_name: $study_name_filter + disease_type: $disease_filter + primary_site: $filterValue + analytical_fraction: $analytical_frac_filter + experiment_type: $exp_type_filter + ethnicity: $ethnicity_filter + race: $race_filter + gender: $gender_filter + tumor_grade: $tumor_grade_filter + sample_type: $sample_type_filter + acquisition_type: $acquisition_type_filter + data_category: $data_category_filter + file_type: $file_type_filter + access: $access_filter + downloadable: $downloadable_filter + biospecimen_status: $biospecimen_status_filter + case_status: $case_status_filter + getAll: $getAll + ) { + total + uiStudies { + study_id + pdc_study_id + submitter_id_name + study_description + study_submitter_id + program_name + project_name + disease_type + primary_site + analytical_fraction + experiment_type + embargo_date + cases_count + aliquots_count + filesCount { + file_type + data_category + files_count + __typename + } + supplementaryFilesCount { + data_category + file_type + files_count + __typename + } + nonSupplementaryFilesCount { + data_category + file_type + files_count + __typename + } + contacts { + name + institution + email + url + __typename + } + versions { + number + __typename + } + __typename + } + pagination { + count + sort + from + page + total + pages + size + __typename + } + __typename + } + } + """ + + variables = { + "offset_value": 0, + "limit_value": 10, + "sort_value": "", + "program_name_filter": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name_filter": "", + "study_name_filter": "", + "disease_filter": "", + "filterValue": "", + "analytical_frac_filter": "", + "exp_type_filter": "", + "ethnicity_filter": "", + "race_filter": "", + "gender_filter": "", + "tumor_grade_filter": "", + "sample_type_filter": "", + "acquisition_type_filter": "", + "data_category_filter": "", + "file_type_filter": "", + "access_filter": "", + "downloadable_filter": "", + "biospecimen_status_filter": "", + "case_status_filter": "", + "getAll": False, + } + + try: + response = httpx.post( + mds_url, json={"query": query, "variables": variables} + ) + response.raise_for_status() + response_data = response.json() + results["results"] = response_data["data"]["getPaginatedUIStudy"][ + "uiStudies" + ] + logger.info( + f"Fetched {response_data['data']['getPaginatedUICase']['total']} records from PDC)" + ) + + except httpx.TimeoutException as exc: + logger.error(f"An timeout error occurred while requesting {mds_url}.") + raise + except httpx.HTTPError as exc: + logger.error( + f"An HTTP error occurred while requesting {mds_url} {exc}. Returning {len(results['results'])} results." + ) + except Exception as exc: + logger.error( + f"An error occurred while requesting {mds_url} {exc}. Returning {len(results['results'])} results." + ) + return results + + @staticmethod + def addGen3ExpectedFields( + item, mappings, keepOriginalFields, globalFieldFilters, schema + ): + """ + Map item fields to gen3 normalized fields + using the mapping and adding the location + """ + results = item + if mappings is not None: + mapped_fields = RemoteMetadataAdapter.mapFields( + item, mappings, globalFieldFilters, schema + ) + if keepOriginalFields: + results.update(mapped_fields) + else: + results = mapped_fields + + return results + + def normalizeToGen3MDSFields(self, data, **kwargs) -> Dict[str, Any]: + """ + Iterates over the response. + :param data: + :return: + """ + mappings = kwargs.get("mappings", None) + keepOriginalFields = kwargs.get("keepOriginalFields", False) + globalFieldFilters = kwargs.get("globalFieldFilters", []) + schema = kwargs.get("schema", {}) + + results = {} + for item in data["results"]: + normalized_item = PDCSubjectAdapter.addGen3ExpectedFields( + item, + mappings, + keepOriginalFields, + globalFieldFilters, + schema, + ) + tag_list = [] + for tag_category in ["disease_type", "primary_site"]: + if normalized_item[tag_category]: + tag_string = normalized_item[tag_category] + if ";" in tag_string: + tags = tag_string.split(";") + for tag in tags: + tag_list.append({"name": tag, "category": tag_category}) + else: + tag_list.append({"name": tag_string, "category": tag_category}) + + else: + tag_list.append({"name": "", "category": tag_category}) + + normalized_item["tags"] = tag_list + results[normalized_item["_unique_id"]] = { + "_guid_type": "discovery_metadata", + "gen3_discovery": normalized_item, + } + + return results + + def gather_metadata( gather, mds_url, @@ -1627,6 +2317,8 @@ def gather_metadata( "gdc": GDCAdapter, "cidc": CIDCAdapter, "pdc": PDCAdapter, + "pdcsubject": PDCSubjectAdapter, + "pdcstudy": PDCStudyAdapter, } diff --git a/tests/test_agg_mds_adapters.py b/tests/test_agg_mds_adapters.py index bd11ad30..8baa2e6c 100644 --- a/tests/test_agg_mds_adapters.py +++ b/tests/test_agg_mds_adapters.py @@ -9,6 +9,8 @@ add_icpsr_source_url, FieldFilters, get_json_path_value, + add_clinical_trials_source_url, + uppercase, ) import httpx @@ -136,3 +138,13 @@ def test_json_path_expression(): ["results1.csv", "results2.csv", "results3.csv"], ["results4.csv", "results5.csv", "results6.csv"], ] + + +def test_add_clinical_trials_source_url(): + integer = 1 + assert add_clinical_trials_source_url(integer) == 1 + + +def test_uppercase(): + interger = 1 + assert uppercase(interger) == 1 diff --git a/tests/test_agg_mds_clinicaltrials_adapter.py b/tests/test_agg_mds_clinicaltrials_adapter.py index 5f891294..232f591b 100644 --- a/tests/test_agg_mds_clinicaltrials_adapter.py +++ b/tests/test_agg_mds_clinicaltrials_adapter.py @@ -1094,3 +1094,19 @@ def test_get_metadata_clinicaltrials(): ) except Exception as exc: assert isinstance(exc, RetryError) == True + + respx.get( + "http://test/ok?expr=should+error+timeout&fmt=json&min_rnk=1&max_rnk=1" + ).mock(side_effect=httpx.HTTPError("This is a HTTP Error")) + + assert ( + get_metadata( + "clinicaltrials", + "http://test/ok", + filters={"term": "should+error+timeout", "maxItems": 1, "batchSize": 1}, + mappings=field_mappings, + perItemValues=item_values, + keepOriginalFields=True, + ) + == {} + ) diff --git a/tests/test_agg_mds_pdc_study_adapter.py b/tests/test_agg_mds_pdc_study_adapter.py new file mode 100644 index 00000000..220c7a66 --- /dev/null +++ b/tests/test_agg_mds_pdc_study_adapter.py @@ -0,0 +1,445 @@ +import respx +import httpx + +from mds.agg_mds.adapters import get_metadata + + +@respx.mock +def test_get_metadata_pdc_study(): + json_response = r""" + { + "data": { + "getPaginatedUIStudy": { + "total": 1, + "uiStudies": [ + { + "study_id": "study_id_1", + "pdc_study_id": "PDC000123", + "submitter_id_name": "APOLLO LUAD - Phosphoproteome - FeNTA", + "study_description": "We present a deep proteogenomic profiling study of 87 lung adenocarcinoma (LUAD) tumors obtained in the United States", + "study_submitter_id": "APOLLO LUAD - Phosphoproteome - FeNTA", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "disease_type": "Lung Adenocarcinoma;Other", + "primary_site": "Bronchus and lung;Not Reported", + "analytical_fraction": "Phosphoproteome", + "experiment_type": "TMT11", + "embargo_date": null, + "cases_count": 101, + "aliquots_count": 101, + "filesCount": [ + { + "file_type": "Document", + "data_category": "Other Metadata", + "files_count": 1, + "__typename": "File" + }, + { + "file_type": "Open Standard", + "data_category": "Peptide Spectral Matches", + "files_count": 120, + "__typename": "File" + } + ], + "supplementaryFilesCount": [ + { + "data_category": "Other Metadata", + "file_type": "Document", + "files_count": 1, + "__typename": "File" + } + ], + "nonSupplementaryFilesCount": [ + { + "data_category": "Peptide Spectral Matches", + "file_type": "Open Standard", + "files_count": 120, + "__typename": "File" + }, + { + "data_category": "Peptide Spectral Matches", + "file_type": "Text", + "files_count": 120, + "__typename": "File" + } + ], + "contacts": [], + "versions": [ + { + "number": "1", + "__typename": "Version" + } + ], + "__typename": "UIStudy" + } + ] + } + } + } + """ + + field_mappings = { + "authz": "/VA", + "tags": [ + {"name": "path:disease_type", "category": "disease_type"}, + {"name": "path:primary_site", "category": "primary_site"}, + ], + "_unique_id": "path:study_id", + "pdc_study_id": "path:pdc_study_id", + "submitter_id_name": "path:submitter_id_name", + "study_description": "path:study_description", + "study_submitter_id": "path:study_submitter_id", + "program_name": "path:program_name", + "project_name": "path:project_name", + "disease_type": "path:disease_type", + "primary_site": "path:primary_site", + "analytical_fraction": "path:analytical_fraction", + "experiment_type": "path:experiment_type", + "embargo_date": "path:embargo_date", + "cases_count": "path:cases_count", + "aliquots_count": "path:aliquots_count", + "filesCount": "path:filesCount", + "supplementaryFilesCount": "path:supplementaryFilesCount", + "nonSupplementaryFilesCount": "path:nonSupplementaryFilesCount", + "contacts": "path:contacts", + "versions": "path:versions", + "__typename": "path:__typename", + "$myVar.id#$!_+~*/": "path:invalid_path", + } + + query = """ + query FilteredStudiesDataPaginated( + $offset_value: Int + $limit_value: Int + $sort_value: String + $program_name_filter: String! + $project_name_filter: String! + $study_name_filter: String! + $disease_filter: String! + $filterValue: String! + $analytical_frac_filter: String! + $exp_type_filter: String! + $ethnicity_filter: String! + $race_filter: String! + $gender_filter: String! + $tumor_grade_filter: String! + $sample_type_filter: String! + $acquisition_type_filter: String! + $data_category_filter: String! + $file_type_filter: String! + $access_filter: String! + $downloadable_filter: String! + $biospecimen_status_filter: String! + $case_status_filter: String! + $getAll: Boolean! + ) { + getPaginatedUIStudy( + offset: $offset_value + limit: $limit_value + sort: $sort_value + program_name: $program_name_filter + project_name: $project_name_filter + study_name: $study_name_filter + disease_type: $disease_filter + primary_site: $filterValue + analytical_fraction: $analytical_frac_filter + experiment_type: $exp_type_filter + ethnicity: $ethnicity_filter + race: $race_filter + gender: $gender_filter + tumor_grade: $tumor_grade_filter + sample_type: $sample_type_filter + acquisition_type: $acquisition_type_filter + data_category: $data_category_filter + file_type: $file_type_filter + access: $access_filter + downloadable: $downloadable_filter + biospecimen_status: $biospecimen_status_filter + case_status: $case_status_filter + getAll: $getAll + ) { + total + uiStudies { + study_id + pdc_study_id + submitter_id_name + study_description + study_submitter_id + program_name + project_name + disease_type + primary_site + analytical_fraction + experiment_type + embargo_date + cases_count + aliquots_count + filesCount { + file_type + data_category + files_count + __typename + } + supplementaryFilesCount { + data_category + file_type + files_count + __typename + } + nonSupplementaryFilesCount { + data_category + file_type + files_count + __typename + } + contacts { + name + institution + email + url + __typename + } + versions { + number + __typename + } + __typename + } + pagination { + count + sort + from + page + total + pages + size + __typename + } + __typename + } + } + """ + + variables = { + "offset_value": 0, + "limit_value": 10, + "sort_value": "", + "program_name_filter": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name_filter": "", + "study_name_filter": "", + "disease_filter": "", + "filterValue": "", + "analytical_frac_filter": "", + "exp_type_filter": "", + "ethnicity_filter": "", + "race_filter": "", + "gender_filter": "", + "tumor_grade_filter": "", + "sample_type_filter": "", + "acquisition_type_filter": "", + "data_category_filter": "", + "file_type_filter": "", + "access_filter": "", + "downloadable_filter": "", + "biospecimen_status_filter": "", + "case_status_filter": "", + "getAll": False, + } + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(return_value=httpx.Response(status_code=200, content=json_response)) + + filters = {"size": 5} + + assert ( + get_metadata("pdcstudy", None, filters=filters, mappings=field_mappings) == {} + ) + + expected_result = { + "study_id_1": { + "_guid_type": "discovery_metadata", + "gen3_discovery": { + "authz": "/VA", + "tags": [ + {"name": "Lung Adenocarcinoma", "category": "disease_type"}, + {"name": "Other", "category": "disease_type"}, + {"name": "Bronchus and lung", "category": "primary_site"}, + {"name": "Not Reported", "category": "primary_site"}, + ], + "_unique_id": "study_id_1", + "pdc_study_id": "PDC000123", + "submitter_id_name": "APOLLO LUAD - Phosphoproteome - FeNTA", + "study_description": "We present a deep proteogenomic profiling study of 87 lung adenocarcinoma (LUAD) tumors obtained in the United States", + "study_submitter_id": "APOLLO LUAD - Phosphoproteome - FeNTA", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "disease_type": "Lung Adenocarcinoma;Other", + "primary_site": "Bronchus and lung;Not Reported", + "analytical_fraction": "Phosphoproteome", + "experiment_type": "TMT11", + "embargo_date": None, + "cases_count": 101, + "aliquots_count": 101, + "filesCount": [ + { + "file_type": "Document", + "data_category": "Other Metadata", + "files_count": 1, + "__typename": "File", + }, + { + "file_type": "Open Standard", + "data_category": "Peptide Spectral Matches", + "files_count": 120, + "__typename": "File", + }, + ], + "supplementaryFilesCount": [ + { + "data_category": "Other Metadata", + "file_type": "Document", + "files_count": 1, + "__typename": "File", + } + ], + "nonSupplementaryFilesCount": [ + { + "data_category": "Peptide Spectral Matches", + "file_type": "Open Standard", + "files_count": 120, + "__typename": "File", + }, + { + "data_category": "Peptide Spectral Matches", + "file_type": "Text", + "files_count": 120, + "__typename": "File", + }, + ], + "contacts": [], + "versions": [{"number": "1", "__typename": "Version"}], + "__typename": "UIStudy", + }, + }, + } + + actual_result = get_metadata( + "pdcstudy", "http://test/ok", filters=filters, mappings=field_mappings + ) + assert actual_result == expected_result + + expected_result = { + "study_id_1": { + "_guid_type": "discovery_metadata", + "gen3_discovery": { + "authz": "/VA", + "tags": [ + {"name": "Lung Adenocarcinoma", "category": "disease_type"}, + {"name": "Other", "category": "disease_type"}, + {"name": "Bronchus and lung", "category": "primary_site"}, + {"name": "Not Reported", "category": "primary_site"}, + ], + "_unique_id": "study_id_1", + "study_id": "study_id_1", + "pdc_study_id": "PDC000123", + "submitter_id_name": "APOLLO LUAD - Phosphoproteome - FeNTA", + "study_description": "We present a deep proteogenomic profiling study of 87 lung adenocarcinoma (LUAD) tumors obtained in the United States", + "study_submitter_id": "APOLLO LUAD - Phosphoproteome - FeNTA", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "disease_type": "Lung Adenocarcinoma;Other", + "primary_site": "Bronchus and lung;Not Reported", + "analytical_fraction": "Phosphoproteome", + "experiment_type": "TMT11", + "embargo_date": None, + "cases_count": 101, + "aliquots_count": 101, + "filesCount": [ + { + "file_type": "Document", + "data_category": "Other Metadata", + "files_count": 1, + "__typename": "File", + }, + { + "file_type": "Open Standard", + "data_category": "Peptide Spectral Matches", + "files_count": 120, + "__typename": "File", + }, + ], + "supplementaryFilesCount": [ + { + "data_category": "Other Metadata", + "file_type": "Document", + "files_count": 1, + "__typename": "File", + } + ], + "nonSupplementaryFilesCount": [ + { + "data_category": "Peptide Spectral Matches", + "file_type": "Open Standard", + "files_count": 120, + "__typename": "File", + }, + { + "data_category": "Peptide Spectral Matches", + "file_type": "Text", + "files_count": 120, + "__typename": "File", + }, + ], + "contacts": [], + "versions": [{"number": "1", "__typename": "Version"}], + "__typename": "UIStudy", + }, + }, + } + + actual_result = get_metadata( + "pdcstudy", + "http://test/ok", + filters=filters, + mappings=field_mappings, + keepOriginalFields=True, + ) + assert actual_result == expected_result + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(side_effect=httpx.TimeoutException) + + assert ( + get_metadata( + "pdcstudy", "http://test/ok", filters=filters, mappings=field_mappings + ) + == {} + ) + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(side_effect=httpx.HTTPError("This is a HTTP Error")) + + assert ( + get_metadata( + "pdcstudy", "http://test/ok", filters=filters, mappings=field_mappings + ) + == {} + ) + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(side_effect=Exception) + + assert ( + get_metadata( + "pdcstudy", "http://test/ok", filters=filters, mappings=field_mappings + ) + == {} + ) diff --git a/tests/test_agg_mds_pdc_subject_adapter.py b/tests/test_agg_mds_pdc_subject_adapter.py new file mode 100644 index 00000000..c97f89c4 --- /dev/null +++ b/tests/test_agg_mds_pdc_subject_adapter.py @@ -0,0 +1,558 @@ +import respx +import httpx + +from mds.agg_mds.adapters import get_metadata + + +@respx.mock +def test_get_metadata_pdc_subject(): + json_response = r""" + { + "data": { + "getPaginatedUICase": { + "total": 2, + "uiCases": [ + { + "aliquot_id": "test_aliquot_id_0", + "sample_id": "test_sample_id_0", + "case_id": "test_case_id_0", + "aliquot_submitter_id": "AP-ABCD", + "aliquot_is_ref": "No", + "aliquot_status": "Qualified", + "aliquot_quantity": null, + "aliquot_volume": null, + "amount": null, + "analyte_type": null, + "concentration": null, + "case_status": "Qualified", + "sample_status": "Qualified", + "sample_submitter_id": "AP-ABCD", + "sample_is_ref": null, + "biospecimen_anatomic_site": null, + "biospecimen_laterality": null, + "composition": "Solid Tissue", + "current_weight": null, + "days_to_collection": null, + "days_to_sample_procurement": null, + "diagnosis_pathologically_confirmed": null, + "freezing_method": null, + "initial_weight": null, + "intermediate_dimension": null, + "longest_dimension": null, + "method_of_sample_procurement": null, + "pathology_report_uuid": null, + "preservation_method": null, + "sample_type_id": null, + "shortest_dimension": null, + "time_between_clamping_and_freezing": null, + "time_between_excision_and_freezing": null, + "tissue_type": "Tumor", + "tumor_code": null, + "tumor_code_id": null, + "tumor_descriptor": null, + "case_submitter_id": "AP-ABCD", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "sample_type": "Primary Tumor", + "disease_type": "Test Type", + "primary_site": "Bronchus and lung", + "tissue_collection_type": null, + "sample_ordinal": null, + "__typename": "UICase" + }, + { + "aliquot_id": "test_aliquot_id_1", + "sample_id": "test_sample_id_1", + "case_id": "test_case_id_1", + "aliquot_submitter_id": "AP-DCBA", + "aliquot_is_ref": "No", + "aliquot_status": "Qualified", + "aliquot_quantity": null, + "aliquot_volume": null, + "amount": null, + "analyte_type": null, + "concentration": null, + "case_status": "Qualified", + "sample_status": "Qualified", + "sample_submitter_id": "AP-DCBA", + "sample_is_ref": null, + "biospecimen_anatomic_site": null, + "biospecimen_laterality": null, + "composition": "Solid Tissue", + "current_weight": null, + "days_to_collection": null, + "days_to_sample_procurement": null, + "diagnosis_pathologically_confirmed": null, + "freezing_method": null, + "initial_weight": null, + "intermediate_dimension": null, + "longest_dimension": null, + "method_of_sample_procurement": null, + "pathology_report_uuid": null, + "preservation_method": null, + "sample_type_id": null, + "shortest_dimension": null, + "time_between_clamping_and_freezing": null, + "time_between_excision_and_freezing": null, + "tissue_type": "Tumor", + "tumor_code": null, + "tumor_code_id": null, + "tumor_descriptor": null, + "case_submitter_id": "AP-DCBA", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "sample_type": "Primary Tumor", + "disease_type": "Test Type 1", + "primary_site": "Feet", + "tissue_collection_type": null, + "sample_ordinal": null, + "__typename": "UICase" + }] + } + } + } + """ + + field_mappings = { + "authz": "/VA", + "tags": [ + {"name": "path:disease_type", "category": "disease_type"}, + {"name": "path:primary_site", "category": "primary_site"}, + ], + "_unique_id": "path:case_id", + "subject_apollo_id": "path:sample_submitter_id", + "aliquot_id": "path:aliquot_id", + "sample_id": "path:sample_id", + "aliquot_submitter_id": "path:aliquot_submitter_id", + "aliquot_is_ref": "path:aliquot_is_ref", + "aliquot_status": "path:aliquot_status", + "aliquot_quantity": "path:aliquot_quantity", + "aliquot_volume": "path:aliquot_volume", + "amount": "path:amount", + "analyte_type": "path:analyte_type", + "concentration": "path:concentration", + "case_status": "path:case_status", + "sample_status": "path:sample_status", + "sample_is_ref": "path:sample_is_ref", + "biospecimen_anatomic_site": "path:biospecimen_anatomic_site", + "biospecimen_laterality": "path:biospecimen_laterality", + "composition": "path:composition", + "current_weight": "path:current_weight", + "days_to_collection": "path:days_to_collection", + "days_to_sample_procurement": "path:days_to_sample_procurement", + "diagnosis_pathologically_confirmed": "path:diagnosis_pathologically_confirmed", + "freezing_method": "path:freezing_method", + "initial_weight": "path:initial_weight", + "intermediate_dimension": "path:intermediate_dimension", + "longest_dimension": "path:longest_dimension", + "method_of_sample_procurement": "path:method_of_sample_procurement", + "pathology_report_uuid": "path:pathology_report_uuid", + "preservation_method": "path:preservation_method", + "sample_type_id": "path:sample_type_id", + "shortest_dimension": "path:shortest_dimension", + "time_between_clamping_and_freezing": "path:time_between_clamping_and_freezing", + "time_between_excision_and_freezing": "path:time_between_excision_and_freezing", + "tissue_type": "path:tissue_type", + "tumor_code": "path:tumor_code", + "tumor_code_id": "path:tumor_code_id", + "tumor_descriptor": "path:tumor_descriptor", + "case_submitter_id": "path:case_submitter_id", + "program_name": "path:program_name", + "project_name": "path:project_name", + "sample_type": "path:sample_type", + "disease_type": "path:disease_type", + "primary_site": "path:primary_site", + "tissue_collection_type": "path:tissue_collection_type", + "sample_ordinal": "path:sample_ordinal", + "__typename": "path:__typename", + } + + query = """ + query FilteredCasesDataPaginated($offset_value: Int, $limit_value: Int, $sort_value: String, $program_name_filter: String!, $project_name_filter: String!, $study_name_filter: String!, $disease_filter: String!, $filterValue: String!, $analytical_frac_filter: String!, $exp_type_filter: String!, $ethnicity_filter: String!, $race_filter: String!, $gender_filter: String!, $tumor_grade_filter: String!, $sample_type_filter: String!, $acquisition_type_filter: String!, $data_category_filter: String!, $file_type_filter: String!, $access_filter: String!, $downloadable_filter: String!, $biospecimen_status_filter: String!, $case_status_filter: String!, $getAll: Boolean!) { + getPaginatedUICase(offset: $offset_value, limit: $limit_value, sort: $sort_value, program_name: $program_name_filter, project_name: $project_name_filter, study_name: $study_name_filter, disease_type: $disease_filter, primary_site: $filterValue, analytical_fraction: $analytical_frac_filter, experiment_type: $exp_type_filter, ethnicity: $ethnicity_filter, race: $race_filter, gender: $gender_filter, tumor_grade: $tumor_grade_filter, sample_type: $sample_type_filter, acquisition_type: $acquisition_type_filter, data_category: $data_category_filter, file_type: $file_type_filter, access: $access_filter, downloadable: $downloadable_filter, biospecimen_status: $biospecimen_status_filter, case_status: $case_status_filter, getAll: $getAll) { + total + uiCases { + aliquot_id + sample_id + case_id + aliquot_submitter_id + aliquot_is_ref + aliquot_status + aliquot_quantity + aliquot_volume + amount + analyte_type + concentration + case_status + sample_status + sample_submitter_id + sample_is_ref + biospecimen_anatomic_site + biospecimen_laterality + composition + current_weight + days_to_collection + days_to_sample_procurement + diagnosis_pathologically_confirmed + freezing_method + initial_weight + intermediate_dimension + longest_dimension + method_of_sample_procurement + pathology_report_uuid + preservation_method + sample_type_id + shortest_dimension + time_between_clamping_and_freezing + time_between_excision_and_freezing + tissue_type + tumor_code + tumor_code_id + tumor_descriptor + case_submitter_id + program_name + project_name + sample_type + disease_type + primary_site + tissue_collection_type + sample_ordinal + __typename + } + pagination { + count + sort + from + page + total + pages + size + __typename + } + __typename + } + } + """ + + variables = { + "offset_value": 0, + "limit_value": 1000, + "sort_value": "", + "program_name_filter": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name_filter": "", + "study_name_filter": "", + "disease_filter": "", + "filterValue": "", + "analytical_frac_filter": "", + "exp_type_filter": "", + "ethnicity_filter": "", + "race_filter": "", + "gender_filter": "", + "tumor_grade_filter": "", + "sample_type_filter": "", + "acquisition_type_filter": "", + "data_category_filter": "", + "file_type_filter": "", + "access_filter": "", + "downloadable_filter": "", + "biospecimen_status_filter": "", + "case_status_filter": "", + "getAll": False, + } + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(return_value=httpx.Response(status_code=200, content=json_response)) + + filters = {"size": 5} + + assert ( + get_metadata("pdcsubject", None, filters=filters, mappings=field_mappings) == {} + ) + + expected_result = { + "test_case_id_0": { + "_guid_type": "discovery_metadata", + "gen3_discovery": { + "authz": "/VA", + "tags": [ + {"name": "Test Type", "category": "disease_type"}, + {"name": "Bronchus and lung", "category": "primary_site"}, + ], + "_unique_id": "test_case_id_0", + "subject_apollo_id": "AP-ABCD", + "aliquot_id": "test_aliquot_id_0", + "sample_id": "test_sample_id_0", + "aliquot_submitter_id": "AP-ABCD", + "aliquot_is_ref": "No", + "aliquot_status": "Qualified", + "aliquot_quantity": None, + "aliquot_volume": None, + "amount": None, + "analyte_type": None, + "concentration": None, + "case_status": "Qualified", + "sample_status": "Qualified", + "sample_is_ref": None, + "biospecimen_anatomic_site": None, + "biospecimen_laterality": None, + "composition": "Solid Tissue", + "current_weight": None, + "days_to_collection": None, + "days_to_sample_procurement": None, + "diagnosis_pathologically_confirmed": None, + "freezing_method": None, + "initial_weight": None, + "intermediate_dimension": None, + "longest_dimension": None, + "method_of_sample_procurement": None, + "pathology_report_uuid": None, + "preservation_method": None, + "sample_type_id": None, + "shortest_dimension": None, + "time_between_clamping_and_freezing": None, + "time_between_excision_and_freezing": None, + "tissue_type": "Tumor", + "tumor_code": None, + "tumor_code_id": None, + "tumor_descriptor": None, + "case_submitter_id": "AP-ABCD", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "sample_type": "Primary Tumor", + "disease_type": "Test Type", + "primary_site": "Bronchus and lung", + "tissue_collection_type": None, + "sample_ordinal": None, + "__typename": "UICase", + }, + }, + "test_case_id_1": { + "_guid_type": "discovery_metadata", + "gen3_discovery": { + "authz": "/VA", + "tags": [ + {"name": "Test Type 1", "category": "disease_type"}, + {"name": "Feet", "category": "primary_site"}, + ], + "_unique_id": "test_case_id_1", + "subject_apollo_id": "AP-DCBA", + "aliquot_id": "test_aliquot_id_1", + "sample_id": "test_sample_id_1", + "aliquot_submitter_id": "AP-DCBA", + "aliquot_is_ref": "No", + "aliquot_status": "Qualified", + "aliquot_quantity": None, + "aliquot_volume": None, + "amount": None, + "analyte_type": None, + "concentration": None, + "case_status": "Qualified", + "sample_status": "Qualified", + "sample_is_ref": None, + "biospecimen_anatomic_site": None, + "biospecimen_laterality": None, + "composition": "Solid Tissue", + "current_weight": None, + "days_to_collection": None, + "days_to_sample_procurement": None, + "diagnosis_pathologically_confirmed": None, + "freezing_method": None, + "initial_weight": None, + "intermediate_dimension": None, + "longest_dimension": None, + "method_of_sample_procurement": None, + "pathology_report_uuid": None, + "preservation_method": None, + "sample_type_id": None, + "shortest_dimension": None, + "time_between_clamping_and_freezing": None, + "time_between_excision_and_freezing": None, + "tissue_type": "Tumor", + "tumor_code": None, + "tumor_code_id": None, + "tumor_descriptor": None, + "case_submitter_id": "AP-DCBA", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "sample_type": "Primary Tumor", + "disease_type": "Test Type 1", + "primary_site": "Feet", + "tissue_collection_type": None, + "sample_ordinal": None, + "__typename": "UICase", + }, + }, + } + + assert ( + get_metadata( + "pdcsubject", "http://test/ok", filters=filters, mappings=field_mappings + ) + == expected_result + ) + + expected_result = { + "test_case_id_0": { + "_guid_type": "discovery_metadata", + "gen3_discovery": { + "authz": "/VA", + "tags": [ + {"name": "Test Type", "category": "disease_type"}, + {"name": "Bronchus and lung", "category": "primary_site"}, + ], + "_unique_id": "test_case_id_0", + "subject_apollo_id": "AP-ABCD", + "aliquot_id": "test_aliquot_id_0", + "sample_id": "test_sample_id_0", + "case_id": "test_case_id_0", + "aliquot_submitter_id": "AP-ABCD", + "aliquot_is_ref": "No", + "aliquot_status": "Qualified", + "aliquot_quantity": None, + "aliquot_volume": None, + "amount": None, + "analyte_type": None, + "concentration": None, + "case_status": "Qualified", + "sample_status": "Qualified", + "sample_submitter_id": "AP-ABCD", + "sample_is_ref": None, + "biospecimen_anatomic_site": None, + "biospecimen_laterality": None, + "composition": "Solid Tissue", + "current_weight": None, + "days_to_collection": None, + "days_to_sample_procurement": None, + "diagnosis_pathologically_confirmed": None, + "freezing_method": None, + "initial_weight": None, + "intermediate_dimension": None, + "longest_dimension": None, + "method_of_sample_procurement": None, + "pathology_report_uuid": None, + "preservation_method": None, + "sample_type_id": None, + "shortest_dimension": None, + "time_between_clamping_and_freezing": None, + "time_between_excision_and_freezing": None, + "tissue_type": "Tumor", + "tumor_code": None, + "tumor_code_id": None, + "tumor_descriptor": None, + "case_submitter_id": "AP-ABCD", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "sample_type": "Primary Tumor", + "disease_type": "Test Type", + "primary_site": "Bronchus and lung", + "tissue_collection_type": None, + "sample_ordinal": None, + "__typename": "UICase", + }, + }, + "test_case_id_1": { + "_guid_type": "discovery_metadata", + "gen3_discovery": { + "authz": "/VA", + "tags": [ + {"name": "Test Type 1", "category": "disease_type"}, + {"name": "Feet", "category": "primary_site"}, + ], + "_unique_id": "test_case_id_1", + "subject_apollo_id": "AP-DCBA", + "aliquot_id": "test_aliquot_id_1", + "sample_id": "test_sample_id_1", + "case_id": "test_case_id_1", + "aliquot_submitter_id": "AP-DCBA", + "aliquot_is_ref": "No", + "aliquot_status": "Qualified", + "aliquot_quantity": None, + "aliquot_volume": None, + "amount": None, + "analyte_type": None, + "concentration": None, + "case_status": "Qualified", + "sample_status": "Qualified", + "sample_submitter_id": "AP-DCBA", + "sample_is_ref": None, + "biospecimen_anatomic_site": None, + "biospecimen_laterality": None, + "composition": "Solid Tissue", + "current_weight": None, + "days_to_collection": None, + "days_to_sample_procurement": None, + "diagnosis_pathologically_confirmed": None, + "freezing_method": None, + "initial_weight": None, + "intermediate_dimension": None, + "longest_dimension": None, + "method_of_sample_procurement": None, + "pathology_report_uuid": None, + "preservation_method": None, + "sample_type_id": None, + "shortest_dimension": None, + "time_between_clamping_and_freezing": None, + "time_between_excision_and_freezing": None, + "tissue_type": "Tumor", + "tumor_code": None, + "tumor_code_id": None, + "tumor_descriptor": None, + "case_submitter_id": "AP-DCBA", + "program_name": "Applied Proteogenomics OrganizationaL Learning and Outcomes - APOLLO", + "project_name": "APOLLO1", + "sample_type": "Primary Tumor", + "disease_type": "Test Type 1", + "primary_site": "Feet", + "tissue_collection_type": None, + "sample_ordinal": None, + "__typename": "UICase", + }, + }, + } + + actual_result = get_metadata( + "pdcsubject", + "http://test/ok", + filters=filters, + mappings=field_mappings, + keepOriginalFields=True, + ) + assert actual_result == expected_result + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(side_effect=httpx.TimeoutException) + + assert ( + get_metadata( + "pdcsubject", "http://test/ok", filters=filters, mappings=field_mappings + ) + == {} + ) + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(side_effect=httpx.HTTPError("This is a HTTP Error")) + + assert ( + get_metadata( + "pdcsubject", "http://test/ok", filters=filters, mappings=field_mappings + ) + == {} + ) + + respx.post( + "http://test/ok", + json={"query": query, "variables": variables}, + ).mock(side_effect=Exception) + + assert ( + get_metadata( + "pdcsubject", "http://test/ok", filters=filters, mappings=field_mappings + ) + == {} + )