-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathsnippets_test.py
More file actions
387 lines (276 loc) · 11.6 KB
/
snippets_test.py
File metadata and controls
387 lines (276 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from guestbook import Greeting
import snippets
from snippets_models import (Account, Address, Article,
Bar, Contact, FlexEmployee, Message)
def test_query_account_equality(testbed):
Account(userid=42).put()
Account(userid=43).put()
query = snippets.query_account_equality()
accounts = query.fetch()
assert len(accounts) == 1
assert accounts[0].userid == 42
def test_query_account_inequality(testbed):
Account(userid=32).put()
Account(userid=42).put()
Account(userid=43).put()
query = snippets.query_account_inequality()
accounts = query.fetch()
assert len(accounts) == 2
assert all(a.userid > 40 for a in accounts)
def test_query_account_multiple_filters(testbed):
Account(userid=40).put()
Account(userid=49).put()
Account(userid=50).put()
Account(userid=6).put()
Account(userid=62).put()
query = snippets.query_account_multiple_filters()
accounts = query.fetch()
assert len(accounts) == 2
assert all(40 <= a.userid < 50 for a in accounts)
def test_query_account_in_steps(testbed):
Account(userid=40).put()
Account(userid=49).put()
Account(userid=50).put()
Account(userid=6).put()
Account(userid=62).put()
_, _, query = snippets.query_account_in_steps()
accounts = query.fetch()
assert len(accounts) == 2
assert all(40 <= a.userid < 50 for a in accounts)
def test_query_article_inequality(testbed):
Article(tags=['perl']).put()
Article(tags=['perl', 'python']).put()
query = snippets.query_article_inequality()
articles = query.fetch()
assert len(articles) == 1
def test_query_article_inequality_explicit(testbed):
Article(tags=['perl']).put()
Article(tags=['perl', 'python']).put()
query = snippets.query_article_inequality_explicit()
articles = query.fetch()
assert len(articles) == 1
def test_articles_with_tags_example(testbed):
snippets.articles_with_tags_example()
def test_query_article_in(testbed):
Article(tags=['perl']).put()
Article(tags=['perl', 'python']).put()
Article(tags=['ruby']).put()
Article(tags=['php']).put()
query = snippets.query_article_in()
articles = query.fetch()
assert len(articles) == 3
def test_query_article_in_equivalent(testbed):
Article(tags=['perl']).put()
Article(tags=['perl', 'python']).put()
Article(tags=['ruby']).put()
Article(tags=['php']).put()
query = snippets.query_article_in_equivalent()
articles = query.fetch()
assert len(articles) == 3
def test_query_article_nested(testbed):
Article(tags=['python']).put() # excluded - no non-python
Article(tags=['ruby']).put() # excluded - no python
Article(tags=['python', 'ruby']).put() # included
Article(tags=['python', 'jruby']).put() # included
Article(tags=['python', 'ruby', 'jruby']).put() # included
Article(tags=['python', 'php']).put() # included
Article(tags=['python', 'perl']).put() # excluded
query = snippets.query_article_nested()
articles = query.fetch()
assert len(articles) == 4
def test_query_greeting_order(testbed):
Greeting(content='3').put()
Greeting(content='2').put()
Greeting(content='1').put()
Greeting(content='2').put()
query = snippets.query_greeting_order()
greetings = query.fetch()
assert (greetings[0].content < greetings[1].content < greetings[3].content)
assert greetings[1].content == greetings[2].content
assert greetings[1].date > greetings[2].date
def test_query_greeting_multiple_orders(testbed):
Greeting(content='3').put()
Greeting(content='2').put()
Greeting(content='1').put()
Greeting(content='2').put()
query = snippets.query_greeting_multiple_orders()
greetings = query.fetch()
assert (greetings[0].content < greetings[1].content < greetings[3].content)
assert greetings[1].content == greetings[2].content
assert greetings[1].date > greetings[2].date
def test_query_purchase_with_customer_key(testbed):
Customer, Purchase, do_query = snippets.query_purchase_with_customer_key()
charles = Customer(name='Charles')
charles.put()
snoop_key = Customer(name='Snoop').put()
Purchase(price=123, customer=charles.key).put()
Purchase(price=234, customer=snoop_key).put()
purchases = do_query(charles)
assert len(purchases) == 1
assert purchases[0].price == 123
def test_query_purchase_with_ancestor_key(testbed):
Customer, Purchase, create_purchase, do_query = (
snippets.query_purchase_with_ancestor_key())
charles = Customer(name='Charles')
charles.put()
snoop = Customer(name='Snoop')
snoop.put()
charles_purchase = create_purchase(charles)
charles_purchase.price = 123
charles_purchase.put()
snoop_purchase = create_purchase(snoop)
snoop_purchase.price = 234
snoop_purchase.put()
purchases = do_query(snoop)
assert len(purchases) == 1
assert purchases[0].price == 234
def test_print_query(testbed, capsys):
snippets.print_query()
stdout, _ = capsys.readouterr()
assert '' in stdout
def test_query_contact_with_city(testbed):
address = Address(type='home', street='Spear St', city='Amsterdam')
address.put()
Contact(name='Bertus Aafjes', addresses=[address]).put()
address1 = Address(type='home', street='Damrak St', city='Amsterdam')
address1.put()
address2 = Address(type='work', street='Spear St', city='San Francisco')
address2.put()
Contact(name='Willem Jan Aalders', addresses=[address1, address2]).put()
address = Address(type='home', street='29th St', city='San Francisco')
address.put()
Contact(name='Hans Aarsman', addresses=[address]).put()
query = snippets.query_contact_with_city()
contacts = query.fetch()
assert len(contacts) == 2
def test_query_contact_sub_entities_beware(testbed):
address = Address(type='home', street='Spear St', city='Amsterdam')
address.put()
Contact(name='Bertus Aafjes', addresses=[address]).put()
address1 = Address(type='home', street='Damrak St', city='Amsterdam')
address1.put()
address2 = Address(type='work', street='Spear St', city='San Francisco')
address2.put()
Contact(name='Willem Jan Aalders', addresses=[address1, address2]).put()
address = Address(type='home', street='29th St', city='San Francisco')
address.put()
Contact(name='Hans Aarsman', addresses=[address]).put()
query = snippets.query_contact_sub_entities_beware()
contacts = query.fetch()
assert len(contacts) == 2
for contact in contacts:
assert ('Spear St' in [a.street for a in contact.addresses] or
'Amsterdam' in [a.city for a in contact.addresses])
def test_query_contact_multiple_values_in_single_sub_entity(testbed):
address = Address(type='home', street='Spear St', city='Amsterdam')
address.put()
Contact(name='Bertus Aafjes', addresses=[address]).put()
address1 = Address(type='home', street='Damrak St', city='Amsterdam')
address1.put()
address2 = Address(type='work', street='Spear St', city='San Francisco')
address2.put()
Contact(name='Willem Jan Aalders', addresses=[address1, address2]).put()
address = Address(type='home', street='29th St', city='San Francisco')
address.put()
Contact(name='Hans Aarsman', addresses=[address]).put()
query = snippets.query_contact_multiple_values_in_single_sub_entity()
contacts = query.fetch()
assert len(contacts) == 1
assert any(a.city == 'San Francisco' and a.street == 'Spear St'
for a in contacts[0].addresses)
def test_query_properties_named_by_string_on_expando(testbed):
FlexEmployee(location='SF').put()
FlexEmployee(location='Amsterdam').put()
query = snippets.query_properties_named_by_string_on_expando()
employees = query.fetch()
assert len(employees) == 1
def test_query_properties_named_by_string_for_defined_properties(testbed):
Article(title='from').put()
Article(title='to').put()
query = snippets.query_properties_named_by_string_for_defined_properties(
'title', 'from')
articles = query.fetch()
assert len(articles) == 1
def test_query_properties_named_by_string_using_getattr(testbed):
Article(title='from').put()
Article(title='to').put()
query = snippets.query_properties_named_by_string_using_getattr(
'title', 'from')
articles = query.fetch()
assert len(articles) == 1
def test_order_query_results_by_property(testbed):
Article(title='2').put()
Article(title='1').put()
FlexEmployee(location=2).put()
FlexEmployee(location=1).put()
expando_query, property_query = snippets.order_query_results_by_property(
'title')
assert expando_query.fetch()[0].location == 1
assert property_query.fetch()[0].title == '1'
def test_print_query_keys(testbed, capsys):
for i in range(3):
Article(title='title {}'.format(i)).put()
snippets.print_query_keys(Article.query())
stdout, _ = capsys.readouterr()
assert "Key('Article'" in stdout
def test_reverse_queries(testbed):
for i in range(11):
Bar().put()
(bars, cursor, more), (r_bars, r_cursor, r_more) = (
snippets.reverse_queries())
assert len(bars) == 10
assert len(r_bars) == 10
for prev_bar, bar in zip(bars, bars[1:]):
assert prev_bar.key < bar.key
for prev_bar, bar in zip(r_bars, r_bars[1:]):
assert prev_bar.key > bar.key
def test_fetch_message_accounts_inefficient(testbed):
for i in range(1, 6):
Account(username='Account %s' % i, id=i).put()
Message(content='Message %s' % i, userid=i).put()
message_account_pairs = snippets.fetch_message_accounts_inefficient(
Message.query().order(Message.userid))
assert len(message_account_pairs) == 5
print repr(message_account_pairs)
for i in range(1, 6):
message, account = message_account_pairs[i - 1]
assert message.content == 'Message %s' % i
assert account.username == 'Account %s' % i
def test_fetch_message_accounts_efficient(testbed):
for i in range(1, 6):
Account(username='Account %s' % i, id=i).put()
Message(content='Message %s' % i, userid=i).put()
message_account_pairs = snippets.fetch_message_accounts_efficient(
Message.query().order(Message.userid))
assert len(message_account_pairs) == 5
for i in range(1, 6):
message, account = message_account_pairs[i - 1]
assert message.content == 'Message %s' % i
assert account.username == 'Account %s' % i
def test_fetch_good_articles_using_gql_with_explicit_bind(testbed):
for i in range(1, 6):
Article(stars=i).put()
query, query2 = snippets.fetch_good_articles_using_gql_with_explicit_bind()
articles = query2.fetch()
assert len(articles) == 2
assert all(a.stars > 3 for a in articles)
def test_fetch_good_articles_using_gql_with_inlined_bind(testbed):
for i in range(1, 6):
Article(stars=i).put()
query = snippets.fetch_good_articles_using_gql_with_inlined_bind()
articles = query.fetch()
assert len(articles) == 2
assert all(a.stars > 3 for a in articles)