def test_len_unpack_not(): """Even though not(not(x)) -> x shouldn't exist, its length should be the inner length""" lt, gt = conditions_for("<", ">") outer = NotCondition(lt) condition = NotCondition(outer) assert len(condition) == len(outer) == 1 # Swap inner for an AND with length 2 and_ = AndCondition(lt, gt) outer.values[0] = and_ assert len(condition) == len(outer) == len(and_) == 2
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
def empty_conditions(): return [ Condition(), AndCondition(), OrCondition(), NotCondition(Condition()) ]
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
@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 (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])"),