def test_empty_repo_folder_export(self, temp_dir): """Check a Node's empty repository folder is exported properly""" from aiida.common.folders import Folder from aiida.tools.importexport.dbexport import export_tree node = orm.Dict().store() node_uuid = node.uuid node_repo = RepositoryFolder(section=Repository._section_name, uuid=node_uuid) # pylint: disable=protected-access self.assertTrue( node_repo.exists(), msg='Newly created and stored Node should have had an existing repository folder' ) for filename, is_file in node_repo.get_content_list(only_paths=False): abspath_filename = os.path.join(node_repo.abspath, filename) if is_file: os.remove(abspath_filename) else: shutil.rmtree(abspath_filename, ignore_errors=False) self.assertFalse( node_repo.get_content_list(), msg='Repository folder should be empty, instead the following was found: {}'.format( node_repo.get_content_list() ) ) archive_variants = { 'archive folder': os.path.join(temp_dir, 'export_tree'), 'tar archive': os.path.join(temp_dir, 'export.tar.gz'), 'zip archive': os.path.join(temp_dir, 'export.zip') } export_tree([node], folder=Folder(archive_variants['archive folder']), silent=True) export([node], filename=archive_variants['tar archive'], file_format='tar.gz', silent=True) export([node], filename=archive_variants['zip archive'], file_format='zip', silent=True) for variant, filename in archive_variants.items(): self.reset_database() node_count = orm.QueryBuilder().append(orm.Dict, project='uuid').count() self.assertEqual(node_count, 0, msg='After DB reset {} Dict Nodes was (wrongly) found'.format(node_count)) import_data(filename, silent=True) builder = orm.QueryBuilder().append(orm.Dict, project='uuid') imported_node_count = builder.count() self.assertEqual( imported_node_count, 1, msg='After {} import a single Dict Node should have been found, ' 'instead {} was/were found'.format(variant, imported_node_count) ) imported_node_uuid = builder.all()[0][0] self.assertEqual( imported_node_uuid, node_uuid, msg='The wrong UUID was found for the imported {}: ' '{}. It should have been: {}'.format(variant, imported_node_uuid, node_uuid) )
def test_export_tree(): """Check `what` in export_tree()""" from aiida.common.folders import SandboxFolder with warnings.catch_warnings(): # To avoid printing them in output (pytest.mark.filterwarnings does not work) warnings.filterwarnings('ignore', category=AiidaDeprecationWarning) what = [] with SandboxFolder() as folder: with pytest.warns(AiidaDeprecationWarning, match='`what` is deprecated, please use `entities` instead'): dbexport.export_tree(what=what, folder=folder) folder.erase(create_empty_folder=True) with pytest.warns( AiidaDeprecationWarning, match='`what` is deprecated, the supplied `entities` input will be used' ): dbexport.export_tree(entities=what, what=what, folder=folder) folder.erase(create_empty_folder=True) with pytest.raises(TypeError, match='`entities` must be specified'): dbexport.export_tree(folder=folder) folder.erase(create_empty_folder=True) with pytest.raises(TypeError, match='`folder` must be specified'): dbexport.export_tree(entities=what)
def test_export_tree(): """Check `what` in export_tree()""" from aiida.common.folders import SandboxFolder what = [] with SandboxFolder() as folder: with pytest.warns( AiidaDeprecationWarning, match='`what` is deprecated, please use `entities` instead'): dbexport.export_tree(what=what, folder=folder) folder.erase(create_empty_folder=True) with pytest.warns( AiidaDeprecationWarning, match= '`what` is deprecated, the supplied `entities` input will be used' ): dbexport.export_tree(entities=what, what=what, folder=folder) folder.erase(create_empty_folder=True) with pytest.raises(TypeError, match='`entities` must be specified'): dbexport.export_tree(folder=folder) folder.erase(create_empty_folder=True) with pytest.raises(TypeError, match='`folder` must be specified'): dbexport.export_tree(entities=what)
def test_control_of_licenses(self): """Test control of licenses.""" from aiida.common.folders import SandboxFolder from aiida.tools.importexport.dbexport import export_tree struct = orm.StructureData() struct.source = {'license': 'GPL'} struct.store() folder = SandboxFolder() export_tree([struct], folder=folder, silent=True, allowed_licenses=['GPL']) # Folder should contain two files of metadata + nodes/ self.assertEqual(len(folder.get_content_list()), 3) folder = SandboxFolder() export_tree([struct], folder=folder, silent=True, forbidden_licenses=['Academic']) # Folder should contain two files of metadata + nodes/ self.assertEqual(len(folder.get_content_list()), 3) folder = SandboxFolder() with self.assertRaises(LicensingException): export_tree([struct], folder=folder, silent=True, allowed_licenses=['CC0']) folder = SandboxFolder() with self.assertRaises(LicensingException): export_tree([struct], folder=folder, silent=True, forbidden_licenses=['GPL']) def cc_filter(license_): return license_.startswith('CC') def gpl_filter(license_): return license_ == 'GPL' def crashing_filter(): raise NotImplementedError('not implemented yet') folder = SandboxFolder() with self.assertRaises(LicensingException): export_tree([struct], folder=folder, silent=True, allowed_licenses=cc_filter) folder = SandboxFolder() with self.assertRaises(LicensingException): export_tree([struct], folder=folder, silent=True, forbidden_licenses=gpl_filter) folder = SandboxFolder() with self.assertRaises(LicensingException): export_tree([struct], folder=folder, silent=True, allowed_licenses=crashing_filter) folder = SandboxFolder() with self.assertRaises(LicensingException): export_tree([struct], folder=folder, silent=True, forbidden_licenses=crashing_filter)