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

Move ndb property subclasses snippets from cloudsite #288

Merged
merged 2 commits into from
Apr 27, 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
79 changes: 34 additions & 45 deletions appengine/ndb/entities/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,79 +12,68 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect

from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.ext.ndb.google_imports import datastore_errors
import pytest
import snippets


@pytest.yield_fixture
def client(testbed):
yield testbed

for name, obj in inspect.getmembers(snippets):
if inspect.isclass(obj) and issubclass(obj, ndb.Model):
ndb.delete_multi(obj.query().iter(keys_only=True))


def test_create_model_using_keyword_arguments(client):
def test_create_model_using_keyword_arguments(testbed):
result = snippets.create_model_using_keyword_arguments()
assert isinstance(result, snippets.Account)


def test_create_model_using_attributes(client):
def test_create_model_using_attributes(testbed):
result = snippets.create_model_using_attributes()
assert isinstance(result, snippets.Account)


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


def test_demonstrate_model_constructor_type_checking(client):
def test_demonstrate_model_constructor_type_checking(testbed):
with pytest.raises(datastore_errors.BadValueError):
snippets.demonstrate_model_constructor_type_checking()


def test_dmonstrate_model_attribute_type_checking(client):
def test_dmonstrate_model_attribute_type_checking(testbed):
with pytest.raises(datastore_errors.BadValueError):
snippets.dmonstrate_model_attribute_type_checking(
snippets.create_model_using_keyword_arguments())


def test_save_model(client):
def test_save_model(testbed):
result = snippets.save_model(
snippets.create_model_using_keyword_arguments())
assert isinstance(result, snippets.ndb.Key)


def test_get_model(client):
def test_get_model(testbed):
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):
def test_get_key_kind_and_id(testbed):
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):
def test_get_url_safe_key(testbed):
sandy_key = snippets.save_model(
snippets.create_model_using_keyword_arguments())
result = snippets.get_url_safe_key(sandy_key)
assert isinstance(result, str)


def test_get_model_from_url_safe_key(client):
def test_get_model_from_url_safe_key(testbed):
sandy_key = snippets.save_model(
snippets.create_model_using_keyword_arguments())
result = snippets.get_model_from_url_safe_key(
Expand All @@ -93,7 +82,7 @@ def test_get_model_from_url_safe_key(client):
assert result.username == 'Sandy'


def test_get_key_and_numeric_id_from_url_safe_key(client):
def test_get_key_and_numeric_id_from_url_safe_key(testbed):
sandy_key = snippets.save_model(
snippets.create_model_using_keyword_arguments())
urlsafe = snippets.get_url_safe_key(sandy_key)
Expand All @@ -104,7 +93,7 @@ def test_get_key_and_numeric_id_from_url_safe_key(client):
assert isinstance(kind_string, str)


def test_update_model_from_key(client):
def test_update_model_from_key(testbed):
sandy = snippets.create_model_using_keyword_arguments()
sandy_key = snippets.save_model(sandy)
urlsafe = snippets.get_url_safe_key(sandy_key)
Expand All @@ -114,92 +103,92 @@ def test_update_model_from_key(client):
assert key.get().email == '[email protected]'


def test_delete_model(client):
def test_delete_model(testbed):
sandy = snippets.create_model_using_keyword_arguments()
snippets.save_model(sandy)
snippets.delete_model(sandy)
assert sandy.key.get() is None


def test_create_model_with_named_key(client):
def test_create_model_with_named_key(testbed):
result = snippets.create_model_with_named_key()
assert '[email protected]' == result


def test_set_key_directly(client):
def test_set_key_directly(testbed):
account = snippets.Account()
snippets.set_key_directly(account)
assert account.key.id() == '[email protected]'


def test_create_model_with_generated_id(client):
def test_create_model_with_generated_id(testbed):
result = snippets.create_model_with_generated_id()
assert isinstance(result.key.id(), long)


def test_demonstrate_models_with_parent_hierarchy(client):
def test_demonstrate_models_with_parent_hierarchy(testbed):
snippets.demonstrate_models_with_parent_hierarchy()


def test_equivalent_ways_to_define_key_with_parent(client):
def test_equivalent_ways_to_define_key_with_parent(testbed):
snippets.equivalent_ways_to_define_key_with_parent()


def test_create_root_key(client):
def test_create_root_key(testbed):
result = snippets.create_root_key()
assert result.id() == '[email protected]'


def test_create_model_with_parent_keys(client):
def test_create_model_with_parent_keys(testbed):
result = snippets.create_model_with_parent_keys()
assert result.message_text == 'Hello'


def test_get_parent_key_of_model(client):
def test_get_parent_key_of_model(testbed):
initial_revision = snippets.create_model_with_parent_keys()
result = snippets.get_parent_key_of_model(initial_revision)
assert result.kind() == 'Message'


def test_operate_on_multiple_keys_at_once(client):
def test_operate_on_multiple_keys_at_once(testbed):
snippets.operate_on_multiple_keys_at_once([
snippets.Account(email='[email protected]'), snippets.Account(email='[email protected]')])


def test_create_expando_model(client):
def test_create_expando_model(testbed):
result = snippets.create_expando_model()
assert result.foo == 1


def test_get_properties_defined_on_expando(client):
def test_get_properties_defined_on_expando(testbed):
result = snippets.get_properties_defined_on_expando(
snippets.create_expando_model())
assert result['foo'] is not None
assert result['bar'] is not None
assert result['tags'] is not None


def test_create_expando_model_with_defined_properties(client):
def test_create_expando_model_with_defined_properties(testbed):
result = snippets.create_expando_model_with_defined_properties()
assert result.name == 'Sandy'


def test_create_expando_model_that_isnt_indexed_by_default(client):
def test_create_expando_model_that_isnt_indexed_by_default(testbed):
result = snippets.create_expando_model_that_isnt_indexed_by_default()
assert result['foo']
assert result['bar']


def test_demonstrate_wrong_way_to_query_expando(client):
def test_demonstrate_wrong_way_to_query_expando(testbed):
with pytest.raises(AttributeError):
snippets.demonstrate_wrong_way_to_query_expando()


def test_demonstrate_right_way_to_query_expando(client):
def test_demonstrate_right_way_to_query_expando(testbed):
snippets.demonstrate_right_way_to_query_expando()


def test_demonstrate_model_put_and_delete_hooks(client):
def test_demonstrate_model_put_and_delete_hooks(testbed):
iterator = snippets.demonstrate_model_put_and_delete_hooks()
iterator.next()
assert snippets.notification == 'Gee wiz I have a new friend!'
Expand All @@ -208,29 +197,29 @@ def test_demonstrate_model_put_and_delete_hooks(client):
'I have found occasion to rethink our friendship.')


