-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathtest_search.py
More file actions
951 lines (775 loc) · 39.3 KB
/
Copy pathtest_search.py
File metadata and controls
951 lines (775 loc) · 39.3 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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
from datetime import timedelta
from typing import Any
import grpc
import pytest
from google.protobuf import empty_pb2, wrappers_pb2
from sqlalchemy import select
from couchers.db import session_scope
from couchers.materialized_views import refresh_materialized_views, refresh_materialized_views_rapid
from couchers.models import EventOccurrence, HostingStatus, LanguageAbility, LanguageFluency, MeetupStatus
from couchers.proto import api_pb2, communities_pb2, events_pb2, search_pb2
from couchers.utils import Timestamp_from_datetime, create_coordinate, millis_from_dt, now
from tests.fixtures.db import generate_user
from tests.fixtures.misc import Moderator
from tests.fixtures.sessions import communities_session, events_session, search_session
from tests.test_communities import create_community, testing_communities # noqa
from tests.test_references import create_friend_reference
@pytest.fixture(autouse=True)
def _(testconfig):
pass
def test_Search(testing_communities):
user, token = generate_user()
with search_session(token) as api:
res = api.Search(
search_pb2.SearchReq(
query="Country 1, Region 1",
include_users=True,
include_communities=True,
include_groups=True,
include_places=True,
include_guides=True,
)
)
res = api.Search(
search_pb2.SearchReq(
query="Country 1, Region 1, Attraction",
title_only=True,
include_users=True,
include_communities=True,
include_groups=True,
include_places=True,
include_guides=True,
)
)
def test_UserSearch(testing_communities):
"""Test that UserSearch returns all users if no filter is set."""
user, token = generate_user()
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token) as api:
res = api.UserSearch(search_pb2.UserSearchReq())
assert len(res.results) > 0
assert res.total_items == len(res.results)
res = api.UserSearchV2(search_pb2.UserSearchReq())
assert len(res.results) > 0
assert res.total_items == len(res.results)
def test_regression_search_in_area(db):
"""
Makes sure search_in_area works.
At the equator/prime meridian intersection (0,0), one degree is roughly 111 km.
"""
# outside
user1, token1 = generate_user(geom=create_coordinate(1, 0), geom_radius=100)
# outside
user2, token2 = generate_user(geom=create_coordinate(0, 1), geom_radius=100)
# inside
user3, token3 = generate_user(geom=create_coordinate(0.1, 0), geom_radius=100)
# inside
user4, token4 = generate_user(geom=create_coordinate(0, 0.1), geom_radius=100)
# outside
user5, token5 = generate_user(geom=create_coordinate(10, 10), geom_radius=100)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token5) as api:
res = api.UserSearch(
search_pb2.UserSearchReq(
search_in_area=search_pb2.Area(
lat=0,
lng=0,
radius=100000,
)
)
)
assert [result.user.user_id for result in res.results] == [user3.id, user4.id]
res = api.UserSearchV2(
search_pb2.UserSearchReq(
search_in_area=search_pb2.Area(
lat=0,
lng=0,
radius=100000,
)
)
)
assert [result.user_id for result in res.results] == [user3.id, user4.id]
def test_user_search_in_rectangle(db):
"""
Makes sure search_in_rectangle works as expected.
"""
# outside
user1, token1 = generate_user(geom=create_coordinate(-1, 0), geom_radius=100)
# outside
user2, token2 = generate_user(geom=create_coordinate(0, -1), geom_radius=100)
# inside
user3, token3 = generate_user(geom=create_coordinate(0.1, 0.1), geom_radius=100)
# inside
user4, token4 = generate_user(geom=create_coordinate(1.2, 0.1), geom_radius=100)
# outside (not fully inside)
user5, token5 = generate_user(geom=create_coordinate(0, 0), geom_radius=100)
# outside
user6, token6 = generate_user(geom=create_coordinate(0.1, 1.2), geom_radius=100)
# outside
user7, token7 = generate_user(geom=create_coordinate(10, 10), geom_radius=100)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token5) as api:
res = api.UserSearch(
search_pb2.UserSearchReq(
search_in_rectangle=search_pb2.RectArea(
lat_min=0,
lat_max=2,
lng_min=0,
lng_max=1,
)
)
)
assert [result.user.user_id for result in res.results] == [user3.id, user4.id]
res = api.UserSearchV2(
search_pb2.UserSearchReq(
search_in_rectangle=search_pb2.RectArea(
lat_min=0,
lat_max=2,
lng_min=0,
lng_max=1,
)
)
)
assert [result.user_id for result in res.results] == [user3.id, user4.id]
def test_user_filter_complete_profile(db):
"""
Make sure the completed profile flag returns only completed user profile
"""
user_complete_profile, token6 = generate_user(complete_profile=True)
user_incomplete_profile, token7 = generate_user(complete_profile=False)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token7) as api:
res = api.UserSearch(search_pb2.UserSearchReq(profile_completed=wrappers_pb2.BoolValue(value=False)))
assert user_incomplete_profile.id in [result.user.user_id for result in res.results]
res = api.UserSearchV2(search_pb2.UserSearchReq(profile_completed=wrappers_pb2.BoolValue(value=False)))
assert user_incomplete_profile.id in [result.user_id for result in res.results]
with search_session(token6) as api:
res = api.UserSearch(search_pb2.UserSearchReq(profile_completed=wrappers_pb2.BoolValue(value=True)))
assert [result.user.user_id for result in res.results] == [user_complete_profile.id]
res = api.UserSearchV2(search_pb2.UserSearchReq(profile_completed=wrappers_pb2.BoolValue(value=True)))
assert [result.user_id for result in res.results] == [user_complete_profile.id]
def test_user_filter_meetup_status(db):
"""
Make sure the completed profile flag returns only completed user profile
"""
user_wants_to_meetup, token8 = generate_user(meetup_status=MeetupStatus.wants_to_meetup)
user_does_not_want_to_meet, token9 = generate_user(meetup_status=MeetupStatus.does_not_want_to_meetup)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token8) as api:
res = api.UserSearch(search_pb2.UserSearchReq(meetup_status_filter=[api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP]))
assert user_wants_to_meetup.id in [result.user.user_id for result in res.results]
res = api.UserSearchV2(search_pb2.UserSearchReq(meetup_status_filter=[api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP]))
assert user_wants_to_meetup.id in [result.user_id for result in res.results]
with search_session(token9) as api:
res = api.UserSearch(
search_pb2.UserSearchReq(meetup_status_filter=[api_pb2.MEETUP_STATUS_DOES_NOT_WANT_TO_MEETUP])
)
assert [result.user.user_id for result in res.results] == [user_does_not_want_to_meet.id]
res = api.UserSearchV2(
search_pb2.UserSearchReq(meetup_status_filter=[api_pb2.MEETUP_STATUS_DOES_NOT_WANT_TO_MEETUP])
)
assert [result.user_id for result in res.results] == [user_does_not_want_to_meet.id]
def test_user_filter_language(db):
"""
Test filtering users by language ability.
"""
user_with_german_beginner, token11 = generate_user(hosting_status=HostingStatus.can_host)
user_with_japanese_conversational, token12 = generate_user(hosting_status=HostingStatus.can_host)
user_with_german_fluent, token13 = generate_user(hosting_status=HostingStatus.can_host)
with session_scope() as session:
session.add(
LanguageAbility(
user_id=user_with_german_beginner.id, language_code="deu", fluency=LanguageFluency.beginner
),
)
session.add(
LanguageAbility(
user_id=user_with_japanese_conversational.id,
language_code="jpn",
fluency=LanguageFluency.fluent,
)
)
session.add(
LanguageAbility(user_id=user_with_german_fluent.id, language_code="deu", fluency=LanguageFluency.fluent)
)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token11) as api:
res = api.UserSearch(
search_pb2.UserSearchReq(
language_ability_filter=[
api_pb2.LanguageAbility(
code="deu",
fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
)
]
)
)
assert [result.user.user_id for result in res.results] == [user_with_german_fluent.id]
res = api.UserSearchV2(
search_pb2.UserSearchReq(
language_ability_filter=[
api_pb2.LanguageAbility(
code="deu",
fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
)
]
)
)
assert [result.user_id for result in res.results] == [user_with_german_fluent.id]
res = api.UserSearch(
search_pb2.UserSearchReq(
language_ability_filter=[
api_pb2.LanguageAbility(
code="jpn",
fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_CONVERSATIONAL,
)
]
)
)
assert [result.user.user_id for result in res.results] == [user_with_japanese_conversational.id]
res = api.UserSearchV2(
search_pb2.UserSearchReq(
language_ability_filter=[
api_pb2.LanguageAbility(
code="jpn",
fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_CONVERSATIONAL,
)
]
)
)
assert [result.user_id for result in res.results] == [user_with_japanese_conversational.id]
def test_user_filter_strong_verification(db):
user1, token1 = generate_user()
user2, _ = generate_user(strong_verification=True)
user3, _ = generate_user()
user4, _ = generate_user(strong_verification=True)
user5, _ = generate_user(strong_verification=True)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token1) as api:
res = api.UserSearch(search_pb2.UserSearchReq(only_with_strong_verification=False))
assert [result.user.user_id for result in res.results] == [user1.id, user2.id, user3.id, user4.id, user5.id]
res = api.UserSearchV2(search_pb2.UserSearchReq(only_with_strong_verification=False))
assert [result.user_id for result in res.results] == [user1.id, user2.id, user3.id, user4.id, user5.id]
res = api.UserSearch(search_pb2.UserSearchReq(only_with_strong_verification=True))
assert [result.user.user_id for result in res.results] == [user2.id, user4.id, user5.id]
res = api.UserSearchV2(search_pb2.UserSearchReq(only_with_strong_verification=True))
assert [result.user_id for result in res.results] == [user2.id, user4.id, user5.id]
def test_regression_search_only_with_references(db):
user1, token1 = generate_user()
user2, _ = generate_user()
user3, _ = generate_user()
user4, _ = generate_user(delete_user=True)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with session_scope() as session:
# user 2 has references
create_friend_reference(session, user1.id, user2.id, timedelta(days=1))
create_friend_reference(session, user3.id, user2.id, timedelta(days=1))
create_friend_reference(session, user4.id, user2.id, timedelta(days=1))
# user 3 only has reference from a deleted user
create_friend_reference(session, user4.id, user3.id, timedelta(days=1))
with search_session(token1) as api:
res = api.UserSearch(search_pb2.UserSearchReq(only_with_references=False))
assert [result.user.user_id for result in res.results] == [user1.id, user2.id, user3.id]
res = api.UserSearchV2(search_pb2.UserSearchReq(only_with_references=False))
assert [result.user_id for result in res.results] == [user1.id, user2.id, user3.id]
res = api.UserSearch(search_pb2.UserSearchReq(only_with_references=True))
assert [result.user.user_id for result in res.results] == [user2.id]
res = api.UserSearchV2(search_pb2.UserSearchReq(only_with_references=True))
assert [result.user_id for result in res.results] == [user2.id]
def test_user_search_exactly_user_ids(db):
"""
Test that UserSearch with exactly_user_ids returns only those users and ignores other filters.
"""
# Create users with different properties
user1, token1 = generate_user()
user2, _ = generate_user(strong_verification=True)
user3, _ = generate_user(complete_profile=True)
user4, _ = generate_user(meetup_status=MeetupStatus.wants_to_meetup)
user5, _ = generate_user(delete_user=True) # Deleted user
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token1) as api:
# Test that exactly_user_ids returns only the specified users
res = api.UserSearch(search_pb2.UserSearchReq(exactly_user_ids=[user2.id, user3.id, user4.id]))
assert sorted([result.user.user_id for result in res.results]) == sorted([user2.id, user3.id, user4.id])
res = api.UserSearchV2(search_pb2.UserSearchReq(exactly_user_ids=[user2.id, user3.id, user4.id]))
assert sorted([result.user_id for result in res.results]) == sorted([user2.id, user3.id, user4.id])
# Test that exactly_user_ids ignores other filters
res = api.UserSearch(
search_pb2.UserSearchReq(
exactly_user_ids=[user2.id, user3.id, user4.id],
only_with_strong_verification=True, # This would normally filter out user3 and user4
)
)
assert sorted([result.user.user_id for result in res.results]) == sorted([user2.id, user3.id, user4.id])
res = api.UserSearchV2(
search_pb2.UserSearchReq(
exactly_user_ids=[user2.id, user3.id, user4.id],
only_with_strong_verification=True, # This would normally filter out user3 and user4
)
)
assert sorted([result.user_id for result in res.results]) == sorted([user2.id, user3.id, user4.id])
# Test with non-existent user IDs (should be ignored)
res = api.UserSearch(search_pb2.UserSearchReq(exactly_user_ids=[user1.id, 99999]))
assert [result.user.user_id for result in res.results] == [user1.id]
res = api.UserSearchV2(search_pb2.UserSearchReq(exactly_user_ids=[user1.id, 99999]))
assert [result.user_id for result in res.results] == [user1.id]
# Test with deleted user ID (should be ignored due to visibility filter)
res = api.UserSearch(search_pb2.UserSearchReq(exactly_user_ids=[user1.id, user5.id]))
assert [result.user.user_id for result in res.results] == [user1.id]
res = api.UserSearchV2(search_pb2.UserSearchReq(exactly_user_ids=[user1.id, user5.id]))
assert [result.user_id for result in res.results] == [user1.id]
@pytest.fixture
def sample_event_data() -> dict[str, Any]:
"""Dummy data for creating events."""
start_time = now() + timedelta(hours=2)
end_time = start_time + timedelta(hours=3)
return {
"title": "Dummy Title",
"content": "Dummy content.",
"photo_key": None,
"offline_information": events_pb2.OfflineEventInformation(address="Near Null Island", lat=0.1, lng=0.2),
"start_time": Timestamp_from_datetime(start_time),
"end_time": Timestamp_from_datetime(end_time),
"timezone": "UTC",
}
@pytest.fixture
def create_event(sample_event_data):
"""Factory for creating events."""
def _create_event(event_api, **kwargs) -> EventOccurrence:
"""Create an event with default values, unless overridden by kwargs."""
return event_api.CreateEvent(events_pb2.CreateEventReq(**{**sample_event_data, **kwargs})) # type: ignore
return _create_event
@pytest.fixture
def sample_community(db) -> int:
"""Create large community spanning from (-50, 0) to (50, 2) as events can only be created within communities."""
user, _ = generate_user()
with session_scope() as session:
return create_community(session, -50, 50, "Community", [user], [], None).id
def test_EventSearch_no_filters(testing_communities):
"""Test that EventSearch returns all events if no filter is set."""
user, token = generate_user()
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq())
assert len(res.events) > 0
def test_event_search_by_query(sample_community, create_event):
"""Test that EventSearch finds events by title (and content if query_title_only=False)."""
user, token = generate_user()
with events_session(token) as api:
event1 = create_event(api, title="Lorem Ipsum")
event2 = create_event(api, content="Lorem Ipsum")
create_event(api)
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq(query=wrappers_pb2.StringValue(value="Ipsum")))
assert len(res.events) == 2
assert {result.event_id for result in res.events} == {event1.event_id, event2.event_id}
res = api.EventSearch(
search_pb2.EventSearchReq(query=wrappers_pb2.StringValue(value="Ipsum"), query_title_only=True)
)
assert len(res.events) == 1
assert res.events[0].event_id == event1.event_id
def test_event_search_by_time(sample_community, create_event):
"""Test that EventSearch filters with the given time range."""
user, token = generate_user()
with events_session(token) as api:
event1 = create_event(
api,
start_time=Timestamp_from_datetime(now() + timedelta(hours=1)),
end_time=Timestamp_from_datetime(now() + timedelta(hours=2)),
)
event2 = create_event(
api,
start_time=Timestamp_from_datetime(now() + timedelta(hours=4)),
end_time=Timestamp_from_datetime(now() + timedelta(hours=5)),
)
event3 = create_event(
api,
start_time=Timestamp_from_datetime(now() + timedelta(hours=7)),
end_time=Timestamp_from_datetime(now() + timedelta(hours=8)),
)
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq(before=Timestamp_from_datetime(now() + timedelta(hours=6))))
assert len(res.events) == 2
assert {result.event_id for result in res.events} == {event1.event_id, event2.event_id}
res = api.EventSearch(search_pb2.EventSearchReq(after=Timestamp_from_datetime(now() + timedelta(hours=3))))
assert len(res.events) == 2
assert {result.event_id for result in res.events} == {event2.event_id, event3.event_id}
res = api.EventSearch(
search_pb2.EventSearchReq(
before=Timestamp_from_datetime(now() + timedelta(hours=6)),
after=Timestamp_from_datetime(now() + timedelta(hours=3)),
)
)
assert len(res.events) == 1
assert res.events[0].event_id == event2.event_id
def test_event_search_by_circle(sample_community, create_event):
"""Test that EventSearch only returns events within the given circle."""
user, token = generate_user()
with events_session(token) as api:
inside_pts = [(0.1, 0.01), (0.01, 0.1)]
for i, (lat, lng) in enumerate(inside_pts):
create_event(
api,
title=f"Inside area {i}",
offline_information=events_pb2.OfflineEventInformation(lat=lat, lng=lng, address=f"Inside area {i}"),
)
outside_pts = [(1, 0.1), (0.1, 1), (10, 1)]
for i, (lat, lng) in enumerate(outside_pts):
create_event(
api,
title=f"Outside area {i}",
offline_information=events_pb2.OfflineEventInformation(lat=lat, lng=lng, address=f"Outside area {i}"),
)
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq(search_in_area=search_pb2.Area(lat=0, lng=0, radius=100000)))
assert len(res.events) == len(inside_pts)
assert all(event.title.startswith("Inside area") for event in res.events)
def test_event_search_by_rectangle(sample_community, create_event):
"""Test that EventSearch only returns events within the given rectangular area."""
user, token = generate_user()
with events_session(token) as api:
inside_pts = [(0.1, 0.2), (1.2, 0.2)]
for i, (lat, lng) in enumerate(inside_pts):
create_event(
api,
title=f"Inside area {i}",
offline_information=events_pb2.OfflineEventInformation(lat=lat, lng=lng, address=f"Inside area {i}"),
)
outside_pts = [(-1, 0.1), (0.1, 0.01), (-0.01, 0.01), (0.1, 1.2), (10, 1)]
for i, (lat, lng) in enumerate(outside_pts):
create_event(
api,
title=f"Outside area {i}",
offline_information=events_pb2.OfflineEventInformation(lat=lat, lng=lng, address=f"Outside area {i}"),
)
with search_session(token) as api:
res = api.EventSearch(
search_pb2.EventSearchReq(
search_in_rectangle=search_pb2.RectArea(lat_min=0, lat_max=2, lng_min=0.1, lng_max=1)
)
)
assert len(res.events) == len(inside_pts)
assert all(event.title.startswith("Inside area") for event in res.events)
def test_event_search_pagination(sample_community, create_event):
"""Test that EventSearch paginates correctly.
Check that
- <page_size> events are returned, if available
- sort order is applied (default: past=False)
- the next page token is correct
"""
user, token = generate_user()
anchor_time = now()
with events_session(token) as api:
for i in range(5):
create_event(
api,
title=f"Event {i + 1}",
start_time=Timestamp_from_datetime(anchor_time + timedelta(hours=i + 1)),
end_time=Timestamp_from_datetime(anchor_time + timedelta(hours=i + 1, minutes=30)),
)
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq(past=False, page_size=4))
assert len(res.events) == 4
assert [event.title for event in res.events] == ["Event 1", "Event 2", "Event 3", "Event 4"]
assert res.next_page_token == str(millis_from_dt(anchor_time + timedelta(hours=5, minutes=30)))
res = api.EventSearch(search_pb2.EventSearchReq(page_size=4, page_token=res.next_page_token))
assert len(res.events) == 1
assert res.events[0].title == "Event 5"
assert res.next_page_token == ""
res = api.EventSearch(
search_pb2.EventSearchReq(
past=True, page_size=2, page_token=str(millis_from_dt(anchor_time + timedelta(hours=4, minutes=30)))
)
)
assert len(res.events) == 2
assert [event.title for event in res.events] == ["Event 4", "Event 3"]
assert res.next_page_token == str(millis_from_dt(anchor_time + timedelta(hours=2, minutes=30)))
res = api.EventSearch(search_pb2.EventSearchReq(past=True, page_size=2, page_token=res.next_page_token))
assert len(res.events) == 2
assert [event.title for event in res.events] == ["Event 2", "Event 1"]
assert res.next_page_token == ""
def test_event_search_pagination_with_page_number(sample_community, create_event):
"""Test that EventSearch paginates correctly with page number.
Check that
- <page_size> events are returned, if available
- sort order is applied (default: past=False)
- <page_number> is respected
- <total_items> is correct
"""
user, token = generate_user()
anchor_time = now()
with events_session(token) as api:
for i in range(5):
create_event(
api,
title=f"Event {i + 1}",
start_time=Timestamp_from_datetime(anchor_time + timedelta(hours=i + 1)),
end_time=Timestamp_from_datetime(anchor_time + timedelta(hours=i + 1, minutes=30)),
)
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq(page_size=2, page_number=1))
assert len(res.events) == 2
assert [event.title for event in res.events] == ["Event 1", "Event 2"]
assert res.total_items == 5
res = api.EventSearch(search_pb2.EventSearchReq(page_size=2, page_number=2))
assert len(res.events) == 2
assert [event.title for event in res.events] == ["Event 3", "Event 4"]
assert res.total_items == 5
res = api.EventSearch(search_pb2.EventSearchReq(page_size=2, page_number=3))
assert len(res.events) == 1
assert [event.title for event in res.events] == ["Event 5"]
assert res.total_items == 5
# Verify no more pages
res = api.EventSearch(search_pb2.EventSearchReq(page_size=2, page_number=4))
assert not res.events
assert res.total_items == 5
def test_event_search_online_status(sample_community, create_event):
"""Test that EventSearch respects only_online and only_offline filters and by default returns both."""
user, token = generate_user()
with events_session(token) as api:
create_event(api, title="Offline event")
create_event(
api,
title="Online event",
online_information=events_pb2.OnlineEventInformation(link="https://couchers.org/meet/"),
parent_community_id=sample_community,
offline_information=events_pb2.OfflineEventInformation(),
)
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq())
assert len(res.events) == 2
assert {event.title for event in res.events} == {"Offline event", "Online event"}
res = api.EventSearch(search_pb2.EventSearchReq(only_online=True))
assert {event.title for event in res.events} == {"Online event"}
res = api.EventSearch(search_pb2.EventSearchReq(only_offline=True))
assert {event.title for event in res.events} == {"Offline event"}
def test_event_search_filter_subscription_attendance_organizing_my_communities(
sample_community, create_event, moderator: Moderator
):
"""Test that EventSearch respects subscribed, attending, organizing and my_communities filters and by default
returns all events.
"""
_, token = generate_user()
other_user, other_token = generate_user()
with communities_session(token) as api:
api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=sample_community))
with session_scope() as session:
create_community(session, 55, 60, "Other community", [other_user], [], None)
with events_session(other_token) as api:
e_subscribed = create_event(api, title="Subscribed event")
e_attending = create_event(api, title="Attending event")
create_event(api, title="Community event")
create_event(
api,
title="Other community event",
offline_information=events_pb2.OfflineEventInformation(lat=58, lng=1, address="Somewhere"),
)
# Approve all events so they're visible to other users
with session_scope() as session:
occurrence_ids = session.execute(select(EventOccurrence.id)).scalars().all()
for oid in occurrence_ids:
moderator.approve_event_occurrence(oid)
with events_session(token) as api:
create_event(api, title="Organized event")
api.SetEventSubscription(events_pb2.SetEventSubscriptionReq(event_id=e_subscribed.event_id, subscribe=True))
api.SetEventAttendance(
events_pb2.SetEventAttendanceReq(
event_id=e_attending.event_id, attendance_state=events_pb2.ATTENDANCE_STATE_GOING
)
)
with search_session(token) as api:
res = api.EventSearch(search_pb2.EventSearchReq())
assert {event.title for event in res.events} == {
"Subscribed event",
"Attending event",
"Community event",
"Other community event",
"Organized event",
}
res = api.EventSearch(search_pb2.EventSearchReq(subscribed=True))
assert {event.title for event in res.events} == {"Subscribed event", "Organized event"}
res = api.EventSearch(search_pb2.EventSearchReq(attending=True))
assert {event.title for event in res.events} == {"Attending event", "Organized event"}
res = api.EventSearch(search_pb2.EventSearchReq(organizing=True))
assert {event.title for event in res.events} == {"Organized event"}
res = api.EventSearch(search_pb2.EventSearchReq(my_communities=True))
assert {event.title for event in res.events} == {
"Subscribed event",
"Attending event",
"Community event",
"Organized event",
}
res = api.EventSearch(search_pb2.EventSearchReq(subscribed=True, attending=True))
assert {event.title for event in res.events} == {"Subscribed event", "Attending event", "Organized event"}
def test_event_search_exclude_attending(sample_community, create_event, moderator: Moderator):
"""Test that exclude_attending removes events the user is attending or organizing."""
user, token = generate_user()
other_user, other_token = generate_user()
with communities_session(token) as api:
api.JoinCommunity(communities_pb2.JoinCommunityReq(community_id=sample_community))
with session_scope() as session:
create_community(session, 55, 60, "Other community", [other_user], [], None)
with events_session(other_token) as api:
e_attending = create_event(api, title="Attending event")
e_community_only = create_event(api, title="Community only event")
create_event(
api,
title="Other community event",
offline_information=events_pb2.OfflineEventInformation(lat=58, lng=1, address="Somewhere"),
)
with session_scope() as session:
occurrence_ids = session.execute(select(EventOccurrence.id)).scalars().all()
for oid in occurrence_ids:
moderator.approve_event_occurrence(oid)
with events_session(token) as api:
e_organized = create_event(api, title="Organized event")
api.SetEventAttendance(
events_pb2.SetEventAttendanceReq(
event_id=e_attending.event_id, attendance_state=events_pb2.ATTENDANCE_STATE_GOING
)
)
with search_session(token) as api:
# baseline: my_communities returns all community events including attended/organized
res = api.EventSearch(search_pb2.EventSearchReq(my_communities=True))
assert {event.title for event in res.events} == {
"Attending event",
"Community only event",
"Organized event",
}
# my_communities + exclude_attending: drops attended and organized events
res = api.EventSearch(search_pb2.EventSearchReq(my_communities=True, exclude_attending=True))
assert {event.title for event in res.events} == {"Community only event"}
# exclude_attending alone (no other filter = all events): drops attended and organized
res = api.EventSearch(search_pb2.EventSearchReq(exclude_attending=True))
assert {event.title for event in res.events} == {"Community only event", "Other community event"}
# attending + exclude_attending is invalid
with pytest.raises(grpc.RpcError) as e:
api.EventSearch(search_pb2.EventSearchReq(attending=True, exclude_attending=True))
assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
def test_regression_search_multiple_pages(db):
"""
There was a bug when there are multiple pages of results
"""
user, token = generate_user()
user_ids = [user.id]
for _ in range(10):
other_user, _ = generate_user()
user_ids.append(other_user.id)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token) as api:
res = api.UserSearchV2(search_pb2.UserSearchReq(page_size=5))
assert [result.user_id for result in res.results] == user_ids[:5]
assert res.next_page_token
def test_regression_search_no_results(db):
"""
There was a bug when there were no results
"""
# put us far away
user, token = generate_user()
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
with search_session(token) as api:
res = api.UserSearchV2(search_pb2.UserSearchReq(only_with_references=True))
assert len(res.results) == 0
def test_user_filter_same_gender_only(db):
"""Test that same_gender_only filter works correctly"""
# Create users with different genders and strong verification status
woman_with_sv, token_woman_with_sv = generate_user(strong_verification=True, gender="Woman")
woman_without_sv, token_woman_without_sv = generate_user(strong_verification=False, gender="Woman")
man_with_sv, token_man_with_sv = generate_user(strong_verification=True, gender="Man")
man_without_sv, _ = generate_user(strong_verification=False, gender="Man")
other_woman_with_sv, _ = generate_user(strong_verification=True, gender="Woman")
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
# Test 1: Woman with strong verification should see only women when same_gender_only=True
with search_session(token_woman_with_sv) as api:
res = api.UserSearch(search_pb2.UserSearchReq(same_gender_only=True))
result_ids = [result.user.user_id for result in res.results]
assert woman_with_sv.id in result_ids
assert woman_without_sv.id in result_ids
assert other_woman_with_sv.id in result_ids
assert man_with_sv.id not in result_ids
assert man_without_sv.id not in result_ids
res = api.UserSearchV2(search_pb2.UserSearchReq(same_gender_only=True))
result_ids = [result.user_id for result in res.results]
assert woman_with_sv.id in result_ids
assert woman_without_sv.id in result_ids
assert other_woman_with_sv.id in result_ids
assert man_with_sv.id not in result_ids
assert man_without_sv.id not in result_ids
# Test 2: Man with strong verification should see only men when same_gender_only=True
with search_session(token_man_with_sv) as api:
res = api.UserSearch(search_pb2.UserSearchReq(same_gender_only=True))
result_ids = [result.user.user_id for result in res.results]
assert man_with_sv.id in result_ids
assert man_without_sv.id in result_ids
assert woman_with_sv.id not in result_ids
assert woman_without_sv.id not in result_ids
assert other_woman_with_sv.id not in result_ids
res = api.UserSearchV2(search_pb2.UserSearchReq(same_gender_only=True))
result_ids = [result.user_id for result in res.results]
assert man_with_sv.id in result_ids
assert man_without_sv.id in result_ids
assert woman_with_sv.id not in result_ids
assert woman_without_sv.id not in result_ids
assert other_woman_with_sv.id not in result_ids
# Test 3: Woman without strong verification should get an error
with search_session(token_woman_without_sv) as api:
with pytest.raises(Exception) as e:
api.UserSearch(search_pb2.UserSearchReq(same_gender_only=True))
assert "NEED_STRONG_VERIFICATION" in str(e.value) or "FAILED_PRECONDITION" in str(e.value)
with pytest.raises(Exception) as e:
api.UserSearchV2(search_pb2.UserSearchReq(same_gender_only=True))
assert "NEED_STRONG_VERIFICATION" in str(e.value) or "FAILED_PRECONDITION" in str(e.value)
# Test 4: When same_gender_only=False, should see all users
with search_session(token_woman_with_sv) as api:
res = api.UserSearch(search_pb2.UserSearchReq(same_gender_only=False))
result_ids = [result.user.user_id for result in res.results]
assert woman_with_sv.id in result_ids
assert woman_without_sv.id in result_ids
assert other_woman_with_sv.id in result_ids
assert man_with_sv.id in result_ids
assert man_without_sv.id in result_ids
res = api.UserSearchV2(search_pb2.UserSearchReq(same_gender_only=False))
result_ids = [result.user_id for result in res.results]
assert woman_with_sv.id in result_ids
assert woman_without_sv.id in result_ids
assert other_woman_with_sv.id in result_ids
assert man_with_sv.id in result_ids
assert man_without_sv.id in result_ids
def test_user_filter_same_gender_only_with_other_filters(db):
"""Test that same_gender_only filter works correctly combined with other filters"""
# Create users with different properties
woman_host, token_woman = generate_user(
strong_verification=True, gender="Woman", hosting_status=HostingStatus.can_host
)
woman_cant_host, _ = generate_user(strong_verification=True, gender="Woman", hosting_status=HostingStatus.cant_host)
man_host, _ = generate_user(strong_verification=True, gender="Man", hosting_status=HostingStatus.can_host)
refresh_materialized_views_rapid(empty_pb2.Empty())
refresh_materialized_views(empty_pb2.Empty())
# Test: Combine same_gender_only with hosting_status filter
with search_session(token_woman) as api:
res = api.UserSearch(
search_pb2.UserSearchReq(same_gender_only=True, hosting_status_filter=[api_pb2.HOSTING_STATUS_CAN_HOST])
)
result_ids = [result.user.user_id for result in res.results]
# Should only see woman who can host
assert woman_host.id in result_ids
assert woman_cant_host.id not in result_ids
assert man_host.id not in result_ids
res = api.UserSearchV2(
search_pb2.UserSearchReq(same_gender_only=True, hosting_status_filter=[api_pb2.HOSTING_STATUS_CAN_HOST])
)
result_ids = [result.user_id for result in res.results]
assert woman_host.id in result_ids
assert woman_cant_host.id not in result_ids
assert man_host.id not in result_ids