Ejemplo n.º 1
0
def test_modify_data_group_options(testdir):
    cc = DataGroups.from_file(
        os.path.join(testdir, 'control', "control_test-energy"))
    cc.modify_data_group_options("dft", {
        "gridsize": "gridsize m5",
        "ntheta": "ntheta 30"
    })

    dg = cc.show_data_group("dft")
    mod_lines = set(s.strip() for s in dg.splitlines())
    assert mod_lines == {"gridsize m5", "ntheta 30", "functional b-p", ""}

    cc = DataGroups.from_file(
        os.path.join(testdir, 'control', "control_test-energy"))
    cc.modify_data_group_options("dft", {"gridsize": None, "ntheta": None})

    dg = cc.show_data_group("dft")
    mod_lines = set(s.strip() for s in dg.splitlines())
    assert mod_lines == {"functional b-p", ""}

    cc = DataGroups.empty()
    cc.modify_data_group_options("dft", {
        "gridsize": None,
        "ntheta": "ntheta 30"
    })
    dg = cc.show_data_group("dft")
    mod_lines = set(s.strip() for s in dg.splitlines())
    assert mod_lines == {"ntheta 30", ""}
Ejemplo n.º 2
0
def test_empty(control):
    c = DataGroups.empty()
    assert "$end" == str(c).strip()
Ejemplo n.º 3
0
def test_datagroups():
    """Testing the DataGroups object."""
    # Testing : no input
    with pytest.raises(expected_exception=ValueError,
                       match=r'^Both "string" and "dg_list" are None.$'):
        DataGroups()

    # Testing initialization
    dg = DataGroups(string=' $title\n$end')
    assert dg.initial_string == ' $title\n$end'
    assert str(dg) == '$title\n$end\n'
    assert dg.dg_list == ['$title\n', '$end\n']
    assert dg.ndg == 1

    dg = DataGroups(
        dg_list=['$title title\n', '$coord  file=coord\n', '$end\n'])
    assert dg.initial_string == '$title title\n$coord  file=coord\n$end\n'

    assert str(dg) == '$title title\n$coord  file=coord\n$end\n'

    dg = DataGroups(
        string=' $title\n$end',
        dg_list=['$title title\n', '$coord  file=coord\n', '$end\n'])
    assert dg.initial_string == ' $title\n$end'
    assert str(dg) == '$title title\n$coord  file=coord\n$end\n'

    # Testing add data group
    dg.add_data_group('scfiterlimit', '30')
    assert dg.dg_list[-2] == '$scfiterlimit 30\n'
    assert dg.initial_string == ' $title\n$end'
    assert str(dg) == '$title title\n$coord  file=coord\n' \
                      '$scfiterlimit 30\n$end\n'

    # Testing show data group
    coord_data_block = dg.show_data_group('coord')
    assert coord_data_block == '  file=coord\n'
    assert dg.show_data_group("non_existing_dg") is None
    assert dg.show_data_group("non_existing_dg",
                              default="fake_value") == "fake_value"

    # Testing kill data group
    dg.kill_data_group('coord')
    assert dg.dg_list == ['$title title\n', '$scfiterlimit 30\n', '$end\n']

    # Testing add data group with "-" and space
    dg = DataGroups.empty()
    dg.add_data_group('user-defined bonds', '    file=coord')
    assert dg.dg_list[-2] == '$user-defined bonds    file=coord\n'

    # Testing add data group with existing data group
    dg.add_data_group('scfiterlimit', '30')
    with pytest.raises(expected_exception=RuntimeError,
                       match=r'^Data group "\$scfiterlimit" already exists '
                       r'in this DataGroups object.$'):
        dg.add_data_group('scfiterlimit', '60')

    # Testing add data group with invalid name
    dg = DataGroups.empty()
    invalid_name_regex = r'^Data group should start with a letter and be ' \
                         r'followed by alphanumeric characters, a space, "-", "_", "\(" or "\)".$'
    with pytest.raises(expected_exception=ValueError,
                       match=invalid_name_regex):
        dg.add_data_group('0datagroup', '')

    with pytest.raises(expected_exception=ValueError,
                       match=invalid_name_regex):
        dg.add_data_group('data.group', '')

    # Testing show data group with strict=False
    dg = DataGroups(dg_list=[
        '$title title\n', '$coord  file=coord\n', '$scfiterlimit 30\n',
        '$scfconv 6\n', '$scfdamp   start=0.700  '
        'step=0.050  min=0.050\n', '$end\n'
    ])
    scfiter_data_block = dg.show_data_group('scfiter', strict=False)
    assert scfiter_data_block == '30\n'
    scfda_data_block = dg.show_data_group('scfda', strict=False)
    assert scfda_data_block == '  start=0.700  step=0.050  min=0.050\n'

    # Testing show data group with strict=False with multiple matches
    with pytest.raises(expected_exception=RuntimeError,
                       match=r'^Found multiple occurrences of data group '
                       r'"\$scf".$'):
        dg.show_data_group('scf', strict=False)

    # Testing change data group
    scfiterlimit_data_block = dg.show_data_group('scfiterlimit', strict=True)
    assert scfiterlimit_data_block == ' 30\n'
    dg.change_data_group('scfiterlimit', '40')
    scfiterlimit_data_block = dg.show_data_group('scfiterlimit', strict=True)
    assert scfiterlimit_data_block == ' 40\n'
    assert str(dg).endswith('step=0.050  min=0.050\n$scfiterlimit 40\n$end\n')
    # when the value is None change_data_group should remove the data group
    dg.change_data_group('scfiterlimit', None)
    assert "scfiterlimit" not in str(dg)

    # Testing as_dict/from_dict
    dg = DataGroups(dg_list=[
        '$title title\n', '$coord  file=coord\n', '$scfiterlimit 30\n',
        '$scfconv 6\n', '$scfdamp   start=0.700  '
        'step=0.050  min=0.050\n', '$end\n'
    ])
    dg_dict = dg.as_dict()
    dg_from_dict = DataGroups.from_dict(dg_dict)
    assert dg.dg_list == dg_from_dict.dg_list
    assert str(dg) == str(dg_from_dict)
    assert dg.initial_string == dg_from_dict.initial_string

    dg = DataGroups(dg_list=[
        '$title title\n', '$coord  file=coord\n', '$scfiterlimit 30\n',
        '$scfconv 6\n', '$scfdamp   start=0.700  '
        'step=0.050  min=0.050\n', '$end\n'
    ],
                    string='$title\n$end\n')
    dg_dict = dg.as_dict()
    dg_from_dict = DataGroups.from_dict(dg_dict)
    assert dg.dg_list == dg_from_dict.dg_list
    assert str(dg) == str(dg_from_dict)
    assert dg.initial_string == dg_from_dict.initial_string

    # Test to_file and from_file
    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'control_test')
        dg.to_file(filename=fname)
        dg_from_file = DataGroups.from_file(filename=fname)
        assert dg.dg_list == dg_from_file.dg_list