def test_reserve_model_ids(client):
def test_reserve_model_ids(testbed):
first, last = snippets.reserve_model_ids()
assert last - first >= 99


def test_reserve_model_ids_with_a_parent(client):
def test_reserve_model_ids_with_a_parent(testbed):
first, last = snippets.reserve_model_ids_with_a_parent(
snippets.Friend().key)
assert last - first >= 99


def test_construct_keys_from_range_of_reserved_ids(client):
def test_construct_keys_from_range_of_reserved_ids(testbed):
result = snippets.construct_keys_from_range_of_reserved_ids(
*snippets.reserve_model_ids())
assert len(result) == 100


def test_reserve_model_ids_up_to(client):
def test_reserve_model_ids_up_to(testbed):
first, last = snippets.reserve_model_ids_up_to(5)
assert last - first >= 4


def test_model_with_user(client):
def test_model_with_user(testbed):
user = users.User(email='[email protected]', _user_id='123')
item = snippets.ModelWithUser(user_id=user.user_id())
item.put()
Expand Down
33 changes: 10 additions & 23 deletions appengine/ndb/properties/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect

from google.appengine.ext import ndb
import pytest
import snippets


@pytest.yield_fixture
def client(testbed):
yield testbed

for name, obj in inspect.getmembers(snippets):
if inspect.isclass(obj) and issubclass(obj, ndb.Model):
ndb.delete_multi(obj.query().iter(keys_only=True))


def test_account(client):
def test_account(testbed):
account = snippets.Account(
username='flan',
userid=123,
email='[email protected]')
account.put()


def test_employee(client):
def test_employee(testbed):
employee = snippets.Employee(
full_name='Hob Gadling',
retirement_age=600)
employee.put()


def test_article(client):
def test_article(testbed):
article = snippets.create_article()
assert article.title == 'Python versus Ruby'
assert article.stars == 3
assert sorted(article.tags) == sorted(['python', 'ruby'])


def test_create_contact(client):
def test_create_contact(testbed):
guido = snippets.create_contact()
assert guido.name == 'Guido'
addresses = guido.addresses
Expand All @@ -62,29 +49,29 @@ def test_create_contact(client):
assert addresses[1].city == 'SF'


def test_contact_with_local_structured_property(client):
def test_contact_with_local_structured_property(testbed):
guido = snippets.create_contact_with_local_structured_property()
assert guido.name == 'Guido'
addresses = guido.addresses
assert addresses[0].type == 'home'
assert addresses[1].type == 'work'


def test_create_some_entity(client):
def test_create_some_entity(testbed):
entity = snippets.create_some_entity()
assert entity.name == 'Nick'
assert entity.name_lower == 'nick'


def test_computed_property(client):
def test_computed_property(testbed):
entity = snippets.create_some_entity()
entity.name = 'Nick'
assert entity.name_lower == 'nick'
entity.name = 'Nickie'
assert entity.name_lower == 'nickie'


def test_create_note_store(client):
def test_create_note_store(testbed):
note_stores, _ = snippets.create_note_store()
assert len(note_stores) == 1
assert note_stores[0].name == 'excellent'
Expand All @@ -93,7 +80,7 @@ def test_create_note_store(client):
assert note_stores[0].note.when == 50


def test_notebook(client):
def test_notebook(testbed):
note1 = snippets.Note(
text='Refused to die.',
when=1389)
Expand All @@ -113,7 +100,7 @@ def test_notebook(client):
stored_notebook.put()


def test_part(client, capsys):
def test_part(testbed, capsys):
snippets.print_part()
stdout, _ = capsys.readouterr()
assert stdout.strip() == 'RED'
11 changes: 11 additions & 0 deletions appengine/ndb/property_subclasses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## App Engine Datastore NDB Property Subclasses Samples

This contains snippets used in the NDB property subclasses documentation,
demonstrating various operation on ndb property subclasses.

<!-- auto-doc-link -->
These samples are used on the following documentation page:

> https://cloud.google.com/appengine/docs/python/ndb/subclassprop

<!-- end-auto-doc-link -->
Loading