Skip to content

Commit

Permalink
tests: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 4, 2024
1 parent 92a2946 commit 206a665
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 95 deletions.
34 changes: 34 additions & 0 deletions tests/test_yaml_dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

import pytest

import yamling


def test_dump_yaml():
data = {"a": 1, "b": [2, 3, 4], "c": {"d": 5}}
dumped = yamling.dump_yaml(data)
assert yamling.load_yaml(dumped) == data


def test_class_mapping():
from collections import OrderedDict

data = OrderedDict([("b", 2), ("a", 1)])
# Test with OrderedDict mapping using dict's representation
dumped = yamling.dump_yaml(data, class_mappings={OrderedDict: dict})
assert "!!" not in dumped
# Test without mapping (default OrderedDict representation)
dumped_no_mapping = yamling.dump_yaml(data)
expected_no_mapping = (
"!!python/object/apply:collections.OrderedDict\n"
"- - - b\n"
" - 2\n"
" - - a\n"
" - 1\n"
)
assert dumped_no_mapping == expected_no_mapping


if __name__ == "__main__":
pytest.main([__file__])
112 changes: 112 additions & 0 deletions tests/test_yaml_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING

import pytest
import yaml
import yaml_include

import yamling
from yamling import load


if TYPE_CHECKING:
import pathlib


def test_basic_load():
assert yamling.load_yaml("foo: bar") == {"foo": "bar"}
assert yamling.load_yaml("[1, 2, 3]") == [1, 2, 3]
assert yamling.load_yaml("42") == 42 # noqa: PLR2004


def test_load_modes():
yaml_str = "!!python/name:os.system"
with pytest.raises(yaml.constructor.ConstructorError):
yamling.load_yaml(yaml_str, mode="safe")
assert yamling.load_yaml(yaml_str, mode="unsafe") is os.system


def test_env_tag():
os.environ["TEST_VAR"] = "42"
assert yamling.load_yaml("!ENV TEST_VAR") == 42 # noqa: PLR2004
assert yamling.load_yaml("!ENV [NONEXISTENT]") is None
assert yamling.load_yaml("!ENV [NONEXISTENT, 'default']") == "default"


@pytest.fixture
def temp_yaml_file(tmp_path: pathlib.Path) -> pathlib.Path:
content = "test: value"
file_path = tmp_path / "test.yaml"
file_path.write_text(content)
return file_path


def test_include_constructor(temp_yaml_file: pathlib.Path):
yaml_str = f"!include {temp_yaml_file!s}"
result = yamling.load_yaml(yaml_str)
assert result == {"test": "value"}


def test_invalid_yaml():
with pytest.raises(yamling.YAMLError):
yamling.load_yaml("{invalid: yaml: content")


def test_empty_yaml():
assert yamling.load_yaml("") is None
assert yamling.load_yaml(" ") is None


def test_safe_loader():
loader = load.get_safe_loader(yaml.SafeLoader)
assert loader.yaml_constructors["!relative"] is not None


def test_get_include_constructor():
"""Test get_include_constructor with different filesystem types."""
constructor = load.get_include_constructor()
assert isinstance(constructor, yaml_include.Constructor)

constructor = load.get_include_constructor(fs="file")
assert isinstance(constructor, yaml_include.Constructor)

with pytest.raises(TypeError):
load.get_include_constructor(fs=123) # Invalid type


def test_get_loader():
"""Test get_loader with different configurations."""
loader = load.get_loader(yaml.SafeLoader)
assert loader.yaml_constructors["!include"] is not None
assert loader.yaml_constructors["!ENV"] is not None

loader = load.get_loader(yaml.SafeLoader, enable_include=False)
assert "!include" not in loader.yaml_constructors

loader = load.get_loader(yaml.SafeLoader, enable_env=False)
assert "!ENV" not in loader.yaml_constructors


def test_load_yaml_with_include(tmp_path: pathlib.Path):
"""Test load_yaml with include path."""
include_file = tmp_path / "include.yaml"
include_file.write_text("included: true")

yaml_str = f"!include {include_file!s}"
result = yamling.load_yaml(yaml_str, include_base_path=tmp_path)
assert result == {"included": True}


def test_load_yaml_with_modes():
"""Test load_yaml with different modes."""
yaml_str = "!!python/name:os.system"
with pytest.raises(yaml.constructor.ConstructorError):
yamling.load_yaml(yaml_str, mode="safe")

assert yamling.load_yaml(yaml_str, mode="unsafe") is os.system


if __name__ == "__main__":
pytest.main([__file__])
95 changes: 0 additions & 95 deletions tests/test_yamltools.py

This file was deleted.

0 comments on commit 206a665

Please sign in to comment.