1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import inspect
16-
1715from google .appengine .api import users
1816from google .appengine .ext import ndb
1917from google .appengine .ext .ndb .google_imports import datastore_errors
2018import pytest
2119import 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 == 'sandy@example.co.uk'
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 'sandy@example.com' == 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 () == 'sandy@example.com'
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 () == 'sandy@example.com'
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 = 'a@a.com' ), snippets .Account (email = 'b@b.com' )])
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 = 'user@example.com' , _user_id = '123' )
235224 item = snippets .ModelWithUser (user_id = user .user_id ())
236225 item .put ()
0 commit comments