def unmap_dataset( project: Project, *, source_dataset: Dataset, remove_dataset_from_project: bool = False, skip_if_missing: bool = False, ) -> None: """ Wholly unmaps a dataset and optionally removes it from a project. Args: source_dataset: the source dataset (Dataset object not a string) to unmap project: the project in which to unmap the dataset remove_dataset_from_project: boolean to also remove the dataset from the project skip_if_missing: boolean to skip if dataset is not in project. If set to false and dataset is not in project will raise a RuntimeError Returns: None Raises: RuntimeError: if `source_dataset` is not in `project` and `skip_if_missing` not set to True """ # check to make sure dataset is in project and log a warning if it is not if source_dataset.name not in [x.name for x in project.input_datasets()]: if skip_if_missing: LOGGER.warning( f"Dataset to unmap {source_dataset.name} not in project {project.name}! " f"However skip_if_missing flag is set so will do nothing" ) return None else: error_message = ( f"Dataset to unmap {source_dataset.name} not in project " f"{project.name} and skip_if_missing not set to True so failing! " ) LOGGER.error(error_message) raise RuntimeError(error_message) # the resource ids of attribute mappings unfortunately change when you delete one # so need to just do this until there are no mappings left for the source dataset of interest while True: mappings = [ x for x in project.attribute_mappings().stream() if x.input_dataset_name == source_dataset.name ] # if no mappings found for this dataset then break if not mappings: break for mapping in mappings: # can only delete one then have to break out of inner loop project.attribute_mappings().delete_by_resource_id(mapping.resource_id) break # optionally remove dataset from the project if remove_dataset_from_project: project.remove_input_dataset(source_dataset)
def test_project_remove_input_dataset(self): dataset_id = self.dataset_json[0]["relativeId"] responses.add(responses.GET, self.input_datasets_url, json=self.dataset_json) responses.add( responses.DELETE, f"{self.input_datasets_url}?id={dataset_id}", status=204 ) responses.add(responses.GET, self.input_datasets_url, json=[]) project = Project(self.tamr, self.project_json[0]) dataset = next(project.input_datasets().stream()) response = project.remove_input_dataset(dataset) self.assertEqual(response.status_code, 204) input_datasets = project.input_datasets() self.assertEqual(list(input_datasets), [])