Esempio n. 1
0
    def test_orWith(self):

        tests = (
            (expression.isExpression("A", "1", True),
             expression.isExpression("B", "2", True),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.isExpression("A", "1", True),
             expression.andExpression((expression.isExpression("B", "2",
                                                               True), )),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.isExpression("A", "1", True),
             expression.andExpression((
                 expression.isExpression("B", "2", True),
                 expression.isExpression("C", "3", True),
             )), "(is(A, 1, True) OR (is(B, 2, True) AND is(C, 3, True)))"),
            (expression.isExpression("A", "1", True),
             expression.orExpression((expression.isExpression("B", "2",
                                                              True), )),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.isExpression("A", "1", True),
             expression.orExpression((
                 expression.isExpression("B", "2", True),
                 expression.isExpression("C", "3", True),
             )), "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"),
            (expression.andExpression((expression.isExpression("A", "1",
                                                               True), )),
             expression.isExpression("B", "2", True),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.andExpression((
                expression.isExpression("A", "1", True),
                expression.isExpression("B", "2", True),
            )), expression.isExpression("C", "3", True),
             "((is(A, 1, True) AND is(B, 2, True)) OR is(C, 3, True))"),
            (expression.orExpression((expression.isExpression("A", "1",
                                                              True), )),
             expression.isExpression("B", "2", True),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.orExpression((
                expression.isExpression("A", "1", True),
                expression.isExpression("B", "2", True),
            )), expression.isExpression("C", "3", True),
             "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"),
        )

        for expr1, expr2, result in tests:
            self.assertEqual(str(expr1.orWith(expr2)),
                             result,
                             msg="Failed on %s" % (result, ))
Esempio n. 2
0
def compfilterExpression(compfilter, fields):
    """
    Create an expression for a single comp-filter element.

    @param compfilter: the L{ComponentFilter} element.
    @return: a L{baseExpression} for the expression tree.
    """

    # Handle is-not-defined case
    if not compfilter.defined:
        # Test for TYPE != <<component-type name>>
        return expression.isnotExpression(fields["TYPE"], compfilter.filter_name, True)

    # Determine logical expression grouping
    logical = expression.andExpression if compfilter.filter_test == "allof" else expression.orExpression

    expressions = []
    if isinstance(compfilter.filter_name, str) or isinstance(compfilter.filter_name, unicode):
        expressions.append(expression.isExpression(fields["TYPE"], compfilter.filter_name, True))
    else:
        expressions.append(expression.inExpression(fields["TYPE"], compfilter.filter_name, True))

    # Handle time-range
    if compfilter.qualifier and isinstance(compfilter.qualifier, TimeRange):
        start, end, startfloat, endfloat = getTimerangeArguments(compfilter.qualifier)
        expressions.append(expression.timerangeExpression(start, end, startfloat, endfloat))

    # Handle properties - we can only do UID right now
    props = []
    for p in [x for x in compfilter.filters if isinstance(x, PropertyFilter)]:
        props.append(propfilterExpression(p, fields))
    if len(props) > 1:
        propsExpression = logical(props)
    elif len(props) == 1:
        propsExpression = props[0]
    else:
        propsExpression = None

    # Handle embedded components - we do not right now as our Index does not handle them
    comps = []
    for _ignore in [x for x in compfilter.filters if isinstance(x, ComponentFilter)]:
        raise ValueError
    if len(comps) > 1:
        compsExpression = logical(comps)
    elif len(comps) == 1:
        compsExpression = comps[0]
    else:
        compsExpression = None

    # Now build compound expression
    if ((propsExpression is not None) and (compsExpression is not None)):
        expressions.append(logical([propsExpression, compsExpression]))
    elif propsExpression is not None:
        expressions.append(propsExpression)
    elif compsExpression is not None:
        expressions.append(compsExpression)

    # Now build return expression
    return expression.andExpression(expressions)
