示例#1
0
def test_model_registration():
    """Test that models are automatically registered with the policy."""
    from test_app import models
    from oso import Variable

    assert (next(
        Oso.query_rule("models", models.TestRegistration(),
                       Variable("x")))["bindings"]["x"] == 1)
    assert (next(
        Oso.query_rule("models", models.TestRegistration2(),
                       Variable("x")))["bindings"]["x"] == 2)
示例#2
0
def test_partial_unification():
    Oso.load_str("f(x, y) if x = y and x = 1;")
    results = Oso.query_rule("f",
                             Variable("x"),
                             Variable("y"),
                             accept_expression=True)
    first = next(results)["bindings"]
    assert first["x"] == 1
    assert first["y"] == 1

    with pytest.raises(StopIteration):
        next(results)

    Oso.load_str("g(x, y) if x = y and y > 1;")
    results = Oso.query_rule("g",
                             Variable("x"),
                             Variable("y"),
                             accept_expression=True)
    first = next(results)["bindings"]

    # TODO not ideal that these are swapped in order (y = x) not (x = y).
    # this is a hard case, we want the (y > 1) to be this in both cases AND keep the x = y.
    assert first["x"] == Expression(
        "And",
        [
            Expression("Unify",
                       [Variable("y"), Variable("_this")]),
            Expression("Gt", [Variable("y"), 1]),
        ],
    )
    assert first["y"] == Expression(
        "And",
        [
            Expression("Unify",
                       [Variable("_this"), Variable("x")]),
            Expression("Gt", [Variable("_this"), 1]),
        ],
    )
示例#3
0
def test_partial_unification():
    Oso.load_str("f(x, y) if x = y and x = 1;")
    results = Oso.query_rule("f",
                             Variable("x"),
                             Variable("y"),
                             accept_expression=True)
    first = next(results)["bindings"]
    assert first["x"] == 1
    assert first["y"] == 1

    with pytest.raises(StopIteration):
        next(results)

    Oso.load_str("g(x, y) if x = y and y > 1;")
    results = Oso.query_rule("g",
                             Variable("x"),
                             Variable("y"),
                             accept_expression=True)
    first = next(results)["bindings"]
    assert first["x"] == Expression("And",
                                    [Expression("Gt", [Variable("_this"), 1])])
    assert first["y"] == Expression("And",
                                    [Expression("Gt", [Variable("_this"), 1])])
示例#4
0
def test_policy_autoload():
    """Test that policies are loaded from policy directory."""
    # These rules are added by the policies in the test app.
    assert next(Oso.query_rule("policy_load_test", 1))
    assert next(Oso.query_rule("policy_load_test", 2))