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_kpoints_card():
    # unknown scheme: error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.custom_sett_dict = {"kpoints": {"scheme": "monkhorst-pack"}}
    with pytest.raises(NotImplementedError):
        print(pwig.kpoints_card)
    # default scheme from presets
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.calculation_presets = "scf"
    assert pwig.kpoints_card == "K_POINTS {automatic}\n9 9 9 0 0 0"
    pwig = PwxInputGenerator(
        crystal_structure=al_fcc_struct,
        custom_sett_dict={"kpoints": {
            "scheme": "gamma"
        }},
    )
    assert pwig.kpoints_card == "K_POINTS {gamma}"
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")