def test_delete_private_attribute(recursive_namelist): from pytest import raises nl = Namelist(recursive_namelist) nl._private = 0 del nl._private with raises(AttributeError): nl._private
def test_delete_private_attribute(recursive_namelist): from pytest import raises nl = Namelist(recursive_namelist) nl._private = 0 del nl._private with raises(AttributeError): nl._private
def test_write_read_loop(recursive_namelist): nl = Namelist(recursive_namelist) reread = Namelist() reread.read_string(nl.write()) assert len(reread.electrons) == 0 assert reread.system.ibrav == 2 assert reread.system.occupations == 'smearing' assert reread.control.prefix == 'al'
def test_simple_back_to_ordered(simple_namelist): nl = Namelist(simple_namelist) assert len(nl) > 0 back = nl.namelist() assert len(back) == len(simple_namelist) for back_key, key in zip(back, simple_namelist): assert back_key == key assert back[key] == simple_namelist[key]
def test_simple_back_to_ordered(simple_namelist): nl = Namelist(simple_namelist) assert len(nl) > 0 back = nl.namelist() assert len(back) == len(simple_namelist) for back_key, key in zip(back, simple_namelist): assert back_key == key assert back[key] == simple_namelist[key]
def test_recursive_back_to_ordered(recursive_namelist): from collections import OrderedDict nl = Namelist(recursive_namelist) assert len(nl) > 0 back = nl.namelist() assert len(back) == len(recursive_namelist) for back_key, key in zip(back, recursive_namelist): assert back_key == key assert isinstance(back[key], OrderedDict)
def test_recursive_back_to_ordered(recursive_namelist): from collections import OrderedDict nl = Namelist(recursive_namelist) assert len(nl) > 0 back = nl.namelist() assert len(back) == len(recursive_namelist) for back_key, key in zip(back, recursive_namelist): assert back_key == key assert isinstance(back[key], OrderedDict)
def test_delete_namelist_attribute(recursive_namelist): from pytest import raises nl = Namelist(recursive_namelist) del nl.system.ibrav with raises(AttributeError): nl.system.ibrav assert 'ibrav' not in nl.system.namelist()
def test_deleting_uknown_attribute_fails(recursive_namelist): from pytest import raises nl = Namelist(recursive_namelist) with raises(AttributeError): del nl.system.ibravi with raises(AttributeError): del nl.system._ibravi
def get_namelist(ibrav, **kwargs): from pylada.espresso import Namelist result = Namelist({'ibrav': ibrav}) for key, value in kwargs.items(): if value is not None: setattr(result, key, value) return result
def test_write_read_loop(recursive_namelist): nl = Namelist(recursive_namelist) reread = Namelist() reread.read_string(nl.write()) assert len(reread.electrons) == 0 assert reread.system.ibrav == 2 assert reread.system.occupations == 'smearing' assert reread.control.prefix == 'al'
def test_free_cell(celldim, subtitle, expected_scale): from numpy import allclose, abs, array from pylada.espresso import Card, Namelist namelist = Namelist({'ibrav': 0}) if celldim is not None: namelist.celldm = celldim card = Card('CELL_PARAMETERS', subtitle=subtitle, value=""" 1 2 3 2 3 4 4 4 6 """) cell, scale = sh._read_free(namelist, card) assert allclose(cell, array([[1, 2, 4], [2, 3, 4], [3, 4, 6]], dtype='float64')) assert abs(scale - expected_scale) < 1e-8
def test_scalar_namelist_attributes(simple_namelist, name, type_, value): from numpy import abs, allclose from pylada.misc import Sequence nl = Namelist(simple_namelist) assert hasattr(nl, name) assert isinstance(getattr(nl, name), type_) if type_ == float: assert abs(getattr(nl, name) - value) < 1e-12 elif type_ == list: assert allclose(getattr(nl, name), value, 1e-12) else: assert getattr(nl, name) == value
def test_none_arguments_do_not_appear_in_dict(simple_namelist): nl = Namelist(simple_namelist) nl.ibrav = None assert 'ibrav' not in nl.namelist()
def test_none_arguments_do_not_appear_in_dict(simple_namelist): nl = Namelist(simple_namelist) nl.ibrav = None assert 'ibrav' not in nl.namelist()
def test_set_known_attributes(recursive_namelist): nl = Namelist(recursive_namelist) nl.system.ibrav = 2 assert nl.system.ibrav == 2
def test_add_namelist_attribute(recursive_namelist): nl = Namelist(recursive_namelist) nl.system.bravasi = 2 assert nl.system.bravasi == 2 assert 'bravasi' in nl.system.namelist()
def test_add_private_attribute(recursive_namelist): nl = Namelist(recursive_namelist) nl.system._bravasi = 2 assert nl.system._bravasi == 2 assert '_bravasi' not in nl.system.namelist()
def test_empty_namelists_do_appear(recursive_namelist): nl = Namelist(recursive_namelist) assert hasattr(nl, 'electrons') assert isinstance(getattr(nl, 'electrons'), Namelist) assert len(nl.electrons) == 0
def test_recursive_namelist_attributes(recursive_namelist): nl = Namelist(recursive_namelist) assert hasattr(nl, 'system') assert isinstance(getattr(nl, 'system'), Namelist) assert getattr(nl.system, 'ibrav', 0) == 2 assert len(nl) == 3