Esempio n. 3
0
def compfilterExpression(compfilter, fields):
    """
    Create an expression for a single comp-filter element.

    @param compfilter: the L{ComponentFilter} element.
    @return: a L{baseExpression} for the expression tree.
    """

    # Handle is-not-defined case
    if not compfilter.defined:
        # Test for TYPE != <<component-type name>>
        return expression.isnotExpression(fields["TYPE"], compfilter.filter_name, True)

    # Determine logical expression grouping
    logical = expression.andExpression if compfilter.filter_test == "allof" else expression.orExpression

    expressions = []
    if isinstance(compfilter.filter_name, str) or isinstance(compfilter.filter_name, unicode):
        expressions.append(expression.isExpression(fields["TYPE"], compfilter.filter_name, True))
    else:
        expressions.append(expression.inExpression(fields["TYPE"], compfilter.filter_name, True))

    # Handle time-range
    if compfilter.qualifier and isinstance(compfilter.qualifier, TimeRange):
        start, end, startfloat, endfloat = getTimerangeArguments(compfilter.qualifier)
        expressions.append(expression.timerangeExpression(start, end, startfloat, endfloat))

    # Handle properties - we can only do UID right now
    props = []
    for p in [x for x in compfilter.filters if isinstance(x, PropertyFilter)]:
        props.append(propfilterExpression(p, fields))
    if len(props) > 1:
        propsExpression = logical(props)
    elif len(props) == 1:
        propsExpression = props[0]
    else:
        propsExpression = None

    # Handle embedded components - we do not right now as our Index does not handle them
    comps = []
    for _ignore in [x for x in compfilter.filters if isinstance(x, ComponentFilter)]:
        raise ValueError
    if len(comps) > 1:
        compsExpression = logical(comps)
    elif len(comps) == 1:
        compsExpression = comps[0]
    else:
        compsExpression = None

    # Now build compound expression
    if ((propsExpression is not None) and (compsExpression is not None)):
        expressions.append(logical([propsExpression, compsExpression]))
    elif propsExpression is not None:
        expressions.append(propsExpression)
    elif compsExpression is not None:
        expressions.append(compsExpression)

    # Now build return expression
    return expression.andExpression(expressions)
    def test_and_query(self):

        resource = self.FakeHomeChild()
        obj = resource._objectSchema
        expr = expression.andExpression((
            expression.isExpression(obj.UID, 5678, False),
            expression.isnotExpression(obj.RESOURCE_NAME, "foobar.ics", False),
        ))
        select, args = SQLQueryGenerator(expr, resource,
                                         resource.id()).generate()
        self.assertEqual(
            select.toSQL(),
            SQLFragment(
                "select distinct RESOURCE_NAME, ICALENDAR_UID from CALENDAR_OBJECT where CALENDAR_RESOURCE_ID = ? and ICALENDAR_UID = ? and RESOURCE_NAME != ?",
                [1234, 5678, "foobar.ics"]))
        self.assertEqual(args, {})
Esempio n. 5
0
    def test_and_query(self):

        resource = self.FakeHomeChild()
        obj = resource._objectSchema
        expr = expression.andExpression((
            expression.isExpression(obj.UID, 5678, False),
            expression.isnotExpression(obj.RESOURCE_NAME, "foobar.ics", False),
        ))
        select, args = SQLQueryGenerator(expr, resource, resource.id()).generate()
        self.assertEqual(
            select.toSQL(),
            SQLFragment(
                "select distinct RESOURCE_NAME, ICALENDAR_UID from CALENDAR_OBJECT where CALENDAR_RESOURCE_ID = ? and ICALENDAR_UID = ? and RESOURCE_NAME != ?",
                [1234, 5678, "foobar.ics"]
            )
        )
        self.assertEqual(args, {})
