def test_ancestor_with_composite_filter():
        key = key_module.Key("Foo", 123)
        foo = model.StringProperty("foo")
        food = model.StringProperty("food")
        query = query_module.QueryOptions(
            ancestor=key,
            filters=query_module.AND(foo == "bar", food == "barn"),
        )
        query_pb = _datastore_query._query_to_protobuf(query)

        filter_pb1 = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="foo"),
            op=query_pb2.PropertyFilter.EQUAL,
            value=entity_pb2.Value(string_value="bar"),
        )
        filter_pb2 = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="food"),
            op=query_pb2.PropertyFilter.EQUAL,
            value=entity_pb2.Value(string_value="barn"),
        )
        ancestor_pb = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="__key__"),
            op=query_pb2.PropertyFilter.HAS_ANCESTOR,
        )
        ancestor_pb.value.key_value.CopyFrom(key._key.to_protobuf())
        expected_pb = query_pb2.Query(filter=query_pb2.Filter(
            composite_filter=query_pb2.CompositeFilter(
                op=query_pb2.CompositeFilter.AND,
                filters=[
                    query_pb2.Filter(property_filter=filter_pb1),
                    query_pb2.Filter(property_filter=filter_pb2),
                    query_pb2.Filter(property_filter=ancestor_pb),
                ],
            )))
        assert query_pb == expected_pb
class KindPropertyNamePropertyTypeStat(BaseKindStatistic):
    """Statistic on (kind, property_name, property_type) tuples in Cloud
    Datastore.

    There is an instance of the KindPropertyNamePropertyTypeStat for every
    (kind, property_name, property_type) tuple in the application's datastore.

    Attributes:
      property_type (str): the property type associated with the statistic
          instance.
      property_name (str): the name of the property associated with the
          statistic instance.
      builtin_index_bytes (int): the number of bytes taken up to store
          built-in index entries
      builtin_index_count (int): the number of built-in index entries.
    """

    STORED_KIND_NAME = "__Stat_PropertyType_PropertyName_Kind__"

    property_type = model.StringProperty()

    property_name = model.StringProperty()

    builtin_index_bytes = model.IntegerProperty(default=0)

    builtin_index_count = model.IntegerProperty(default=0)
 def test_has_next_async_exhausted():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that"))
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     iterator._result_sets = []
     assert not iterator.has_next_async().result()
Beispiel #4
0
    def test_iterate_async_ordered():
        foo = model.StringProperty("foo")
        query = query_module.QueryOptions(
            filters=query_module.OR(foo == "this", foo == "that")
        )
        iterator = _datastore_query._MultiQueryIteratorImpl(query)
        iterator._sortable = True
        iterator._result_sets = [
            MockResultSet(["a", "c", "e", "g", "i"]),
            MockResultSet(["b", "d", "f", "h", "j"]),
        ]

        @tasklets.tasklet
        def iterate():
            results = []
            while (yield iterator.has_next_async()):
                results.append(iterator.next())
            raise tasklets.Return(results)

        assert iterate().result() == [
            "a",
            "b",
            "c",
            "d",
            "e",
            "f",
            "g",
            "h",
            "i",
            "j",
        ]
Beispiel #5
0
    def test_iterate_async_raw():
        foo = model.StringProperty("foo")
        query = query_module.QueryOptions(
            filters=query_module.OR(foo == "this", foo == "that")
        )
        iterator = _datastore_query._MultiQueryIteratorImpl(query, raw=True)
        iterator._result_sets = [
            MockResultSet(["a", "c", "e", "g", "i"]),
            MockResultSet(["b", "d", "f", "h", "j"]),
        ]

        @tasklets.tasklet
        def iterate():
            results = []
            while (yield iterator.has_next_async()):
                results.append(iterator.next())
            raise tasklets.Return(results)

        assert iterate().result() == [
            MockResult("a"),
            MockResult("c"),
            MockResult("e"),
            MockResult("g"),
            MockResult("i"),
            MockResult("b"),
            MockResult("d"),
            MockResult("f"),
            MockResult("h"),
            MockResult("j"),
        ]

        with pytest.raises(StopIteration):
            iterator.next()
 def test_cursor_after():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that"))
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     with pytest.raises(exceptions.BadArgumentError):
         iterator.cursor_after()
