예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
def test_pseudos(get_pseudo_potential_data):
    """Test the `PseudoPotentialFamily.pseudos` property."""
    pseudos = {
        'Ar': get_pseudo_potential_data('Ar').store(),
        'He': get_pseudo_potential_data('He').store(),
    }
    family = PseudoPotentialFamily(label='label').store()
    family.add_nodes(list(pseudos.values()))
    assert family.pseudos == pseudos
예제 #4
0
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')
예제 #5
0
def test_parse_pseudos_from_directory_non_file_nested(tmpdir):
    """Test the `PseudoPotentialFamily.parse_pseudos_from_directory` class method for folder containing a non-file.

    Note that a subdirectory containing the pseudos is fine, but if we find a directory and any other object at the
    base path, it should raise.
    """
    os.makedirs(os.path.join(str(tmpdir), 'pseudos', 'directory'))
    with open(os.path.join(str(tmpdir), 'pseudos', 'Ar.upf'), 'wb'):
        pass

    with pytest.raises(ValueError, match=r'dirpath `.*` contains at least one entry that is not a file'):
        PseudoPotentialFamily.parse_pseudos_from_directory(str(tmpdir))
예제 #6
0
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')
예제 #7
0
def test_construct():
    """Test the construction of `PseudoPotentialFamily` works."""
    label = 'label'
    family = PseudoPotentialFamily(label=label)
    assert isinstance(family, PseudoPotentialFamily)
    assert not family.is_stored
    assert family.label == label

    label = 'family'
    description = 'description'
    family = PseudoPotentialFamily(label=label, description=description)
    assert isinstance(family, PseudoPotentialFamily)
    assert not family.is_stored
    assert family.label == label
    assert family.description == description
예제 #8
0
def test_elements(get_pseudo_family):
    """Test the `PseudoPotentialFamily.elements` property."""
    elements = ['Ar', 'He']
    family = get_pseudo_family(elements=elements)
    assert sorted(family.elements) == elements

    family = PseudoPotentialFamily(label='empty').store()
    assert family.elements == []
예제 #9
0
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()))
예제 #10
0
def test_pseudo_type(get_pseudo_potential_data):
    """Test ``PseudoPotentialFamily.pseudo_type`` property."""
    family = PseudoPotentialFamily(label='label').store()
    assert family.pseudo_type is None

    pseudo = get_pseudo_potential_data('Ar').store()
    family.add_nodes((pseudo,))
    assert family.pseudo_type == pseudo.get_entry_point_name()

    family.clear()
    assert family.pseudo_type is None

    family.add_nodes((pseudo,))
    assert family.pseudo_type == pseudo.get_entry_point_name()

    family.remove_nodes(pseudo)
    assert family.pseudo_type is None
예제 #11
0
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()))
예제 #12
0
def test_add_nodes(get_pseudo_family, get_pseudo_potential_data):
    """Test that `PseudoPotentialFamily.add_nodes` method."""
    family = get_pseudo_family(elements=('Rn',))
    assert family.count() == 1

    pseudos = get_pseudo_potential_data('Ar').store()
    family.add_nodes(pseudos)
    assert family.count() == 2

    pseudos = (get_pseudo_potential_data('Ne').store(),)
    family.add_nodes(pseudos)
    assert family.count() == 3

    pseudos = (get_pseudo_potential_data('He').store(), get_pseudo_potential_data('Kr').store())
    family.add_nodes(pseudos)
    assert family.count() == 5

    # Test for an unstored family
    family = PseudoPotentialFamily(label='label')
    with pytest.raises(exceptions.ModificationNotAllowed):
        family.add_nodes(pseudos)
예제 #13
0
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')