-
Notifications
You must be signed in to change notification settings - Fork 411
/
Copy pathsearch_test.py
707 lines (612 loc) · 27.7 KB
/
search_test.py
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# Copyright 2021 Google Inc.
#
# 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.
import testing_config # isort: split
import datetime
from unittest import mock
from internals import core_enums, notifier, search
from internals.core_models import FeatureEntry, MilestoneSet, Stage
from internals.review_models import Gate, Vote
from internals.search_queries import Interval
class SearchRETest(testing_config.CustomTestCase):
# Note: User queries will always have a space appended before parsing.
def test_empty_query(self):
"""An empty user query string should yield zero serach terms."""
self.assertEqual([], search.TERM_RE.findall(''))
self.assertEqual([], search.TERM_RE.findall(' '))
def test_structured_query_terms(self):
"""We can parse operator terms."""
self.assertEqual(
[('', 'field', '=', 'value', '')],
search.TERM_RE.findall('field=value '))
self.assertEqual(
[('', 'field', '>', 'value', '')],
search.TERM_RE.findall('field>value '))
self.assertEqual(
[('', 'flag_name', '=', 'version', '')],
search.TERM_RE.findall('flag_name=version '))
self.assertEqual(
[('', 'flag_name', '=', 'enable-super-stuff', '')],
search.TERM_RE.findall('flag_name=enable-super-stuff '))
self.assertEqual(
[('', 'flag_name', '=', '"enable super stuff"', '')],
search.TERM_RE.findall('flag_name="enable super stuff" '))
def test_structured_query_terms__complex(self):
"""We can parse complex operator terms."""
self.assertEqual(
[('-', 'field', '=', 'value', '')],
search.TERM_RE.findall('-field=value '))
self.assertEqual(
[('OR ', 'field', '>', 'value', '')],
search.TERM_RE.findall('OR field>value '))
self.assertEqual(
[('-', 'flag_name', '=', 'version', '')],
search.TERM_RE.findall('-flag_name=version '))
self.assertEqual(
[('OR ', 'flag_name', '=', 'enable-super-stuff', '')],
search.TERM_RE.findall('OR flag_name=enable-super-stuff '))
def test_structured_query_terms__quick_or(self):
"""We can parse queries that use quick-OR syntax for multiple values."""
self.assertEqual(
[('', 'field', '=', 'value1,value2,value3', '')],
search.TERM_RE.findall('field=value1,value2,value3 '))
self.assertEqual(
[('', 'field', '=', '1,2,3', '')],
search.TERM_RE.findall('field=1,2,3 '))
self.assertEqual(
[('', 'field', '=', '"one","two","three"', '')],
search.TERM_RE.findall('field="one","two","three" '))
self.assertEqual(
[('', 'field', '=', '"enum one","enum two","enum three"', '')],
search.TERM_RE.findall('field="enum one","enum two","enum three" '))
def test_structured_query_terms__interval(self):
"""We can parse queries that use interval syntax for paired inequalities."""
self.assertEqual(
[('', 'field', '=', '1..7', '')], search.TERM_RE.findall('field=1..7 ')
)
self.assertEqual(
[('', 'field', '=', '2024-01-01..2024-04-01', '')],
search.TERM_RE.findall('field=2024-01-01..2024-04-01 '),
)
self.assertEqual(
[('', 'field', '=', '"one".."three"', '')],
search.TERM_RE.findall('field="one".."three" '),
)
def test_text_terms(self):
"""We can parse text terms."""
self.assertEqual(
[('', '', '', '', 'hello')],
search.TERM_RE.findall('hello '))
self.assertEqual(
[('', '', '', '', '"hello there people"')],
search.TERM_RE.findall('"hello there people" '))
self.assertEqual(
[('', '', '', '', '"memory location $0x25"')],
search.TERM_RE.findall('"memory location $0x25" '))
def test_text_terms__complex(self):
"""We can parse complex text terms."""
self.assertEqual(
[('-', '', '', '', 'hello')],
search.TERM_RE.findall('-hello '))
self.assertEqual(
[('-', '', '', '', '"hello there people"')],
search.TERM_RE.findall('-"hello there people" '))
self.assertEqual(
[('OR ', '', '', '', '"memory location $0x25"')],
search.TERM_RE.findall('OR "memory location $0x25" '))
def test_malformed(self):
"""Malformed queries are treated like full text, junk ignored."""
self.assertEqual(
[],
search.TERM_RE.findall(':: = == := > >> >>> '))
self.assertEqual(
[('', '', '', '', 'word')],
search.TERM_RE.findall('=word '))
self.assertEqual(
[('', '', '', '', '1,2..3')], search.TERM_RE.findall('field=1,2..3 ')
)
self.assertEqual(
[('', '', '', '', '1..2..3')], search.TERM_RE.findall('field=1..2..3 ')
)
class SearchParsingTest(testing_config.CustomTestCase):
def test_parse_query_value__dates(self):
d = datetime.datetime
context = search.QueryContext(now=d(2024, 5, 15), current_stable_milestone=0)
self.assertEqual(d(2024, 5, 15), search.parse_query_value('now', context))
self.assertEqual(d(2024, 5, 13), search.parse_query_value('now-2d', context))
self.assertEqual(d(2024, 5, 18), search.parse_query_value('now+3d', context))
self.assertEqual(d(2024, 4, 10), search.parse_query_value('now-5w', context))
self.assertEqual(d(2024, 6, 5), search.parse_query_value('now+3w', context))
self.assertEqual(
'now+1000000000d',
search.parse_query_value('now+1000000000d', context),
'Overflow should fall back to a string.',
)
self.assertEqual(d(2023, 7, 8), search.parse_query_value('2023-07-08', context))
self.assertEqual(d(2023, 7, 8), search.parse_query_value('2023-7-8', context))
# And some cases that shouldn't parse:
self.assertEqual('nows', search.parse_query_value('nows', context))
self.assertEqual('now-2days', search.parse_query_value('now-2days', context))
self.assertEqual('now + 2d', search.parse_query_value('now + 2d', context))
self.assertEqual('now-5weeks', search.parse_query_value('now-5weeks', context))
self.assertEqual('2023-13-8', search.parse_query_value('2023-13-8', context))
self.assertEqual('2023/07/08', search.parse_query_value('2023/07/08', context))
def test_parse_query_value__milestones(self):
context = search.QueryContext(
now=datetime.datetime(1, 1, 1), current_stable_milestone=123
)
self.assertEqual(123, search.parse_query_value('current_stable', context))
self.assertEqual(122, search.parse_query_value('current_stable-1', context))
self.assertEqual(128, search.parse_query_value('current_stable+5', context))
# And some cases that shouldn't parse:
self.assertEqual(
'current_stable_m', search.parse_query_value('current_stable_m', context)
)
self.assertEqual(
'current_stable+2m', search.parse_query_value('current_stable+2m', context)
)
def test_parse_query_value__intervals(self):
context = search.QueryContext(
now=datetime.datetime(2024, 5, 15), current_stable_milestone=0
)
self.assertEqual([Interval(1, 5)], search.parse_query_value_list('1..5', context))
self.assertEqual(
[Interval(datetime.datetime(2023, 1, 1), datetime.datetime(2024, 1, 1))],
search.parse_query_value_list('2023-01-01..2024-01-01', context),
)
# This parses, but it's excluded by the regex and isn't converted into an efficient query.
self.assertEqual(
[1, Interval(2, 3)], search.parse_query_value_list('1,2..3', context)
)
self.assertEqual(
['1..2..3'],
search.parse_query_value_list('1..2..3', context),
"Don't get confused by funny syntax",
)
class SearchFunctionsTest(testing_config.CustomTestCase):
def setUp(self):
self.featureentry_1 = FeatureEntry(
created=datetime.datetime(2024, 4, 4),
name='feature 1',
summary='sum',
category=1,
web_dev_views=1,
impl_status_chrome=3,
)
self.featureentry_1.owner_emails = ['[email protected]']
self.featureentry_1.editor_emails = ['[email protected]']
self.featureentry_1.cc_emailss = ['[email protected]']
self.featureentry_1.put()
self.featureentry_1_shipping_stage = Stage(
feature_id=self.featureentry_1.key.id(),
stage_type=core_enums.STAGE_BLINK_SHIPPING,
milestones=MilestoneSet(desktop_first=123),
)
self.featureentry_1_shipping_stage.put()
self.featureentry_1.active_stage_id = self.featureentry_1_shipping_stage.key.id()
self.featureentry_1.put()
self.featureentry_2 = FeatureEntry(
created=datetime.datetime(2024, 3, 4),
name='feature 2',
summary='sum',
category=2,
web_dev_views=1,
impl_status_chrome=3,
accurate_as_of=datetime.datetime(2024, 5, 23),
)
self.featureentry_2.owner_emails = ['[email protected]']
self.featureentry_2.put()
notifier.FeatureStar.set_star(
'[email protected]', self.featureentry_1.key.integer_id())
self.featureentry_3 = FeatureEntry(
created=datetime.datetime(2024, 2, 4),
name='feature 3',
summary='sum',
category=3,
web_dev_views=1,
impl_status_chrome=3,
unlisted=True,
)
self.featureentry_3.owner_emails = ['[email protected]']
self.featureentry_3.put()
self.featureentry_4 = FeatureEntry(
created=datetime.datetime(2024, 1, 4),
name='feature 4',
summary='sum',
category=4,
web_dev_views=1,
impl_status_chrome=4,
feature_type=core_enums.FEATURE_TYPE_ENTERPRISE_ID,
)
self.featureentry_4.owner_emails = ['[email protected]']
self.featureentry_4.put()
def tearDown(self):
notifier.FeatureStar.set_star(
'[email protected]', self.featureentry_1.key.integer_id(),
starred=False)
self.featureentry_1.key.delete()
self.featureentry_2.key.delete()
self.featureentry_3.key.delete()
self.featureentry_4.key.delete()
for kind in [Gate, FeatureEntry]:
for entity in kind.query():
entity.key.delete()
@mock.patch('internals.approval_defs.fields_approvable_by')
def test_process_pending_approval_me_query__none(
self, mock_approvable_by):
"""Nothing is pending."""
testing_config.sign_in('[email protected]', 111)
now = datetime.datetime.now()
mock_approvable_by.return_value = set([1, 2, 3])
future = search.process_pending_approval_me_query()
feature_ids = search._resolve_promise_to_id_list(future)
self.assertEqual(0, len(feature_ids))
@mock.patch('internals.approval_defs.fields_approvable_by')
def test_process_pending_approval_me_query__some__nonapprover(
self, mock_approvable_by):
"""It's not a pending approval for you."""
testing_config.sign_in('[email protected]', 111)
now = datetime.datetime.now()
mock_approvable_by.return_value = set()
Gate(
feature_id=self.featureentry_1.key.integer_id(), stage_id=1,
gate_type=1, state=Vote.REVIEW_REQUESTED,
requested_on=now).put()
future = search.process_pending_approval_me_query()
feature_ids = search._resolve_promise_to_id_list(future)
self.assertEqual(0, len(feature_ids))
@mock.patch('internals.approval_defs.fields_approvable_by')
def test_process_pending_approval_me_query__some__approver(
self, mock_approvable_by):
"""We can return a list of features pending approval."""
testing_config.sign_in('[email protected]', 111)
time_1 = datetime.datetime.now() - datetime.timedelta(days=4)
time_2 = datetime.datetime.now()
mock_approvable_by.return_value = set([1, 2, 3])
Gate(
feature_id=self.featureentry_2.key.integer_id(), stage_id=1,
gate_type=1, state=Vote.REVIEW_REQUESTED,
requested_on=time_1).put()
Gate(
feature_id=self.featureentry_1.key.integer_id(), stage_id=1,
gate_type=1, state=Vote.REVIEW_REQUESTED,
requested_on=time_2).put()
future = search.process_pending_approval_me_query()
feature_ids = search._resolve_promise_to_id_list(future)
self.assertEqual(2, len(feature_ids))
# Results are not sorted.
self.assertCountEqual(
[self.featureentry_2.key.integer_id(),
self.featureentry_1.key.integer_id()],
feature_ids)
@mock.patch('internals.approval_defs.fields_approvable_by')
def test_process_pending_approval_me_query__mixed__approver(
self, mock_approvable_by):
"""We can find gates that are pending approval."""
testing_config.sign_in('[email protected]', 111)
time_1 = datetime.datetime.now() - datetime.timedelta(days=4)
time_2 = datetime.datetime.now()
mock_approvable_by.return_value = set([1, 2, 3])
Gate(
feature_id=self.featureentry_2.key.integer_id(), stage_id=1,
gate_type=1, state=Vote.REVIEW_REQUESTED,
requested_on=time_1).put()
Gate(
feature_id=self.featureentry_1.key.integer_id(), stage_id=1,
gate_type=1, state=Vote.APPROVED,
requested_on=time_2).put()
future = search.process_pending_approval_me_query()
feature_ids = search._resolve_promise_to_id_list(future)
self.assertEqual(1, len(feature_ids))
# Results are not sorted.
self.assertEqual(
[self.featureentry_2.key.integer_id()],
feature_ids)
def test_process_starred_me_query__anon(self):
"""Anon always has an empty list of starred features."""
testing_config.sign_out()
actual = search.process_starred_me_query()
self.assertEqual(actual, [])
def test_process_starred_me_query__none(self):
"""We can return a list of features starred by the user."""
testing_config.sign_in('[email protected]', 111)
actual = search.process_starred_me_query()
self.assertEqual(actual, [])
def test_process_starred_me_query__some(self):
"""We can return a list of features starred by the user."""
testing_config.sign_in('[email protected]', 111)
actual = search.process_starred_me_query()
self.assertEqual(len(actual), 1)
self.assertEqual(actual[0], self.featureentry_1.key.integer_id())
@mock.patch('internals.approval_defs.fields_approvable_by')
def test_process_recent_reviews_query__none(
self, mock_approvable_by):
"""Nothing has been reviewed recently."""
mock_approvable_by.return_value = set({1, 2, 3})
future = search.process_recent_reviews_query()
feature_ids = search._resolve_promise_to_id_list(future)
self.assertEqual(0, len(feature_ids))
@mock.patch('internals.approval_defs.fields_approvable_by')
def test_process_recent_reviews_query__some(
self, mock_approvable_by):
"""Some features have been reviewed recently."""
mock_approvable_by.return_value = set({1, 2, 3})
time_1 = datetime.datetime.now() - datetime.timedelta(days=4)
time_2 = datetime.datetime.now()
time_3 = datetime.datetime.now() - datetime.timedelta(days=100)
fe_1_id = self.featureentry_1.key.integer_id()
fe_2_id = self.featureentry_2.key.integer_id()
gate_1 = Gate(
feature_id=fe_1_id, stage_id=1, gate_type=1, state=Vote.NA,
requested_on=time_2)
gate_1.put()
vote_1_1 = Vote(
feature_id=fe_1_id, gate_id=gate_1.key.integer_id(),
state=Vote.NA, set_on=time_2, set_by='[email protected]')
vote_1_1.put()
gate_2 = Gate(
feature_id=fe_2_id, stage_id=1, gate_type=1, state=Vote.APPROVED,
requested_on=time_1)
gate_2.put()
vote_2_1 = Vote(
feature_id=fe_2_id, gate_id=gate_2.key.integer_id(),
state=Vote.APPROVED, set_on=time_1, set_by='[email protected]')
vote_2_1.put()
vote_2_2 = Vote(
feature_id=fe_2_id, gate_id=gate_2.key.integer_id(),
state=Vote.APPROVED, set_on=time_3, set_by='[email protected]')
vote_2_2.put()
future = search.process_recent_reviews_query()
feature_ids = search._resolve_promise_to_id_list(future)
# Note: vote_2_2 does not contribute to the list because it is too old.
self.assertEqual([fe_1_id, fe_2_id], feature_ids)
def test_sort_by_total_order__empty(self):
"""Sorting an empty list works."""
feature_ids = []
total_order_ids = []
actual = search._sort_by_total_order(feature_ids, total_order_ids)
self.assertEqual([], actual)
total_order_ids = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
actual = search._sort_by_total_order(feature_ids, total_order_ids)
self.assertEqual([], actual)
def test_sort_by_total_order__normal(self):
"""We can sort the results according to the total order."""
feature_ids = [10, 1, 9, 4]
total_order_ids = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
actual = search._sort_by_total_order(feature_ids, total_order_ids)
self.assertEqual([10, 9, 4, 1], actual)
def test_sort_by_total_order__unordered_at_end(self):
"""If the results include features not present in the total order,
they are put at the end of the list in ID order."""
feature_ids = [999, 10, 998, 1, 9, 997, 4]
total_order_ids = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
actual = search._sort_by_total_order(feature_ids, total_order_ids)
self.assertEqual([10, 9, 4, 1, 997, 998, 999], actual)
def test_sort_by_total_order__multiple_items(self):
"""If the sort order is done via join, the total_order could have
multiple copies of the same feature IDs. We use the earliest."""
feature_ids = [10, 1, 9, 4]
total_order_ids = [10, 9, 8, 7, 9, 6, 10, 5, 8, 4, 7, 3, 9, 6, 2, 1, 1, 4]
actual = search._sort_by_total_order(feature_ids, total_order_ids)
self.assertEqual([10, 9, 4, 1], actual)
def test_make_cache_key(self):
"""We can make a search cache key."""
self.assertEqual(
('FeatureSearch||sort_spec=None|show_unlisted=True|'
'show_deleted=False|show_enterprise=False|'
'start=0|num=100|name_only=True'),
search.make_cache_key('', None, True, False, False, 0, 100, True))
self.assertEqual(
('FeatureSearch|canvas|sort_spec=created.when|show_unlisted=False|'
'show_deleted=True|show_enterprise=True|'
'start=1|num=20|name_only=False'),
search.make_cache_key(
'canvas', 'created.when', False, True, True, 1, 20, False))
def test_is_cacheable(self):
"""We can make a search cache key."""
self.assertTrue(search.is_cacheable('', True))
self.assertTrue(search.is_cacheable('canvas', True))
self.assertTrue(search.is_cacheable('feature_type=4', True))
self.assertFalse(search.is_cacheable('starred-by:me', True))
self.assertFalse(search.is_cacheable('owner:me', True))
self.assertFalse(search.is_cacheable('pending-approval-by:me', True))
self.assertFalse(search.is_cacheable('is:recently-reviewed', True))
self.assertFalse(search.is_cacheable('created.when<now', True))
self.assertFalse(search.is_cacheable('shipping>current_stable', True))
self.assertFalse(search.is_cacheable('canvas', False))
@mock.patch('internals.search.process_pending_approval_me_query')
@mock.patch('internals.search.process_starred_me_query')
@mock.patch('internals.search_queries.handle_me_query_async')
@mock.patch('internals.search.process_recent_reviews_query')
def test_process_query__predefined(
self, mock_recent, mock_own_me, mock_star_me, mock_pend_me):
"""We can match predefined queries."""
mock_recent.return_value = [self.featureentry_1.key.integer_id()]
mock_own_me.return_value = [self.featureentry_2.key.integer_id()]
mock_star_me.return_value = [self.featureentry_1.key.integer_id()]
mock_pend_me.return_value = [self.featureentry_2.key.integer_id()]
actual_pending, tc = search.process_query('pending-approval-by:me')
self.assertEqual(actual_pending[0]['name'], 'feature 2')
actual_star_me, tc = search.process_query('starred-by:me')
self.assertEqual(actual_star_me[0]['name'], 'feature 1')
actual_own_me, tc = search.process_query('owner:me')
self.assertEqual(actual_own_me[0]['name'], 'feature 2')
actual_recent, tc = search.process_query('is:recently-reviewed')
self.assertEqual(actual_recent[0]['name'],'feature 1')
@mock.patch('internals.search.process_pending_approval_me_query')
@mock.patch('internals.search.process_starred_me_query')
@mock.patch('internals.search_queries.handle_me_query_async')
@mock.patch('internals.search.process_recent_reviews_query')
def test_process_query__negated_predefined(
self, mock_recent, mock_own_me, mock_star_me, mock_pend_me):
"""We can match predefined queries."""
mock_recent.return_value = [self.featureentry_1.key.integer_id()]
mock_own_me.return_value = [self.featureentry_2.key.integer_id()]
mock_star_me.return_value = [self.featureentry_1.key.integer_id()]
mock_pend_me.return_value = [self.featureentry_2.key.integer_id()]
actual_pending, tc = search.process_query('-pending-approval-by:me')
self.assertEqual(actual_pending[0]['name'], 'feature 1')
actual_star_me, tc = search.process_query('-starred-by:me')
self.assertEqual(actual_star_me[0]['name'], 'feature 2')
actual_own_me, tc = search.process_query('-owner:me')
self.assertEqual(actual_own_me[0]['name'], 'feature 1')
actual_recent, tc = search.process_query('-is:recently-reviewed')
self.assertEqual(actual_recent[0]['name'],'feature 2')
def test_process_query__single_field(self):
"""We can run single-field queries."""
actual, tc = search.process_query('')
self.assertEqual(2, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2'])
actual, tc = search.process_query('category="Web Components"')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 1')
actual, tc = search.process_query('category=Miscellaneous')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 2')
actual, tc = search.process_query('category="Miscellaneous"')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 2')
actual, tc = search.process_query('name="feature 2"')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 2')
actual, tc = search.process_query('browsers.webdev.view="Strongly positive"')
self.assertEqual(2, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2'])
actual, tc = search.process_query(
'accurate_as_of=now-3d',
context=search.QueryContext(
now=datetime.datetime(2024, 5, 26), current_stable_milestone=0
),
)
self.assertEqual(1, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 2'])
def test_process_query__milestones(self):
"""We can find milestones."""
actual, tc = search.process_query(
'browsers.chrome.desktop=current_stable+2',
context=search.QueryContext(
now=datetime.datetime(1, 1, 1), current_stable_milestone=121
),
)
self.assertCountEqual([f['name'] for f in actual], ['feature 1'])
def test_process_query__interval(self):
"""We can run interval queries."""
self.featureentry_3.unlisted = False # Increase the set of possible results.
self.featureentry_3.put()
actual, tc = search.process_query('created.when=2024-02-02..2024-04-01')
self.assertCountEqual([f['name'] for f in actual], ['feature 2', 'feature 3'])
def test_process_query__show_deleted_unlisted(self):
"""We can run queries without deleted/unlisted features."""
actual, tc = search.process_query('', show_deleted=True)
self.assertEqual(2, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2'])
actual, tc = search.process_query('', show_unlisted=True)
self.assertEqual(3, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2', 'feature 3'])
actual, tc = search.process_query(
'', show_unlisted=True, show_deleted=True)
self.assertEqual(3, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2', 'feature 3'])
actual, tc = search.process_query(
'category="Web Components" category=Security', show_unlisted=True)
self.assertEqual([], actual)
actual, tc = search.process_query(
'category="Web Components" OR category=Security', show_unlisted=True)
self.assertEqual(2, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 3'])
actual, tc = search.process_query(
'category="Web Components" OR category=Security', show_deleted=True)
self.assertEqual(1, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1'])
def test_process_query__show_enterprise(self):
"""We can run queries without or without enterprise features."""
actual, tc = search.process_query('')
self.assertEqual(2, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2'])
actual, tc = search.process_query('', show_enterprise=True)
self.assertEqual(3, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2', 'feature 4'])
def test_process_query__negated_single_field(self):
"""We can run single-field queries."""
actual, tc = search.process_query('-category="Web Components"')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 2')
actual, tc = search.process_query('-category=Miscellaneous')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 1')
actual, tc = search.process_query('-category="Miscellaneous"')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 1')
actual, tc = search.process_query('-name="feature 2"')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 1')
actual, tc = search.process_query('-browsers.webdev.view="Strongly positive"')
self.assertEqual(0, len(actual))
def test_process_query__multiple_fields(self):
"""We can run multi-field queries."""
actual, tc = search.process_query(
'category="Web Components" category=Miscellaneous')
self.assertEqual([], actual)
actual, tc = search.process_query(
'category="Web Components" OR category=Miscellaneous')
self.assertEqual(2, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2'])
actual, tc = search.process_query(
'category="Web Components" -category=Miscellaneous')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 1')
actual, tc = search.process_query(
'browsers.webdev.view="Strongly positive" -category=Miscellaneous')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 1')
actual, tc = search.process_query(
'browsers.webdev.view="Strongly positive" '
'-category=Miscellaneous OR category=Miscellaneous')
self.assertEqual(2, len(actual))
self.assertCountEqual(
[f['name'] for f in actual],
['feature 1', 'feature 2'])
actual, tc = search.process_query(
'browsers.webdev.view="Strongly positive" -category=Miscellaneous '
'OR category="Web Components" -category=Miscellaneous')
self.assertEqual(1, len(actual))
self.assertEqual(actual[0]['name'], 'feature 1')
@mock.patch('logging.warning')
def test_process_query__bad(self, mock_warn):
"""Query terms that are not valid, give warnings."""
self.assertEqual(
search.process_query('any:thing e=lse'),
([], 0))
self.assertEqual(2, len(mock_warn.mock_calls))