-
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.
Merge pull request #296 from GoogleCloudPlatform/ndb-queries
Move in ndb queries code snippets & tests
- Loading branch information
Showing
10 changed files
with
841 additions
and
4 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
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
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
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
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 Queries Samples | ||
|
||
This contains snippets used in the NDB queries documentation, demonstrating | ||
various ways to make ndb queries. | ||
|
||
<!-- auto-doc-link --> | ||
These samples are used on the following documentation page: | ||
|
||
> https://cloud.google.com/appengine/docs/python/ndb/queries | ||
<!-- 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,75 @@ | ||
# 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 cgi | ||
|
||
from google.appengine.datastore.datastore_query import Cursor | ||
from google.appengine.ext import ndb | ||
import webapp2 | ||
|
||
|
||
class Greeting(ndb.Model): | ||
"""Models an individual Guestbook entry with content and date.""" | ||
content = ndb.StringProperty() | ||
date = ndb.DateTimeProperty(auto_now_add=True) | ||
|
||
@classmethod | ||
def query_book(cls, ancestor_key): | ||
return cls.query(ancestor=ancestor_key).order(-cls.date) | ||
|
||
|
||
class MainPage(webapp2.RequestHandler): | ||
GREETINGS_PER_PAGE = 20 | ||
|
||
def get(self): | ||
guestbook_name = self.request.get('guestbook_name') | ||
ancestor_key = ndb.Key('Book', guestbook_name or '*notitle*') | ||
greetings = Greeting.query_book(ancestor_key).fetch( | ||
self.GREETINGS_PER_PAGE) | ||
|
||
self.response.out.write('<html><body>') | ||
|
||
for greeting in greetings: | ||
self.response.out.write( | ||
'<blockquote>%s</blockquote>' % cgi.escape(greeting.content)) | ||
|
||
self.response.out.write('</body></html>') | ||
|
||
|
||
class List(webapp2.RequestHandler): | ||
GREETINGS_PER_PAGE = 10 | ||
|
||
def get(self): | ||
"""Handles requests like /list?cursor=1234567.""" | ||
cursor = Cursor(urlsafe=self.request.get('cursor')) | ||
greets, next_cursor, more = Greeting.query().fetch_page( | ||
self.GREETINGS_PER_PAGE, start_cursor=cursor) | ||
|
||
self.response.out.write('<html><body>') | ||
|
||
for greeting in greets: | ||
self.response.out.write( | ||
'<blockquote>%s</blockquote>' % cgi.escape(greeting.content)) | ||
|
||
if more and next_cursor: | ||
self.response.out.write('<a href="/list?cursor=%s">More...</a>' % | ||
next_cursor.urlsafe()) | ||
|
||
self.response.out.write('</body></html>') | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
('/', MainPage), | ||
('/list', List), | ||
], debug=True) |
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,66 @@ | ||
# 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 re | ||
|
||
from google.appengine.ext import ndb | ||
import guestbook | ||
import pytest | ||
import webtest | ||
|
||
|
||
@pytest.fixture | ||
def app(testbed): | ||
return webtest.TestApp(guestbook.app) | ||
|
||
|
||
def test_main(app): | ||
# Add a greeting to find | ||
guestbook.Greeting( | ||
content='Hello world', | ||
parent=ndb.Key('Book', 'brane3')).put() | ||
|
||
# Add a greeting to not find. | ||
guestbook.Greeting( | ||
content='Flat sheet', | ||
parent=ndb.Key('Book', 'brane2')).put() | ||
|
||
response = app.get('/?guestbook_name=brane3') | ||
|
||
assert response.status_int == 200 | ||
assert 'Hello world' in response.body | ||
assert 'Flat sheet' not in response.body | ||
|
||
|
||
def test_list(app): | ||
# Add greetings to find | ||
for i in range(11): | ||
guestbook.Greeting(content='Greeting {}'.format(i)).put() | ||
|
||
response = app.get('/list') | ||
assert response.status_int == 200 | ||
|
||
assert 'Greeting 0' in response.body | ||
assert 'Greeting 9' in response.body | ||
assert 'Greeting 10' not in response.body | ||
|
||
next_page = re.search(r'href="([^"]+)"', response.body).group(1) | ||
assert next_page is not None | ||
|
||
response = app.get(next_page) | ||
assert response.status_int == 200 | ||
|
||
assert 'Greeting 0' not in response.body | ||
assert 'Greeting 10' in response.body | ||
assert 'More' not in response.body |
Oops, something went wrong.