示例#1
0
 def test_init_raises_ValueError_when_passing_bad_op_and_parent(self):
     fn = lambda: xpathselect.Query(None, b'//', b'..')
     self.assertThat(
         fn,
         raises(
             InvalidXPathQuery(
                 "Operation must be CHILD while selecting a parent")))
示例#2
0
    def new_from_path_and_id(path, id):
        """Create a new Query object from a path and id.

        :param path: The full path to the node you want to construct the query
            for.
        :param id: The object id of the node you want to construct the query
            for.

        :raises TypeError: If the path attribute is not 'bytes'.
        :raises ValueError: If the path does not start with b'/'

        """
        if not isinstance(path, bytes):
            raise TypeError("'path' attribute must be bytes, not '%s'" %
                            type(path).__name__)
        nodes = list(filter(None, path.split(b'/')))
        if not path.startswith(b'/') or not nodes:
            raise InvalidXPathQuery("Invalid path '%s'." % path.decode())

        query = None
        for i, n in enumerate(nodes):
            if query is None:
                query = Query.root(n)
            else:
                if i == len(nodes) - 1:
                    query = query.select_child(n, dict(id=id))
                else:
                    query = query.select_child(n)
        return query
示例#3
0
 def test_cannot_select_child_on_pseudo_tree_root(self):
     fn = lambda: xpathselect.Query.pseudo_tree_root().select_child('foo')
     self.assertThat(
         fn,
         raises(
             InvalidXPathQuery(
                 "Cannot select children from a pseudo-tree-root query.")))
示例#4
0
 def test_init_raises_ValueError_when_passing_filters_and_parent(self):
     fn = lambda: xpathselect.Query(None, b'/', b'..', dict(foo=123))
     self.assertThat(
         fn,
         raises(
             InvalidXPathQuery(
                 "Cannot specify filters while selecting a parent")))
示例#5
0
def _try_encode_type_name(name):
    if isinstance(name, str):
        try:
            name = name.encode('ascii')
        except UnicodeEncodeError:
            raise InvalidXPathQuery("Type name '%s', must be ASCII encodable" %
                                    (name))
    return name
示例#6
0
 def test_init_raises_ValueError_on_invalid_descendant_search(self):
     fn = lambda: xpathselect.Query(None, b'//', b'*')
     self.assertThat(
         fn,
         raises(
             InvalidXPathQuery(
                 "Must provide at least one server-side filter when searching "
                 "for descendants and using a wildcard node.")))
示例#7
0
 def test_deriving_from_client_side_filtered_query_raises_ValueError(self):
     q = xpathselect.Query.root("Foo") \
         .select_descendant("Baz", dict(name="\u2026"))
     fn = lambda: q.select_child("Foo")
     self.assertThat(
         fn,
         raises(
             InvalidXPathQuery(
                 "Cannot create a new query from a parent that requires "
                 "client-side filter processing.")))
示例#8
0
    def __init__(self, parent, operation, query, filters={}):
        """Create a new query object.

        You shouldn't need to call this directly.

        :param parent: The parent query object. Pass in None to make the root
            query object.
        :param operation: The operation object to perform on the result from
            the parent node.
        :param query: The query expression for this node.
        :param filters: A dictionary of filters to apply.

        :raises TypeError: If the 'query' parameter is not 'bytes'.
        :raises TypeError: If the operation parameter is not 'bytes'.
        :raises InvalidXPathQuery: If parent is specified, and the parent
            query needs client side filter processing. Only the last query in
            the query chain can have filters that need to be executed on the
            client-side.
        :raises InvalidXPathQuery: If operation is not one of the members of
            the Query.Operation class.
        :raises InvalidXPathQuery: If the query is set to Query.WILDCARD and
            'filters' does not contain any server-side filters, and operation
            is set to Query.Operation.DESCENDANT.
        :raises InvalidXPathQuery: When 'filters' are specified while trying
            to select a parent node in the introspection tree.

        """
        if not isinstance(query, bytes):
            raise TypeError("'query' parameter must be bytes, not %s" %
                            type(query).__name__)
        if not isinstance(operation, bytes):
            raise TypeError("'operation' parameter must be bytes, not '%s'" %
                            type(operation).__name__)
        if (parent and parent.needs_client_side_filtering()):
            raise InvalidXPathQuery(
                "Cannot create a new query from a parent that requires "
                "client-side filter processing.")
        if query == Query.PARENT and filters:
            raise InvalidXPathQuery(
                "Cannot specify filters while selecting a parent")
        if query == Query.PARENT and operation != Query.Operation.CHILD:
            raise InvalidXPathQuery(
                "Operation must be CHILD while selecting a parent")
        if operation not in Query.Operation.ALL:
            raise InvalidXPathQuery("Invalid operation '%s'." %
                                    operation.decode())
        if parent and parent.server_query_bytes() == b'/':
            raise InvalidXPathQuery(
                "Cannot select children from a pseudo-tree-root query.")
        self._parent = parent
        self._operation = operation
        self._query = query
        self._server_filters = {
            k: v
            for k, v in filters.items()
            if _is_valid_server_side_filter_param(k, v)
        }
        self._client_filters = {
            k: v
            for k, v in filters.items() if k not in self._server_filters
        }
        if (operation == Query.Operation.DESCENDANT and query == Query.WILDCARD
                and not self._server_filters):
            raise InvalidXPathQuery(
                "Must provide at least one server-side filter when searching "
                "for descendants and using a wildcard node.")
示例#9
0
 def test_creating_root_query_with_unicode_app_name_raises(self):
     self.assertThat(
         lambda: xpathselect.Query.root("\u2026"),
         raises(
             InvalidXPathQuery("Type name '%s', must be ASCII encodable" %
                               ('\u2026'))))
示例#10
0
 def test_new_from_path_and_id_raises_ValueError_on_invalid_path2(self):
     fn = lambda: xpathselect.Query.new_from_path_and_id(b'/', 42)
     self.assertThat(fn, raises(InvalidXPathQuery("Invalid path '/'.")))
示例#11
0
 def test_init_raises_ValueError_on_invalid_operation(self):
     fn = lambda: xpathselect.Query(None, b'foo', b'sdf')
     self.assertThat(fn,
                     raises(InvalidXPathQuery("Invalid operation 'foo'.")))