async def test_add_friend_existing(self): success = await self.client.CreateConnectionForUsers( CreateConnectionForUsersRequest( firstUserID=20, secondUserID=25, ) ) self.assertTrue(success.success) res1 = await self.client.GetFriendsForUser( GetFriendsForUserRequest( user=User( userID=20, ) ) ) u1_friends_list = sorted(list(res1.friends)) res2 = await self.client.GetFriendsForUser( GetFriendsForUserRequest( user=User( userID=25, ) ) ) u2_friends_list = sorted(list(res2.friends)) self.assertListEqual(u1_friends_list, [5, 10, 25]) self.assertListEqual(u2_friends_list, [5, 10, 20])
async def test_delete_friend_existing(self): await self.client.DeleteConnectionBetweenUsers( DeleteConnectionBetweenUsersRequest( firstUserID=8, secondUserID=1, ) ) res1 = await self.client.GetFriendsForUser( GetFriendsForUserRequest( user=User( userID=8, ) ) ) u1_friends_list = sorted(list(res1.friends)) res2 = await self.client.GetFriendsForUser( GetFriendsForUserRequest( user=User( userID=1, ) ) ) u2_friends_list = sorted(list(res2.friends)) self.assertListEqual(u1_friends_list, [3, 5]) self.assertListEqual(u2_friends_list, [5, 6])
def test_add_friend_existing(self): success = self.server.add_friend( CreateConnectionForUsersRequest( firstUserID=20, secondUserID=25, )) self.assertTrue(success.success) u1_friends_list = list( self.server.get_user_friends_from_cache( GetFriendsForUserRequest(user=User(userID=20, ))).friends) u2_friends_list = list( self.server.get_user_friends_from_cache( GetFriendsForUserRequest(user=User(userID=25, ))).friends) self.assertListEqual(u1_friends_list, [5, 10, 25]) self.assertListEqual(u2_friends_list, [5, 10, 20])
def test_delete_friend_existing(self): success = self.server.delete_friend_from_cache( DeleteConnectionBetweenUsersRequest( firstUserID=8, secondUserID=1, )) self.assertTrue(success) u1_friends_list = list( self.server.get_user_friends_from_cache( GetFriendsForUserRequest(user=User(userID=8, ))).friends) u2_friends_list = list( self.server.get_user_friends_from_cache( GetFriendsForUserRequest(user=User(userID=1, ))).friends) self.assertListEqual(u1_friends_list, [3, 5]) self.assertListEqual(u2_friends_list, [5, 6])
def test_add_awaiting_friend(self): success = self.server.add_awaiting_friend_to_cache( AddAwaitingFriendRequest( firstUserID=90, secondUserID=95, )) self.assertTrue(success.success) u1_friends_list = list( self.server.get_user_awaiting_friends_from_cache( GetFriendsForUserRequest(user=User(userID=90, ))).friends) u2_friends_list = list( self.server.get_user_awaiting_friends_from_cache( GetFriendsForUserRequest(user=User(userID=95, ))).friends) self.assertListEqual(u1_friends_list, [95]) self.assertListEqual(u2_friends_list, [90])
async def GetRecommendationsForUser( self, stream: 'grpclib.server.Stream[' 'GetRecommendationsForUserRequest, ' 'GetRecommendationsForUserResponse]' ) -> None: logger.debug("Received request to GetRecommendationsForUser") request = await stream.recv_message() uid = request.user.userID num_recs = request.numberRecommendations recs = self.friend_finder.get_recommendations(num_recs, uid) user_object_list = list() auth = stream.metadata["authorization"] user_names = await self.get_usernames_from_user_service(auth, recs) if len(user_names) != len(recs): raise GRPCError( Status.INTERNAL, 'An inconsistency exists for one or more users', [], ) for name, uid in zip(user_names, recs): user_object_list.append(User( userID=uid, userName=name, )) res = GetRecommendationsForUserResponse( recommendations=user_object_list) await stream.send_message(res)
async def test_get_awaiting_friend(self): res = await self.client.GetAwaitingFriendsForUser(GetFriendsForUserRequest( user=User( userID=70, ) ) ) self.assertListEqual(list(res.friends), [65])
def test_get_nonexisting_friends_list(self): resp = self.server.get_user_friends_from_cache( GetFriendsForUserRequest(user=User( userID=1000, userName="******", email="test", ))) friends = list(resp.friends) self.assertListEqual(friends, [])
async def test_get_nonexisting_friends_list(self): resp = await self.client.GetFriendsForUser(GetFriendsForUserRequest( user=User( userID=900, userName="******", email="test", ) )) friends = list(resp.friends) self.assertListEqual(friends, [])
async def test_get_existing_friends_list(self): resp = await self.client.GetFriendsForUser(GetFriendsForUserRequest( user=User( userID=5, userName="******", email="test", ) )) friends = list(resp.friends) # includes friends who add self.assertListEqual(friends, [1, 2, 3, 6, 8, 20, 25])
async def test_get_awaiting_friends_usernames(self): res = await self.client.GetAwaitingFriendsUsernamesForUser( GetFriendsForUserRequest( user=User( userID=self.test_user_id, ) ), metadata={"authorization": f"Bearer {self.token}"} ) friends = sorted(list(res.friends)) self.assertListEqual(friends, ["testuser3"])
async def test_get_recommendations_for_user(self): res = await self.client.GetRecommendationsForUser( GetRecommendationsForUserRequest( user=User( userID=1000, ), numberRecommendations=2 ), metadata={"authorization": f"Bearer {self.token}"} ) recommendations_list = res.recommendations self.assertEqual(len(recommendations_list), 2) self.assertEqual(recommendations_list[0].userID, 1004) self.assertEqual(recommendations_list[1].userID, 1005)
async def test_add_awaiting_friend(self): success = await self.client.AddAwaitingFriend( AddAwaitingFriendRequest( firstUserID=90, secondUserID=95, ) ) self.assertTrue(success.success) res2 = await self.client.GetAwaitingFriendsForUser( GetFriendsForUserRequest( user=User( userID=95, ) ) ) u2_friends_list = list(res2.friends) self.assertListEqual(u2_friends_list, [90])
def test_get_awaiting_friend(self): res = self.server.get_user_awaiting_friends_from_cache( GetFriendsForUserRequest(user=User(userID=65, ))) self.assertListEqual(list(res.friends), [70])