def test_create_from_folder_duplicate(filepath_pseudos): """Test that `PseudoPotentialFamily.create_from_folder` raises for duplicate label.""" label = 'label' PseudoPotentialFamily(label=label).store() with pytest.raises(ValueError, match=r'the PseudoPotentialFamily `.*` already exists'): PseudoPotentialFamily.create_from_folder(filepath_pseudos(), label)
def test_create_from_folder_deduplicate(filepath_pseudos, deduplicate): """Test the `PseudoPotentialFamily.create_from_folder` class method.""" from aiida_pseudo.groups.family.upf import UpfFamily # We create an existing `PseudoPotentialFamily` as well as a `UpfFamily` to test that the deduplication mechanism # will only ever check for pseudo potentials of the exact same type and not allow subclasses original = PseudoPotentialFamily.create_from_folder( filepath_pseudos(), 'original_family') UpfFamily.create_from_folder(filepath_pseudos(), 'upf_family') family = PseudoPotentialFamily.create_from_folder(filepath_pseudos(), 'duplicate_family', deduplicate=deduplicate) assert isinstance(family, PseudoPotentialFamily) assert family.is_stored pseudo_count = len(os.listdir(filepath_pseudos())) original_pseudos = {pseudo.pk for pseudo in original.pseudos.values()} family_pseudos = {pseudo.pk for pseudo in family.pseudos.values()} if deduplicate: assert QueryBuilder().append(PseudoPotentialFamily.pseudo_type, subclassing=False).count() == pseudo_count assert not original_pseudos.difference(family_pseudos) else: assert QueryBuilder().append( PseudoPotentialFamily.pseudo_type, subclassing=False).count() == pseudo_count * 2 assert not original_pseudos.intersection(family_pseudos)
def test_create_from_folder_duplicate_element(tmpdir, filepath_pseudos): """Test the `PseudoPotentialFamily.create_from_folder` class method for folder containing duplicate element.""" distutils.dir_util.copy_tree(filepath_pseudos(), str(tmpdir)) with open(os.path.join(str(tmpdir), 'Ar.UPF'), 'wb'): pass with pytest.raises(ValueError, match=r'directory `.*` contains pseudo potentials with duplicate elements'): PseudoPotentialFamily.create_from_folder(str(tmpdir), 'label')
def test_create_from_folder_parse_fail(tmpdir): """Test the `PseudoPotentialFamily.create_from_folder` class method for file that fails to parse. Since the base pseudo potential class cannot really fail to parse, since there is no parsing, this would be difficult to test, however, the constructor parses the filename for the element, and that can fail if the filename has the incorrect format. """ with open(os.path.join(str(tmpdir), 'Arr.upf'), 'wb'): pass with pytest.raises(exceptions.ParsingError, match=r'`.*` constructor did not define the element .*'): PseudoPotentialFamily.create_from_folder(str(tmpdir), 'label')
def test_create_from_folder(filepath_pseudos): """Test the `PseudoPotentialFamily.create_from_folder` class method.""" label = 'label' family = PseudoPotentialFamily.create_from_folder(filepath_pseudos(), label) assert isinstance(family, PseudoPotentialFamily) assert family.is_stored assert family.label == label assert len(family.nodes) == len(os.listdir(filepath_pseudos()))
def test_create_from_folder_nested(filepath_pseudos, tmpdir): """Test the `PseudoPotentialFamily.create_from_folder` class method when the pseudos are in a subfolder.""" filepath = str(tmpdir / 'subdirectory') distutils.dir_util.copy_tree(filepath_pseudos(), filepath) label = 'label' family = PseudoPotentialFamily.create_from_folder(str(tmpdir), label) assert isinstance(family, PseudoPotentialFamily) assert family.is_stored assert family.label == label assert len(family.nodes) == len(os.listdir(filepath_pseudos()))
def test_create_from_folder_empty(tmpdir): """Test the `PseudoPotentialFamily.create_from_folder` class method for empty folder.""" with pytest.raises(ValueError, match=r'no pseudo potentials were parsed from.*'): PseudoPotentialFamily.create_from_folder(str(tmpdir), 'label')