def test_link_dry_run_failure_output(mocker: MockerFixture, capsys): metainfo_file = MetainfoFile({ "info_hash": "meaningless", "name": "some_name" }) location = Path() link_service = mocker.Mock(spec=LinkService) link_service.get_incomplete_id_by_metainfo_file.return_value = { metainfo_file: 1 } link_service.change_location.side_effect = RuntimeError("something") find_service = mocker.Mock(spec=FindService) torrent_data = TorrentData(metainfo_file, location) missing_torrent_data = TorrentData(metainfo_file) find_service.find.return_value = {torrent_data, missing_torrent_data} command = LinkCommand(link_service, find_service) output = command.dry_run() output.dry_run_display() result = capsys.readouterr().out assert (result == "\n".join([ "Found the following torrents:", "some_name at .", "Couldn't find data for the following torrents:", "some_name", ]) + "\n")
async def socketio_subscriber_handlers( socketio_client_factory: Callable, client_session_id: UUIDStr, mocker: MockerFixture, ) -> NamedTuple: """socketio SUBSCRIBER Somehow this emulates the logic of the front-end: it connects to a session (client_session_id) and registers two event handlers that are be called when a message is received """ # connect to websocket session # NOTE: will raise socketio.exceptions.ConnectionError: Unexpected status code 401 in server response # if client does not hold an authentication token sio: socketio.AsyncClient = await socketio_client_factory(client_session_id ) WEBSOCKET_LOG_EVENTNAME = "logger" # called when log messages are received mock_log_handler = mocker.Mock() sio.on(WEBSOCKET_LOG_EVENTNAME, handler=mock_log_handler) WEBSOCKET_NODE_UPDATE_EVENTNAME = "nodeUpdated" # called when a node was updated (e.g. progress) mock_node_update_handler = mocker.Mock() sio.on(WEBSOCKET_NODE_UPDATE_EVENTNAME, handler=mock_node_update_handler) return namedtuple("_MockHandlers", "log_handler node_update_handler")( mock_log_handler, mock_node_update_handler)
def test_database_db_query(mocker: MockerFixture): db = MysqldbDatabase("loc", "user", "password", "database") with pytest.raises(Exception, match="initiate the connection"): db.db_query("") db._db = mocker.Mock() db._db.open = True with pytest.raises(TypeError, match="Database.query a str"): db.db_query(1) with pytest.raises(TypeError, match="maxrows must be an int"): db.db_query("", maxrows="") with pytest.raises(ValueError, match="maxrows must be positive"): db.db_query("", maxrows=-1) with pytest.raises(TypeError, match="how must be an int"): db.db_query("", how="") with pytest.raises(ValueError, match="how must be 0, 1, or 2"): db.db_query("", how=4) db._db.store_result.return_value = None assert not db.db_query("") db._db.store_result.return_value = mocker.Mock() db._db.store_result.return_value.fetch_row.return_value = [{ "a": bytes("foo", "utf8") }] assert db.db_query("") == [{"a": "foo"}]
def test_create_opc_client( event_loop: asyncio.AbstractEventLoop, expect_user_pass: bool, mocker: MockerFixture, url: str, with_cert_file: bool, ) -> None: mocked_asyncua_client = mocker.patch("asyncua.Client") mocked_asyncua_client.return_value.set_security = mocker.AsyncMock() config = mocker.Mock(server_url=url) if with_cert_file: config.configure_mock(cert_file="certFile", private_key_file="keyFile") else: config.configure_mock(cert_file=None, private_key_file=None) opcua_client = OPCUAClient(config, mocker.Mock(), mocker.Mock(), mocker.Mock()) created_client = event_loop.run_until_complete( opcua_client._create_opc_client()) assert mocked_asyncua_client.call_args_list == [ mocker.call(url="//opc/server.url") ] set_user = cast(Mock, created_client.set_user) set_password = cast(Mock, created_client.set_password) expected_set_user_call = [] expected_set_pw_call = [] expected_set_security_call = [] if expect_user_pass: expected_set_user_call.append(mocker.call("user")) expected_set_pw_call.append(mocker.call("pass")) if with_cert_file: expected_set_security_call.append( mocker.call(SecurityPolicyBasic256Sha256, "certFile", "keyFile")) assert set_user.call_args_list == expected_set_user_call assert set_password.call_args_list == expected_set_pw_call assert created_client.set_security.await_args_list == expected_set_security_call
def test_init_calls_mark_muted( self, mocker: MockerFixture, stream_name: str, title: str, is_muted_topic_return_value: bool, is_muted_called: bool, ) -> None: mark_muted = mocker.patch(MODULE + ".TopicButton.mark_muted") controller = mocker.Mock() controller.model.is_muted_topic = mocker.Mock( return_value=is_muted_topic_return_value) controller.model.stream_dict = {205: {"name": stream_name}} view = mocker.Mock() topic_button = TopicButton( stream_id=205, topic=title, controller=controller, view=view, count=0, ) if is_muted_called: mark_muted.assert_called_once_with() else: mark_muted.assert_not_called()
def test_datachange_notification( influx_write: bool, mocker: MockerFixture, node_id: str, opcua_client: OPCUAClient, ) -> None: data_change_message_mock = mocker.patch( "opcua_webhmi_bridge.opcua.OPCDataChangeMessage") node = mocker.Mock() node.configure_mock(**{"nodeid.Identifier": node_id}) value = mocker.sentinel.value mocker.patch.object(opcua_client, "set_status") opcua_client.datachange_notification(node, value, mocker.Mock()) set_status = cast(Mock, opcua_client.set_status) message_instance = data_change_message_mock.return_value assert set_status.call_args_list == [mocker.call(LinkStatus.Up)] assert data_change_message_mock.call_args_list == [ mocker.call(node_id=node_id, ua_object=value) ] record_last_opc_data = cast( Mock, opcua_client._centrifugo_proxy_server.record_last_opc_data) assert record_last_opc_data.call_args_list == [ mocker.call(message_instance) ] messaging_writer_put = cast(Mock, opcua_client._frontend_messaging_writer.put) assert messaging_writer_put.call_args_list == [ mocker.call(message_instance) ] influx_writer_put = cast(Mock, opcua_client._influx_writer.put) expected_influx_put_call = [mocker.call(message_instance) ] if influx_write else [] assert influx_writer_put.call_args_list == expected_influx_put_call
def test_activate_called_once_on_keypress( self, mocker: MockerFixture, enter_key: str, widget_size: Callable[[Widget], urwid_Size], caption: str = "some user", email: str = "some_email", user_id: int = 5, ) -> None: user: Dict[str, Any] = { "email": email, "user_id": user_id, "full_name": caption, } activate = mocker.patch(MODULE + ".UserButton.activate") user_button = UserButton( user=user, controller=mocker.Mock(), view=mocker.Mock(), color=mocker.Mock(), state_marker="*", count=mocker.Mock(), ) size = widget_size(user_button) user_button.keypress(size, enter_key) assert activate.call_count == 1
def test_init_calls_top_button( self, mocker: MockerFixture, emoji_unit: Tuple[str, str, List[str]], to_vary_in_message: Dict[str, Any], message_fixture: Message, count: int, ) -> None: controller = mocker.Mock() controller.model.has_user_reacted_to_message = mocker.Mock( return_value=False) update_widget = mocker.patch(MODULE + ".EmojiButton.update_widget") top_button = mocker.patch(MODULE + ".TopButton.__init__") caption = ", ".join([emoji_unit[0], *emoji_unit[2]]) message_fixture["reactions"] = to_vary_in_message["reactions"] emoji_button = EmojiButton( controller=controller, emoji_unit=emoji_unit, message=message_fixture, reaction_count=count, is_selected=lambda *_: False, toggle_selection=lambda *_: None, ) top_button.assert_called_once_with( controller=controller, caption=caption, prefix_character="", show_function=emoji_button.update_emoji_button, ) assert emoji_button.emoji_name == emoji_unit[0] assert emoji_button.reaction_count == count
def test_run_threads(mocker: MockerFixture): c = Crawler('https://project-plato.com', '/tmp/project-plato') c._to_visit.add('https://project-plato.com/a') c._to_visit.add('https://project-plato.com/b') c._lookup_links = mocker.Mock() c._store_data = mocker.Mock() mock_req = mocker.patch('crawler.executor.requests') mock_req.get.return_value = response_mock = mocker.Mock() response_mock.status_code = 200 response_mock.content = b'<html>hi</html>' c.run() assert len(c._visited_urls) == 3 assert c._lookup_links.call_count == 3 c._lookup_links.assert_called_with('<html>hi</html>') assert c._store_data.call_count == 3 c._store_data.assert_any_call('https://project-plato.com', b'<html>hi</html>') c._store_data.assert_any_call('https://project-plato.com/a', b'<html>hi</html>') c._store_data.assert_any_call('https://project-plato.com/b', b'<html>hi</html>')
async def test_client_ws_connect(mocker: pytest_mock.MockerFixture): runner_mock = mocker.Mock() runner_mock.wait = AsyncMock() m = mocker.patch("pybotters.client.WebSocketRunner", return_value=runner_mock) hdlr_str = mocker.Mock() hdlr_bytes = mocker.Mock() hdlr_json = mocker.Mock() async with pybotters.Client() as client: ret = await client.ws_connect( "ws://test.org", send_str='{"foo":"bar"}', send_bytes=b'{"foo":"bar"}', send_json={"foo": "bar"}, hdlr_str=hdlr_str, hdlr_bytes=hdlr_bytes, hdlr_json=hdlr_json, ) assert m.called assert m.call_args == [ ("ws://test.org", client._session), { "send_str": '{"foo":"bar"}', "send_bytes": b'{"foo":"bar"}', "send_json": { "foo": "bar" }, "hdlr_str": hdlr_str, "hdlr_bytes": hdlr_bytes, "hdlr_json": hdlr_json, }, ] assert ret == runner_mock
def controller(self, mocker: MockerFixture) -> Controller: # Patch these unconditionally to avoid calling in __init__ self.poll_for_events = mocker.patch(MODEL + ".poll_for_events") mocker.patch(MODULE + ".Controller.show_loading") self.main_loop = mocker.patch( MODULE + ".urwid.MainLoop", return_value=mocker.Mock() ) self.config_file = "path/to/zuliprc" self.theme_name = "zt_dark" self.theme = generate_theme("zt_dark", 256) self.in_explore_mode = False self.autohide = True # FIXME Add tests for no-autohide self.notify_enabled = False self.maximum_footlinks = 3 result = Controller( self.config_file, self.maximum_footlinks, self.theme_name, self.theme, 256, self.in_explore_mode, self.autohide, self.notify_enabled, ) result.view.message_view = mocker.Mock() # set in View.__init__ result.model.server_url = SERVER_URL return result
def test_add_run_display(mocker: MockerFixture, capsys): api = mocker.Mock(spec=TransmissionApi) api.add_torrent.return_value = CommandResult() service = AddService(api) fs = mocker.Mock(spec=Filesystem) metainfo_paths = {Path("/", "test_path", str(n)) for n in range(2)} metainfo_files = { MetainfoFile({ "info_hash": path, "name": "some_name" }, path) for path in metainfo_paths } command = AddCommand(service, fs, metainfo_files) output: AddOutput = command.run() output.display() result = capsys.readouterr().out assert (result == "\n".join([ "2 torrents were added:", "some_name", "some_name", "2 torrents were deleted:", "some_name at /test_path/0", "some_name at /test_path/1", ]) + "\n")
def test_setup_cors(mocker: MockerFixture) -> None: from fastapi.middleware.cors import CORSMiddleware origins = [ "http://localhost", "http://localhost:8080", ] fakeapp = mocker.Mock() fakeapp.add_middleware = mocker.Mock() cors.setup_cors(fakeapp, origins) cors.setup_cors(fakeapp) assert fakeapp.add_middleware.call_count == 2 args, kwargs = fakeapp.add_middleware.call_args_list[0] assert args[0] == CORSMiddleware assert kwargs == { 'allow_origins': origins, 'allow_credentials': True, 'allow_methods': ['*'], 'allow_headers': ['*'] } # check out the default args version too args, kwargs = fakeapp.add_middleware.call_args_list[1] assert args[0] == CORSMiddleware assert kwargs == { 'allow_origins': cors.default_origins, 'allow_credentials': True, 'allow_methods': ['*'], 'allow_headers': ['*'] }
def test_error_archive_run(mocker: MockerFixture): archive_path = Path("/", "test_path") fs = mocker.Mock(spec=Filesystem) client = mocker.Mock(spec=TransmissionApi) client.get_errors_by_id.return_value = QueryResult( {1: (1, "some tracker error"), 2: (3, "some local error")} ) client.get_torrent_files_by_id.return_value = QueryResult( {1: Path("/some/path"), 2: Path("/some/path2")} ) client.get_torrent_name_by_id.return_value = QueryResult( {1: "some_name", 2: "another_name"} ) command = ErrorArchiveCommand(archive_path, fs, client) result = command.run() assert result.local_errors == { ArchiveAction( 2, "another_name", Path("/some/path2"), client_error=(3, "some local error") ) } assert result.tracker_errors == { ArchiveAction( 1, "some_name", Path("/some/path"), client_error=(1, "some tracker error") ) }
def test_editor_mode_error_on_multiple_enter( self, mocker: MockerFixture, controller: Controller ) -> None: controller.enter_editor_mode_with(mocker.Mock()) with pytest.raises(AssertionError): controller.enter_editor_mode_with(mocker.Mock())
def test_list_issues_from_repo_no_filter_request( mocker: MockerFixture, domain_gh_conn_settings: List[cs.GhConnectionSettings], domain_issues: List[i.Issue], mock_github3_login: MockerFixture, ) -> None: """It returns empty result.""" issue1 = mocker.Mock(title="issue title", number=3) issue1.labels.return_value = [] issue2 = mocker.Mock(title="doesnt match title", number=4) issue2.labels.return_value = [] repo = mock_github3_login.return_value.repositories.return_value[1] repo.issues.return_value = [ issue1, issue2, ] response = gc.GithubService( domain_gh_conn_settings[0]).list_issues_from_repo( REPO, NO_FILTER_REQUEST_ISSUES) assert len(response) == 2 assert response[0].number == domain_issues[3].number assert response[1].number == domain_issues[2].number assert response[0].title == domain_issues[3].title assert response[1].title == domain_issues[2].title
def test_display_errors(mocker: MockerFixture, capsys): archive_path = Path("/", "test_path") fs = mocker.Mock(spec=Filesystem) client = mocker.Mock(spec=TransmissionApi) client.get_errors_by_id.return_value = QueryResult( {1: (1, "some tracker error"), 2: (3, "some local error")} ) client.get_torrent_files_by_id.return_value = QueryResult( {1: Path("/some/path"), 2: Path("/some/path2")} ) client.get_torrent_name_by_id.return_value = QueryResult( {1: "some_name", 2: "another_name"} ) command = ErrorArchiveCommand(archive_path, fs, client) output = command.run() output.display() result = capsys.readouterr().out assert ( result == "\n".join( [ "Found 1 torrent local errors:", 'another_name with error "some local error"', "Found 1 torrent tracker errors:", 'some_name with error "some tracker error"', "Moved 1 metainfo files to /test_path/tracker_error", "\x1b[32m✓ another_name", "Moved 1 metainfo files to /test_path/local_error", "\x1b[32m✓ some_name", ] ) + "\n" )
def test_create_03(mocker: MockerFixture) -> None: """test push task creation""" taskcluster = mocker.patch("orion_decision.scheduler.Taskcluster", autospec=True) queue = taskcluster.get_service.return_value now = datetime.utcnow() root = FIXTURES / "services03" evt = mocker.Mock(spec=GithubEvent()) evt.repo.path = root evt.repo.git = mocker.Mock(return_value="\n".join( str(p) for p in root.glob("**/*"))) evt.commit = "commit" evt.branch = "push" evt.event_type = "push" evt.http_url = "https://example.com" evt.pull_request = None sched = Scheduler(evt, now, "group", "secret", "push") sched.services["test1"].dirty = True sched.create_tasks() assert queue.createTask.call_count == 2 build_task_id, build_task = queue.createTask.call_args_list[0][0] assert build_task == yaml_load( BUILD_TASK.substitute( clone_url="https://example.com", commit="commit", deadline=stringDate(now + DEADLINE), dockerfile="test1/Dockerfile", expires=stringDate(now + ARTIFACTS_EXPIRE), load_deps="0", max_run_time=int(MAX_RUN_TIME.total_seconds()), now=stringDate(now), owner_email=OWNER_EMAIL, provisioner=PROVISIONER_ID, route="index.project.fuzzing.orion.test1.push", scheduler=SCHEDULER_ID, service_name="test1", source_url=SOURCE_URL, task_group="group", worker=WORKER_TYPE, )) _, push_task = queue.createTask.call_args_list[1][0] push_expected = yaml_load( PUSH_TASK.substitute( clone_url="https://example.com", commit="commit", deadline=stringDate(now + DEADLINE), docker_secret="secret", max_run_time=int(MAX_RUN_TIME.total_seconds()), now=stringDate(now), owner_email=OWNER_EMAIL, provisioner=PROVISIONER_ID, scheduler=SCHEDULER_ID, service_name="test1", source_url=SOURCE_URL, task_group="group", worker=WORKER_TYPE, )) push_expected["dependencies"].append(build_task_id) assert push_task == push_expected
def mock_github3_login(mocker: MockerFixture) -> MockerFixture: """Fixture for mocking github3.login.""" mock = mocker.patch("github3.login", autospec=True) mock.return_value.repositories.return_value = [ mocker.Mock(full_name="staticdev/nope"), mocker.Mock(full_name="staticdev/omg"), ] return mock
def fake_tree(mocker: MockerFixture) -> XsdTree: fake_root = XsdNode("root", element=mocker.Mock()) fake_attr = XsdAttribute("attr", element=mocker.Mock()) fake_attr.enumeration = ["v1", "v2"] fake_root.attributes[fake_attr.name] = fake_attr child = XsdNode("child", element=mocker.Mock(), parent=fake_root) child.max_occurs = 1 return XsdTree(fake_root)
def mock_client_session(mocker: pytest_mock.MockerFixture, mock_response: Any) -> MagicMock: session: MagicMock = mocker.MagicMock() cast(Any, session).__aenter__.return_value = session session.get = mocker.Mock(return_value=mock_response) session.post = mocker.Mock(return_value=mock_response) return session
def test_service_circular_deps(mocker: MockerFixture, fixture: str) -> None: """test that circular service dependencies raise an error""" root = FIXTURES / fixture repo = mocker.Mock(spec="orion_decision.git.GitRepo") repo.path = root repo.git = mocker.Mock(return_value="\n".join(str(p) for p in root.glob("**/*"))) with pytest.raises(RuntimeError) as exc: Services(repo) assert "cycle" in str(exc)
def mock_read_block_by_number(mocker: pytest_mock.MockerFixture, expected_number: int) -> None: """ mock_read_block_by_number """ header_mock = mocker.Mock() header_mock.block_number = expected_number block_mock = mocker.Mock() block_mock.header = header_mock mock_block_by_number = mocker.patch.object(chain.Blockchain, 'read_block_by_number') mock_block_by_number.return_value = block_mock if expected_number is not None else None
def fake_tree_with_attrs(mocker: MockerFixture) -> XsdTree: fake_root = XsdNode("root", element=mocker.Mock()) fake_attr = XsdAttribute("one", element=mocker.Mock()) fake_root.attributes[fake_attr.name] = fake_attr fake_attr = XsdAttribute("two", element=mocker.Mock()) fake_root.attributes[fake_attr.name] = fake_attr fake_attr = XsdAttribute("three", element=mocker.Mock()) fake_root.attributes[fake_attr.name] = fake_attr XsdNode("child", element=mocker.Mock(), parent=fake_root) return XsdTree(fake_root)
def test_service_path_dep_top_level(mocker: MockerFixture) -> None: """test that similarly named files at top-level don't affect service deps""" root = FIXTURES / "services08" repo = mocker.Mock(spec="orion_decision.git.GitRepo") repo.path = root repo.git = mocker.Mock(return_value="\n".join(str(p) for p in root.glob("**/*"))) svcs = Services(repo) assert set(svcs) == {"test1"} svcs.mark_changed_dirty([root / "setup.py"]) assert not svcs["test1"].dirty
def mock_read_block_by_hash(mocker: pytest_mock.MockerFixture, expected_hash: str) -> None: """ mock_read_block_by_hash """ header_mock = mocker.Mock() header_mock.hash = bytes.fromhex(expected_hash) if expected_hash else None block_mock = mocker.Mock() block_mock.header = header_mock mock_block_by_number = mocker.patch.object(chain.Blockchain, 'read_block_by_hash') mock_block_by_number.return_value = block_mock if expected_hash is not None else None
def mock_github3_login(mocker: MockerFixture) -> MockerFixture: """Fixture for mocking github3.login.""" mock_repo = mocker.Mock(full_name=REPO) mock_repo2 = mocker.Mock(full_name=REPO2) mock = mocker.patch("github3.login", autospec=True) mock.return_value.repositories.return_value = [ mock_repo2, mock_repo, ] return mock
def opcua_client(mocker: MockerFixture) -> OPCUAClient: config = mocker.Mock( cert_file="certFile", private_key_file="keyFile", monitor_nodes=["monitornode1", "monitornode2"], record_nodes=["recnode1", "recnode2"], retry_delay=1234, ) centrifugo_proxy_server = mocker.Mock(last_opc_status=None) return OPCUAClient(config, centrifugo_proxy_server, mocker.Mock(), mocker.Mock())
def mock_transaction_count_by_hash(mocker: pytest_mock.MockerFixture, block_hash: str, transaction_count: int) -> None: """ mock_transaction_count_by_hash """ body_mock = mocker.Mock() body_mock.transactions = [None] * transaction_count block_mock = mocker.Mock() block_mock.body = body_mock mock_block_by_number = mocker.patch.object(chain.Blockchain, 'read_block_by_hash') mock_block_by_number.return_value = block_mock if block_hash is not None else None
def test_services_repo(mocker: MockerFixture) -> None: """test that local services (not known to git) are ignored""" root = FIXTURES / "services03" repo = mocker.Mock(spec="orion_decision.git.GitRepo") repo.path = root repo.git = mocker.Mock( return_value="\n".join( str(p) for p in root.glob("**/*") if "test3" not in str(p) ) ) svcs = Services(repo) assert set(svcs) == {"test1", "test2", "test4", "test5", "test6", "test7"} assert len(svcs) == 6