예제 #1
0
def test_logical_operator():
    d1 = ObjectSet()
    d1.add_object("host", {"id": "a1", "name": "b1"})

    d2 = ObjectSet()
    d2.add_object("host", {"id": "a1", "name": "c1"})

    d3 = ObjectSet()
    d3.add_object("host", {"id": "a2", "name": "c1"})

    eq1 = EqualOperator("host.id", "a1")
    eq2 = EqualOperator("host.name", "b1")

    # AND
    andop = AndOperator([eq1, eq2])

    assert andop.op == OP.AND
    assert andop.expr() == "((host.id eq 'a1') AND (host.name eq 'b1'))"

    assert andop.eval(d1)
    assert not andop.eval(d2)

    # OR
    orop = OrOperator([eq1, eq2])
    assert orop.op == OP.OR
    assert orop.expr() == "((host.id eq 'a1') OR (host.name eq 'b1'))"

    assert orop.eval(d1)
    assert orop.eval(d2)
    assert not orop.eval(d3)
예제 #2
0
    def test(self):
        self.instance = LogicalOperator("AND", [EqualOperator("host.id", "1"), NotEqualOperator("host.path", "2")])

        self.instance.eval(None)

        assert self.instance.expr() == "((host.id eq '1') AND (host.path not_eq '2'))"
        os = ObjectSet()
        os.add_object("host", {"id": "1", "path": "2"})
        assert self.instance.render(os) == "(('1' eq '1') AND ('2' not_eq '2'))"
예제 #3
0
def test_equal_operator():
    eq = EqualOperator("host.id", "localhost")

    # the __repr__
    assert str(eq) == "operator:eq"

    assert eq.op == OP.EQ
    assert eq.expr() == "(host.id eq 'localhost')"

    d1 = ObjectSet()
    d1.add_object("host", {"id": "localhost"})
    assert eq.eval(d1)

    d2 = ObjectSet()
    d2.add_object("host", {"id": "remote"})
    assert not eq.eval(d2)

    # TODO: add some more case?
    d3 = ObjectSet()
    d3.add_object("host", {"id": 1})
    assert not eq.eval(d3)
예제 #4
0
def test_binary_operator_eval_positive():
    """
    op = eq => one of attr equals one of value

    attr = 1; value = 1; True
    attr = 1; value = [1, 2]; True
    attr = [1, 2]; value = 2; True
    attr = [1, 2]; value = [5, 1]; True

    attr = [1, 2]; value = [3, 4]; False
    """
    d1 = ObjectSet()
    d1.add_object(
        "host",
        {
            "id": 1,
        },
    )
    d2 = ObjectSet()
    d2.add_object(
        "host",
        {
            "id": [1, 2],
        },
    )

    eq1 = EqualOperator("host.id", 1)
    assert eq1.eval(d1)

    eq2 = EqualOperator("host.id", [1, 2])
    assert eq2.eval(d1)

    eq3 = EqualOperator("host.id", 2)
    assert eq3.eval(d2)

    eq4 = EqualOperator("host.id", [5, 1])
    assert eq4.eval(d2)

    eq5 = EqualOperator("host.id", [3, 4])
    assert not eq5.eval(d2)

    # IN
    d3 = ObjectSet()
    d3.add_object(
        "host",
        {
            "id": [1, 2],
        },
    )
    # one of [1,2] in [2, 4]
    eq6 = InOperator("host.id", [2, 4])
    assert eq6.eval(d3)

    # CONTAINS
    d4 = ObjectSet()
    d4.add_object(
        "host",
        {
            "id": [1, 2],
        },
    )
    # [1, 2] contains 1 of [2,4]
    eq6 = ContainsOperator("host.id", [2, 4])
    assert eq6.eval(d4)