Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
test_yaml_utils.py84 linesDownload Raw Back to yaml
1from textwrap import dedent2 3import pytest4from yaml import YAMLError5 6from core.tools.utils.yaml_utils import load_yaml_file7 8EXAMPLE_YAML_FILE = "example_yaml.yaml"9INVALID_YAML_FILE = "invalid_yaml.yaml"10NON_EXISTING_YAML_FILE = "non_existing_file.yaml"11 12 13@pytest.fixture14def prepare_example_yaml_file(tmp_path, monkeypatch) -> str:15    monkeypatch.chdir(tmp_path)16    file_path = tmp_path.joinpath(EXAMPLE_YAML_FILE)17    file_path.write_text(18        dedent(19            """\20        address:21            city: Example City22            country: Example Country23        age: 3024        gender: male25        languages:26            - Python27            - Java28            - C++29        empty_key:30        """31        )32    )33    return str(file_path)34 35 36@pytest.fixture37def prepare_invalid_yaml_file(tmp_path, monkeypatch) -> str:38    monkeypatch.chdir(tmp_path)39    file_path = tmp_path.joinpath(INVALID_YAML_FILE)40    file_path.write_text(41        dedent(42            """\43        address:44                   city: Example City45            country: Example Country46        age: 3047        gender: male48        languages:49        - Python50        - Java51        - C++52        """53        )54    )55    return str(file_path)56 57 58def test_load_yaml_non_existing_file():59    assert load_yaml_file(file_path=NON_EXISTING_YAML_FILE) == {}60    assert load_yaml_file(file_path="") == {}61 62    with pytest.raises(FileNotFoundError):63        load_yaml_file(file_path=NON_EXISTING_YAML_FILE, ignore_error=False)64 65 66def test_load_valid_yaml_file(prepare_example_yaml_file):67    yaml_data = load_yaml_file(file_path=prepare_example_yaml_file)68    assert len(yaml_data) > 069    assert yaml_data["age"] == 3070    assert yaml_data["gender"] == "male"71    assert yaml_data["address"]["city"] == "Example City"72    assert set(yaml_data["languages"]) == {"Python", "Java", "C++"}73    assert yaml_data.get("empty_key") is None74    assert yaml_data.get("non_existed_key") is None75 76 77def test_load_invalid_yaml_file(prepare_invalid_yaml_file):78    # yaml syntax error79    with pytest.raises(YAMLError):80        load_yaml_file(file_path=prepare_invalid_yaml_file, ignore_error=False)81 82    # ignore error83    assert load_yaml_file(file_path=prepare_invalid_yaml_file) == {}84