def setUp(self): self.sequence = [1, 2, 3] self.random_sequence = [2, 1, 3] spy2(random.sample) when(random).sample(any_(list), any_(int)).thenReturn(self.random_sequence)
def testSpy2(self): spy2('os.path.exists') import os.path assert not os.path.exists('/Foo') verify('os.path', times=1).exists('/Foo')
def test_shuffle_double_blink_when_diff_one_player(self): spy2(time.sleep) when2(time.sleep, any_(float)).thenReturn(None) spy2(Plugin.shuffle) when2(Plugin.shuffle).thenReturn(None) connected_players(fake_player(1, "Red Player1", team="red"), fake_player(2, "Red Player2", team="red"), fake_player(3, "Red Player3", team="red"), fake_player(4, "Red Player4", team="red"), fake_player(5, "Blue Player1", team="blue"), fake_player(6, "Blue Player2", team="blue"), fake_player(7, "Blue Player3", team="blue"), fake_player(8, "Blue Player4", team="blue"), fake_player(9, "Blue Player5", team="blue")) autoready.shuffle_double_blink(10) verify(time, times=2).sleep(0.2) verify(time).sleep(0.3) verify(Plugin, times=0).shuffle() assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^1 ^7:^1 ", times=2) assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^10^7:^110", times=2)
def test_stop_when_target_time_is_unset(self): spy2(self.countdown_thread.is_alive) when2(self.countdown_thread.is_alive).thenReturn(True) self.countdown_thread.stop() assert_that(self.countdown_thread.seconds_left, is_(self.countdown_thread.duration))
def test_search_transliteration_empty(query, expected, fragment_finder, fragment_repository): spy2(fragment_repository.query_by_transliteration) query = TransliterationQuery(query) test_result = fragment_finder.search_transliteration(query) verifyZeroInteractions(fragment_repository) assert test_result == expected unstub()
def test_allready(self): spy2(Plugin.allready) when2(Plugin.allready).thenReturn(None) autoready.allready(0) verify(Plugin).allready() assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^20^7:^200")
def test_stop_when_thread_is_running(self): spy2(self.countdown_thread.is_alive) when2(self.countdown_thread.is_alive).thenReturn(True) test_target_time = self.fake_thread_runtime + timedelta( seconds=11, milliseconds=999, microseconds=999) self.countdown_thread._target_time = test_target_time # pylint: disable=W0212 self.countdown_thread.stop() assert_that(self.countdown_thread.seconds_left, is_(11))
def test_blink(self): spy2(time.sleep) when2(time.sleep, any_(float)).thenReturn(None) autoready.blink(8) verify(time).sleep(0.4) assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^1 ^7:^1 ") assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^10^7:^108")
def test_warning_blink(self): spy2(time.sleep) when2(time.sleep, any_(float)).thenReturn(None) autoready.warning_blink(30, "thirty_second_warning") verify(time).sleep(0.4) assert_plugin_played_sound("thirty_second_warning") assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^1 ^7:^1 ") assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^10^7:^130")
def setUp(self): setup_plugin() setup_cvars({ "qlx_fastvoteTypes": "map", "qlx_fastvoteStrategy": "threshold", "qlx_fastvoteThresholdFastPassDiff": "6", "qlx_fastvoteThresholdFastFailDiff": "5", "qlx_fastvoteParticipationPercentage": "0.67" }) spy2(Plugin.force_vote) spy2(Plugin.current_vote_count) self.plugin = fastvotes()
def test_wear_off_double_blink(self): spy2(time.sleep) when2(time.sleep, any_(float)).thenReturn(None) autoready.wear_off_double_blink(8) verify(time, times=2).sleep(0.2) verify(time).sleep(0.3) assert_plugin_played_sound("sound/items/wearoff.ogg") assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^1 ^7:^1 ", times=2) assert_plugin_center_printed( "Match will ^2auto-start^7 in\n^10^7:^108", times=2)
def test_disabled_command_run(): """ Make sure that mirror_project() results in calling run_command(). """ spy2(opengrok_tools.utils.mirror.run_command) project_name = "foo" config = {DISABLED_CMD_PROPERTY: {COMMAND_PROPERTY: ["cat"]}, PROJECTS_PROPERTY: {project_name: {DISABLED_PROPERTY: True}}} assert mirror_project(config, project_name, False, None, None) == CONTINUE_EXITVAL verify(opengrok_tools.utils.mirror).run_command(ANY, project_name)
def test_environment_loop(env_id, graph_param, seed, num_envs): """Tests Meta RL Loop.""" # Create a single adaptation environment. adapt_envs = env_utils.create_environment( env_id=env_id, graph_param=graph_param, seed=seed, batch_size=num_envs, num_adapt_steps=100, add_fewshot_wrapper=True, use_multi_processing=False, ) spy2(adapt_envs.step) # To keep track of # of invocations. environment_spec = specs.make_environment_spec(adapt_envs) # Create test environment. test_envs = env_utils.create_environment(env_id=env_id, graph_param=graph_param, seed=seed, batch_size=num_envs, num_adapt_steps=100, add_fewshot_wrapper=True, use_multi_processing=False) # Create meta agent (default: MSGI-Random). meta_agent = agents.MSGI(environment_spec=environment_spec, num_adapt_steps=100, num_trial_splits=5, branch_neccessary_first=True, exploration='random', temp=200, w_a=3.0, beta_a=8.0, ep_or=0.8, temp_or=2.0) # Run meta loop. meta_loop = environment_loop.EnvironmentMetaLoop( adapt_environment=adapt_envs, test_environment=test_envs, meta_agent=meta_agent, label='meta_eval') meta_loop.run( num_trials=3, num_adapt_steps=100, # should be greater than TimeLimit num_test_episodes=1, num_trial_splits=5) expected_steps = 3 * 100 mockito.verify(adapt_envs, times=expected_steps).step(...)
def test_ignore_errors_hooks(monkeypatch, hook_type, per_project): """ Test that ignore errors property overrides failed hook. """ def mock_get_repos(*args, **kwargs): return [mock(spec=GitRepository)] spy2(opengrok_tools.utils.mirror.process_hook) project_name = "foo" hook_dir = "/befelemepeseveze" hook_name = "nonexistent" if per_project: config = { HOOKDIR_PROPERTY: hook_dir, PROJECTS_PROPERTY: { project_name: { IGNORE_ERR_PROPERTY: True, HOOKS_PROPERTY: { hook_type: hook_name } } } } else: config = { IGNORE_ERR_PROPERTY: True, HOOKDIR_PROPERTY: hook_dir, PROJECTS_PROPERTY: { project_name: { HOOKS_PROPERTY: { hook_type: hook_name } } } } with monkeypatch.context() as m: mock_get_repos.called = False m.setattr("opengrok_tools.utils.mirror.get_repos_for_project", mock_get_repos) src_root = "srcroot" assert mirror_project(config, project_name, False, None, src_root) == SUCCESS_EXITVAL verify(opengrok_tools.utils.mirror).\ process_hook(hook_type, os.path.join(hook_dir, hook_name), src_root, project_name, None, None) # Necessary to disable the process_hook spy otherwise mockito will # complain about recursive invocation. unstub()
def testA(self): with pytest.raises(TypeError) as exc: when2(os) assert str(exc.value) == "can't guess origin of 'os'" cp = os.path.commonprefix with pytest.raises(TypeError) as exc: spy2(cp) assert str(exc.value) == "can't guess origin of 'cp'" ptch = patch with pytest.raises(TypeError) as exc: ptch(os.path.exists, lambda: 'boo') assert str(exc.value) == "could not destructure first argument"
def setUp(self): spy2(time.sleep) when2(time.sleep, any_(float)).thenReturn(None) self.mocked_function42 = mock() self.mocked_function21 = mock() timed_test_actions = { 42: self.mocked_function42, 21: self.mocked_function21 } self.countdown_thread = CountdownThread( 125, timed_actions=timed_test_actions) self.fake_thread_runtime = datetime(year=2022, month=4, day=4, hour=11, minute=11, second=11) self.countdown_thread._now = self.fake_thread_runtime # pylint: disable=W0212
def setUp(self): setup_plugin() setup_cvars({ "qlx_modeVoteNewMapDefault": "vql", }) spy2(Plugin.callvote) spy2(minqlx.console_command) spy2(minqlx.client_command) self.plugin = custom_modes_vote()
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 setup_vote_in_progress(self): spy2(Plugin.is_vote_active) when2(Plugin.is_vote_active).thenReturn(True)
def testSpy2(self): spy2(os.path.exists) assert os.path.exists('/Foo') is False verify(os.path).exists('/Foo')
def qlstats_response(self, status_code=200): spy2(minqlx.console_command) response = mock(Response) response.status_code = status_code response.text = "" return response
def testA(self): dummy = Dummy() spy2(dummy.foo) assert dummy.foo() == 'foo' verify(dummy).foo()
def testModule(self): spy2(time.time) assert time.time() is not None verify(time).time()
def testB(self): spy2(Dummy.class_method) assert Dummy.class_method('foo') == 'foo' verify(Dummy).class_method('foo')