Esempio n. 6
0
def propfilterExpression(propfilter, fields):
    """
    Create an expression for a single prop-filter element.

    @param propfilter: the L{PropertyFilter} element.
    @return: a L{baseExpression} for the expression tree.
    """

    # Only handle UID right now
    if propfilter.filter_name != "UID":
        raise ValueError

    # Handle is-not-defined case
    if not propfilter.defined:
        # Test for <<field>> != "*"
        return expression.isExpression(fields["UID"], "", True)

    # Handle embedded parameters/text-match
    params = []
    for filter in propfilter.filters:
        if isinstance(filter, TextMatch):
            if filter.match_type == "equals":
                tm = expression.isnotExpression if filter.negate else expression.isExpression
            elif filter.match_type == "contains":
                tm = expression.notcontainsExpression if filter.negate else expression.containsExpression
            elif filter.match_type == "starts-with":
                tm = expression.notstartswithExpression if filter.negate else expression.startswithExpression
            elif filter.match_type == "ends-with":
                tm = expression.notendswithExpression if filter.negate else expression.endswithExpression
            params.append(
                tm(fields[propfilter.filter_name], str(filter.text), True))
        else:
            # No embedded parameters - not right now as our Index does not handle them
            raise ValueError

    # Now build return expression
    if len(params) > 1:
        if propfilter.propfilter_test == "anyof":
            return expression.orExpression(params)
        else:
            return expression.andExpression(params)
    elif len(params) == 1:
        return params[0]
    else:
        return None
Esempio n. 7
0
def propfilterExpression(propfilter, fields):
    """
    Create an expression for a single prop-filter element.

    @param propfilter: the L{PropertyFilter} element.
    @return: a L{baseExpression} for the expression tree.
    """

    # Only handle UID right now
    if propfilter.filter_name != "UID":
        raise ValueError

    # Handle is-not-defined case
    if not propfilter.defined:
        # Test for <<field>> != "*"
        return expression.isExpression(fields["UID"], "", True)

    # Handle embedded parameters/text-match
    params = []
    for filter in propfilter.filters:
        if isinstance(filter, TextMatch):
            if filter.match_type == "equals":
                tm = expression.isnotExpression if filter.negate else expression.isExpression
            elif filter.match_type == "contains":
                tm = expression.notcontainsExpression if filter.negate else expression.containsExpression
            elif filter.match_type == "starts-with":
                tm = expression.notstartswithExpression if filter.negate else expression.startswithExpression
            elif filter.match_type == "ends-with":
                tm = expression.notendswithExpression if filter.negate else expression.endswithExpression
            params.append(tm(fields[propfilter.filter_name], str(filter.text), True))
        else:
            # No embedded parameters - not right now as our Index does not handle them
            raise ValueError

    # Now build return expression
    if len(params) > 1:
        if propfilter.propfilter_test == "anyof":
            return expression.orExpression(params)
        else:
            return expression.andExpression(params)
    elif len(params) == 1:
        return params[0]
    else:
        return None
    def test_orWith(self):

        tests = (
            (
                expression.isExpression("A", "1", True),
                expression.isExpression("B", "2", True),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.andExpression((
                    expression.isExpression("B", "2", True),
                )),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.andExpression((
                    expression.isExpression("B", "2", True),
                    expression.isExpression("C", "3", True),
                )),
                "(is(A, 1, True) OR (is(B, 2, True) AND is(C, 3, True)))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.orExpression((
                    expression.isExpression("B", "2", True),
                )),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.orExpression((
                    expression.isExpression("B", "2", True),
                    expression.isExpression("C", "3", True),
                )),
                "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"
            ),
            (
                expression.andExpression((
                    expression.isExpression("A", "1", True),
                )),
                expression.isExpression("B", "2", True),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.andExpression((
                    expression.isExpression("A", "1", True),
                    expression.isExpression("B", "2", True),
                )),
                expression.isExpression("C", "3", True),
                "((is(A, 1, True) AND is(B, 2, True)) OR is(C, 3, True))"
            ),
            (
                expression.orExpression((
                    expression.isExpression("A", "1", True),
                )),
                expression.isExpression("B", "2", True),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.orExpression((
                    expression.isExpression("A", "1", True),
                    expression.isExpression("B", "2", True),
                )),
                expression.isExpression("C", "3", True),
                "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"
            ),
        )

        for expr1, expr2, result in tests:
            self.assertEqual(str(expr1.orWith(expr2)), result, msg="Failed on %s" % (result,))