def test_is_key_valid(self): key = entity_pb2.Key() # Complete with name, no ancestor datastore_helper.add_key_path(key, 'kind', 'name') self.assertTrue(helper.is_key_valid(key)) key = entity_pb2.Key() # Complete with id, no ancestor datastore_helper.add_key_path(key, 'kind', 12) self.assertTrue(helper.is_key_valid(key)) key = entity_pb2.Key() # Incomplete, no ancestor datastore_helper.add_key_path(key, 'kind') self.assertFalse(helper.is_key_valid(key)) key = entity_pb2.Key() # Complete with name and ancestor datastore_helper.add_key_path(key, 'kind', 'name', 'kind2', 'name2') self.assertTrue(helper.is_key_valid(key)) key = entity_pb2.Key() # Complete with id and ancestor datastore_helper.add_key_path(key, 'kind', 'name', 'kind2', 123) self.assertTrue(helper.is_key_valid(key)) key = entity_pb2.Key() # Incomplete with ancestor datastore_helper.add_key_path(key, 'kind', 'name', 'kind2') self.assertFalse(helper.is_key_valid(key)) key = entity_pb2.Key() self.assertFalse(helper.is_key_valid(key))
def make_ancestor_query(kind, namespace, ancestor): """Creates a Cloud Datastore ancestor query. The returned query will fetch all the entities that have the parent key name set to the given `ancestor`. """ ancestor_key = entity_pb2.Key() datastore_helper.add_key_path(ancestor_key, kind, ancestor) if namespace is not None: ancestor_key.partition_id.namespace_id = namespace query = query_pb2.Query() query.kind.add().name = kind datastore_helper.set_property_filter( query.filter, '__key__', PropertyFilter.HAS_ANCESTOR, ancestor_key) return query