From 62ca2a7c9a0f30a05b74fb528c2b7b38843ad524 Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Fri, 29 Apr 2016 09:43:41 -0700 Subject: [PATCH] Move projection queries snippets to github --- appengine/ndb/projection_queries/README.md | 11 ++++ appengine/ndb/projection_queries/snippets.py | 61 +++++++++++++++++++ .../ndb/projection_queries/snippets_test.py | 46 ++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 appengine/ndb/projection_queries/README.md create mode 100644 appengine/ndb/projection_queries/snippets.py create mode 100644 appengine/ndb/projection_queries/snippets_test.py diff --git a/appengine/ndb/projection_queries/README.md b/appengine/ndb/projection_queries/README.md new file mode 100644 index 000000000000..e71b8ed461de --- /dev/null +++ b/appengine/ndb/projection_queries/README.md @@ -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. + + +These samples are used on the following documentation page: + +> https://cloud.google.com/appengine/docs/python/ndb/projectionqueries + + diff --git a/appengine/ndb/projection_queries/snippets.py b/appengine/ndb/projection_queries/snippets.py new file mode 100644 index 000000000000..fb0c32543511 --- /dev/null +++ b/appengine/ndb/projection_queries/snippets.py @@ -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 diff --git a/appengine/ndb/projection_queries/snippets_test.py b/appengine/ndb/projection_queries/snippets_test.py new file mode 100644 index 000000000000..e21a5c414e41 --- /dev/null +++ b/appengine/ndb/projection_queries/snippets_test.py @@ -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()