def test_refts_set_file_type_to_reftimeseries(self): super(RefTimeSeriesFileTypeMetaDataTest, self).assert_federated_irods_available() # here we are using a valid ref time series for setting it # to RefTimeseries file type which includes metadata extraction fed_test_file_full_path = '/{zone}/home/{username}/{fname}'.format( zone=settings.HS_USER_IRODS_ZONE, username=self.user.username, fname=self.refts_file_name) res_upload_files = [] fed_res_path = hydroshare.utils.get_federated_zone_home_path(fed_test_file_full_path) res_title = 'Untitled resource' self.composite_resource = hydroshare.create_resource( resource_type='CompositeResource', owner=self.user, title=res_title, files=res_upload_files, source_names=[fed_test_file_full_path], fed_res_path=fed_res_path, move=False, metadata=[], auto_aggregate=False ) # test resource is created on federated zone self.assertNotEqual(self.composite_resource.resource_federation_path, '') self.assertEqual(self.composite_resource.files.all().count(), 1) res_file = self.composite_resource.files.first() # check that the resource file is not associated with any logical file self.assertEqual(res_file.has_logical_file, False) fed_file_path = "data/contents/{}".format(self.refts_file_name) self.assertEqual(os.path.join('data', 'contents', res_file.short_path), fed_file_path) # set the tif file to RefTimeseries file type RefTimeseriesLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) # test that the content of the json file is same is what we have # saved in json_file_content field of the file metadata object res_file = self.composite_resource.files.first() logical_file = res_file.logical_file self.assertEqual(logical_file.metadata.json_file_content, res_file.fed_resource_file.read()) # test extracted ref time series file type metadata assert_ref_time_series_file_type_metadata(self) self.composite_resource.delete()
def test_create_aggregation_1(self): # here we are using a valid time series json file for setting it # to RefTimeSeries file type which includes metadata extraction # this resource file is at the root of folder hierarchy self.res_title = "Untitled resource" self.create_composite_resource(self.refts_file) self.assertEqual(self.composite_resource.files.all().count(), 1) res_file = self.composite_resource.files.first() # test resource file is not in a folder self.assertEqual(res_file.file_folder, None) # check that the resource file is not associated with any logical file self.assertEqual(res_file.has_logical_file, False) # check that there is no RefTimeseriesLogicalFile object self.assertEqual(RefTimeseriesLogicalFile.objects.count(), 0) # set the json file to RefTimeseries file type RefTimeseriesLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) # check that there is one RefTimeseriesLogicalFile object self.assertEqual(RefTimeseriesLogicalFile.objects.count(), 1) res_file = self.composite_resource.files.first() # test resource file is not in a folder self.assertEqual(res_file.file_folder, None) self.assertEqual(res_file.logical_file_type_name, self.logical_file_type_name) # test extracted ref time series file type metadata assert_ref_time_series_file_type_metadata(self) # test that the content of the json file is same is what we have # saved in json_file_content field of the file metadata object res_file = self.composite_resource.files.first() logical_file = res_file.logical_file self.assertEqual(logical_file.metadata.json_file_content, res_file.resource_file.read()) # test resource file is not in a folder self.assertEqual(res_file.file_folder, None) self.composite_resource.delete()
def test_set_file_type_to_refts_res_metadata(self): # here we are using a valid time series json file for setting it # to RefTimeSeries file type which includes metadata extraction. # resource level metadata (excluding coverage) should not be updated # as part setting the json file to RefTimeseries file type self.refts_file_obj = open(self.refts_file, 'r') res_title = "Test Composite Resource" self._create_composite_resource(title=res_title) # set resource abstract self.composite_resource.metadata.create_element('description', abstract="Some abstract") # add resource level keywords self.composite_resource.metadata.create_element('subject', value="key-word-1") self.composite_resource.metadata.create_element('subject', value="CUAHSI") self.assertEqual(self.composite_resource.files.all().count(), 1) res_file = self.composite_resource.files.first() # check that the resource file is associated with GenericLogicalFile self.assertEqual(res_file.has_logical_file, True) self.assertEqual(res_file.logical_file_type_name, "GenericLogicalFile") # check that there is one GenericLogicalFile object self.assertEqual(GenericLogicalFile.objects.count(), 1) # check that there is no RefTimeseriesLogicalFile object self.assertEqual(RefTimeseriesLogicalFile.objects.count(), 0) # set the json file to RefTimeseries file type RefTimeseriesLogicalFile.set_file_type(self.composite_resource, res_file.id, self.user) # check that there is one RefTimeseriesLogicalFile object self.assertEqual(RefTimeseriesLogicalFile.objects.count(), 1) # test that the resource title has not changed self.assertEqual(self.composite_resource.metadata.title.value, res_title) # test that the abstract has not changed self.assertEqual(self.composite_resource.metadata.description.abstract, "Some abstract") # resource keywords should have been updated (with one keyword added from the json file) keywords = [kw.value for kw in self.composite_resource.metadata.subjects.all()] for kw in keywords: self.assertIn(kw, ["key-word-1", "CUAHSI", "Time Series"]) self.composite_resource.delete()
def _test_valid_missing_optional_elements(self): self.assertEqual(self.composite_resource.files.all().count(), 1) res_file = self.composite_resource.files.first() # check that the resource file is associated with any logical file self.assertEqual(res_file.has_logical_file, False) # check that there is no RefTimeseriesLogicalFile object self.assertEqual(RefTimeseriesLogicalFile.objects.count(), 0) # set the json file to RefTimeseries file type RefTimeseriesLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) # check that there is one RefTimeseriesLogicalFile object self.assertEqual(RefTimeseriesLogicalFile.objects.count(), 1) # test that the content of the json file is same is what we have # saved in json_file_content field of the file metadata object res_file = self.composite_resource.files.first() logical_file = res_file.logical_file self.assertTrue(isinstance(logical_file, RefTimeseriesLogicalFile)) self.assertEqual(logical_file.metadata.json_file_content, res_file.resource_file.read())
def test_file_rename(self): # test that a resource file that is part of a RefTimeseriesLogicalFile object # can be renamed self.create_composite_resource(self.refts_file) res_file = self.composite_resource.files.first() base_file_name, ext = os.path.splitext(res_file.file_name) self.assertEqual(res_file.file_name, self.refts_file_name) # create refts aggregation RefTimeseriesLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) # file should not be in a folder self.assertEqual(res_file.file_folder, None) # test rename of file is allowed src_path = 'data/contents/{}'.format(res_file.file_name) tgt_path = "data/contents/{0}_1{1}".format(base_file_name, ext) move_or_rename_file_or_folder(self.user, self.composite_resource.short_id, src_path, tgt_path) res_file = self.composite_resource.files.first() self.assertEqual(res_file.file_name, '{0}_1{1}'.format(base_file_name, ext)) self.composite_resource.delete()
def _test_invalid_json_file(self, invalid_json_file_name): invalid_json_file = 'hs_file_types/tests/{}'.format( invalid_json_file_name) self.create_composite_resource(invalid_json_file) self.assertEqual(self.composite_resource.files.all().count(), 1) res_file = self.composite_resource.files.first() # check that the resource file is not associated with any logical file self.assertEqual(res_file.has_logical_file, False) # trying to set this invalid tif file to RefTimeseries file type should raise # ValidationError with self.assertRaises(ValidationError): RefTimeseriesLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) # test that the invalid file did not get deleted self.assertEqual(self.composite_resource.files.all().count(), 1) # check that the resource file is not associated with any logical file self.assertEqual(res_file.has_logical_file, False) self.composite_resource.delete()
def test_aggregation_xml_file_paths(self): # test the aggregation meta and map xml file paths with file name and folder name # changes self.create_composite_resource(self.refts_file) # there should be one resource file self.assertEqual(self.composite_resource.files.all().count(), 1) res_file = self.composite_resource.files.first() base_file_name, ext = os.path.splitext(res_file.file_name) # check that the resource file is associated with GenericLogicalFile self.assertEqual(res_file.has_logical_file, False) # set file to generic logical file type RefTimeseriesLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) res_file = self.composite_resource.files.first() self.assertEqual(res_file.logical_file_type_name, self.logical_file_type_name) logical_file = res_file.logical_file expected_meta_path = '{}_meta.xml'.format(res_file.file_name) expected_map_path = '{}_resmap.xml'.format(res_file.file_name) self.assertEqual(logical_file.metadata_short_file_path, expected_meta_path) self.assertEqual(logical_file.map_short_file_path, expected_map_path) # test xml file paths after moving the file into a folder new_folder = 'test_folder' create_folder(self.composite_resource.short_id, 'data/contents/{}'.format(new_folder)) src_path = 'data/contents/{}'.format(res_file.file_name) tgt_path = 'data/contents/{0}/{1}'.format(new_folder, res_file.file_name) move_or_rename_file_or_folder(self.user, self.composite_resource.short_id, src_path, tgt_path) res_file = self.composite_resource.files.first() logical_file = res_file.logical_file expected_meta_path = '{0}/{1}_meta.xml'.format(new_folder, res_file.file_name) expected_map_path = '{0}/{1}_resmap.xml'.format( new_folder, res_file.file_name) self.assertEqual(logical_file.metadata_short_file_path, expected_meta_path) self.assertEqual(logical_file.map_short_file_path, expected_map_path) # test xml file paths after renaming the file src_path = 'data/contents/{0}/{1}'.format(new_folder, res_file.file_name) tgt_path = 'data/contents/{0}/{1}_1{2}'.format(new_folder, base_file_name, ext) move_or_rename_file_or_folder(self.user, self.composite_resource.short_id, src_path, tgt_path) res_file = self.composite_resource.files.first() logical_file = res_file.logical_file expected_meta_path = '{0}/{1}_meta.xml'.format(new_folder, res_file.file_name) expected_map_path = '{0}/{1}_resmap.xml'.format( new_folder, res_file.file_name) self.assertEqual(logical_file.metadata_short_file_path, expected_meta_path) self.assertEqual(logical_file.map_short_file_path, expected_map_path) # test the xml file path after renaming the folder folder_rename = '{}_1'.format(new_folder) src_path = 'data/contents/{}'.format(new_folder) tgt_path = 'data/contents/{}'.format(folder_rename) move_or_rename_file_or_folder(self.user, self.composite_resource.short_id, src_path, tgt_path) res_file = self.composite_resource.files.first() logical_file = res_file.logical_file expected_meta_path = '{0}/{1}_meta.xml'.format(folder_rename, res_file.file_name) expected_map_path = '{0}/{1}_resmap.xml'.format( folder_rename, res_file.file_name) self.assertEqual(logical_file.metadata_short_file_path, expected_meta_path) self.assertEqual(logical_file.map_short_file_path, expected_map_path) self.composite_resource.delete()
def test_aggregation_name(self): # test the aggregation_name property for the refts aggregation (logical file) self.create_composite_resource(self.refts_file) # there should be one resource file self.assertEqual(self.composite_resource.files.all().count(), 1) res_file = self.composite_resource.files.first() base_file_name, ext = os.path.splitext(res_file.file_name) # check that the resource file is associated with GenericLogicalFile self.assertEqual(res_file.has_logical_file, False) # set file to refts logical file type RefTimeseriesLogicalFile.set_file_type(self.composite_resource, self.user, res_file.id) res_file = self.composite_resource.files.first() self.assertEqual(res_file.logical_file_type_name, self.logical_file_type_name) logical_file = res_file.logical_file self.assertEqual(logical_file.aggregation_name, res_file.file_name) # test the aggregation name after moving the file into a folder new_folder = 'refts_folder' create_folder(self.composite_resource.short_id, 'data/contents/{}'.format(new_folder)) src_path = 'data/contents/{}'.format(res_file.file_name) tgt_path = 'data/contents/{0}/{1}'.format(new_folder, res_file.file_name) move_or_rename_file_or_folder(self.user, self.composite_resource.short_id, src_path, tgt_path) res_file = self.composite_resource.files.first() logical_file = res_file.logical_file expected_aggregation_name = '{0}/{1}'.format(new_folder, res_file.file_name) self.assertEqual(logical_file.aggregation_name, expected_aggregation_name) # test the aggregation name after renaming the file src_path = 'data/contents/{0}/{1}'.format(new_folder, res_file.file_name) tgt_path = 'data/contents/{0}/{1}_1{2}'.format(new_folder, base_file_name, ext) move_or_rename_file_or_folder(self.user, self.composite_resource.short_id, src_path, tgt_path) res_file = self.composite_resource.files.first() logical_file = res_file.logical_file expected_aggregation_name = '{0}/{1}_1{2}'.format( new_folder, base_file_name, ext) self.assertEqual(logical_file.aggregation_name, expected_aggregation_name) # test the aggregation name after renaming the folder folder_rename = '{}_1'.format(new_folder) src_path = 'data/contents/{}'.format(new_folder) tgt_path = 'data/contents/{}'.format(folder_rename) move_or_rename_file_or_folder(self.user, self.composite_resource.short_id, src_path, tgt_path) logical_file = res_file.logical_file expected_aggregation_name = '{0}/{1}'.format(folder_rename, res_file.file_name) self.assertEqual(logical_file.aggregation_name, expected_aggregation_name) self.composite_resource.delete()