Best Python code snippet using autotest_python
TestScrimTeamsManager.py
Source:TestScrimTeamsManager.py
...58 "smaller team max size than team min size.")59 self._assert_raises_correct_exception(expected_exception, ScrimTeamsManager, mock_game,60 self.participant_manager)61 def test_init_given_team_min_size_larger_than_max_size_when_unlimited_max_size_then_init_successful(self):62 self._setup_manager(5, 0, 1)63 def test_init_given_premade_team_with_identical_sizes_as_game_then_init_successful(self):64 team_name = "Valid team"65 mock_game = _create_mock_game(5, 6, 2)66 valid_team = Team(team_name, [], 5, 6)67 manager = ScrimTeamsManager(mock_game, self.participant_manager, teams=[valid_team])68 self.assertEqual(team_name, manager.get_game_teams()[0].name)69 def test_init_given_premade_team_with_stricter_but_valid_sizes_as_game_then_init_successful(self):70 team_name = "Valid team"71 mock_game = _create_mock_game(4, 7, 2)72 valid_team = Team(team_name, [], 5, 6)73 manager = ScrimTeamsManager(mock_game, self.participant_manager, teams=[valid_team])74 self.assertEqual(team_name, manager.get_game_teams()[0].name)75 def test_init_given_premade_team_when_team_name_conflicts_with_standard_teams_then_error_raised(self):76 mock_game = _create_mock_game(5, 5, 2)77 invalid_team = Team(ScrimTeamsManager.PARTICIPANTS)78 expected_exception = \79 BotBaseRespondToContextException(f"Cannot create a scrim with a premade team name conflicting with a "80 f"name reserved for standard teams ({ScrimTeamsManager.PARTICIPANTS})")81 self._assert_raises_correct_exception(expected_exception, ScrimTeamsManager, mock_game,82 self.participant_manager, teams=[invalid_team])83 def test_init_given_duplicate_premade_team_names_then_error_raised(self):84 mock_game = _create_mock_game(5, 5, 2)85 duplicate_name = "Duplicate team"86 team_1 = Team(duplicate_name, [], 5, 5)87 team_2 = Team(duplicate_name, [], 5, 5)88 expected_exception = \89 BotBaseRespondToContextException(f"Cannot create a scrim with premade teams having identical names "90 f"({duplicate_name})")91 self._assert_raises_correct_exception(expected_exception, ScrimTeamsManager, mock_game,92 self.participant_manager, teams=[team_1, team_2])93 def test_init_given_duplicate_premade_team_names_when_name_conflicts_with_standard_teams_then_error_raised(self):94 mock_game = _create_mock_game(5, 5, 2)95 duplicate_name = ScrimTeamsManager.SPECTATORS96 team_1 = Team(duplicate_name)97 team_2 = Team(duplicate_name)98 expected_exception = \99 BotBaseRespondToContextException(f"Cannot create a scrim with a premade team name conflicting with a name "100 f"reserved for standard teams ({duplicate_name})")101 self._assert_raises_correct_exception(expected_exception, ScrimTeamsManager, mock_game,102 self.participant_manager, teams=[team_1, team_2])103 def test_init_given_invalid_sized_premade_team_min_players_then_error_raised(self):104 mock_game = _create_mock_game(5, 5, 2)105 team_name = "Invalid team"106 invalid_team = Team(team_name, [], 3, 5)107 expected_exception = \108 BotBaseRespondToContextException(f"Cannot create a scrim with a premade team with a size incompatible with "109 f"the chosen game ({team_name})")110 self._assert_raises_correct_exception(expected_exception, ScrimTeamsManager, mock_game,111 self.participant_manager, teams=[invalid_team])112 def test_init_given_invalid_sized_premade_team_max_players_then_error_raised(self):113 mock_game = _create_mock_game(5, 5, 2)114 team_name = "Invalid team"115 invalid_team = Team(team_name, [], 5, 7)116 expected_exception = \117 BotBaseRespondToContextException(f"Cannot create a scrim with a premade team with a size incompatible with "118 f"the chosen game ({team_name})")119 self._assert_raises_correct_exception(expected_exception, ScrimTeamsManager, mock_game,120 self.participant_manager, teams=[invalid_team])121 def test_init_given_premade_team_with_too_many_players_then_error_raised(self):122 mock_game = _create_mock_game(5, 5, 2)123 team_name = "Invalid team"124 mock_players = [self._create_mock_user() for _ in range(6)]125 invalid_team = Team(team_name, mock_players, 5, 5)126 expected_exception = \127 BotBaseRespondToContextException(f"Cannot create a scrim with a premade team with a size incompatible with "128 f"the chosen game ({team_name})")129 self._assert_raises_correct_exception(expected_exception, ScrimTeamsManager, mock_game,130 self.participant_manager, teams=[invalid_team])131 def test_init_given_valid_team_voice_channels_then_corresponding_game_team_channels_set(self):132 min_size, max_size, team_count = 5, 5, 3133 mock_game = _create_mock_game(min_size, max_size, team_count)134 channel_ids = self.id_generator.generate_viable_id_group(team_count)135 mock_team = MagicMock()136 teams_channels = [generate_mock_voice_channel(channel_id, mock_team) for channel_id in channel_ids]137 manager = ScrimTeamsManager(mock_game, self.participant_manager, team_channels=teams_channels)138 for team, channel in zip(manager.get_game_teams(), teams_channels):139 self.assertEqual(team.voice_channel, channel)140 def test_init_given_valid_lobby_channel_then_standard_team_channels_set(self):141 min_size, max_size, team_count = 3, 4, 5142 mock_game = _create_mock_game(min_size, max_size, team_count)143 lobby_channel = generate_mock_voice_channel(self.id_generator.generate_viable_id())144 manager = ScrimTeamsManager(mock_game, self.participant_manager, team_channels=[], lobby=lobby_channel)145 for team in manager.get_standard_teams():146 self.assertEqual(team.voice_channel, lobby_channel)147 def test_init_given_too_many_voice_channels_then_only_needed_channels_assigned(self):148 min_size, max_size, team_count = 2, 7, 4149 mock_game = _create_mock_game(min_size, max_size, team_count)150 channel_ids = self.id_generator.generate_viable_id_group(team_count + 1)151 teams_channels = [generate_mock_voice_channel(channel_id) for channel_id in channel_ids]152 manager = ScrimTeamsManager(mock_game, self.participant_manager, team_channels=teams_channels)153 for team in manager.get_game_teams():154 self.assertIn(team.voice_channel, teams_channels)155 for team in manager.get_standard_teams():156 self.assertIsNone(team.voice_channel)157 def test_get_standard_teams_given_valid_setup_then_all_standard_teams_returned(self):158 min_size, max_size, team_count = 5, 5, 2159 manager = self._setup_manager(min_size, max_size, team_count)160 standard_teams = manager.get_standard_teams()161 self._assert_valid_standard_teams(standard_teams, max_size, min_size, team_count)162 def test_get_game_teams_given_valid_setup_when_no_premade_teams_then_correct_teams_returned(self):163 min_size, max_size, team_count = 5, 6, 4164 manager = self._setup_manager(min_size, max_size, team_count)165 game_teams = manager.get_game_teams()166 self._assert_valid_game_teams(game_teams, max_size, min_size, team_count)167 def test_get_game_teams_given_valid_setup_with_partly_premade_teams_then_correct_teams_returned(self):168 min_size, max_size, team_count = 5, 6, 4169 mock_game = _create_mock_game(min_size, max_size, team_count)170 mock_team = Team("Premade team", [], min_size, max_size)171 manager = ScrimTeamsManager(mock_game, self.participant_manager, teams=[mock_team])172 game_teams = manager.get_game_teams()173 self.assertEqual(mock_team, game_teams[0])174 self._assert_valid_game_teams(game_teams, max_size, min_size, team_count)175 def test_get_game_teams_given_valid_setup_with_only_premade_teams_then_correct_teams_returned(self):176 min_size, max_size, team_count = 5, 6, 4177 mock_game = _create_mock_game(min_size, max_size, team_count)178 mock_teams = [Team(f"Premade {i + 1}", [], min_size, max_size) for i in range(team_count)]179 manager = ScrimTeamsManager(mock_game, self.participant_manager, teams=mock_teams)180 game_teams = manager.get_game_teams()181 self.assertEqual(mock_teams, game_teams)182 self._assert_valid_game_teams(game_teams, max_size, min_size, team_count)183 def test_add_player_given_participants_not_full_when_player_added_to_participants_then_insert_successful(self):184 tester_name = "Tester"185 manager = self._setup_manager()186 mock_player = MagicMock()187 mock_player.display_name = tester_name188 manager.add_player(manager.PARTICIPANTS, mock_player)189 updated_participants = manager.get_standard_teams()[0]190 self.assert_in_team(mock_player, updated_participants)191 def test_add_player_given_participants_full_when_player_added_to_participants_then_inserted_to_queue(self):192 min_size, max_size, team_count = 3, 3, 2193 manager = self._setup_manager(min_size, max_size, team_count)194 for i in range(max_size * team_count):195 mock_user = self._create_mock_user()196 manager.add_player(manager.PARTICIPANTS, mock_user)197 mock_player = self._create_mock_user()198 mock_player.id = self.id_generator.generate_viable_id()199 manager.add_player(manager.PARTICIPANTS, mock_player)200 updated_queue = manager.get_standard_teams()[2]201 self.assert_in_team(mock_player, updated_queue)202 def test_add_player_when_added_to_spectators_then_insert_successful(self):203 manager = self._setup_manager()204 mock_player = self._create_mock_user()205 mock_player.id = self.id_generator.generate_viable_id()206 manager.add_player(manager.SPECTATORS, mock_player)207 updated_participants = manager.get_standard_teams()[1]208 self.assert_in_team(mock_player, updated_participants)209 def test_add_player_when_player_in_a_team_already_then_invalid_join_raised(self):210 manager = self._setup_manager()211 mock_player = self._create_mock_user()212 mock_player.id = self.id_generator.generate_viable_id()213 expected_exception = BotInvalidJoinException(mock_player, manager.get_standard_teams()[0],214 f"already a member of the team "215 f"'{manager.get_standard_teams()[1].name}'")216 manager.add_player(manager.SPECTATORS, mock_player)217 self._assert_raises_correct_exception(expected_exception, manager.add_player, manager.PARTICIPANTS, mock_player)218 def test_add_player_given_team_not_full_when_added_to_game_teams_with_team_name_then_insert_successful(self):219 min_size, max_size, team_count = 6, 6, 5220 manager = self._setup_manager(min_size, max_size, team_count)221 for team in range(team_count):222 with self.subTest(f"Adding player to game team with team name (Team {team + 1})"):223 mock_player = MagicMock()224 mock_player.id = self.id_generator.generate_viable_id()225 manager.add_player(f"Team {team + 1}", mock_player)226 updated_participants = manager.get_game_teams()[team]227 self.assert_in_team(mock_player, updated_participants)228 def test_add_player_given_team_not_full_when_added_to_game_teams_with_team_number_then_insert_successful(self):229 min_size, max_size, team_count = 1, 3, 4230 manager = self._setup_manager(min_size, max_size, team_count)231 for team in range(team_count):232 with self.subTest(f"Adding player to game team with team number (Team {team + 1})"):233 mock_player = self._create_mock_user()234 manager.add_player(team, mock_player)235 updated_participants = manager.get_game_teams()[team]236 self.assert_in_team(mock_player, updated_participants)237 def test_add_player_given_team_full_when_added_to_game_team_then_error_raised(self):238 min_size, max_size, team_count = 1, 5, 4239 manager = self._setup_manager(min_size, max_size, team_count)240 self._fill_teams(manager, max_size, team_count)241 mock_player = self._create_mock_user()242 for team in range(team_count):243 with self.subTest(f"Adding player to full game team (Team {team + 1})"):244 expected_exception = BotInvalidJoinException(mock_player, manager.get_game_teams()[team],245 "Could not add a player into a full team.")246 self._assert_raises_correct_exception(expected_exception, manager.add_player, team, mock_player)247 def test_has_enough_participants_given_no_participants_then_false_returned(self):248 manager = self._setup_manager()249 self.assertFalse(manager.has_enough_participants)250 def test_has_enough_participants_given_participants_under_min_team_size_times_team_count_then_false_returned(self):251 min_size, max_size, team_count = 2, 2, 8252 manager = self._setup_manager(min_size, max_size, team_count)253 for _ in range(min_size*team_count - 1):254 manager.add_player(manager.PARTICIPANTS, MagicMock())255 self.assertFalse(manager.has_enough_participants)256 def test_has_enough_participants_given_participants_at_min_team_size_times_team_count_then_true_returned(self):257 min_size, max_size, team_count = 4, 8, 2258 manager = self._setup_manager(min_size, max_size, team_count)259 for _ in range(min_size * team_count):260 manager.add_player(manager.PARTICIPANTS, MagicMock())261 self.assertTrue(manager.has_enough_participants)262 def test_has_enough_participants_given_participants_at_max_team_size_times_team_count_then_true_returned(self):263 min_size, max_size, team_count = 5, 9, 5264 manager = self._setup_manager(min_size, max_size, team_count)265 for _ in range(max_size * team_count):266 manager.add_player(manager.PARTICIPANTS, self._create_mock_user())267 self.assertTrue(manager.has_enough_participants)268 def test_remove_player_given_valid_standard_team_when_called_with_player_in_team_then_player_removed(self):269 manager = self._setup_manager()270 mock_player = self._create_mock_user()271 for index, team in enumerate([manager.PARTICIPANTS, manager.SPECTATORS, manager.QUEUE]):272 with self.subTest(f"Remove player from standard team ({team})"):273 manager.add_player(team, mock_player)274 manager.remove_player(team, mock_player)275 standard_teams = manager.get_standard_teams()276 self.assertNotIn(mock_player, standard_teams[index].members)277 def test_remove_player_given_valid_game_team_when_called_with_player_in_team_then_player_removed(self):278 min_size, max_size, team_count = 3, 5, 6279 manager = self._setup_manager(min_size, max_size, team_count)280 mock_player = self._create_mock_user()281 for team in range(team_count):282 with self.subTest(f"Remove player from team with team name (Team {team + 1})"):283 manager.add_player(team, mock_player)284 manager.remove_player(team, mock_player)285 game_teams = manager.get_game_teams()286 self.assertNotIn(mock_player, game_teams[team].members)287 def test_remove_player_given_valid_team_when_called_with_player_not_in_team_then_error_raised(self):288 manager = self._setup_manager()289 mock_player = self._create_mock_user()290 expected_exception = BotInvalidPlayerRemoval(mock_player, manager.get_game_teams()[0])291 self._assert_raises_correct_exception(expected_exception, manager.remove_player, 0, mock_player)292 def test_remove_player_given_queue_has_players_when_removed_from_participants_then_filled_from_queue(self):293 min_size, max_size, team_count = 3, 5, 6294 manager = self._setup_manager(min_size, max_size, team_count)295 mock_player = self._create_mock_user()296 for _ in range(max_size*team_count - 1):297 manager.add_player(manager.PARTICIPANTS, self._create_mock_user())298 manager.add_player(manager.PARTICIPANTS, mock_player)299 manager.add_player(manager.QUEUE, self._create_mock_user())300 manager.remove_player(manager.PARTICIPANTS, mock_player)301 standard_teams = manager.get_standard_teams()302 self.assertEqual(max_size*team_count, len(standard_teams[0].members))303 self.assertEqual(0, len(standard_teams[2].members))304 def test_set_team_given_valid_move_then_player_removed_from_original_team_and_added_to_new_team(self):305 manager = self._setup_manager()306 mock_player = self._create_mock_user()307 manager.add_player(manager.PARTICIPANTS, mock_player)308 manager.set_team(manager.SPECTATORS, mock_player)309 standard_teams = manager.get_standard_teams()310 self.assertEqual(0, len(standard_teams[0].members))311 self.assertEqual(1, len(standard_teams[1].members))312 def test_set_team_given_valid_move_when_moved_from_full_participants_and_queue_has_people_then_filled(self):313 min_size, max_size, team_count = 3, 5, 6314 manager = self._setup_manager(min_size, max_size, team_count)315 mock_player = MagicMock()316 for _ in range(max_size * team_count - 1):317 manager.add_player(manager.PARTICIPANTS, self._create_mock_user())318 manager.add_player(manager.PARTICIPANTS, mock_player)319 manager.add_player(manager.QUEUE, MagicMock())320 manager.set_team(manager.SPECTATORS, mock_player)321 standard_teams = manager.get_standard_teams()322 self.assertEqual(max_size * team_count, len(standard_teams[0].members))323 self.assertEqual(0, len(standard_teams[2].members))324 def test_set_team_given_player_not_in_any_team_then_error_raised(self):325 manager = self._setup_manager()326 player_name = "Invalid player"327 mock_player = self._create_mock_user()328 mock_player.display_name = player_name329 expected_exception = BotInvalidPlayerRemoval(mock_player, manager.get_standard_teams()[1])330 self._assert_raises_correct_exception(expected_exception, manager.set_team, manager.SPECTATORS, mock_player)331 standard_teams = manager.get_standard_teams()332 self.assertEqual(0, len(standard_teams[0].members))333 def test_set_team_given_team_full_then_error_raised(self):334 manager = self._setup_manager()335 mock_player = self._create_mock_user()336 manager.add_player(manager.PARTICIPANTS, mock_player)337 for _ in range(5):338 manager.add_player(0, MagicMock())339 expected_exception = BotInvalidJoinException(mock_player, manager.get_game_teams()[0],340 "Could not add a player into a full team.")341 self._assert_raises_correct_exception(expected_exception, manager.set_team, 0, mock_player)342 def test_clear_queue_given_players_in_queue_then_all_players_removed(self):343 manager = self._setup_manager()344 manager.add_player(manager.QUEUE, self._create_mock_user())345 manager.clear_queue()346 self.assertEqual(0, len(manager.get_standard_teams()[2].members))347 def test_clear_queue_given_no_players_in_queue_then_nothing_happens(self):348 manager = self._setup_manager()349 manager.clear_queue()350 self.assertEqual(0, len(manager.get_standard_teams()[2].members))351 def test_has_full_teams_when_min_team_sizes_met_then_returns_true(self):352 min_size, max_size, team_count = 3, 5, 6353 manager = self._setup_manager(min_size, max_size, team_count)354 for team in range(team_count):355 for player in range(min_size):356 manager.add_player(team, self._create_mock_user())357 self.assertTrue(manager.has_full_teams)358 def test_has_full_teams_when_max_team_sizes_met_then_returns_true(self):359 min_size, max_size, team_count = 3, 5, 6360 manager = self._setup_manager(min_size, max_size, team_count)361 for team in range(team_count):362 for player in range(max_size):363 manager.add_player(team, self._create_mock_user())364 self.assertTrue(manager.has_full_teams)365 def test_has_full_teams_when_all_min_team_sizes_not_met_then_returns_false(self):366 min_size, max_size, team_count = 3, 5, 6367 manager = self._setup_manager(min_size, max_size, team_count)368 for team in range(team_count):369 for player in range(min_size-1):370 manager.add_player(team, self._create_mock_user())371 self.assertFalse(manager.has_full_teams)372 def test_has_participants_when_participants_present_returns_true(self):373 manager = self._setup_manager()374 manager.add_player(manager.PARTICIPANTS, MagicMock())375 self.assertTrue(manager.has_participants)376 def test_has_participants_when_no_participants_present_returns_false(self):377 manager = self._setup_manager()378 self.assertFalse(manager.has_participants)379 def test_all_players_in_voice_chat_when_all_players_are_in_voice_chat_returns_true(self):380 min_size, max_size, team_count = 5, 5, 3381 mock_guild = self._create_mock_guild()382 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)383 manager._participant_manager = self.participant_manager384 mock_voice_state = generate_mock_voice_state(mock_guild.id)385 mock_participant = AsyncMock()386 self.participant_manager.try_get_participant.return_value = mock_participant387 mock_participant.voice = mock_voice_state388 for team in range(team_count):389 for _ in range(min_size):390 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))391 self.assertTrue(manager.all_players_in_voice_chat)392 def test_all_players_in_voice_chat_when_one_player_not_in_voice_chat_returns_false(self):393 min_size, max_size, team_count = 5, 6, 3394 mock_guild = self._create_mock_guild()395 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)396 manager._participant_manager = self.participant_manager397 mock_voice_state = generate_mock_voice_state(mock_guild.id)398 for team in range(team_count):399 for _ in range(min_size):400 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))401 player_not_in_voice = MagicMock()402 player_not_in_voice.user_id = self.id_generator.generate_viable_id()403 self.mocked_voice_states[player_not_in_voice.user_id] = player_not_in_voice404 manager.add_player(0, player_not_in_voice)405 self.assertFalse(manager.all_players_in_voice_chat)406 def test_all_players_in_voice_chat_when_one_player_in_wrong_voice_chat_returns_false(self):407 min_size, max_size, team_count = 5, 7, 3408 mock_guild = self._create_mock_guild()409 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)410 manager._participant_manager = self.participant_manager411 mock_voice_state = generate_mock_voice_state(mock_guild.id)412 for team in range(team_count):413 for _ in range(min_size):414 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))415 invalid_player = self._create_mock_player_with_voice_state(416 generate_mock_voice_state(self.id_generator.generate_viable_id()))417 manager.add_player(0, invalid_player)418 self.assertFalse(manager.all_players_in_voice_chat)419 def test_all_players_in_voice_chat_when_no_team_voice_channel_then_returns_false(self):420 min_size, max_size, team_count = 5, 7, 3421 mock_guild = self._create_mock_guild()422 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)423 manager.get_game_teams()[0].voice_channel = None424 for team in range(team_count):425 for _ in range(min_size):426 manager.add_player(team, MagicMock())427 manager._participant_manager = self.participant_manager428 self.assertFalse(manager.all_players_in_voice_chat)429 def test_supports_voice_when_enough_channels_present_then_true(self):430 mock_game = _create_mock_game(5, 5, 3)431 teams_channels = [MagicMock()] * 3432 manager = ScrimTeamsManager(mock_game, team_channels=teams_channels)433 self.assertTrue(manager.supports_voice)434 def test_supports_voice_when_not_enough_channels_present_then_false(self):435 mock_game = _create_mock_game(5, 5, 3)436 manager = ScrimTeamsManager(mock_game)437 self.assertFalse(manager.supports_voice)438 def test_all_players_given_players_in_all_teams_returns_all_players(self):439 min_size, max_size, team_count = 3, 5, 6440 added_players = []441 manager = self._setup_manager(min_size, max_size, team_count)442 for team in range(team_count):443 for player in range(max_size):444 mock_user = self._create_mock_user()445 added_players.append(mock_user)446 manager.add_player(team, mock_user)447 self.assertEqual(len(manager.all_participants), len(added_players))448 for user in added_players:449 self.assertIn(user, manager.all_participants)450 async def test_try_move_to_voice_when_all_players_present_then_all_players_moved_to_their_teams_voice_channel(self):451 min_size, max_size, team_count = 5, 5, 3452 mock_guild = self._create_mock_guild()453 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)454 manager._channel_provider = self.channel_provider455 manager._participant_manager = self.participant_manager456 mock_voice_state = generate_mock_voice_state(mock_guild.id)457 mock_voice_channel = MagicMock()458 self.channel_provider.get_channel.return_value = mock_voice_channel459 for team in range(team_count):460 for _ in range(min_size):461 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))462 self.assertTrue(await manager.try_move_to_voice())463 for team in manager.get_game_teams():464 for player in team.members:465 self.mocked_voice_states[player.user_id].move_to.assert_called_with(mock_voice_channel,466 reason="Setting up a scrim.")467 async def test_try_move_to_voice_when_one_player_not_in_voice_chat_returns_false(self):468 min_size, max_size, team_count = 5, 5, 3469 mock_guild = self._create_mock_guild()470 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)471 manager._channel_provider = self.channel_provider472 manager._participant_manager = self.participant_manager473 mock_voice_state = generate_mock_voice_state(mock_guild.id)474 skipped = False475 for team in range(team_count):476 for _ in range(min_size):477 if not skipped:478 skipped = True479 manager.add_player(team, self._create_mock_player_with_voice_state(None))480 continue481 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))482 self.assertFalse(await manager.try_move_to_voice())483 async def test_try_move_to_voice_when_one_player_in_wrong_voice_chat_returns_false(self):484 min_size, max_size, team_count = 5, 5, 3485 mock_guild = self._create_mock_guild()486 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)487 manager._channel_provider = self.channel_provider488 manager._participant_manager = self.participant_manager489 mock_voice_state = generate_mock_voice_state(mock_guild.id)490 mock_invalid_voice_state = generate_mock_voice_state(self.id_generator.generate_nonviable_id())491 skipped = False492 for team in range(team_count):493 for _ in range(min_size):494 if not skipped:495 skipped = True496 manager.add_player(team, self._create_mock_player_with_voice_state(mock_invalid_voice_state))497 continue498 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))499 self.assertFalse(await manager.try_move_to_voice())500 async def test_move_to_lobby_given_lobby_exists_and_all_players_in_voice_then_all_players_moved_to_lobby(self):501 min_size, max_size, team_count = 5, 5, 3502 mock_guild = self._create_mock_guild()503 manager = self._setup_mock_game_with_voice_and_lobby(max_size, min_size, team_count, mock_guild)504 manager._channel_provider = self.channel_provider505 manager._participant_manager = self.participant_manager506 mock_voice_state = generate_mock_voice_state(mock_guild.id)507 mock_voice_channel = MagicMock()508 self.channel_provider.get_channel.return_value = mock_voice_channel509 for team in range(team_count):510 for _ in range(min_size):511 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))512 await manager.move_to_lobby()513 for team in manager.get_game_teams():514 for player in team.members:515 self.mocked_voice_states[player.user_id].move_to.assert_called_with(mock_voice_channel,516 reason="Ending a scrim.")517 async def test_move_to_lobby_given_no_lobby_channel_then_nobody_moved(self):518 min_size, max_size, team_count = 5, 5, 3519 mock_guild = self._create_mock_guild()520 manager = self._setup_mock_game_with_voice(max_size, min_size, team_count, mock_guild)521 manager._channel_provider = self.channel_provider522 manager._participant_manager = self.participant_manager523 mock_voice_state = generate_mock_voice_state(mock_guild.id)524 mock_voice_channel = MagicMock()525 self.channel_provider.get_channel.return_value = mock_voice_channel526 for team in range(team_count):527 for _ in range(min_size):528 manager.add_player(team, self._create_mock_player_with_voice_state(mock_voice_state))529 await manager.move_to_lobby()530 for team in manager.get_game_teams():531 for player in team.members:532 self.mocked_voice_states[player.user_id].move_to.assert_not_called()533 def test_clear_teams_given_all_players_in_game_teams_then_all_players_moved_to_participants(self):534 min_size, max_size, team_count = 3, 5, 6535 manager = self._setup_manager(min_size, max_size, team_count)536 players = []537 for team in range(team_count):538 for player in range(max_size):539 mock_user = self._create_mock_user()540 players.append(mock_user)541 manager.add_player(team, mock_user)542 manager.clear_teams()543 for player in players:544 self.assertIn(player, manager.get_standard_teams()[0].members)545 def test_clear_teams_given_some_players_in_teams_then_all_players_moved_to_participants_and_no_errors_raised(self):546 min_size, max_size, team_count = 3, 5, 6547 manager = self._setup_manager(min_size, max_size, team_count)548 iterations = 10549 for iteration in range(1, iterations + 1):550 with self.subTest(f"Testing team clearing with random amount of players in team (iteration {iteration} "551 f"out of {iterations})"):552 for team in manager._teams.values():553 team.members.clear()554 players = []555 for team in range(team_count):556 for player in range(max_size):557 mock_user = self._create_mock_user()558 players.append(mock_user)559 if random.random() > 0.5:560 manager.add_player(team, mock_user)561 else:562 manager.add_player(manager.PARTICIPANTS, mock_user)563 manager.clear_teams()564 for player in players:565 self.assertIn(player, manager.get_standard_teams()[0].members)566 def _create_mock_guild(self):567 guild_id = self.id_generator.generate_viable_id()568 mock_guild = MagicMock()569 mock_guild.id = guild_id570 return mock_guild571 def _setup_manager(self, min_size=5, max_size=5, team_count=2):572 mock_game = _create_mock_game(min_size, max_size, team_count)573 return ScrimTeamsManager(mock_game, self.participant_manager)574 def _setup_mock_game_with_voice(self, max_size, min_size, team_count, mock_guild):575 mock_game = _create_mock_game(min_size, max_size, team_count)576 channel_ids = self.id_generator.generate_viable_id_group(team_count)577 teams_channels = [generate_mock_voice_channel(channel_id, guild=mock_guild) for channel_id in channel_ids]578 manager = ScrimTeamsManager(mock_game, team_channels=teams_channels)579 return manager580 def _setup_mock_game_with_voice_and_lobby(self, max_size, min_size, team_count, mock_guild):581 mock_game = _create_mock_game(min_size, max_size, team_count)582 channel_ids = self.id_generator.generate_viable_id_group(team_count)583 teams_channels = [generate_mock_voice_channel(channel_id, guild=mock_guild) for channel_id in channel_ids]584 lobby_channel = generate_mock_voice_channel(self.id_generator.generate_viable_id())585 manager = ScrimTeamsManager(mock_game, team_channels=teams_channels, lobby=lobby_channel)...
logging_manager_test.py
Source:logging_manager_test.py
...113 else:114 print >>self.stdout, 'system %s' % suffix115 logging.info('logging %s', suffix)116 PipedStringIO.read_all_pipes()117 def _setup_manager(self, manager_class=logging_manager.LoggingManager):118 def set_stdout(file_object):119 self.stdout = file_object120 manager = manager_class()121 manager.manage_stream(self.stdout, logging.INFO, set_stdout)122 return manager123 def _run_test(self, manager_class):124 manager = self._setup_manager(manager_class)125 self._say(1)126 manager.start_logging()127 self._say(2)128 manager.redirect_to_stream(self._log1)129 self._say(3)130 manager.tee_redirect_to_stream(self._log2)131 self._say(4)132 manager.undo_redirect()133 self._say(5)134 manager.undo_redirect()135 self._say(6)136 manager.stop_logging()137 self._say(7)138 def _grab_fd_info(self):139 command = 'ls -l /proc/%s/fd' % os.getpid()140 proc = subprocess.Popen(command.split(), shell=True,141 stdout=subprocess.PIPE)142 return proc.communicate()[0]143 def _compare_logs(self, log_buffer, expected_text):144 actual_lines = log_buffer.getvalue().splitlines()145 expected_lines = expected_text.splitlines()146 if self._real_system_calls:147 # because of the many interacting processes, we can't ensure perfect148 # interleaving. so compare sets of lines rather than ordered lines.149 actual_lines = set(actual_lines)150 expected_lines = set(expected_lines)151 self.assertEquals(actual_lines, expected_lines)152 def _check_results(self):153 # ensure our stdout was restored154 self.assertEquals(self.stdout, self._original_stdout)155 if self._real_system_calls:156 # ensure FDs were left in their original state157 self.assertEquals(self._grab_fd_info(), self._fd_info)158 self._compare_logs(self.stdout, _EXPECTED_STDOUT)159 self._compare_logs(self._log1, _EXPECTED_LOG1)160 self._compare_logs(self._log2, _EXPECTED_LOG2)161 def test_logging_manager(self):162 self._run_test(logging_manager.LoggingManager)163 self._check_results()164 def test_fd_redirection_logging_manager(self):165 self._real_system_calls = True166 self._fd_info = self._grab_fd_info()167 self._run_test(logging_manager.FdRedirectionLoggingManager)168 self._check_results()169 def test_tee_redirect_debug_dir(self):170 manager = self._setup_manager()171 manager.start_logging()172 manager.tee_redirect_debug_dir('/fake/dir', tag='mytag')173 print >>self.stdout, 'hello'174 manager.undo_redirect()175 print >>self.stdout, 'goodbye'176 manager.stop_logging()177 self._compare_logs(self.stdout,178 'INFO: mytag : hello\nINFO: goodbye')179 self._compare_logs(self._config_object.log, 'hello\n')180class MonkeyPatchTestCase(unittest.TestCase):181 def setUp(self):182 filename = os.path.split(__file__)[1]183 if filename.endswith('.pyc'):184 filename = filename[:-1]...
test_engine.py
Source:test_engine.py
...6 None, evaluator=None, models={}, optimizers={}, max_epochs=10)7 extension = ppe.training.extensions.LogReport()8 engine.extend(extension)9 # Create the actual manager object10 engine._setup_manager(10)11 # Extension is not stored as is so we make sure it was detected12 assert len(engine.manager._extensions) == 113 def test_engine_state_dict(self):14 manager = ppe.training.ExtensionsManager(15 {}, {}, 10, iters_per_epoch=100)16 engine = ppe.training._trainer.Trainer(17 None, evaluator=None, models={}, optimizers={}, max_epochs=10)18 engine._setup_manager(100)19 assert engine.state_dict() == manager.state_dict()20 def test_engine_load_state_dict(self):21 manager = ppe.training.ExtensionsManager(22 {}, {}, 10, iters_per_epoch=100)23 engine = ppe.training._trainer.Trainer(24 None, evaluator=None, models={}, optimizers={}, max_epochs=1)25 engine.load_state_dict(manager.state_dict())26 engine._setup_manager(20)27 assert engine.state_dict() == manager.state_dict()28 def test_engine_load_state_dict_2(self):29 manager = ppe.training.ExtensionsManager(30 {}, {}, 10, iters_per_epoch=100)31 engine = ppe.training._trainer.Trainer(32 None, evaluator=None, models={}, optimizers={}, max_epochs=1)33 engine._setup_manager(20)34 engine.load_state_dict(manager.state_dict())35 assert engine.state_dict() == manager.state_dict()36class TestEngineInvalid:37 def test_engine_wrong_models(self):38 with pytest.raises(ValueError, match='model must be an instance'):39 ppe.training._trainer.Trainer(40 None, evaluator=None, models=object(), optimizers={}, max_epochs=10)41 def test_engine_not_started(self):42 engine = ppe.training._trainer.Trainer(43 None, evaluator=None, models={}, optimizers={}, max_epochs=10)44 with pytest.raises(RuntimeError, match='is not started'):45 engine.state_dict()46 with pytest.raises(RuntimeError, match='is not started'):47 engine.manager48 def test_extend_after_init(self):49 engine = ppe.training._trainer.Trainer(50 None, evaluator=None, models={}, optimizers={}, max_epochs=10)51 engine._setup_manager(10)52 extension = ppe.training.extensions.LogReport()53 with pytest.raises(RuntimeError, match='cannot extend after'):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!