-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move projection queries snippets to github
- Loading branch information
Jerjou Cheng
committed
Apr 29, 2016
1 parent
7321446
commit 8ee47de
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_mdb_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_mdb_grouping(testbed): | ||
snippets.Article(title="one", author="Two", tags=["three"]).put() | ||
|
||
snippets.demonstrate_mdb_grouping() | ||
|
||
|
||
def test_declare_multiple_valued_property(testbed): | ||
snippets.declare_multiple_valued_property() |