Skip to content

Commit 88ac54b

Browse files
author
Jerjou Cheng
committed
Add snippets for ndb/properties
1 parent a365c5c commit 88ac54b

File tree

3 files changed

+280
-0
lines changed

3 files changed

+280
-0
lines changed

appengine/ndb/properties/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## App Engine Datastore NDB Properties Samples
2+
3+
This contains snippets used in the NDB properties documentation, demonstrating
4+
various operation on ndb properties.
5+
6+
<!-- auto-doc-link -->
7+
These samples are used on the following documentation page:
8+
9+
> https://cloud.google.com/appengine/docs/python/ndb/properties
10+
11+
<!-- end-auto-doc-link -->

appengine/ndb/properties/snippets.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START notestore_imports]
16+
from google.appengine.ext import ndb
17+
from google.appengine.ext.ndb import msgprop
18+
# [END notestore_imports]
19+
from protorpc import messages
20+
21+
22+
class Account(ndb.Model):
23+
username = ndb.StringProperty()
24+
userid = ndb.IntegerProperty()
25+
email = ndb.StringProperty()
26+
27+
28+
class Employee(ndb.Model):
29+
full_name = ndb.StringProperty('n')
30+
retirement_age = ndb.IntegerProperty('r')
31+
32+
33+
class Article(ndb.Model):
34+
title = ndb.StringProperty()
35+
stars = ndb.IntegerProperty()
36+
tags = ndb.StringProperty(repeated=True)
37+
38+
39+
def create_article():
40+
article = Article(
41+
title='Python versus Ruby',
42+
stars=3,
43+
tags=['python', 'ruby'])
44+
article.put()
45+
return article
46+
47+
48+
class Address(ndb.Model):
49+
type = ndb.StringProperty() # E.g., 'home', 'work'
50+
street = ndb.StringProperty()
51+
city = ndb.StringProperty()
52+
53+
54+
class Contact(ndb.Model):
55+
name = ndb.StringProperty()
56+
addresses = ndb.StructuredProperty(Address, repeated=True)
57+
58+
59+
class ContactWithLocalStructuredProperty(ndb.Model):
60+
name = ndb.StringProperty()
61+
addresses = ndb.LocalStructuredProperty(Address, repeated=True)
62+
63+
64+
def create_contact():
65+
guido = Contact(
66+
name='Guido',
67+
addresses=[
68+
Address(
69+
type='home',
70+
city='Amsterdam'),
71+
Address(
72+
type='work',
73+
street='Spear St',
74+
city='SF')])
75+
76+
guido.put()
77+
return guido
78+
79+
80+
def create_contact_with_local_structured_property():
81+
guido = ContactWithLocalStructuredProperty(
82+
name='Guido',
83+
addresses=[
84+
Address(
85+
type='home',
86+
city='Amsterdam'),
87+
Address(
88+
type='work',
89+
street='Spear St',
90+
city='SF')])
91+
92+
guido.put()
93+
return guido
94+
95+
96+
class SomeEntity(ndb.Model):
97+
name = ndb.StringProperty()
98+
name_lower = ndb.ComputedProperty(lambda self: self.name.lower())
99+
100+
101+
def create_some_entity():
102+
entity = SomeEntity(name='Nick')
103+
entity.put()
104+
return entity
105+
106+
107+
class Note(messages.Message):
108+
text = messages.StringField(1, required=True)
109+
when = messages.IntegerField(2)
110+
111+
112+
class NoteStore(ndb.Model):
113+
note = msgprop.MessageProperty(Note, indexed_fields=['when'])
114+
name = ndb.StringProperty()
115+
116+
117+
def create_note_store():
118+
my_note = Note(text='Excellent note', when=50)
119+
120+
ns = NoteStore(note=my_note, name='excellent')
121+
key = ns.put()
122+
123+
new_notes = NoteStore.query(NoteStore.note.when >= 10).fetch()
124+
return new_notes, key
125+
126+
127+
class Notebook(messages.Message):
128+
notes = messages.MessageField(Note, 1, repeated=True)
129+
130+
131+
class SignedStorableNotebook(ndb.Model):
132+
author = ndb.StringProperty()
133+
nb = msgprop.MessageProperty(
134+
Notebook, indexed_fields=['notes.text', 'notes.when'])
135+
136+
137+
class Color(messages.Enum):
138+
RED = 620
139+
GREEN = 495
140+
BLUE = 450
141+
142+
143+
class Part(ndb.Model):
144+
name = ndb.StringProperty()
145+
color = msgprop.EnumProperty(Color, required=True)
146+
147+
148+
def print_part():
149+
p1 = Part(name='foo', color=Color.RED)
150+
print p1.color # prints "RED"
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import inspect
16+
17+
from google.appengine.ext import ndb
18+
import pytest
19+
import snippets
20+
21+
22+
@pytest.yield_fixture
23+
def client(testbed):
24+
yield testbed
25+
26+
for name, obj in inspect.getmembers(snippets):
27+
if inspect.isclass(obj) and issubclass(obj, ndb.Model):
28+
ndb.delete_multi(obj.query().iter(keys_only=True))
29+
30+
31+
def test_account(client):
32+
account = snippets.Account(
33+
username='flan',
34+
userid=123,
35+
36+
account.put()
37+
38+
39+
def test_employee(client):
40+
employee = snippets.Employee(
41+
full_name='Hob Gadling',
42+
retirement_age=600)
43+
employee.put()
44+
45+
46+
def test_article(client):
47+
article = snippets.create_article()
48+
assert article.title == 'Python versus Ruby'
49+
assert article.stars == 3
50+
assert sorted(article.tags) == sorted(['python', 'ruby'])
51+
52+
53+
def test_create_contact(client):
54+
guido = snippets.create_contact()
55+
assert guido.name == 'Guido'
56+
addresses = guido.addresses
57+
assert addresses[0].type == 'home'
58+
assert addresses[1].type == 'work'
59+
assert addresses[0].street is None
60+
assert addresses[1].street == 'Spear St'
61+
assert addresses[0].city == 'Amsterdam'
62+
assert addresses[1].city == 'SF'
63+
64+
65+
def test_contact_with_local_structured_property(client):
66+
guido = snippets.create_contact_with_local_structured_property()
67+
assert guido.name == 'Guido'
68+
addresses = guido.addresses
69+
assert addresses[0].type == 'home'
70+
assert addresses[1].type == 'work'
71+
72+
73+
def test_create_some_entity(client):
74+
entity = snippets.create_some_entity()
75+
assert entity.name == 'Nick'
76+
assert entity.name_lower == 'nick'
77+
78+
79+
def test_computed_property(client):
80+
entity = snippets.create_some_entity()
81+
entity.name = 'Nick'
82+
assert entity.name_lower == 'nick'
83+
entity.name = 'Nickie'
84+
assert entity.name_lower == 'nickie'
85+
86+
87+
def test_create_note_store(client):
88+
note_stores, _ = snippets.create_note_store()
89+
assert len(note_stores) == 1
90+
assert note_stores[0].name == 'excellent'
91+
assert note_stores[0].name == 'excellent'
92+
assert note_stores[0].note.text == 'Excellent note'
93+
assert note_stores[0].note.when == 50
94+
95+
96+
def test_notebook(client):
97+
note1 = snippets.Note(
98+
text='Refused to die.',
99+
when=1389)
100+
note2 = snippets.Note(
101+
text='Printed some things',
102+
when=1489)
103+
note3 = snippets.Note(
104+
text='Learned to use a sword',
105+
when=1589)
106+
107+
notebook = snippets.Notebook(
108+
notes=[note1, note2, note3])
109+
stored_notebook = snippets.SignedStorableNotebook(
110+
author='Hob Gadling',
111+
nb=notebook)
112+
113+
stored_notebook.put()
114+
115+
116+
def test_part(client, capsys):
117+
snippets.print_part()
118+
stdout, _ = capsys.readouterr()
119+
assert stdout.strip() == 'RED'

0 commit comments

Comments
 (0)