Esempio n. 1
0
def test_field_specializers(load_file, qvar):
    # Contains test queries.
    load_file(Path(__file__).parent / "policies/people.pol")

    # Test method ordering w/field specializers.
    assert qvar('froody(Manager{name: "Sam"}, x)', "x") == [1]
    assert qvar('froody(Manager{name: "Sam", id: 1}, x)', "x") == [2, 1]
    assert qvar(
        'froody(Manager{name: "Sam", id: 1, manager: Person{name: "Sam"}}, x)', "x"
    ) == [3, 2, 1]
Esempio n. 2
0
def test_query(load_file, polar, query):
    """Test that queries work with variable arguments"""

    load_file(Path(__file__).parent / "test_file.polar")
    # plaintext polar query: query("f(x)") == [{"x": 1}, {"x": 2}, {"x": 3}]

    assert query(Predicate(name="f", args=[Variable("a")])) == [
        {"a": 1},
        {"a": 2},
        {"a": 3},
    ]
Esempio n. 3
0
def test_groups(load_file, qeval, query):
    # Contains test queries.
    load_file(Path(__file__).parent / "policies/groups.pol")

    # Check that we can't instantiate groups.
    with pytest.raises(PolarRuntimeException):
        qeval("G{}")

    # Test rule ordering with groups.
    results = query("check_order(A{}, action)")
    expected = ["A", "G", "H"]
    assert expected == [result["action"] for result in results]
Esempio n. 4
0
def test_class_definitions(tell, qeval, load_file):
    # Contains test queries.
    load_file(Path(__file__).parent / "policies/classes.pol")

    # Test instantiation errors.
    with pytest.raises(PolarRuntimeException):
        qeval("NotADefinedClassName{foo: 1}")
    with pytest.raises(PolarRuntimeException):
        qeval("Three{foo: One{}}")
    with pytest.raises(PolarRuntimeException):
        qeval("Three{unit: Two{}}")
    with pytest.raises(PolarRuntimeException):
        qeval("Three{unit: One{}, pair: One{}}")
Esempio n. 5
0
def test_helpers(polar, load_file, query, qeval, qvar):
    load_file(Path(__file__).parent / "test_file.polar")  # f(1);
    assert query("f(x)") == [{"x": 1}, {"x": 2}, {"x": 3}]
    assert qvar("f(x)", "x") == [1, 2, 3]
Esempio n. 6
0
def test_load_file(load_file, tell, qeval, qvar):
    load_file(Path(__file__).parent / "policies/test.polar")
    assert qeval('test("true")')
    tell('b("foo")')
    assert qvar("a(x)", "x", one=True) == "foo"
Esempio n. 7
0
def test_group_field_access(load_file, qvar):
    load_file(Path(__file__).parent / "policies/groups.pol")

    assert qvar('get_bar(Baz{bar: "test"}, val)', "val", one=True) == "test"