Skip to content

Commit

Permalink
Missed some ndb snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng committed Apr 14, 2016
1 parent 63363da commit d5401d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
22 changes: 22 additions & 0 deletions appengine/ndb/entities/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def create_model_using_attributes():
return sandy


def create_model_using_populate():
sandy = Account()
sandy.populate(
username='Sandy',
userid=123,
email='sandy@gmail.com')
return sandy


def demonstrate_model_constructor_type_checking():
bad = Account(
username='Sandy', userid='not integer') # raises an exception
Expand All @@ -50,6 +59,17 @@ def save_model(sandy):
return sandy_key


def get_model(sandy_key):
sandy = sandy_key.get()
return sandy


def get_key_kind_and_id(sandy_key):
kind_string = sandy_key.kind() # returns 'Account'
ident = sandy_key.id() # returns '2'
return kind_string, ident


def get_url_safe_key(sandy_key):
url_string = sandy_key.urlsafe()
return url_string
Expand Down Expand Up @@ -123,6 +143,8 @@ def equivalent_ways_to_define_key_with_parent():
ndb.Key('Revision', '1', parent=ndb.Key(
'Message', 123, parent=ndb.Key('Account', 'sandy@example.com')))


def create_root_key():
sandy_key = ndb.Key(Account, 'sandy@example.com')
return sandy_key

Expand Down
26 changes: 25 additions & 1 deletion appengine/ndb/entities/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def test_create_model_using_attributes(client):
assert isinstance(result, snippets.Account)


def test_create_model_using_populate(client):
result = snippets.create_model_using_populate()
assert isinstance(result, snippets.Account)


def test_demonstrate_model_constructor_type_checking(client):
with pytest.raises(datastore_errors.BadValueError):
snippets.demonstrate_model_constructor_type_checking()
Expand All @@ -56,6 +61,21 @@ def test_save_model(client):
assert isinstance(result, snippets.ndb.Key)


def test_get_model(client):
sandy_key = snippets.save_model(
snippets.create_model_using_keyword_arguments())
result = snippets.get_model(sandy_key)
assert isinstance(result, snippets.Account)


def test_get_key_kind_and_id(client):
sandy_key = snippets.save_model(
snippets.create_model_using_keyword_arguments())
kind_string, ident = snippets.get_key_kind_and_id(sandy_key)
assert kind_string == 'Account'
assert isinstance(ident, long)


def test_get_url_safe_key(client):
sandy_key = snippets.save_model(
snippets.create_model_using_keyword_arguments())
Expand Down Expand Up @@ -121,7 +141,11 @@ def test_demonstrate_models_with_parent_hierarchy(client):


def test_equivalent_ways_to_define_key_with_parent(client):
result = snippets.equivalent_ways_to_define_key_with_parent()
snippets.equivalent_ways_to_define_key_with_parent()


def test_create_root_key(client):
result = snippets.create_root_key()
assert result.id() == 'sandy@example.com'


Expand Down

0 comments on commit d5401d2

Please sign in to comment.