Coverage for src/tests/test_search.py: 100%

58 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-22 17:19 +0000

1import pytest 

2from google.protobuf import wrappers_pb2 

3 

4from couchers.models import MeetupStatus 

5from couchers.utils import create_coordinate 

6from proto import api_pb2, search_pb2 

7from tests.test_communities import testing_communities # noqa 

8from tests.test_fixtures import db, generate_user, search_session, testconfig # noqa 

9 

10 

11@pytest.fixture(autouse=True) 

12def _(testconfig): 

13 pass 

14 

15 

16def test_Search(testing_communities): 

17 user, token = generate_user() 

18 with search_session(token) as api: 

19 res = api.Search( 

20 search_pb2.SearchReq( 

21 query="Country 1, Region 1", 

22 include_users=True, 

23 include_communities=True, 

24 include_groups=True, 

25 include_places=True, 

26 include_guides=True, 

27 ) 

28 ) 

29 res = api.Search( 

30 search_pb2.SearchReq( 

31 query="Country 1, Region 1, Attraction", 

32 title_only=True, 

33 include_users=True, 

34 include_communities=True, 

35 include_groups=True, 

36 include_places=True, 

37 include_guides=True, 

38 ) 

39 ) 

40 

41 

42def test_UserSearch(testing_communities): 

43 user, token = generate_user() 

44 with search_session(token) as api: 

45 res = api.UserSearch(search_pb2.UserSearchReq()) 

46 assert len(res.results) > 0 

47 

48 

49def test_regression_search_in_area(db): 

50 """ 

51 Makes sure search_in_area works. 

52 

53 At the equator/prime meridian intersection (0,0), one degree is roughly 111 km. 

54 """ 

55 

56 # outside 

57 user1, token1 = generate_user(geom=create_coordinate(1, 0), geom_radius=100) 

58 # outside 

59 user2, token2 = generate_user(geom=create_coordinate(0, 1), geom_radius=100) 

60 # inside 

61 user3, token3 = generate_user(geom=create_coordinate(0.1, 0), geom_radius=100) 

62 # inside 

63 user4, token4 = generate_user(geom=create_coordinate(0, 0.1), geom_radius=100) 

64 # outside 

65 user5, token5 = generate_user(geom=create_coordinate(10, 10), geom_radius=100) 

66 

67 with search_session(token5) as api: 

68 res = api.UserSearch( 

69 search_pb2.UserSearchReq( 

70 search_in_area=search_pb2.Area( 

71 lat=0, 

72 lng=0, 

73 radius=100000, 

74 ) 

75 ) 

76 ) 

77 assert [result.user.user_id for result in res.results] == [user3.id, user4.id] 

78 

79 

80def test_user_search_in_rectangle(db): 

81 """ 

82 Makes sure search_in_rectangle works as expected. 

83 """ 

84 

85 # outside 

86 user1, token1 = generate_user(geom=create_coordinate(-1, 0), geom_radius=100) 

87 # outside 

88 user2, token2 = generate_user(geom=create_coordinate(0, -1), geom_radius=100) 

89 # inside 

90 user3, token3 = generate_user(geom=create_coordinate(0.1, 0.1), geom_radius=100) 

91 # inside 

92 user4, token4 = generate_user(geom=create_coordinate(1.2, 0.1), geom_radius=100) 

93 # outside (not fully inside) 

94 user5, token5 = generate_user(geom=create_coordinate(0, 0), geom_radius=100) 

95 # outside 

96 user6, token6 = generate_user(geom=create_coordinate(0.1, 1.2), geom_radius=100) 

97 # outside 

98 user7, token7 = generate_user(geom=create_coordinate(10, 10), geom_radius=100) 

99 

100 with search_session(token5) as api: 

101 res = api.UserSearch( 

102 search_pb2.UserSearchReq( 

103 search_in_rectangle=search_pb2.RectArea( 

104 lat_min=0, 

105 lat_max=2, 

106 lng_min=0, 

107 lng_max=1, 

108 ) 

109 ) 

110 ) 

111 assert [result.user.user_id for result in res.results] == [user3.id, user4.id] 

112 

113 

114def test_user_filter_complete_profile(db): 

115 """ 

116 Make sure the completed profile flag returns only completed user profile 

117 """ 

118 user_complete_profile, token6 = generate_user(complete_profile=True) 

119 

120 user_incomplete_profile, token7 = generate_user(complete_profile=False) 

121 

122 with search_session(token7) as api: 

123 res = api.UserSearch(search_pb2.UserSearchReq(profile_completed=wrappers_pb2.BoolValue(value=False))) 

124 assert user_incomplete_profile.id in [result.user.user_id for result in res.results] 

125 

126 with search_session(token6) as api: 

127 res = api.UserSearch(search_pb2.UserSearchReq(profile_completed=wrappers_pb2.BoolValue(value=True))) 

128 assert [result.user.user_id for result in res.results] == [user_complete_profile.id] 

129 

130 

131def test_user_filter_meetup_status(db): 

132 """ 

133 Make sure the completed profile flag returns only completed user profile 

134 """ 

135 user_wants_to_meetup, token8 = generate_user(meetup_status=MeetupStatus.wants_to_meetup) 

136 

137 user_does_not_want_to_meet, token9 = generate_user(meetup_status=MeetupStatus.does_not_want_to_meetup) 

138 

139 with search_session(token8) as api: 

140 res = api.UserSearch(search_pb2.UserSearchReq(meetup_status_filter=[api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP])) 

141 assert user_wants_to_meetup.id in [result.user.user_id for result in res.results] 

142 

143 with search_session(token9) as api: 

144 res = api.UserSearch( 

145 search_pb2.UserSearchReq(meetup_status_filter=[api_pb2.MEETUP_STATUS_DOES_NOT_WANT_TO_MEETUP]) 

146 ) 

147 assert [result.user.user_id for result in res.results] == [user_does_not_want_to_meet.id]