12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import inspect
16
-
17
15
from google .appengine .api import users
18
16
from google .appengine .ext import ndb
19
17
from google .appengine .ext .ndb .google_imports import datastore_errors
20
18
import pytest
21
19
import snippets
22
20
23
21
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 ):
34
23
result = snippets .create_model_using_keyword_arguments ()
35
24
assert isinstance (result , snippets .Account )
36
25
37
26
38
- def test_create_model_using_attributes (client ):
27
+ def test_create_model_using_attributes (testbed ):
39
28
result = snippets .create_model_using_attributes ()
40
29
assert isinstance (result , snippets .Account )
41
30
42
31
43
- def test_create_model_using_populate (client ):
32
+ def test_create_model_using_populate (testbed ):
44
33
result = snippets .create_model_using_populate ()
45
34
assert isinstance (result , snippets .Account )
46
35
47
36
48
- def test_demonstrate_model_constructor_type_checking (client ):
37
+ def test_demonstrate_model_constructor_type_checking (testbed ):
49
38
with pytest .raises (datastore_errors .BadValueError ):
50
39
snippets .demonstrate_model_constructor_type_checking ()
51
40
52
41
53
- def test_dmonstrate_model_attribute_type_checking (client ):
42
+ def test_dmonstrate_model_attribute_type_checking (testbed ):
54
43
with pytest .raises (datastore_errors .BadValueError ):
55
44
snippets .dmonstrate_model_attribute_type_checking (
56
45
snippets .create_model_using_keyword_arguments ())
57
46
58
47
59
- def test_save_model (client ):
48
+ def test_save_model (testbed ):
60
49
result = snippets .save_model (
61
50
snippets .create_model_using_keyword_arguments ())
62
51
assert isinstance (result , snippets .ndb .Key )
63
52
64
53
65
- def test_get_model (client ):
54
+ def test_get_model (testbed ):
66
55
sandy_key = snippets .save_model (
67
56
snippets .create_model_using_keyword_arguments ())
68
57
result = snippets .get_model (sandy_key )
69
58
assert isinstance (result , snippets .Account )
70
59
71
60
72
- def test_get_key_kind_and_id (client ):
61
+ def test_get_key_kind_and_id (testbed ):
73
62
sandy_key = snippets .save_model (
74
63
snippets .create_model_using_keyword_arguments ())
75
64
kind_string , ident = snippets .get_key_kind_and_id (sandy_key )
76
65
assert kind_string == 'Account'
77
66
assert isinstance (ident , long )
78
67
79
68
80
- def test_get_url_safe_key (client ):
69
+ def test_get_url_safe_key (testbed ):
81
70
sandy_key = snippets .save_model (
82
71
snippets .create_model_using_keyword_arguments ())
83
72
result = snippets .get_url_safe_key (sandy_key )
84
73
assert isinstance (result , str )
85
74
86
75
87
- def test_get_model_from_url_safe_key (client ):
76
+ def test_get_model_from_url_safe_key (testbed ):
88
77
sandy_key = snippets .save_model (
89
78
snippets .create_model_using_keyword_arguments ())
90
79
result = snippets .get_model_from_url_safe_key (
@@ -93,7 +82,7 @@ def test_get_model_from_url_safe_key(client):
93
82
assert result .username == 'Sandy'
94
83
95
84
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 ):
97
86
sandy_key = snippets .save_model (
98
87
snippets .create_model_using_keyword_arguments ())
99
88
urlsafe = snippets .get_url_safe_key (sandy_key )
@@ -104,7 +93,7 @@ def test_get_key_and_numeric_id_from_url_safe_key(client):
104
93
assert isinstance (kind_string , str )
105
94
106
95
107
- def test_update_model_from_key (client ):
96
+ def test_update_model_from_key (testbed ):
108
97
sandy = snippets .create_model_using_keyword_arguments ()
109
98
sandy_key = snippets .save_model (sandy )
110
99
urlsafe = snippets .get_url_safe_key (sandy_key )
@@ -114,92 +103,92 @@ def test_update_model_from_key(client):
114
103
assert key .
get ().
email == '[email protected] '
115
104
116
105
117
- def test_delete_model (client ):
106
+ def test_delete_model (testbed ):
118
107
sandy = snippets .create_model_using_keyword_arguments ()
119
108
snippets .save_model (sandy )
120
109
snippets .delete_model (sandy )
121
110
assert sandy .key .get () is None
122
111
123
112
124
- def test_create_model_with_named_key (client ):
113
+ def test_create_model_with_named_key (testbed ):
125
114
result = snippets .create_model_with_named_key ()
126
115
assert '[email protected] ' == result
127
116
128
117
129
- def test_set_key_directly (client ):
118
+ def test_set_key_directly (testbed ):
130
119
account = snippets .Account ()
131
120
snippets .set_key_directly (account )
132
121
assert account .
key .
id ()
== '[email protected] '
133
122
134
123
135
- def test_create_model_with_generated_id (client ):
124
+ def test_create_model_with_generated_id (testbed ):
136
125
result = snippets .create_model_with_generated_id ()
137
126
assert isinstance (result .key .id (), long )
138
127
139
128
140
- def test_demonstrate_models_with_parent_hierarchy (client ):
129
+ def test_demonstrate_models_with_parent_hierarchy (testbed ):
141
130
snippets .demonstrate_models_with_parent_hierarchy ()
142
131
143
132
144
- def test_equivalent_ways_to_define_key_with_parent (client ):
133
+ def test_equivalent_ways_to_define_key_with_parent (testbed ):
145
134
snippets .equivalent_ways_to_define_key_with_parent ()
146
135
147
136
148
- def test_create_root_key (client ):
137
+ def test_create_root_key (testbed ):
149
138
result = snippets .create_root_key ()
150
139
assert result .
id ()
== '[email protected] '
151
140
152
141
153
- def test_create_model_with_parent_keys (client ):
142
+ def test_create_model_with_parent_keys (testbed ):
154
143
result = snippets .create_model_with_parent_keys ()
155
144
assert result .message_text == 'Hello'
156
145
157
146
158
- def test_get_parent_key_of_model (client ):
147
+ def test_get_parent_key_of_model (testbed ):
159
148
initial_revision = snippets .create_model_with_parent_keys ()
160
149
result = snippets .get_parent_key_of_model (initial_revision )
161
150
assert result .kind () == 'Message'
162
151
163
152
164
- def test_operate_on_multiple_keys_at_once (client ):
153
+ def test_operate_on_multiple_keys_at_once (testbed ):
165
154
snippets .operate_on_multiple_keys_at_once ([
166
155
snippets .
Account (
email = '[email protected] ' ),
snippets .
Account (
email = '[email protected] ' )])
167
156
168
157
169
- def test_create_expando_model (client ):
158
+ def test_create_expando_model (testbed ):
170
159
result = snippets .create_expando_model ()
171
160
assert result .foo == 1
172
161
173
162
174
- def test_get_properties_defined_on_expando (client ):
163
+ def test_get_properties_defined_on_expando (testbed ):
175
164
result = snippets .get_properties_defined_on_expando (
176
165
snippets .create_expando_model ())
177
166
assert result ['foo' ] is not None
178
167
assert result ['bar' ] is not None
179
168
assert result ['tags' ] is not None
180
169
181
170
182
- def test_create_expando_model_with_defined_properties (client ):
171
+ def test_create_expando_model_with_defined_properties (testbed ):
183
172
result = snippets .create_expando_model_with_defined_properties ()
184
173
assert result .name == 'Sandy'
185
174
186
175
187
- def test_create_expando_model_that_isnt_indexed_by_default (client ):
176
+ def test_create_expando_model_that_isnt_indexed_by_default (testbed ):
188
177
result = snippets .create_expando_model_that_isnt_indexed_by_default ()
189
178
assert result ['foo' ]
190
179
assert result ['bar' ]
191
180
192
181
193
- def test_demonstrate_wrong_way_to_query_expando (client ):
182
+ def test_demonstrate_wrong_way_to_query_expando (testbed ):
194
183
with pytest .raises (AttributeError ):
195
184
snippets .demonstrate_wrong_way_to_query_expando ()
196
185
197
186
198
- def test_demonstrate_right_way_to_query_expando (client ):
187
+ def test_demonstrate_right_way_to_query_expando (testbed ):
199
188
snippets .demonstrate_right_way_to_query_expando ()
200
189
201
190
202
- def test_demonstrate_model_put_and_delete_hooks (client ):
191
+ def test_demonstrate_model_put_and_delete_hooks (testbed ):
203
192
iterator = snippets .demonstrate_model_put_and_delete_hooks ()
204
193
iterator .next ()
205
194
assert snippets .notification == 'Gee wiz I have a new friend!'
@@ -208,29 +197,29 @@ def test_demonstrate_model_put_and_delete_hooks(client):
208
197
'I have found occasion to rethink our friendship.' )
209
198
210
199
211
- def test_reserve_model_ids (client ):
200
+ def test_reserve_model_ids (testbed ):
212
201
first , last = snippets .reserve_model_ids ()
213
202
assert last - first >= 99
214
203
215
204
216
- def test_reserve_model_ids_with_a_parent (client ):
205
+ def test_reserve_model_ids_with_a_parent (testbed ):
217
206
first , last = snippets .reserve_model_ids_with_a_parent (
218
207
snippets .Friend ().key )
219
208
assert last - first >= 99
220
209
221
210
222
- def test_construct_keys_from_range_of_reserved_ids (client ):
211
+ def test_construct_keys_from_range_of_reserved_ids (testbed ):
223
212
result = snippets .construct_keys_from_range_of_reserved_ids (
224
213
* snippets .reserve_model_ids ())
225
214
assert len (result ) == 100
226
215
227
216
228
- def test_reserve_model_ids_up_to (client ):
217
+ def test_reserve_model_ids_up_to (testbed ):
229
218
first , last = snippets .reserve_model_ids_up_to (5 )
230
219
assert last - first >= 4
231
220
232
221
233
- def test_model_with_user (client ):
222
+ def test_model_with_user (testbed ):
234
223
user = users .
User (
email = '[email protected] ' ,
_user_id = '123' )
235
224
item = snippets .ModelWithUser (user_id = user .user_id ())
236
225
item .put ()
0 commit comments