Esempio n. 1
0
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pytest

from typic.constraints import (
    MultiConstraints,
    StrConstraints,
    IntContraints,
)


@pytest.mark.parametrize(
    argnames=("constraints", "types"),
    argvalues=[
        (MultiConstraints(constraints=(StrConstraints(), IntContraints())), {str, int}),
        (
            MultiConstraints(
                constraints=(
                    MultiConstraints(constraints=(StrConstraints(), IntContraints())),
                    StrConstraints(),
                )
            ),
            {str, int},
        ),
    ],
)
def test_multi_type(constraints, types):
    assert {*constraints.type} == types


def test_empty():
Esempio n. 2
0
def test_validate_values_complex(val: str, constraint: StrConstraints,
                                 expected: str):
    assert constraint.validate(val) == expected
Esempio n. 3
0
def test_validate_values_error(val: str, constraint: StrConstraints,
                               expected: str):
    with pytest.raises(expected):
        constraint.validate(val)
Esempio n. 4
0
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import re

import pytest

from typic.constraints import StrConstraints, ConstraintValueError


@pytest.mark.parametrize(
    argnames=("val", "constraint", "expected"),
    argvalues=[
        ("", StrConstraints(), ""),
        ("foo ", StrConstraints(strip_whitespace=True), "foo"),
        ("min", StrConstraints(min_length=1), "min"),
        ("max", StrConstraints(max_length=3), "max"),
        ("cur", StrConstraints(curtail_length=2), "cu"),
        ("re", StrConstraints(regex=re.compile(r"\w+")), "re"),
    ],
)
def test_validate_values(val: str, constraint: StrConstraints, expected: str):
    assert constraint.validate(val) == expected


@pytest.mark.parametrize(
    argnames=("val", "constraint", "expected"),
    argvalues=[
        ("", StrConstraints(min_length=1), ConstraintValueError),
        ("maxi", StrConstraints(max_length=3), ConstraintValueError),
        (" ", StrConstraints(regex=re.compile(r"\w+")), ConstraintValueError),
    ],
Esempio n. 5
0
        ({1, 2}, SetContraints(max_items=2), {1, 2}),
        (frozenset([1, 2]), FrozenSetConstraints(), frozenset([1, 2])),
    ],
)
def test_validate_values(val: str, constraint: ListConstraints, expected: list):
    assert constraint.validate(val) == expected


@pytest.mark.parametrize(
    argnames=("val", "constraint", "expected"),
    argvalues=[
        ([], ListConstraints(min_items=1), ConstraintValueError),
        ([1, 2, 3], ListConstraints(max_items=2), ConstraintValueError),
        (
            (1, 2),
            TupleConstraints(values=(StrConstraints(), IntContraints())),
            ConstraintValueError,
        ),
        (
            ("foo", "bar"),
            TupleConstraints(values=(StrConstraints(), IntContraints())),
            ConstraintValueError,
        ),
    ],
)
def test_validate_values_error(
    val: str, constraint: ListConstraints, expected: Exception
):
    with pytest.raises(expected):
        constraint.validate(val)
Esempio n. 6
0
@pytest.mark.parametrize(
    argnames=("val", "constraint", "expected"),
    argvalues=[
        (EMPTY, DictConstraints(min_items=1), ConstraintValueError),
        (FOOBAR, DictConstraints(max_items=1), ConstraintValueError),
        (BAR, DictConstraints(required_keys=frozenset(("foo",))), ConstraintValueError),
        (BAR, DictConstraints(key_pattern=FPATT), ConstraintValueError),
        (
            FOOBAR,
            DictConstraints(required_keys=frozenset(("foo",)), total=True),
            ConstraintValueError,
        ),
        (
            FOOBAR,
            DictConstraints(values=StrConstraints(min_length=1)),
            ConstraintValueError,
        ),
        (
            FOOBAR,
            DictConstraints(
                items=FrozenDict({"foo": IntContraints(ge=1)}),
                values=StrConstraints(min_length=1),
            ),
            ConstraintValueError,
        ),
        (
            FOO,
            DictConstraints(patterns=FrozenDict({FPATT: StrConstraints(min_length=1)})),
            ConstraintValueError,
        ),