From d5401d26e5f74ce1b3f4ef3a3dfdb339cb143137 Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Wed, 13 Apr 2016 17:41:28 -0700 Subject: [PATCH] Missed some ndb snippets --- appengine/ndb/entities/snippets.py | 22 +++++++++++++++++++++ appengine/ndb/entities/snippets_test.py | 26 ++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/appengine/ndb/entities/snippets.py b/appengine/ndb/entities/snippets.py index 0634e842e7b0..6eb3a38ddd61 100644 --- a/appengine/ndb/entities/snippets.py +++ b/appengine/ndb/entities/snippets.py @@ -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 @@ -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 @@ -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 diff --git a/appengine/ndb/entities/snippets_test.py b/appengine/ndb/entities/snippets_test.py index 761572238399..4886b772aab5 100644 --- a/appengine/ndb/entities/snippets_test.py +++ b/appengine/ndb/entities/snippets_test.py @@ -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() @@ -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()) @@ -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'