def delete_resource_view(self, resource_view): # type: (Union[ResourceView,Dict,str]) -> None """Delete a resource view from the resource and HDX Args: resource_view (Union[ResourceView,Dict,str]): Either a resource view id or resource view metadata either from a ResourceView object or a dictionary Returns: None """ if isinstance(resource_view, str): if is_valid_uuid(resource_view) is False: raise HDXError('%s is not a valid resource view id!' % resource_view) resource_view = ResourceView({'id': resource_view}, configuration=self.configuration) else: resource_view = self._get_resource_view(resource_view) if 'id' not in resource_view: found = False title = resource_view.get('title') for rv in self.get_resource_views(): if resource_view['title'] == rv['title']: resource_view = rv found = True break if not found: raise HDXError('No resource views have title %s in this resource!' % title) resource_view.delete_from_hdx()
def delete_resource_view(self, resource_view): # type: (Union[ResourceView,Dict,str]) -> None """Delete a resource view from the resource and HDX Args: resource_view (Union[ResourceView,Dict,str]): Either a resource view id or resource view metadata either from a ResourceView object or a dictionary Returns: None """ if isinstance(resource_view, str): if is_valid_uuid(resource_view) is False: raise HDXError('%s is not a valid resource view id!' % resource_view) resource_view = ResourceView({'id': resource_view}, configuration=self.configuration) else: resource_view = self._get_resource_view(resource_view) if 'id' not in resource_view: found = False title = resource_view.get('title') for rv in self.get_resource_views(): if resource_view['title'] == rv['title']: resource_view = rv found = True break if not found: raise HDXError( 'No resource views have title %s in this resource!' % title) resource_view.delete_from_hdx()
def test_delete_from_hdx(self, configuration, post_delete): resource_view = ResourceView.read_from_hdx( 'c06b5a0d-1d41-4a74-a196-41c251c76023') resource_view.delete_from_hdx() resource_view = ResourceView.read_from_hdx( 'c06b5a0d-1d41-4a74-a196-41c251c76023') del resource_view['id'] with pytest.raises(HDXError): resource_view.delete_from_hdx()
def test_read_from_hdx(self, configuration, read): resource_view = ResourceView.read_from_hdx( 'c06b5a0d-1d41-4a74-a196-41c251c76023') assert resource_view['id'] == 'c06b5a0d-1d41-4a74-a196-41c251c76023' assert resource_view['title'] == 'Quick Charts' resource_view = ResourceView.read_from_hdx('TEST2') assert resource_view is None resource_view = ResourceView.read_from_hdx('TEST3') assert resource_view is None
def test_update_yaml(self, configuration, static_yaml): data = copy.deepcopy(self.resource_view_data) resource_view = ResourceView(data) assert resource_view['view_type'] == 'recline_view' assert resource_view['title'] == 'Data Explorer' resource_view.update_from_yaml(static_yaml) assert resource_view['view_type'] == 'hdx_hxl_preview' assert resource_view['title'] == 'Quick Charts' assert resource_view['description'] == 'lala' assert resource_view[ 'resource_id'] == '25982d1c-f45a-45e1-b14e-87d367413045'
def reorder_resource_views(self, resource_views): # type: (List[Union[ResourceView,Dict,str]]) -> None """Order resource views in resource. Args: resource_views (List[Union[ResourceView,Dict,str]]): A list of either resource view ids or resource views metadata from ResourceView objects or dictionaries Returns: None """ if not isinstance(resource_views, list): raise HDXError('ResourceViews should be a list!') ids = list() for resource_view in resource_views: if isinstance(resource_view, str): resource_view_id = resource_view else: resource_view_id = resource_view['id'] if is_valid_uuid(resource_view_id) is False: raise HDXError('%s is not a valid resource view id!' % resource_view) ids.append(resource_view_id) _, result = self._read_from_hdx('resource view', self.data['id'], 'id', ResourceView.actions()['reorder'], order=ids)
def test_get_all_for_resource(self, configuration, post_list): resource_views = ResourceView.get_all_for_resource( '25982d1c-f45a-45e1-b14e-87d367413045') assert resource_views[0][ 'id'] == 'd80301b5-4abd-49bd-bf94-fa4af7b6e7a4' assert resource_views[1][ 'id'] == 'c06b5a0d-1d41-4a74-a196-41c251c76023'
def get_resource_views(self): # type: () -> List[ResourceView] """Get any resource views in the resource Returns: List[ResourceView]: List of resource views """ return ResourceView.get_all_for_resource(self.data['id'])
def generate_resource_view(dataset, quickchart_resourceno=0, bites_disabled=None): if bites_disabled == [True, True, True]: return None resourceview = ResourceView( {'resource_id': dataset.get_resource(quickchart_resourceno)['id']}) resourceview.update_from_yaml() hxl_preview_config = json.loads(resourceview['hxl_preview_config']) bites = hxl_preview_config['bites'] if bites_disabled is not None: for i, disable in reversed(list(enumerate(bites_disabled))): if disable: del bites[i] for bite in bites: bite['type'] = 'key figure' bite['uiProperties']['postText'] = 'percent' del bite['ingredient']['aggregateColumn'] resourceview['hxl_preview_config'] = json.dumps(hxl_preview_config) return resourceview
def _get_resource_view(self, resource_view): # type: (Union[ResourceView,Dict]) -> ResourceView """Get resource view id Args: resource_view (Union[ResourceView,Dict]): ResourceView metadata from a ResourceView object or dictionary Returns: ResourceView: ResourceView object """ if isinstance(resource_view, dict): resource_view = ResourceView(resource_view, configuration=self.configuration) if isinstance(resource_view, ResourceView): return resource_view raise HDXError('Type %s is not a valid resource view!' % type(resource_view).__name__)
def test_copy(self, configuration, read): data = copy.deepcopy(self.resource_view_data) resource_view = ResourceView(data) resource_view.copy(resultdict) assert resource_view['resource_id'] == self.resource_view_data[ 'resource_id'] assert resource_view['view_type'] == 'hdx_hxl_preview' assert resource_view['hxl_preview_config'] == hxl_preview_config data = copy.deepcopy(self.resource_view_data) resource_view = ResourceView(data) resource_view.copy('c06b5a0d-1d41-4a74-a196-41c251c76023') assert resource_view['resource_id'] == self.resource_view_data[ 'resource_id'] assert resource_view['view_type'] == 'hdx_hxl_preview' assert resource_view['hxl_preview_config'] == hxl_preview_config with pytest.raises(HDXError): resource_view.copy('123') with pytest.raises(HDXError): resource_view.copy(5)
def test_update_in_hdx(self, configuration, post_update): resource_view = ResourceView() resource_view['id'] = 'NOTEXIST' with pytest.raises(HDXError): resource_view.update_in_hdx() resource_view['id'] = 'LALA' with pytest.raises(HDXError): resource_view.update_in_hdx() resource_view = ResourceView.read_from_hdx( 'c06b5a0d-1d41-4a74-a196-41c251c76023') assert resource_view['id'] == 'c06b5a0d-1d41-4a74-a196-41c251c76023' assert resource_view['view_type'] == 'hdx_hxl_preview' resource_view['id'] = 'c06b5a0d-1d41-4a74-a196-41c251c76023' resource_view['view_type'] = 'recline_view' resource_view['resource_id'] = 'LALA' resource_view.update_in_hdx() assert resource_view['id'] == 'c06b5a0d-1d41-4a74-a196-41c251c76023' assert resource_view['view_type'] == 'recline_view' assert resource_view['resource_id'] == 'LALA' assert 'state' not in resource_view resource_view['id'] = 'NOTEXIST' with pytest.raises(HDXError): resource_view.update_in_hdx() resource_view['view_type'] = 'hdx_hxl_preview' resource_view['resource_id'] = '25982d1c-f45a-45e1-b14e-87d367413045' resource_view.update_in_hdx() assert resource_view['id'] == 'NOTEXIST' assert resource_view['view_type'] == 'hdx_hxl_preview' assert resource_view[ 'resource_id'] == '25982d1c-f45a-45e1-b14e-87d367413045' del resource_view['id'] resource_view['title'] = 'NOTEXIST' with pytest.raises(HDXError): resource_view.update_in_hdx() data = copy.deepcopy(self.resource_view_data) data['id'] = 'c06b5a0d-1d41-4a74-a196-41c251c76023' data['title'] = 'Quick Charts' data['description'] = 'Custom chart X' resource_view = ResourceView(data) resource_view.create_in_hdx() assert resource_view['id'] == 'c06b5a0d-1d41-4a74-a196-41c251c76023' assert resource_view['description'] == 'Custom chart X' assert resource_view['view_type'] == 'recline_view' assert 'state' not in resource_view
def test_create_in_hdx(self, configuration, post_create): resource_view = ResourceView() with pytest.raises(HDXError): resource_view.create_in_hdx() resource_view['title'] = 'Data Explorer' with pytest.raises(HDXError): resource_view.create_in_hdx() data = copy.deepcopy(self.resource_view_data) resource_view = ResourceView(data) resource_view['title'] = 'A Preview' resource_view.create_in_hdx() assert resource_view['id'] == 'c06b5a0d-1d41-4a74-a196-41c251c76023' assert resource_view['view_type'] == 'hdx_hxl_preview' assert 'state' not in resource_view data['title'] = 'XXX' resource_view = ResourceView(data) with pytest.raises(HDXError): resource_view.create_in_hdx() data['title'] = 'YYY' resource_view = ResourceView(data) with pytest.raises(HDXError): resource_view.create_in_hdx()
def generate_resource_view(dataset): resourceview = ResourceView({'resource_id': dataset.get_resource()['id']}) resourceview.update_from_yaml() return resourceview