Esempio n. 1
0
def test_repeat_as_as_any_of():
    with pytest.raises(QueryValidatorStructureError):
        Query.has("test").as_(str).as_any_of({int, str})

    with pytest.raises(QueryValidatorStructureError):
        Query.has("test").as_any_of({int, str}).as_(str)

    with pytest.raises(QueryValidatorStructureError):
        Query.can_have("test").as_(str).as_any_of({int, str})

    with pytest.raises(QueryValidatorStructureError):
        Query.can_have("test").as_any_of({int, str}).as_(str)
Esempio n. 2
0
def test_has_as_any_of_and():
    valid = Query.has("test").as_any_of({str, int
                                         }).and_("dog").as_any_of({str, int})

    with pytest.raises(QueryValidationError):
        valid({})

    with pytest.raises(QueryValidationError):
        valid({"test": 0.0})

    with pytest.raises(QueryValidationError):
        valid({"dog": 0.0})

    with pytest.raises(QueryValidationError):
        valid({"test": "test"})

    with pytest.raises(QueryValidationError):
        valid({"test": 0})

    with pytest.raises(QueryValidationError):
        valid({"dog": "cat"})

    with pytest.raises(QueryValidationError):
        valid({"dog": 0})

    assert valid({"test": "test", "dog": "cat"})
    assert valid({"test": "test", "dog": 0})
    assert valid({"test": 0, "dog": "cat"})
    assert valid({"test": 0, "dog": 0})
Esempio n. 3
0
def test_has_or():
    valid = Query.has("test").or_("dog").or_("foo")

    with pytest.raises(QueryValidationError):
        valid({})

    assert valid({"test": "test"})
    assert valid({"dog": "cat"})
    assert valid({"foo": "bar"})
    assert valid({"test": 0})
    assert valid({"test": "test", "dog": "cat", "foo": "bar"})
Esempio n. 4
0
def test_has():
    valid = Query.has("test")

    with pytest.raises(QueryValidationError):
        valid({})

    with pytest.raises(QueryValidationError):
        valid({"dog": "cat"})

    assert valid({"test": "test"})
    assert valid({"test": 0})
    assert valid({"test": "test", "dog": "cat"})
Esempio n. 5
0
def test_has_nested_or_and():
    valid = Query.has("test").or_("cat").and_("dog")

    with pytest.raises(QueryValidationError):
        valid({})

    with pytest.raises(QueryValidationError):
        valid({"dog": "cat"})

    with pytest.raises(QueryValidationError):
        valid({"cat": "dog"})

    assert valid({"test": "test"})
    assert valid({"dog": "cat", "cat": "dog"})
Esempio n. 6
0
def test_has_as_or():
    valid = Query.has("test").as_(str).or_("dog").as_(int)

    with pytest.raises(QueryValidationError):
        valid({})

    with pytest.raises(QueryValidationError):
        valid({"test": 0})

    with pytest.raises(QueryValidationError):
        valid({"dog": "cat"})

    assert valid({"test": "test"})
    assert valid({"dog": 0})
    assert valid({"test": "test", "dog": 0})
Esempio n. 7
0
def test_validate_decorator():
    def pre_transform(query):
        if "test0" in query:
            query["test1"] = query["test0"]

    def pre_transform2(query):
        if "test1" in query:
            query["test2"] = int(query["test1"])

    validator = Query.has("test1").as_(str).also.has("test2").as_(int)

    @validate_query(validator, pre_transform, pre_transform2)
    def get(self, query, context=None):
        return query["test2"]

    with pytest.raises(QueryValidationError):
        get(None, {"cat": "dog"})

    with pytest.raises(ValueError):
        get(None, {"test1": "one"})

    assert get(None, {"test0": "1"}) == 1
Esempio n. 8
0
def test_bad_default():
    with pytest.raises(QueryValidatorStructureError):
        Query.has("test").with_default("test")
Esempio n. 9
0
def test_repeat_also():
    with pytest.raises(QueryValidatorStructureError):
        Query.has("test").also.also
Esempio n. 10
0
def test_repeat_have_can_have():
    with pytest.raises(QueryValidatorStructureError):
        Query.has("test").can_have("dog")

    with pytest.raises(QueryValidatorStructureError):
        Query.can_have("test").has("dog")