Beispiel #1
0
def test_eq_empty():
    empty = Condition()
    assert empty == empty

    also_empty = Condition()
    assert empty is not also_empty
    assert empty == also_empty
Beispiel #2
0
def empty_conditions():
    return [
        Condition(),
        AndCondition(),
        OrCondition(),
        NotCondition(Condition())
    ]
Beispiel #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
Beispiel #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
Beispiel #5
0
def test_or_empty_conditions(empty):
    """When conditions are falsey (literal empty or meta with no inner value), simplify instead of nesting:
    ()_1 | ()_2 -> ()_1
    x | () -> x
    () | x -> x
    """
    also_empty = Condition()
    not_empty = condition_for(">")

    assert (empty | not_empty) is not_empty
    assert (not_empty | empty) is not_empty
    assert (empty | also_empty) is empty
    assert (also_empty | empty) is also_empty
Beispiel #6
0
def test_ior_empty_conditions(empty):
    """Similar to or, empty values don't change the non-empty values.  LHS always wins if both empty."""
    also_empty = Condition()
    not_empty = condition_for(">")

    # None of the following modify the object

    original_empty = empty
    empty |= also_empty
    assert empty is original_empty

    original_also_empty = also_empty
    also_empty |= empty
    assert also_empty is original_also_empty

    original_not_empty = not_empty
    not_empty |= empty
    assert not_empty is original_not_empty

    # The only modifying __ior__
    empty |= not_empty
    assert empty is not_empty
Beispiel #7
0
def test_invert_empty():
    """~() -> ()"""
    empty = Condition()
    assert (~empty) is empty
Beispiel #8
0
def test_render_empty(renderer):
    condition = Condition()
    condition.render(renderer)
    assert not renderer.output
Beispiel #9
0
def test_iter_empty():
    condition = Condition()
    assert set(iter_conditions(condition)) == {condition}
    assert next(iter_columns(condition), None) is None
Beispiel #10
0
def test_empty_condition():
    assert Condition().operation is None
Beispiel #11
0
def test_eq_wrong_type():
    """AttributeError returns False"""
    assert not (Condition() == object())
Beispiel #12
0
        # begins_with, contains
        (BeginsWithCondition(column=c, value=2), "begins_with(M.c, 2)"),
        (ContainsCondition(column=c, value=2), "contains(M.c, 2)"),

        # between
        (BetweenCondition(column=c, lower=2, upper=3), "(M.c between [2, 3])"),

        # in
        (InCondition(column=c, values=[]), "(M.c in [])"),
        (InCondition(column=c, values=[2, 3]), "(M.c in [2, 3])"),
        (InCondition(column=c, values=[MockColumn("d"), 3
                                       ]), "(M.c in [<MockColumn[M.d]>, 3])"),

        # empty
        (Condition(), "()")
    ])
def test_repr(condition, expected):
    assert repr(condition) == expected


# CONDITIONS EQUALITY ============================================================================ CONDITIONS EQUALITY


def test_eq_empty():
    empty = Condition()
    assert empty == empty

    also_empty = Condition()
    assert empty is not also_empty
    assert empty == also_empty