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
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
(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 (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):