def setup_plugin() -> None: """Set up a minqlx.Plugin for unit testing. This function will enable spying on certain functions like messages sent to the console through msg, and center_prints on the screen. **Make sure to use :func:`mockito.unstub()` after calling this function to avoid side effects spilling into the next test.** """ spy2(Plugin.msg) when2(Plugin.msg, any_(str)).thenReturn(None) spy2(Plugin.center_print) when2(Plugin.center_print, any_(str)).thenReturn(None) spy2(Plugin.play_sound) when2(Plugin.play_sound, any_(str)).thenReturn(None) spy2(Plugin.players) when2(Plugin.players).thenReturn(None) spy2(Plugin.player) when2(Plugin.player, any_()).thenReturn(None) spy2(Plugin.switch) when2(Plugin.switch, any_, any_).thenReturn(None) spy2(minqlx.set_cvar) when2(minqlx.set_cvar, any_, any_).thenReturn(None) spy2(Plugin.kick) when2(Plugin.kick, any_, any_(str)).thenReturn(None) spy2(minqlx.get_cvar) when2(minqlx.get_cvar, "zmq_stats_enable").thenReturn("1")
def test_warntimer_sets_thread_name(self): setup_cvar("roundtimelimit", "180") patch(time.sleep, lambda _int: None) undecorated(self.warner.warntimer)(self.warner) assert_that(self.warner.warner_thread_name, any_(str))
def test_plays_sound_when_round_still_running(self): warner_thread_name = "test_plays_sound_when_round_still_running1" setup_game_in_progress(game_type="ca") self.warner.warner_thread_name = warner_thread_name undecorated(self.warner.play_thirty_second_warning)(self.warner, warner_thread_name) assert_plugin_played_sound(any_(str))
def test_plays_no_sound_when_next_round_started(self): calling_round_number = 4 setup_game_in_progress(game_type="ca") self.warner.timer_round_number = calling_round_number + 1 undecorated(self.warner.play_thirty_second_warning)( self.warner, calling_round_number) assert_plugin_played_sound(any_(str), times=0)
def test_callback_connect_player_gets_kicked_for_wrong_privacy_settings( self): self.plugin.kick_players = True kicked_player = fake_player(123, "Test Player") connected_players(kicked_player) self.setup_balance_playerprivacy([(kicked_player, "private")]) self.plugin.delayed_kick = mock() when(self.plugin).delayed_kick(any_, any_(str)).thenReturn(None) self.plugin.callback_connect([kicked_player.steam_id], None) verify(self.plugin).delayed_kick(kicked_player.steam_id, any_)
def test_callback_connect_players_privacy_info_not_yet_available(self): self.plugin.kick_players = True not_kicked_player = fake_player(123, "Test Player") connected_players(not_kicked_player) self.setup_balance_playerprivacy({}) self.plugin.delayed_kick = mock() when(self.plugin).delayed_kick(any_, any_(str)).thenReturn(None) self.plugin.callback_connect([not_kicked_player.steam_id], None) verify(self.plugin, times=0).delayed_kick(not_kicked_player.steam_id, any_)
def test_cmd_switch_plugin_kicks_players(self): self.plugin.plugin_enabled = False self.plugin.kick_players = True reply_channel = mocked_channel() kicked_player = fake_player(123, "Test Player") connected_players(kicked_player) self.setup_balance_playerprivacy([(kicked_player, "private")]) self.plugin.delayed_kick = mock() when(self.plugin).delayed_kick(any_, any_(str)).thenReturn(None) self.plugin.cmd_switch_plugin(None, ["!policy"], reply_channel) verify(self.plugin).delayed_kick(kicked_player.steam_id, any_)
def test_callback_connect_player_does_not_get_kicked_for_privacy_settings( self): self.plugin.kick_players = True not_kicked_player = fake_player(123, "Test Player") connected_players(not_kicked_player) self.setup_balance_playerprivacy([(not_kicked_player, "public")]) self.plugin.delayed_kick = mock() when(self.plugin).delayed_kick(any_, any_(str)).thenReturn(None) self.plugin.callback_connect([not_kicked_player.steam_id], None) verify(self.plugin, times=0).delayed_kick(not_kicked_player.steam_id, any)
def test_callback_connect_player_has_exception(self): self.plugin.kick_players = True not_kicked_player = fake_player(123, "Test Player") connected_players(not_kicked_player) self.plugin.exceptions.add(not_kicked_player.steam_id) self.setup_balance_playerprivacy([(not_kicked_player, "private")]) self.plugin.delayed_kick = mock() when(self.plugin).delayed_kick(any_, any_(str)).thenReturn(None) self.plugin.callback_connect([not_kicked_player.steam_id], None) verify(self.plugin, times=0).delayed_kick(not_kicked_player.steam_id, any_)
def test_handle_team_switch_attempt_player_with_max_join_attempts_is_kicked( self): kicked_player = fake_player(123, "Test Player") connected_players(kicked_player) self.plugin.join_attempts[kicked_player.steam_id] = 0 # noinspection PyUnresolvedReferences self.plugin.plugins["balance"].player_info = { kicked_player.steam_id: { "privacy": "private" } } return_code = self.plugin.handle_team_switch_attempt( kicked_player, "spec", "blue") assert_that(return_code, is_(minqlx.RET_STOP_ALL)) verify(kicked_player).kick(any_())
from typing import Union, Any from minqlx import Player, Plugin from mockito import mock, when2, verify # type: ignore # noinspection PyProtectedMember from mockito.matchers import Matcher, any_ # type: ignore # Functions for setting up players in the game and verifying interactions with them. any_team: Matcher = any_(str) def fake_player(steam_id: int, name: str, team: str = "spectator", _id: int = 0, score: int = 0, ping: int = 0) \ -> Player: """A builder for mocked players that assertion can be used to check for certain interactions. **Make sure to use :func:`mockito.unstub()` after calling this function to avoid side effects spilling into the next test.** :param steam_id: the steam_id of this fake_player :param name: the name of the fake_player :param team: the team the player should be on (default "spectator") :param _id: the id on the server of the player (default: 0) :param score: the score of the player (default: 0) :param ping: the ping that player should have (default: 0) :return: a mocked player that might be used to set up the game and interactions can be checked with assertion functions afterwards. """ player = mock(spec=Player, strict=False) player.id = _id
def testShouldTreatNonMatchersAsEqMatcher(self): self.assertTrue(and_("foo", any_(str)).matches("foo")) self.assertFalse(and_("foo", any_(int)).matches("foo"))
def test_plays_no_sound_when_game_not_in_progress(self): setup_game_in_warmup() undecorated(self.warner.play_thirty_second_warning)(self.warner, 4) assert_plugin_played_sound(any_(str), times=0)
def test_plays_no_sound_when_game_is_not_clan_arena(self): setup_game_in_progress(game_type="ft") undecorated(self.warner.play_thirty_second_warning)(self.warner, 4) assert_plugin_played_sound(any_(str), times=0)
def test_plays_no_sound_when_game_is_not_running_anymore(self): setup_no_game() undecorated(self.warner.play_thirty_second_warning)(self.warner, 4) assert_plugin_played_sound(any_(str), times=0)