def test_get_pseudo_names():
    # do not setup potentials: no errors
    pwig = PwxInputGenerator(crystal_structure=al_fcc_struct)
    pseudo_names = pwig._get_pseudo_names()
    assert pseudo_names == {"Al": None}
    # all `pseudo_names` provided in input: return them
    pwig = PwxInputGenerator(crystal_structure=al_fcc_struct)
    pwig.specify_potentials = True
    pwig.custom_sett_dict = {"pseudo_names": {"Al": al_pseudo}}
    assert pwig._get_pseudo_names() == {"Al": al_pseudo}
    # missing pseudos but non-existing `pseudo_dir`: error/no-op
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.specify_potentials = True
    # 1. no `pseudo_dir`: dir not specified error
    with pytest.raises(PwxInputGeneratorError, match="not specified"):
        pwig._get_pseudo_names()
    # 2. missing/wrong `pseudo_dir`: cannot listdir error
    pwig.custom_sett_dict = {"pseudo_dir": "wrong_dir"}
    with pytest.raises(PwxInputGeneratorError, match="list contents"):
        pwig._get_pseudo_names()
    # 3. all ok with correct `pseudo_dir`
    pwig.custom_sett_dict = {"pseudo_dir": pseudo_dir}
    pseudo_names = pwig._get_pseudo_names()
    assert pseudo_names == {
        "Fe": os.path.basename(fe_pseudo),
        "O": os.path.basename(o_pseudo),
    }
    # failed to find some pseudos: list of missing potentials error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.specify_potentials = True
    pwig.custom_sett_dict = {"pseudo_dir": os.path.dirname(pseudo_dir)}
    with pytest.raises(PwxInputGeneratorError, match="Fe, O"):
        pwig._get_pseudo_names()
def test_get_atomic_species_card():
    # specify_potentials = False, no pseudo_dir: no error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.specify_potentials = False
    ref_line = "O      15.99940000  None"
    ac = pwig.atomic_species_card
    assert ac.splitlines()[-1] == ref_line
    # specify_potentials = True, no pseudo_dir: error
    pwig.specify_potentials = True
    with pytest.raises(PwxInputGeneratorError):
        pwig.atomic_species_card
    # normal functionality
    pwig.custom_sett_dict.update({"pseudo_dir": pseudo_dir})
    ref_line = "O      15.99940000  {}".format(os.path.basename(o_pseudo))
    ac = pwig.atomic_species_card
    assert ac.splitlines()[-1] == ref_line
def test_control_namelist_to_str():
    # control namelist without pseudo, settings: error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.specify_potentials = True
    with pytest.raises(PwxInputGeneratorError):
        pwig._namelist_to_str("control")
    # specify_potentials = False: no error
    pwig.specify_potentials = False
    nl = pwig._namelist_to_str("control")
    assert nl == "&CONTROL\n/"
    # with no pseudo, with settings
    pwig.custom_sett_dict.update({"calculation": "scf"})
    nl = pwig._namelist_to_str("control")
    assert nl == '&CONTROL\n    calculation = "scf"\n/'
    # specify_potentials = True: throw error
    pwig.specify_potentials = True
    with pytest.raises(PwxInputGeneratorError):
        pwig._namelist_to_str("control")
    # normal functionality
    pwig.custom_sett_dict.update({"pseudo_dir": pseudo_dir})
    pwig.calculation_presets = "scf"
    control = "\n".join(feo_scf_in.splitlines()[:7])
    assert pwig._namelist_to_str("control") == control
def test_write_input_files():
    import tempfile

    _tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=True)
    filename = _tmp_file.name
    write_location = os.path.dirname(filename)
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.calculation_presets = "scf"
    pwig.specify_potentials = True
    pwig.custom_sett_dict["pseudo_dir"] = pseudo_dir
    pwig.write_location = write_location
    pwig.pwx_input_file = filename
    pwig.write_input_files()
    with open(filename, "r") as fr:
        assert fr.read() == feo_scf_in.rstrip("\n")
def test_write_pwx_input():
    # no input settings: error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    with pytest.raises(PwxInputGeneratorError, match="input settings"):
        pwig.write_pwx_input()
    # no `write_location` input: error
    pwig.calculation_presets = "scf"
    with pytest.raises(PwxInputGeneratorError, match="Location to write"):
        pwig.write_pwx_input()
    # no input filename: error
    with pytest.raises(PwxInputGeneratorError, match="file to write"):
        pwig.write_pwx_input(write_location="/path/to/write_location")

    # all ok
    import tempfile

    _tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=True)
    filename = _tmp_file.name
    write_location = os.path.dirname(filename)
    pwig.specify_potentials = True
    pwig.custom_sett_dict = {"pseudo_dir": pseudo_dir}
    pwig.write_pwx_input(write_location=write_location, filename=filename)
    with open(filename, "r") as fr:
        assert fr.read() == feo_scf_in.rstrip("\n")