Ejemplo n.º 1
0
def get_representations_of_kind(kind, start=None, end=None):
    """Return all representations of properties of kind in the specified range.

    NOTE: This function does not return unindexed properties.

    Args:
        kind: name of kind whose properties you want.
        start: only return properties >= start if start is not None.
        end: only return properties < end if end is not None.

    Returns:
        dict: map of property names to their list of representations.
    """
    # This is required for the query to find the model for __property__
    Property._fix_up_properties()

    query = query_module.Query(kind=Property._get_kind(),
                               ancestor=Property.key_for_kind(kind))
    if start is not None and start != "":
        query = query.filter(
            Property.key >= Property.key_for_property(kind, start))
    if end is not None:
        if end == "":
            return {}
        query = query.filter(
            Property.key < Property.key_for_property(kind, end))

    representations = {}
    results = query.fetch()
    for property in results:
        representations[
            property.property_name] = property.property_representation

    return representations
Ejemplo n.º 2
0
 def test_query_errors():
     # with pytest.raises(TypeError):
     #     query.Query(ancestor=
     #                 query.ParameterizedFunction('user', query.Parameter(1)))
     with pytest.raises(TypeError):
         query.Query(ancestor=42)
     # with pytest.raises(ValueError):
     #     query.Query(ancestor=model.Key('X', None))
     # with pytest.raises(TypeError):
     #     query.Query(ancestor=model.Key('X', 1), app='another')
     # with pytest.raises(TypeError):
     #     query.Query(ancestor=model.Key('X', 1), namespace='another')
     with pytest.raises(TypeError):
         query.Query(filters=42)
     with pytest.raises(TypeError):
         query.Query(orders=42)
Ejemplo n.º 3
0
def get_properties_of_kind(kind, start=None, end=None):
    """Return all properties of kind in the specified range.

    NOTE: This function does not return unindexed properties.

    Args:
        kind (str): name of kind whose properties you want.
        start (str): only return properties >= start if start is not None.
        end (str): only return properties < end if end is not None.

    Returns:
        List[str]: Property names of kind between the (optional) start and end
            values.
    """
    # This is required for the query to find the model for __property__
    Property._fix_up_properties()

    query = query_module.Query(kind=Property._get_kind(),
                               ancestor=Property.key_for_kind(kind))
    if start is not None and start != "":
        query = query.filter(
            Property.key >= Property.key_for_property(kind, start))
    if end is not None:
        if end == "":
            return []
        query = query.filter(
            Property.key < Property.key_for_property(kind, end))

    results = query.fetch()
    return [prop.property_name for prop in results]
Ejemplo n.º 4
0
    def get_query(self):
        """Create and return a Query instance.

        Returns:
            google.cloud.ndb.query.Query: A new query with values extracted
                from the processed GQL query string.
        """
        kind = self.kind()
        if kind is None:
            model_class = model.Model
        else:
            model_class = model.Model._lookup_model(kind)
            kind = model_class._get_kind()
        ancestor = None
        model_filters = list(model_class._default_filters())
        filters = self.query_filters(model_class, model_filters)
        default_options = None
        offset = self.offset()
        limit = self.limit()
        if limit < 0:
            limit = None
        keys_only = self.is_keys_only()
        if not keys_only:
            keys_only = None
        projection = self.projection()
        project = self._app
        namespace = self._namespace
        if self.is_distinct():
            distinct_on = projection
        else:
            distinct_on = None
        order_by = []
        for order in self.orderings():
            order_str, direction = order
            if direction == 2:
                order_str = "-{}".format(order_str)
            order_by.append(order_str)
        return query_module.Query(
            kind=kind,
            ancestor=ancestor,
            filters=filters,
            order_by=order_by,
            project=project,
            namespace=namespace,
            default_options=default_options,
            projection=projection,
            distinct_on=distinct_on,
            limit=limit,
            offset=offset,
            keys_only=keys_only,
        )
Ejemplo n.º 5
0
def get_namespaces(start=None, end=None):
    """Return all namespaces in the specified range.

    Args:
        start (str): only return namespaces >= start if start is not None.
        end (str): only return namespaces < end if end is not None.

    Returns:
        List[str]: Namespace names between the (optional) start and end values.
    """
    # This is required for the query to find the model for __namespace__
    Namespace._fix_up_properties()

    query = query_module.Query(kind=Namespace._get_kind())
    if start is not None:
        query = query.filter(Namespace.key >= Namespace.key_for_namespace(start))
    if end is not None:
        query = query.filter(Namespace.key < Namespace.key_for_namespace(end))

    results = query.fetch()
    return [result.namespace_name for result in results]
Ejemplo n.º 6
0
def get_kinds(start=None, end=None):
    """Return all kinds in the specified range, for the current namespace.

    Args:
        start (str): only return kinds >= start if start is not None.
        end (str): only return kinds < end if end is not None.

    Returns:
        List[str]: Kind names between the (optional) start and end values.
    """
    # This is required for the query to find the model for __kind__
    Kind._fix_up_properties()

    query = query_module.Query(kind=Kind._get_kind())
    if start is not None and start != "":
        query = query.filter(Kind.key >= Kind.key_for_kind(start))
    if end is not None:
        if end == "":
            return []
        query = query.filter(Kind.key < Kind.key_for_kind(end))

    results = query.fetch()
    return [result.kind_name for result in results]
Ejemplo n.º 7
0
 def test_constructor():
     with pytest.raises(NotImplementedError):
         query.Query()
Ejemplo n.º 8
0
 def test_constructor():
     q = query.Query(kind='Foo')
     assert q.kind == 'Foo'
     assert q.ancestor == None
     assert q.filters == None
     assert q.orders == None