class PropertyTypeStat(BaseStatistic):
    """An aggregate of all properties across the entire application by type.

    There is an instance of the PropertyTypeStat for every property type
    (google.appengine.api.datastore_types._PROPERTY_TYPES) in use by the
    application in its datastore.

    Attributes:
        property_type (str): the property type associated with the statistic
            instance.
        entity_bytes (int): the number of bytes taken up to store the statistic
            in Cloud Datastore minus the cost of storing indices.
        builtin_index_bytes (int): the number of bytes taken up to store
        built-in index entries.
        builtin_index_count (int): the number of built-in index entries.
    """

    STORED_KIND_NAME = "__Stat_PropertyType__"

    property_type = model.StringProperty()

    entity_bytes = model.IntegerProperty(default=0)

    builtin_index_bytes = model.IntegerProperty(default=0)

    builtin_index_count = model.IntegerProperty(default=0)
Beispiel #8
0
 def test_iter():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that")
     )
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     assert iter(iterator) is iterator
 def test_index_list():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that"))
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     with pytest.raises(NotImplementedError):
         iterator.index_list()
 def test_probably_has_next_loaded():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that"))
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     iterator._next = "foo"
     assert iterator.probably_has_next()
 def test_probably_has_next_doesnt():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that"))
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     iterator._result_sets = [MockResultSet([])]
     assert not iterator.probably_has_next()
class NamespaceStat(BaseStatistic):
    """An aggregate of all entities across an entire namespace.

    This statistic has one instance per namespace.  The key_name is the
    represented namespace. NamespaceStat entities will only be found
    in the namespace "" (empty string). It contains the total
    number of entities stored and the total number of bytes they take up.

    Attributes:
        subject_namespace (str): the namespace associated with the statistic
            instance.
        entity_bytes (int): the number of bytes taken up to store the statistic
            in Cloud Datastore minus the cost of storing indices.
        builtin_index_bytes (int): the number of bytes taken up to store
            builtin-in index entries.
        builtin_index_count (int): the number of built-in index entries.
        composite_index_bytes (int): the number of bytes taken up to store
            composite index entries.
        composite_index_count (int): the number of composite index entries.
    """

    STORED_KIND_NAME = "__Stat_Namespace__"

    subject_namespace = model.StringProperty()

    entity_bytes = model.IntegerProperty(default=0)

    builtin_index_bytes = model.IntegerProperty(default=0)

    builtin_index_count = model.IntegerProperty(default=0)

    composite_index_bytes = model.IntegerProperty(default=0)

    composite_index_count = model.IntegerProperty(default=0)
 def test_has_next():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that"))
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     iterator.has_next_async = mock.Mock(
         return_value=utils.future_result("bar"))
     assert iterator.has_next() == "bar"
 def test_constructor_sortable():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         filters=query_module.OR(foo == "this", foo == "that"),
         order_by=["foo"],
     )
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     assert iterator._result_sets[0]._query == query_module.QueryOptions(
         filters=foo == "this", order_by=["foo"])
     assert iterator._result_sets[1]._query == query_module.QueryOptions(
         filters=foo == "that", order_by=["foo"])
     assert iterator._sortable
 def test_constructor():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(offset=20,
                                       limit=10,
                                       filters=foo == u"this")
     predicate = object()
     iterator = _datastore_query._PostFilterQueryIteratorImpl(
         query, predicate)
     assert iterator._result_set._query == query_module.QueryOptions(
         filters=foo == u"this")
     assert iterator._offset == 20
     assert iterator._limit == 10
     assert iterator._predicate is predicate
