コード例 #1
0
    def test_script_successful(self):
        # Setup:
        # ... Create a scripter
        conn = utils.MockConnection({"port": "8080", "host": "test", "dbname": "test"})
        script = scripter.Scripter(conn)

        # ... Mock up the server so it returns something from the urn locator
        # ... Make sure that the mock has the script methods included
        mock_obj = mock.MagicMock(spec=Table)
        mock_obj.create_script = mock.MagicMock(return_value='CREATE')
        mock_obj.delete_script = mock.MagicMock(return_value='DELETE')
        mock_obj.update_script = mock.MagicMock(return_value='UPDATE')
        mock_obj.select_script = mock.MagicMock(return_value='SELECT')

        # ... Mocks for SELECT TODO: remove as per (https://github.com/Microsoft/carbon/issues/1764)
        mock_obj.name = 'table'
        mock_obj.schema = 'schema'

        for operation in scripter.ScriptOperation:
            # ... Create a mock to return the object by UR
            script.server.get_object_by_urn = mock.MagicMock(return_value=mock_obj)

            # ... Mock up some metadata
            mock_metadata = ObjectMetadata('//urn/', None, 'obj', 'ObjName')

            # If: I attempt to perform a scripting operation
            result = script.script(operation, mock_metadata)

            # Then:
            # ... I should get something back
            # NOTE: The actual contents of the script is tested in the PGSMO object's unit tests
            utils.assert_not_none_or_whitespace(result)

            # ... The URN should have been used to get the object
            script.server.get_object_by_urn.assert_called_once_with(mock_metadata.urn)
コード例 #2
0
 def setUp(self):
     """Set up mock objects for testing the scripting service.
     Ran before each unit test.
     """
     self.connection = utils.MockConnection({"port": "8080", "host": "test", "dbname": "test"})
     self.scripter = scripter.Scripter(self.connection)
     self.server = self.scripter.server
     self.service = ScriptingService()
コード例 #3
0
    def test_script_no_metadata(self):
        # Setup: Create a scripter
        conn = utils.MockConnection({"port": "8080", "host": "test", "dbname": "test"})
        script = scripter.Scripter(conn)

        # If: I attempt to perform a script operation that is invalid
        # Then: I should get an exception
        with self.assertRaises(Exception):
            script.script(scripter.ScriptOperation.UPDATE, None)
コード例 #4
0
    def test_script_invalid_operation(self):
        # Setup: Create a scripter
        conn = utils.MockConnection({"port": "8080", "host": "test", "dbname": "test"})
        script = scripter.Scripter(conn)

        # If: I attempt to perform a script operation that is invalid
        # Then: I should get an exception
        with self.assertRaises(ValueError):
            script.script('bogus_handler', None)
コード例 #5
0
    def send_definition_using_connected_completions(
            self, request_context: RequestContext,
            scriptparseinfo: ScriptParseInfo, params: TextDocumentPosition,
            context: ConnectionContext) -> bool:
        if not context or not context.is_connected:
            return False

        definition_result: DefinitionResult = None
        completer: PGCompleter = context.pgcompleter
        completions: List[Completion] = completer.get_completions(
            scriptparseinfo.document, None)

        if completions:
            word_under_cursor = scriptparseinfo.document.get_word_under_cursor(
            )
            matching_completion = next(
                completion for completion in completions
                if completion.display == word_under_cursor)
            if matching_completion:
                connection = self._connection_service.get_connection(
                    params.text_document.uri, ConnectionType.QUERY)
                scripter_instance = scripter.Scripter(connection)
                object_metadata = ObjectMetadata(
                    None, None, matching_completion.display_meta,
                    matching_completion.display, matching_completion.schema)
                create_script = scripter_instance.script(
                    ScriptOperation.CREATE, object_metadata)

                if create_script:
                    with tempfile.NamedTemporaryFile(
                            mode='wt',
                            delete=False,
                            encoding='utf-8',
                            suffix='.sql',
                            newline=None) as namedfile:
                        namedfile.write(create_script)
                        if namedfile.name:
                            file_uri = "file:///" + namedfile.name.strip('/')
                            location_in_script = Location(
                                file_uri, Range(Position(0, 1), Position(1,
                                                                         1)))
                            definition_result = DefinitionResult(
                                False, None, [
                                    location_in_script,
                                ])

                            request_context.send_response(
                                definition_result.locations)
                            return True

        if definition_result is None:
            request_context.send_response(DefinitionResult(True, '', []))
            return False
コード例 #6
0
    def test_init(self):
        # Setup: Create a mock connection
        conn = utils.MockConnection({"port": "8080", "host": "test", "dbname": "test"})

        # If: I create a new scripter
        script = scripter.Scripter(conn)

        # Then: Internal state should be properly setup
        self.assertIsInstance(script.server, Server)

        for operation in scripter.ScriptOperation:
            self.assertIn(operation, script.SCRIPT_HANDLERS.keys())
コード例 #7
0
    def test_script_unsupported(self):
        # Setup:
        # ... Create a scripter
        conn = utils.MockConnection({"port": "8080", "host": "test", "dbname": "test"})
        script = scripter.Scripter(conn)

        for operation in scripter.ScriptOperation:
            # ... Mock up the server so it returns something from the urn locator
            mock_obj = {}
            script.server.get_object_by_urn = mock.MagicMock(return_value=mock_obj)

            # ... Mock up some metadata
            mock_metadata = ObjectMetadata('//urn/', None, 'obj', 'ObjName')

            # If: I attempt to perform an operation the handler doesn't support
            # Then:
            # ... I should get an exception
            with self.assertRaises(TypeError):
                script.script(operation, mock_metadata)

            # ... The URN should have been used to get the object
            script.server.get_object_by_urn.assert_called_once_with(mock_metadata.urn)