示例#1
0
    def test_multi_partial_dependency_simple_all_satisifed(self):
        requires = Requires(R("x") == 1, "y") + Requires(R("x") == 2, "z")

        data = {"x": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 1, "y": "hello"}
        requires.validate(data)

        data = {"x": 2, "y": "hello"}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 2, "z": "hello"}
        requires.validate(data)

        data = {"x": 2}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 2, "y": "hello"}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 2, "z": "hello"}
        requires.validate(data)
示例#2
0
    def test_hybrid_with_two_r_expressions_add(self):
        requires = Requires("x", R("y") + R("x") == 1)
        data = {"x": 1, "y": 0}
        requires.validate(data)

        data = {"x": 1, "y": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#3
0
    def test_hybrid_with_complex_sub_both_sides(self):
        req = R("x") - R("y") == 0
        requires = Requires("x", req)
        data = {"x": 1, "y": 1}
        requires.validate(data)

        data = {"x": -1, "y": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#4
0
    def test_hybrid_with_complex_add_both_sides(self):
        req = R("x") + 1 == R("y") + 2
        requires = Requires("x", req)
        data = {"x": 1, "y": 0}
        requires.validate(data)

        data = {"x": 1, "y": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#5
0
    def test_field_required_in_another_field(self):
        requires = Requires(R("x"), R("x").in_(R("y")))
        data = {"x": 1, "y": []}

        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 1, "y": [1, 2]}
        requires.validate(data)
示例#6
0
    def test_hybrid_length_both_sides(self):
        requires = Requires("x", R("x").length() + 1 == R("y").length() - 2)

        data = {"x": [1, 1], "y": [1, 2]}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": [1], "y": [1, 1, 1, 1]}
        requires.validate(data)
示例#7
0
    def test_self_dependency_complex_partial(self):
        requires = Requires(R("x") == 1, R("x") / 1 == 2)
        data = {"x": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)

        requires = Requires(R("x") + 1 == 1, R("x") + 1 / 1 == 1)
        data = {"x": 0}
        requires.validate(data)
示例#8
0
    def test_hybrid_with_complex_pow_both_sides(self):
        req = R("x")**2 == R("y")**4
        requires = Requires("x", req)
        data = {"x": 1, "y": 1}
        requires.validate(data)

        data = {"x": 2, "y": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#9
0
    def test_hybrid_with_complex_div_both_sides(self):
        req = R("x") == R("y") / 2
        requires = Requires("x", req)
        data = {"x": 4, "y": 8}
        requires.validate(data)

        data = {"x": 2, "y": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#10
0
    def test_complex_multi_val_dep_with_function_gte(self):
        requires = Requires(R("x"),
                            Func(len, R("y")) + Func(len, R("z")) > R("x"))

        with pytest.raises(RequirementError):
            data = {"x": 10, "y": [1, 1], "z": [2]}
            requires.validate(data)

        data = {"x": 2, "y": [1, 1], "z": [2]}
        requires.validate(data)
示例#11
0
    def test_complex_multi_val_dep_simple(self):

        requires = Requires(R("x"), R("y") + R("z") == R("x"))

        with pytest.raises(RequirementError):
            data = {"x": 2, "y": 2, "z": 2}
            requires.validate(data)

        data = {"x": 2, "y": 1, "z": 1}
        requires.validate(data)
示例#12
0
    def test_hybrid_with_one_r_expression_add(self):
        requires = Requires("x", R("x") < R("y") + 1)
        data = {"x": 0.5, "y": 0}
        requires.validate(data)

        data = {"x": 1, "y": 1}
        requires.validate(data)

        data = {"x": 2, "y": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#13
0
    def test_required_decorator_works_with_mixed_args_keyword_only(self):
        requires = Requires("x", R("x") > R("y"))

        @validate(requires)
        def somefunction(x, *, y):
            return x, y

        with pytest.raises(RequirementError):
            somefunction(1, y=2)

        somefunction(2, y=1)
示例#14
0
    def test_required_decorator_works_when_called_with_args_kwargs(self):
        requires = Requires("x", R("x") > R("y"))

        @validate(requires)
        def somefunction(x, y):
            return x, y

        with pytest.raises(RequirementError):
            somefunction(10, y=20)

        somefunction(20, y=10)
示例#15
0
    def test_required_decorator_works_with_keyword_only_with_defaults(self):
        requires = Requires("x", R("x") > R("y"))

        @validate(requires)
        def somefunction(*, x=0, y=0):
            return x, y

        with pytest.raises(RequirementError):
            somefunction()

        somefunction(x=2, y=1)
示例#16
0
    def test_required_decorator_works_with_defaults(self):
        requires = Requires("x", R("x") > R("y"))

        @validate(requires)
        def somefunction(x=1, y=2):
            return x, y

        with pytest.raises(RequirementError):
            somefunction()

        somefunction(y=0)
        somefunction(x=3)
示例#17
0
    def test_partial_dependency_simple_in(self):
        req = R("y").in_([1])
        requires = Requires(R("x") == 1, req)
        data = {"x": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 1, "y": 2}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 1, "y": 1}
        requires.validate(data)
示例#18
0
    def test_multi_expression_dependency_correctly_validates(self):
        requires = Requires("x", R("y") >= 1) + Requires("z", R("y") <= 1)

        data = {"x": 1, "y": 2, "z": 1}

        with pytest.raises(RequirementError):
            requires._validate("z", data)

        requires._validate("x", data)
        requires._validate("y", data)

        data = {"x": 1, "y": 1, "z": 1}
        requires._validate("z", data)
示例#19
0
    def test_required_decorator_works_when_called_with_star(self):
        requires = Requires("x", R("x") > R("y"))

        @validate(requires)
        def somefunction(x, y):
            return x, y

        with pytest.raises(RequirementError):
            somefunction(*(11, 21))

        with pytest.raises(RequirementError):
            somefunction(11 * (21, ))

        somefunction(*(21, 11))
示例#20
0
    def test_multi_r_in_expression_correctly_validates(self):
        requires = Requires("x",
                            R("y") >= R("x")) + Requires(
                                "z",
                                R("y") <= R("z"))

        data = {"x": 1, "y": 2, "z": 1}
        requires._validate("x", data)
        requires._validate("y", data)
        with pytest.raises(RequirementError):
            requires._validate("z", data)

        data = {"x": 1, "y": 2, "z": 3}
        requires.validate(data)
示例#21
0
    def test_partial_dependency_with_full(self):
        req = R("y") == 1
        partial = Requires(R("x") == 1, req)
        full = Requires("x", "z")
        requires = partial + full

        data = {"x": 10}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 10, "z": 5}
        requires.validate(data)

        data = {"x": 1, "z": 5}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#22
0
    def test_simple_expression_dependency_equal(self):
        requires = Requires("x", R("y") == 1)
        data = {"x": 1, "y": 2}
        with pytest.raises(RequirementError):
            requires._validate("x", data)

        data = {"x": 1, "y": 1}
        requires._validate("x", data)
示例#23
0
    def test_partial_dependency_simple_present(self):
        requires = Requires(R("x") == 1, "y")
        data = {"x": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 1, "y": "hello"}
        requires.validate(data)
示例#24
0
    def test_simple_length_equals(self):
        requires = Requires("x", R("x").length() == 1)
        data = {"x": []}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": [1]}
        requires.validate(data)
示例#25
0
    def test_simple_self_dependency(self):
        requires = Requires("x", R("x") > 1)
        data = {"x": 1}
        with pytest.raises(RequirementError):
            requires.validate(data)

        data = {"x": 2}
        requires.validate(data)
示例#26
0
    def test_r_in_method_checks_value_in_single_list(self):
        req = R("y").in_([1])
        requires = Requires("x", req)
        data = {"x": 4, "y": 1}
        requires.validate(data)

        data = {"x": 2, "y": 10}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#27
0
    def test_expression_partial_dependency(self):
        partial = ((R("x")**2) * 10) == 40
        requires = Requires(partial, "y")

        data = {"x": 10}
        requires.validate(data)

        data = {"x": 2}
        with pytest.raises(RequirementError):
            requires.validate(data)
示例#28
0
    def test_partial_dependency_greater_than(self):
        requires = Requires(R("x") > 1,
                            R("x") == R("y")) + Requires(
                                R("x") < 1,
                                R("x") != R("y"))

        with pytest.raises(RequirementError):
            data = {"x": 2, "y": 1}
            requires.validate(data)

        with pytest.raises(RequirementError):
            data = {"x": 0, "y": 0}
            requires.validate(data)

        data = {"x": -1, "y": 0}
        requires.validate(data)
示例#29
0
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import pytest

from required import Requires, R, Func
from required.dsl import build_requirements_factory, init_transformer, init_parser

def custom_f(x):
    return x

statement_matches = [
    ("x -> y", Requires("x", "y"), {}),
    ("x -> x > (y + 1)", Requires("x", R("x") > R("y") + 1), {}),
    ("x -> x > y", Requires("x", R("x") > R("y")), {}),
    ("x -> x < y", Requires("x", R("x") < R("y")), {}),
    ("x -> len(x) < y", Requires("x", Func(len, R("x")) < R("y")), {}),
    ("x -> x in y", Requires("x", R("x").in_(R("y"))), {}),
    ("len(x) == 0 -> y", Requires(Func(len, R("x")) == 0, "y"), {}),
    ("len(x) > 0 -> y", Requires(Func(len, R("x")) > 0, "y"), {}),
    ("abs(x) > 0 -> y", Requires(Func(abs, R("x")) > 0, "y"), {}),
    ("x > 0 -> x > y", Requires(R("x") > 0, R("x") > R("y")), {}),
    ("x -> (len(x) + 1) < y", Requires("x", Func(len, R("x")) + 1 < R("y")), {}),
    ("x -> (len(x) + 1) < y", Requires("x", Func(len, R("x")) + 1 < R("y")), {}),
    ("x -> (len(y) + len(z)) < x", Requires("x", Func(len, R("y")) + Func(len, R("z")) < R("x")), {}),
    (
        "x -> y; x -> z > y",
        Requires("x", "y") + Requires("x", R("z") > R("y")),
        {}
    ),
    (
示例#30
0
    def test_non_shorthand(self):
        requires = Requires(R("x"), R("y"))

        with pytest.raises(RequirementError):
            data = {"x": 2}
            requires.validate(data)