Skip to content

Commit 2f298d7

Browse files
committed
Merge pull request #288 from GoogleCloudPlatform/ndb-subclass
Move ndb property subclasses snippets from cloudsite
2 parents 37f55e0 + 8b76ad7 commit 2f298d7

File tree

6 files changed

+315
-68
lines changed

6 files changed

+315
-68
lines changed

appengine/ndb/entities/snippets_test.py

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,79 +12,68 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import inspect
16-
1715
from google.appengine.api import users
1816
from google.appengine.ext import ndb
1917
from google.appengine.ext.ndb.google_imports import datastore_errors
2018
import pytest
2119
import snippets
2220

2321

24-
@pytest.yield_fixture
25-
def client(testbed):
26-
yield testbed
27-
28-
for name, obj in inspect.getmembers(snippets):
29-
if inspect.isclass(obj) and issubclass(obj, ndb.Model):
30-
ndb.delete_multi(obj.query().iter(keys_only=True))
31-
32-
33-
def test_create_model_using_keyword_arguments(client):
22+
def test_create_model_using_keyword_arguments(testbed):
3423
result = snippets.create_model_using_keyword_arguments()
3524
assert isinstance(result, snippets.Account)
3625

3726

38-
def test_create_model_using_attributes(client):
27+
def test_create_model_using_attributes(testbed):
3928
result = snippets.create_model_using_attributes()
4029
assert isinstance(result, snippets.Account)
4130

4231

43-
def test_create_model_using_populate(client):
32+
def test_create_model_using_populate(testbed):
4433
result = snippets.create_model_using_populate()
4534
assert isinstance(result, snippets.Account)
4635

4736

48-
def test_demonstrate_model_constructor_type_checking(client):
37+
def test_demonstrate_model_constructor_type_checking(testbed):
4938
with pytest.raises(datastore_errors.BadValueError):
5039
snippets.demonstrate_model_constructor_type_checking()
5140

5241

53-
def test_dmonstrate_model_attribute_type_checking(client):
42+
def test_dmonstrate_model_attribute_type_checking(testbed):
5443
with pytest.raises(datastore_errors.BadValueError):
5544
snippets.dmonstrate_model_attribute_type_checking(
5645
snippets.create_model_using_keyword_arguments())
5746

5847

59-
def test_save_model(client):
48+
def test_save_model(testbed):
6049
result = snippets.save_model(
6150
snippets.create_model_using_keyword_arguments())
6251
assert isinstance(result, snippets.ndb.Key)
6352

6453

65-
def test_get_model(client):
54+
def test_get_model(testbed):
6655
sandy_key = snippets.save_model(
6756
snippets.create_model_using_keyword_arguments())
6857
result = snippets.get_model(sandy_key)
6958
assert isinstance(result, snippets.Account)
7059

7160

72-
def test_get_key_kind_and_id(client):
61+
def test_get_key_kind_and_id(testbed):
7362
sandy_key = snippets.save_model(
7463
snippets.create_model_using_keyword_arguments())
7564
kind_string, ident = snippets.get_key_kind_and_id(sandy_key)
7665
assert kind_string == 'Account'
7766
assert isinstance(ident, long)
7867

7968

80-
def test_get_url_safe_key(client):
69+
def test_get_url_safe_key(testbed):
8170
sandy_key = snippets.save_model(
8271
snippets.create_model_using_keyword_arguments())
8372
result = snippets.get_url_safe_key(sandy_key)
8473
assert isinstance(result, str)
8574

8675

