Пример #1
0
 def test_execute_operation_calls_timeout_if_not_connected(self):
     # Given an operation with a non-connected context
     task = mock.Mock()
     timeout_task = mock.Mock()
     operations_queue = OperationsQueue(self.mock_service_provider)
     operation = QueuedOperation(self.expected_context_key, task,
                                 timeout_task)
     operation.context = ConnectionContext(self.expected_context_key)
     # When I execute the operation
     operations_queue.execute_operation(operation)
     # Then I expect the timeout task to be called
     timeout_task.assert_called_once()
     # ... and I do not expect the regular task to be called
     task.assert_not_called()
Пример #2
0
 def test_execute_operation_calls_task_and_timouttask_if_task_fails(self):
     # Given an operation where the task will fail (return false)
     context = ConnectionContext(self.expected_context_key)
     context.is_connected = True
     context.pgcompleter = mock.Mock()
     task = mock.MagicMock(return_value=False)
     timeout_task = mock.Mock()
     operations_queue = OperationsQueue(self.mock_service_provider)
     operation = QueuedOperation(self.expected_context_key, task,
                                 timeout_task)
     operation.context = context
     # When I execute the operation
     operations_queue.execute_operation(operation)
     # Then I expect the regular task to be called
     task.assert_called_once()
     actual_context = task.call_args[0][0]
     self.assertEqual(actual_context, context)
     # ... and I also expect the timeout task to be called
     timeout_task.assert_called_once()
Пример #3
0
    def handle_definition_request(
            self, request_context: RequestContext,
            text_document_position: TextDocumentPosition) -> None:
        request_context.send_notification(
            STATUS_CHANGE_NOTIFICATION,
            StatusChangeParams(
                owner_uri=text_document_position.text_document.uri,
                status="DefinitionRequested"))

        def do_send_default_empty_response():
            request_context.send_response([])

        if self.should_skip_intellisense(
                text_document_position.text_document.uri):
            do_send_default_empty_response()
            return

        script_file: ScriptFile = self._workspace_service.workspace.get_file(
            text_document_position.text_document.uri)
        if script_file is None:
            do_send_default_empty_response()
            return

        script_parse_info: ScriptParseInfo = self.get_script_parse_info(
            text_document_position.text_document.uri,
            create_if_not_exists=False)
        if not script_parse_info or not script_parse_info.can_queue():
            do_send_default_empty_response()
            return

        cursor_position: int = len(
            script_file.get_text_in_range(
                Range.from_data(0, 0, text_document_position.position.line,
                                text_document_position.position.character)))
        text: str = script_file.get_all_text()
        script_parse_info.document = Document(text, cursor_position)

        operation = QueuedOperation(
            script_parse_info.connection_key,
            functools.partial(self.send_definition_using_connected_completions,
                              request_context, script_parse_info,
                              text_document_position),
            functools.partial(do_send_default_empty_response))
        self.operations_queue.add_operation(operation)
        request_context.send_notification(
            STATUS_CHANGE_NOTIFICATION,
            StatusChangeParams(
                owner_uri=text_document_position.text_document.uri,
                status="DefinitionRequestCompleted"))
Пример #4
0
 def test_add_operation_succeeds_when_has_key(self):
     # Given I have a connection in the map
     operations_queue = OperationsQueue(self.mock_service_provider)
     operations_queue._context_map[
         self.expected_context_key] = ConnectionContext(
             self.expected_context_key)
     # When I add an operation
     operations_queue.add_operation(
         QueuedOperation(self.expected_context_key, None, None))
     # Then I expect the operation to be added successfully to the queue
     operation: QueuedOperation = operations_queue.queue.get_nowait()
     self.assertEqual(operation.key, self.expected_context_key)
     # ... and I expect the context to have been set automatically
     self.assertEqual(
         operation.context,
         operations_queue._context_map[self.expected_context_key])
Пример #5
0
    def handle_completion_request(self, request_context: RequestContext,
                                  params: TextDocumentPosition) -> None:
        """
        Lookup available completions when valid completion suggestions are requested.
        Sends an array of CompletionItem objects over the wire
        """
        response = []

        def do_send_default_empty_response():
            request_context.send_response(response)

        script_file: ScriptFile = self._workspace_service.workspace.get_file(
            params.text_document.uri)
        if script_file is None:
            do_send_default_empty_response()
            return

        if self.should_skip_intellisense(script_file.file_uri):
            do_send_default_empty_response()
            return

        script_parse_info: ScriptParseInfo = self.get_script_parse_info(
            params.text_document.uri, create_if_not_exists=False)
        if not script_parse_info or not script_parse_info.can_queue():
            self._send_default_completions(request_context, script_file,
                                           params)
        else:
            cursor_position: int = len(
                script_file.get_text_in_range(
                    Range.from_data(0, 0, params.position.line,
                                    params.position.character)))
            text: str = script_file.get_all_text()
            script_parse_info.document = Document(text, cursor_position)
            operation = QueuedOperation(
                script_parse_info.connection_key,
                functools.partial(self.send_connected_completions,
                                  request_context, script_parse_info, params),
                functools.partial(self._send_default_completions,
                                  request_context, script_file, params))
            self.operations_queue.add_operation(operation)
Пример #6
0
 def test_add_operation_without_context_throws(self):
     operations_queue = OperationsQueue(self.mock_service_provider)
     with self.assertRaises(KeyError):
         operations_queue.add_operation(
             QueuedOperation('something', None, None))