def test_bulk_get_huddle_user_ids(self) -> None: hamlet = self.example_user("hamlet") cordelia = self.example_user("cordelia") othello = self.example_user("othello") iago = self.example_user("iago") message_ids = [ self.send_huddle_message(hamlet, [cordelia, othello], "test"), self.send_huddle_message(cordelia, [hamlet, othello, iago], "test"), ] messages = Message.objects.filter(id__in=message_ids).order_by("id") first_huddle_recipient = messages[0].recipient first_huddle_user_ids = list( get_huddle_user_ids(first_huddle_recipient)) second_huddle_recipient = messages[1].recipient second_huddle_user_ids = list( get_huddle_user_ids(second_huddle_recipient)) huddle_user_ids = bulk_get_huddle_user_ids( [first_huddle_recipient, second_huddle_recipient]) self.assertEqual(huddle_user_ids[first_huddle_recipient.id], first_huddle_user_ids) self.assertEqual(huddle_user_ids[second_huddle_recipient.id], second_huddle_user_ids)
def personal_and_huddle_query_function( recipient_ids: List[int], ) -> List[Tuple[int, List[UserDisplayRecipient]]]: """ Return a list of tuples of the form (recipient_id, [list of UserProfiles]) where [list of UserProfiles] has users corresponding to the recipient, so the receiving userin Recipient.PERSONAL case, or in Personal.HUDDLE case - users in the huddle. This is a pretty hacky return value, but it needs to be in this form, for this function to work as the query_function in generic_bulk_cached_fetch. """ recipients = [ Recipient( id=recipient_id, type=recipient_id_to_type_pair_dict[recipient_id][0], type_id=recipient_id_to_type_pair_dict[recipient_id][1], ) for recipient_id in recipient_ids ] # Find all user ids whose UserProfiles we will need to fetch: user_ids_to_fetch: Set[int] = set() huddle_user_ids: Dict[int, List[int]] = {} huddle_user_ids = bulk_get_huddle_user_ids([ recipient for recipient in recipients if recipient.type == Recipient.HUDDLE ]) for recipient in recipients: if recipient.type == Recipient.PERSONAL: user_ids_to_fetch.add(recipient.type_id) else: user_ids_to_fetch = user_ids_to_fetch.union( huddle_user_ids[recipient.id]) # Fetch the needed UserProfiles: user_profiles: Dict[ int, UserDisplayRecipient] = bulk_get_user_profile_by_id( list(user_ids_to_fetch)) # Build the return value: result: List[Tuple[int, List[UserDisplayRecipient]]] = [] for recipient in recipients: if recipient.type == Recipient.PERSONAL: result.append( (recipient.id, [user_profiles[recipient.type_id]])) else: result.append(( recipient.id, [ user_profiles[user_id] for user_id in huddle_user_ids[recipient.id] ], )) return result
def test_bulk_get_huddle_user_ids_empty_list(self) -> None: self.assertEqual(bulk_get_huddle_user_ids([]), {})