87-
def test_get_model_from_url_safe_key(client):
76+
def test_get_model_from_url_safe_key(testbed):
8877
sandy_key = snippets.save_model(
8978
snippets.create_model_using_keyword_arguments())
9079
result = snippets.get_model_from_url_safe_key(
@@ -93,7 +82,7 @@ def test_get_model_from_url_safe_key(client):
9382
assert result.username == 'Sandy'
9483

9584

96-
def test_get_key_and_numeric_id_from_url_safe_key(client):
85+
def test_get_key_and_numeric_id_from_url_safe_key(testbed):
9786
sandy_key = snippets.save_model(
9887
snippets.create_model_using_keyword_arguments())
9988
urlsafe = snippets.get_url_safe_key(sandy_key)
@@ -104,7 +93,7 @@ def test_get_key_and_numeric_id_from_url_safe_key(client):
10493
assert isinstance(kind_string, str)
10594

10695

107-
def test_update_model_from_key(client):
96+
def test_update_model_from_key(testbed):
10897
sandy = snippets.create_model_using_keyword_arguments()
10998
sandy_key = snippets.save_model(sandy)
11099
urlsafe = snippets.get_url_safe_key(sandy_key)
@@ -114,92 +103,92 @@ def test_update_model_from_key(client):
114103
assert key.get().email == '[email protected]'
115104

116105

117-
def test_delete_model(client):
106+
def test_delete_model(testbed):
118107
sandy = snippets.create_model_using_keyword_arguments()
119108
snippets.save_model(sandy)
120109
snippets.delete_model(sandy)
121110
assert sandy.key.get() is None
122111

123112

124-
def test_create_model_with_named_key(client):
113+
def test_create_model_with_named_key(testbed):
125114
result = snippets.create_model_with_named_key()
126115
assert '[email protected]' == result
127116

128117

129-
def test_set_key_directly(client):
118+
def test_set_key_directly(testbed):
130119
account = snippets.Account()
131120
snippets.set_key_directly(account)
132121
assert account.key.id() == '[email protected]'
133122

134123

135-
def test_create_model_with_generated_id(client):
124+
def test_create_model_with_generated_id(testbed):
136125
result = snippets.create_model_with_generated_id()
137126
assert isinstance(result.key.id(), long)
138127

139128

140-
def test_demonstrate_models_with_parent_hierarchy(client):
129+
def test_demonstrate_models_with_parent_hierarchy(testbed):
141130
snippets.demonstrate_models_with_parent_hierarchy()
142131

143132

144-
def test_equivalent_ways_to_define_key_with_parent(client):
133+
def test_equivalent_ways_to_define_key_with_parent(testbed):
145134
snippets.equivalent_ways_to_define_key_with_parent()
146135

147136

148-
def test_create_root_key(client):
137+
def test_create_root_key(testbed):
149138
result = snippets.create_root_key()
150139
assert result.id() == '[email protected]'
151140

152141

153-
def test_create_model_with_parent_keys(client):
142+
def test_create_model_with_parent_keys(testbed):
154143
result = snippets.create_model_with_parent_keys()
155144
assert result.message_text == 'Hello'
156145

157146

158-
def test_get_parent_key_of_model(client):
147+
def test_get_parent_key_of_model(testbed):
159148
initial_revision = snippets.create_model_with_parent_keys()
160149
result = snippets.get_parent_key_of_model(initial_revision)
161150
assert result.kind() == 'Message'
162151

163152

164-
def test_operate_on_multiple_keys_at_once(client):
153+
def test_operate_on_multiple_keys_at_once(testbed):
165154
snippets.operate_on_multiple_keys_at_once([
166155
snippets.Account(email='[email protected]'), snippets.Account(email='[email protected]')])
167156

168157

169-
def test_create_expando_model(client):
158+
def test_create_expando_model(testbed):
170159
result = snippets.create_expando_model()
171160
assert result.foo == 1
172161

173162

174-
def test_get_properties_defined_on_expando(client):
163+
def test_get_properties_defined_on_expando(testbed):
175164
result = snippets.get_properties_defined_on_expando(
176165
snippets.create_expando_model())
177166
assert result['foo'] is not None
178167
assert result['bar'] is not None
179168
assert result['tags'] is not None
180169

181170

182-
def test_create_expando_model_with_defined_properties(client):
171+
def test_create_expando_model_with_defined_properties(testbed):
183172
result = snippets.create_expando_model_with_defined_properties()
184173
assert result.name == 'Sandy'
185174

186175

187-
def test_create_expando_model_that_isnt_indexed_by_default(client):
176+
def test_create_expando_model_that_isnt_indexed_by_default(testbed):
188177
result = snippets.create_expando_model_that_isnt_indexed_by_default()
189178
assert result['foo']
190179
assert result['bar']
191180

192181

193-
def test_demonstrate_wrong_way_to_query_expando(client):
182+
def test_demonstrate_wrong_way_to_query_expando(testbed):
194183
with pytest.raises(AttributeError):
195184
snippets.demonstrate_wrong_way_to_query_expando()
196185

197186

198-
def test_demonstrate_right_way_to_query_expando(client):
187+
def test_demonstrate_right_way_to_query_expando(testbed):
199188
snippets.demonstrate_right_way_to_query_expando()
200189

201190

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

210199

211-
def test_reserve_model_ids(client):
200+
def test_reserve_model_ids(testbed):
212201
first, last = snippets.reserve_model_ids()
213202
assert last - first >= 99
214203

215204

216-
def test_reserve_model_ids_with_a_parent(client):
205+
def test_reserve_model_ids_with_a_parent(testbed):
217206
first, last = snippets.reserve_model_ids_with_a_parent(
218207
snippets.Friend().key)
219208
assert last - first >= 99
220209

221210

222-
def test_construct_keys_from_range_of_reserved_ids(client):
211+
def test_construct_keys_from_range_of_reserved_ids(testbed):
223212
result = snippets.construct_keys_from_range_of_reserved_ids(
224213
*snippets.reserve_model_ids())
225214
assert len(result) == 100
226215

227216

228-
def test_reserve_model_ids_up_to(client):
217+
def test_reserve_model_ids_up_to(testbed):
229218
first, last = snippets.reserve_model_ids_up_to(5)
230219
assert last - first >= 4
231220

232221

233-
def test_model_with_user(client):
222+
def test_model_with_user(testbed):
234223
user = users.User(email='[email protected]', _user_id='123')
235224
item = snippets.ModelWithUser(user_id=user.user_id())
236225
item.put()

appengine/ndb/properties/snippets_test.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,32 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import inspect
16-
17-
from google.appengine.ext import ndb
18-
import pytest
1915
import snippets
2016

2117

22-
@pytest.yield_fixture
23-
def client(testbed):
24-
yield testbed
25-
26-
for name, obj in inspect.getmembers(snippets):
27-
if inspect.isclass(obj) and issubclass(obj, ndb.Model):
28-
ndb.delete_multi(obj.query().iter(keys_only=True))
29-
30-
31-
def test_account(client):
18+
def test_account(testbed):
3219
account = snippets.Account(
3320
username='flan',
3421
userid=123,
3522
3623
account.put()
3724

3825

39-
def test_employee(client):
26+
def test_employee(testbed):
4027
employee = snippets.Employee(
4128
full_name='Hob Gadling',
4229
retirement_age=600)
4330
employee.put()
4431

4532

46-
def test_article(client):
33+
def test_article(testbed):
4734
article = snippets.create_article()
4835
assert article.title == 'Python versus Ruby'
4936
assert article.stars == 3
5037
assert sorted(article.tags) == sorted(['python', 'ruby'])
5138

5239

53-
def test_create_contact(client):
40+
def test_create_contact(testbed):
5441
guido = snippets.create_contact()
5542
assert guido.name == 'Guido'
5643
addresses = guido.addresses
@@ -62,29 +49,29 @@ def test_create_contact(client):
6249
assert addresses[1].city == 'SF'
6350

6451

65-
def test_contact_with_local_structured_property(client):
52+
def test_contact_with_local_structured_property(testbed):
6653
guido = snippets.create_contact_with_local_structured_property()
6754
assert guido.name == 'Guido'
6855
addresses = guido.addresses
6956
assert addresses[0].type == 'home'
7057
assert addresses[1].type == 'work'
7158

7259

73-
def test_create_some_entity(client):
60+
def test_create_some_entity(testbed):
7461
entity = snippets.create_some_entity()
7562
assert entity.name == 'Nick'
7663
assert entity.name_lower == 'nick'
7764

7865

79-
def test_computed_property(client):
66+
def test_computed_property(testbed):
8067
entity = snippets.create_some_entity()
8168
entity.name = 'Nick'
8269
assert entity.name_lower == 'nick'
8370
entity.name = 'Nickie'
8471
assert entity.name_lower == 'nickie'
8572

8673

87-
def test_create_note_store(client):
74+
def test_create_note_store(testbed):
8875
note_stores, _ = snippets.create_note_store()
8976
assert len(note_stores) == 1
9077
assert note_stores[0].name == 'excellent'
@@ -93,7 +80,7 @@ def test_create_note_store(client):
9380
assert note_stores[0].note.when == 50
9481

9582

96-
def test_notebook(client):
83+
def test_notebook(testbed):
9784
note1 = snippets.Note(
9885
text='Refused to die.',
9986
when=1389)
@@ -113,7 +100,7 @@ def test_notebook(client):
113100
stored_notebook.put()
114101

115102

116-
def test_part(client, capsys):
103+
def test_part(testbed, capsys):
117104
snippets.print_part()
118105
stdout, _ = capsys.readouterr()
119106
assert stdout.strip() == 'RED'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## App Engine Datastore NDB Property Subclasses Samples
2+
3+
This contains snippets used in the NDB property subclasses documentation,
4+
demonstrating various operation on ndb property subclasses.
5+
6+
<!-- auto-doc-link -->
7+
These samples are used on the following documentation page:
8+
9+
> https://cloud.google.com/appengine/docs/python/ndb/subclassprop
10+
11+
<!-- end-auto-doc-link -->

0 commit comments

Comments
 (0)