Пример #1
0
def test_or_both_or():
    """(a | b) | (c | d) -> (a | b | c | d)"""
    a, b, c, d = [condition_for(">") for _ in range(4)]
    left = OrCondition(a, b)
    right = OrCondition(c, d)

    assert (left | right).operation == "or"

    assert (left | right).values == [a, b, c, d]
    assert (right | left).values == [c, d, a, b]
Пример #2
0
def test_ior_both_or():
    """other's conditions are appended to self's conditions"""
    a, b, c, d = [condition_for(">") for _ in range(4)]
    left = OrCondition(a, b)
    right = OrCondition(c, d)

    original_left = left
    left |= right
    assert left is original_left
    assert left.values == [a, b, c, d]
    assert right.values == [c, d]
Пример #3
0
def conditions_for(classes, include=None, exclude=None):
    """Returns lambdas that take column"""
    value = "value"
    values = ["0", "1", "2"]
    condition_lambdas = []
    if BeginsWithCondition in classes:
        condition_lambdas.append(
            lambda column: BeginsWithCondition(column, value))
    if BetweenCondition in classes:
        condition_lambdas.append(
            lambda column: BetweenCondition(column, values[0], values[1]))
    if ComparisonCondition in classes:
        condition_lambdas.extend(
            comparisons_for(include=include, exclude=exclude))
    if Condition in classes:
        condition_lambdas.append(lambda column: Condition())
    if ContainsCondition in classes:
        condition_lambdas.append(
            lambda column: ContainsCondition(column, value))
    if InCondition in classes:
        condition_lambdas.append(lambda column: InCondition(column, values))

    # Meta Conditions
    if AndCondition in classes:
        condition_lambdas.append(
            lambda column: AndCondition(column == value, column != value))
    if OrCondition in classes:
        condition_lambdas.append(
            lambda column: OrCondition(column == value, column != value))
    if NotCondition in classes:
        condition_lambdas.append(lambda column: NotCondition(column == value))

    return condition_lambdas
Пример #4
0
def conditions_for(*operations, column=None):
    column = column or MockColumn("c")
    value = 0
    values = [1, 2]
    conditions = []
    if None in operations:
        conditions.append(Condition())
    if "and" in operations:
        left = ComparisonCondition("==", column, value)
        right = ComparisonCondition("!=", column, value)
        conditions.append(AndCondition(left, right))
    if "or" in operations:
        left = ComparisonCondition("==", column, value)
        right = ComparisonCondition("!=", column, value)
        conditions.append(OrCondition(left, right))
    if "not" in operations:
        inner = ComparisonCondition("==", column, value)
        conditions.append(NotCondition(inner))
    if "begins_with" in operations:
        conditions.append(BeginsWithCondition(column, value))
    if "between" in operations:
        conditions.append(BetweenCondition(column, *values))
    if "contains" in operations:
        conditions.append(ContainsCondition(column, value))
    if "in" in operations:
        conditions.append(InCondition(column, values))
    for operation in ("<", "<=", ">", ">=", "!=", "=="):
        if operation in operations:
            conditions.append(ComparisonCondition(operation, column, value))
    return conditions
Пример #5
0
def empty_conditions():
    return [
        Condition(),
        AndCondition(),
        OrCondition(),
        NotCondition(Condition())
    ]
Пример #6
0
def test_or_simplifies(other):
    """When only one condition is an or, the other is put in a new or, in the correct place
    (a | b) | (c > 2) -> (a | b | (c > 2))
    (a > 2) | (b | c) -> ((a > 2) | b | c)
    """
    a, b, = [condition_for(">"), condition_for("<")]
    or_condition = OrCondition(a, b)

    assert (or_condition | other).operation == "or"

    assert (or_condition | other).values == [a, b, other]
    assert (other | or_condition).values == [other, a, b]
Пример #7
0
def test_ior_simplifies(other):
    """Similar to or, other value is pushed into the or (on LHS) or front of a new or (on RHS)"""
    a, b, = [condition_for(">"), condition_for("<")]
    or_condition = OrCondition(a, b)

    original_other = other
    other |= or_condition
    assert other is not original_other
    assert other.values == [original_other, a, b]

    original_or_condition = or_condition
    or_condition |= original_other
    assert or_condition is original_or_condition
    assert or_condition.values == [a, b, original_other]
Пример #8
0
def test_len_cyclic():
    """Cyclic conditions count the cyclic reference"""
    # Here's the structure to create:
    #   root
    #  /    \
    # a      b
    #      /   \
    #     c   root
    root = AndCondition()
    a = ComparisonCondition("<", MockColumn("a"), 3)
    b = OrCondition()
    c = ComparisonCondition(">", MockColumn("c"), 3)
    root.values.extend([a, b])
    b.values.extend([c, root])

    assert len(root) == 4
Пример #9
0
def test_iter_conditions_cyclic():
    """Cyclic conditions can be iterated safely"""
    # Here's the structure to create:
    #   root
    #  /    \
    # a      b
    #      /   \
    #     c   root
    root = AndCondition()
    a = ComparisonCondition("<", MockColumn("a"), 3)
    b = OrCondition()
    c = ComparisonCondition(">", MockColumn("c"), 3)
    root.values.extend([a, b])
    b.values.extend([c, root])

    expected = {root, a, b, c}
    actual = set(iter_conditions(root))
    assert actual == expected
Пример #10
0
    assert b.values == [original_b, original_a]


# CONDITIONS REPR ==================================================================================== CONDITIONS REPR


@pytest.mark.parametrize(
    "condition, expected",
    [
        # and
        (AndCondition(), "( & )"),
        (AndCondition("foo"), "('foo' &)"),
        (AndCondition("a", "b", "c"), "('a' & 'b' & 'c')"),

        # or
        (OrCondition(), "( | )"),
        (OrCondition("foo"), "('foo' |)"),
        (OrCondition("a", "b", "c"), "('a' | 'b' | 'c')"),

        # not
        (NotCondition("a"), "(~'a')"),

        # comparisons
        (ComparisonCondition("<", column=c, value=3), "(M.c < 3)"),
        (ComparisonCondition(">", column=c, value=3), "(M.c > 3)"),
        (ComparisonCondition("<=", column=c, value=3), "(M.c <= 3)"),
        (ComparisonCondition(">=", column=c, value=3), "(M.c >= 3)"),
        (ComparisonCondition("==", column=c, value=3), "(M.c == 3)"),
        (ComparisonCondition("!=", column=c, value=3), "(M.c != 3)"),

        # begins_with, contains