Beispiel #16
0
    def test_filter_pb():
        foo = model.StringProperty("foo")
        query = query_module.QueryOptions(kind="Foo", filters=(foo == "bar"))
        query_pb = _datastore_query._query_to_protobuf(query)

        filter_pb = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="foo"),
            op=query_pb2.PropertyFilter.EQUAL,
            value=entity_pb2.Value(string_value="bar"),
        )
        expected_pb = query_pb2.Query(
            kind=[query_pb2.KindExpression(name="Foo")],
            filter=query_pb2.Filter(property_filter=filter_pb),
        )
        assert query_pb == expected_pb
 def test_constructor():
     foo = model.StringProperty("foo")
     query = query_module.QueryOptions(
         offset=20,
         limit=10,
         filters=query_module.OR(foo == "this", foo == "that"),
     )
     iterator = _datastore_query._MultiQueryIteratorImpl(query)
     assert iterator._result_sets[0]._query == query_module.QueryOptions(
         filters=foo == "this")
     assert iterator._result_sets[1]._query == query_module.QueryOptions(
         filters=foo == "that")
     assert not iterator._sortable
     assert iterator._offset == 20
     assert iterator._limit == 10
class BaseKindStatistic(BaseStatistic):
    """Base Statistic Model class for stats associated with kinds.

    Attributes:
        kind_name (str): the name of the kind associated with the statistic
            instance.
        entity_bytes (int): the number of bytes taken up to store the statistic
            in Cloud Datastore minus the cost of storing indices.
    """

    STORED_KIND_NAME = "__BaseKindStatistic__"

    kind_name = model.StringProperty()

    entity_bytes = model.IntegerProperty(default=0)
class KindCompositeIndexStat(BaseStatistic):
    """Statistic on (kind, composite_index_id) tuples in Cloud Datastore.

    There is an instance of the KindCompositeIndexStat for every unique
    (kind, composite_index_id) tuple in the application's datastore indexes.

    Attributes:
        index_id (int): the id of the composite index associated with the
            statistic instance.
        kind_name (str): the name of the kind associated with the statistic
            instance.
    """

    STORED_KIND_NAME = "__Stat_Kind_CompositeIndex__"

    index_id = model.IntegerProperty()

    kind_name = model.StringProperty()
 def test_constructor():
     with pytest.raises(NotImplementedError):
         model.StringProperty()
 class SomeKind(model.Model):
     prop1 = model.StringProperty()
     prop2 = model.StringProperty()
     prop3 = model.IntegerProperty()
     prop4 = model.IntegerProperty()
 class SomeKind(model.Model):
     prop1 = model.StringProperty()
class Property(_BaseMetadata):
    """Model for __property__ metadata query results."""

    KIND_NAME = "__property__"

    @property
    def property_name(self):
        """Return the property name specified by this entity's key.

        Returns:
            str: the property name.
        """
        return self.key_to_property(self.key)

    @property
    def kind_name(self):
        """Return the kind name specified by this entity's key.

        Returns:
            str: the kind name.
        """
        return self.key_to_kind(self.key)

    property_representation = model.StringProperty(repeated=True)

    @classmethod
    def key_for_kind(cls, kind):
        """Return the __property__ key for kind.

        Args:
            kind (str): kind whose key is requested.

        Returns:
            key.Key: The parent key for __property__ keys of kind.
        """
        return model.Key(Kind.KIND_NAME, kind)

    @classmethod
    def key_for_property(cls, kind, property):
        """Return the __property__ key for property of kind.

        Args:
            kind (str): kind whose key is requested.
            property (str): property whose key is requested.

        Returns:
            key.Key: The key for property of kind.
        """
        return model.Key(Kind.KIND_NAME, kind, Property.KIND_NAME, property)

    @classmethod
    def key_to_kind(cls, key):
        """Return the kind specified by a given __property__ key.

        Args:
            key (key.Key): key whose kind name is requested.

        Returns:
            str: The kind specified by key.
        """
        if key.kind() == Kind.KIND_NAME:
            return key.id()
        else:
            return key.parent().id()

    @classmethod
    def key_to_property(cls, key):
        """Return the property specified by a given __property__ key.

        Args:
            key (key.Key): key whose property name is requested.

        Returns:
            str: property specified by key, or None if the key specified
                only a kind.
        """
        if key.kind() == Kind.KIND_NAME:
            return None
        else:
            return key.id()