Skip to content

Commit

Permalink
Move projection queries snippets to github
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng committed Apr 29, 2016
1 parent 7321446 commit 62ca2a7
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
11 changes: 11 additions & 0 deletions appengine/ndb/projection_queries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## App Engine Datastore NDB Projection Queries Samples

This contains snippets used in the NDB projection queries documentation,
demonstrating various ways to make ndb projection queries.

<!-- auto-doc-link -->
These samples are used on the following documentation page:

> https://cloud.google.com/appengine/docs/python/ndb/projectionqueries
<!-- end-auto-doc-link -->
61 changes: 61 additions & 0 deletions appengine/ndb/projection_queries/snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.ext import ndb


class Article(ndb.Model):
title = ndb.StringProperty()
author = ndb.StringProperty()
tags = ndb.StringProperty(repeated=True)


def print_author_tags():
query = Article.query()
articles = query.fetch(20, projection=[Article.author, Article.tags])
for article in articles:
print(article.author)
print(article.tags)
# article.title will raise a ndb.UnprojectedPropertyError


class Address(ndb.Model):
type = ndb.StringProperty() # E.g., 'home', 'work'
street = ndb.StringProperty()
city = ndb.StringProperty()


class Contact(ndb.Model):
name = ndb.StringProperty()
addresses = ndb.StructuredProperty(Address, repeated=True)


def fetch_sub_properties():
Contact.query().fetch(projection=["name", "addresses.city"])
Contact.query().fetch(projection=[Contact.name, Contact.addresses.city])


def demonstrate_ndb_grouping():
Article.query(projection=[Article.author], group_by=[Article.author])
Article.query(projection=[Article.author], distinct=True)


class Foo(ndb.Model):
A = ndb.IntegerProperty(repeated=True)
B = ndb.StringProperty(repeated=True)


def declare_multiple_valued_property():
entity = Foo(A=[1, 1, 2, 3], B=['x', 'y', 'x'])
return entity
46 changes: 46 additions & 0 deletions appengine/ndb/projection_queries/snippets_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import snippets


def test_print_author_tags(testbed, capsys):
snippets.Article(title="one", author="Two", tags=["three"]).put()

snippets.print_author_tags()

stdout, _ = capsys.readouterr()
assert 'Two' in stdout
assert 'three' in stdout
assert 'one' not in stdout


def test_fetch_sub_properties(testbed):
address = snippets.Address(type="home", street="college", city="staten")
address.put()
address2 = snippets.Address(type="home", street="brighton", city="staten")
address2.put()
snippets.Contact(name="one", addresses=[address, address2]).put()

snippets.fetch_sub_properties()


def test_demonstrate_ndb_grouping(testbed):
snippets.Article(title="one", author="Two", tags=["three"]).put()

snippets.demonstrate_ndb_grouping()


def test_declare_multiple_valued_property(testbed):
snippets.declare_multiple_valued_property()

0 comments on commit 62ca2a7

Please sign in to comment.