Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missed some ndb snippets #260

Merged
merged 1 commit into from
Apr 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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