def test_object_field() -> None:
    """Object field should load correct values."""
    result = check_load(
        "field: !type:tests.fields.test_object_field._OwnedChild\n"
        "  required_field: parent_value\n"
        "  child_field: child_value\n",
        _Owner,
    )
    assert isinstance(result, _Owner)
    assert isinstance(result.field, _OwnedChild)
    assert result.field.required_field == "parent_value"
    assert result.field.child_field == "child_value"
    assert result.field.parent_validate_called
    assert result.field.parent_post_load_called
    assert result.field.child_validate_called
    assert result.field.child_post_load_called

    result = check_load("field: value\n", _Simple)
    assert hasattr(result, "field")
    assert result.field == "value"

    result = check_load("field: !fail value\n", _Simple)
    assert not hasattr(result, "field")

    result = check_load("!fail field: value\n", _Simple)
    assert not hasattr(result, "field")
def test_if_tag_handler() -> None:
    """If tag should load value only if tag is defined."""
    result = check_load(
        "!if(FLAG) test_value",
        field=StringField(),
        tag_handlers=[IfHandler()],
        config=[IfHandler.Config({"FLAG"})],
    )
    assert result == "test_value"

    result = check_load(
        "!if(UNDEFINED) test_value",
        field=StringField(),
        tag_handlers=[IfHandler()],
        config=[IfHandler.Config({"FLAG"})],
    )
    assert result == UNDEFINED

    result = check_load(
        "!if(FLAG) {key: value}",
        field=DictField(StringField()),
        tag_handlers=[IfHandler()],
        config=[IfHandler.Config({"FLAG"})],
    )
    assert result == {"key": "value"}

    result = check_load(
        "!if(FLAG) [item]",
        field=ListField(StringField()),
        tag_handlers=[IfHandler()],
        config=[IfHandler.Config({"FLAG"})],
    )

    assert result == ["item"]
def test_path_field(datadir: Path) -> None:
    """Path field should load correct values."""
    # Absolute path
    file_path = datadir / "some_file.txt"
    _check_field(str(file_path), file_path)

    # Relative path
    file_path = datadir / "file.yaml"
    with open(file_path, "r", encoding="utf-8") as yaml_file:
        result = check_load(yaml_file, _Test, location=str(file_path))
        assert result.path == datadir / "some_file.txt"

    result = check_load("path_not_checked: doesnt_exists.txt", _Test)
    assert result.path_not_checked == Path("doesnt_exists.txt")
Exemple #4
0
def test_loading_context_raises_on_multiple_tag_match() -> None:
    """Loading context should emit an error a tag is ambigous."""
    class _DummyHandler(TagHandler):
        tag_pattern = "^dummy$"

        def load(self, context: ILoadingContext, field: IBaseField) -> Node:
            return context.current_node()

    check_load(
        "!dummy value",
        str,
        expected_error=ErrorCode.MULTIPLE_MATCHING_HANDLERS,
        tag_handlers=[_DummyHandler(), _DummyHandler()],
    )
def _check_tag_error(yaml: str,
                     expected_error: Optional[ErrorCode] = None) -> None:
    result = check_load(
        yaml,
        field=StringField(),
        expected_error=expected_error,
        tag_handlers=[EnvHandler()],
    )
    assert result == UNDEFINED
Exemple #6
0
def _check_first_of_tag(yaml: str,
                        expected_value: Any = None,
                        expected_error: Optional[ErrorCode] = None) -> None:
    if expected_error is not None:
        expected_value = UNDEFINED

    result = check_load(
        yaml,
        field=StringField(),
        tag_handlers=[FirstOfHandler()],
        expected_error=expected_error,
    )

    assert result == expected_value
Exemple #7
0
def _check_merge_tag(
    field: BaseField,
    yaml: str,
    expected_value: Any = None,
    expected_error: Optional[ErrorCode] = None,
) -> None:
    if expected_error is not None:
        expected_value = UNDEFINED

    result = check_load(yaml,
                        field=field,
                        tag_handlers=[MergeHandler()],
                        expected_error=expected_error)

    assert result == expected_value
def check_path_tag_error(
    handler_type: Type[PathHandler],
    yaml: str,
    expected_error: ErrorCode,
    location: Optional[str] = None,
    allow_relative: bool = True,
    roots: Optional[List[Path]] = None,
    expected_value: Any = UNDEFINED,
) -> None:
    """Path tag hanhler inherited tags error test helper."""
    result = check_load(
        yaml,
        field=ListField(StringField()),
        expected_error=expected_error,
        location=location,
        tag_handlers=[handler_type(allow_relative=allow_relative)],
        config=[PathHandler.Config(roots)],
    )

    assert result == expected_value
def check_path_tag(
    handler_type: Type[PathHandler],
    yaml: str,
    expected_value: Any,
    allow_relative: bool = True,
    location: Optional[Path] = None,
    roots: Optional[List[Path]] = None,
) -> None:
    """Path tag hanhler inherited tags test helper."""
    result = check_load(
        yaml,
        field=ListField(StringField()),
        tag_handlers=[handler_type(allow_relative=allow_relative)],
        config=[PathHandler.Config(roots=[] if roots is None else roots, )],
        location=str(location),
    )

    # for glob tag handler.
    if isinstance(expected_value, list):
        assert sorted(result) == sorted(expected_value)
    else:
        assert result == expected_value
def _check_tag(yaml: str, expected: Any) -> None:
    result = check_load(yaml, field=StringField(), tag_handlers=[EnvHandler()])
    assert result == expected