Skip to content

Commit 19bd9c8

Browse files
committed
Merge pull request #260 from GoogleCloudPlatform/ndb
Missed some ndb snippets
2 parents 63363da + d5401d2 commit 19bd9c8

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

appengine/ndb/entities/snippets.py

+22
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ def create_model_using_attributes():
3535
return sandy
3636

3737

38+
def create_model_using_populate():
39+
sandy = Account()
40+
sandy.populate(
41+
username='Sandy',
42+
userid=123,
43+
email='sandy@gmail.com')
44+
return sandy
45+
46+
3847
def demonstrate_model_constructor_type_checking():
3948
bad = Account(
4049
username='Sandy', userid='not integer') # raises an exception
@@ -50,6 +59,17 @@ def save_model(sandy):
5059
return sandy_key
5160

5261

62+
def get_model(sandy_key):
63+
sandy = sandy_key.get()
64+
return sandy
65+
66+
67+
def get_key_kind_and_id(sandy_key):
68+
kind_string = sandy_key.kind() # returns 'Account'
69+
ident = sandy_key.id() # returns '2'
70+
return kind_string, ident
71+
72+
5373
def get_url_safe_key(sandy_key):
5474
url_string = sandy_key.urlsafe()
5575
return url_string
@@ -123,6 +143,8 @@ def equivalent_ways_to_define_key_with_parent():
123143
ndb.Key('Revision', '1', parent=ndb.Key(
124144
'Message', 123, parent=ndb.Key('Account', 'sandy@example.com')))
125145

146+
147+
def create_root_key():
126148
sandy_key = ndb.Key(Account, 'sandy@example.com')
127149
return sandy_key
128150

appengine/ndb/entities/snippets_test.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def test_create_model_using_attributes(client):
3939
assert isinstance(result, snippets.Account)
4040

4141

42+
def test_create_model_using_populate(client):
43+
result = snippets.create_model_using_populate()
44+
assert isinstance(result, snippets.Account)
45+
46+
4247
def test_demonstrate_model_constructor_type_checking(client):
4348
with pytest.raises(datastore_errors.BadValueError):
4449
snippets.demonstrate_model_constructor_type_checking()
@@ -56,6 +61,21 @@ def test_save_model(client):
5661
assert isinstance(result, snippets.ndb.Key)
5762

5863

64+
def test_get_model(client):
65+
sandy_key = snippets.save_model(
66+
snippets.create_model_using_keyword_arguments())
67+
result = snippets.get_model(sandy_key)
68+
assert isinstance(result, snippets.Account)
69+
70+
71+
def test_get_key_kind_and_id(client):
72+
sandy_key = snippets.save_model(
73+
snippets.create_model_using_keyword_arguments())
74+
kind_string, ident = snippets.get_key_kind_and_id(sandy_key)
75+
assert kind_string == 'Account'
76+
assert isinstance(ident, long)
77+
78+
5979
def test_get_url_safe_key(client):
6080
sandy_key = snippets.save_model(
6181
snippets.create_model_using_keyword_arguments())
@@ -121,7 +141,11 @@ def test_demonstrate_models_with_parent_hierarchy(client):
121141

122142

123143
def test_equivalent_ways_to_define_key_with_parent(client):
124-
result = snippets.equivalent_ways_to_define_key_with_parent()
144+
snippets.equivalent_ways_to_define_key_with_parent()
145+
146+
147+
def test_create_root_key(client):
148+
result = snippets.create_root_key()
125149
assert result.id() == 'sandy@example.com'
126150

127151

0 commit comments

Comments
 (0)