def test_handle_text_notification_none(self):
        # Setup:
        # ... Create a workspace service with mock callbacks and a workspace that always returns None
        ws: WorkspaceService = WorkspaceService()
        ws._logger = utils.get_mock_logger()
        ws._text_change_callbacks = [MagicMock()]
        ws._text_open_callbacks = [MagicMock()]
        ws._text_close_callbacks = [MagicMock()]
        ws._workspace, sf = self._get_mock_workspace(all_none=True)

        nc: NotificationContext = utils.get_mock_notification_context()

        # ... Create a list of methods call and parameters to call them with
        test_calls = [
            (ws._handle_did_change_text_doc,
             self._get_change_text_doc_params(), ws._text_change_callbacks[0]),
            (ws._handle_did_open_text_doc, self._get_open_text_doc_params(),
             ws._text_open_callbacks[0]),
            (ws._handle_did_close_text_doc, self._get_close_text_doc_params(),
             ws._text_close_callbacks[0])
        ]

        for call in test_calls:
            # If: The workspace service receives a request to handle a file that shouldn't be processed
            call[0](nc, call[1])

            # Then: The associated notification callback should not have been called
            call[2].assert_not_called()
Esempio n. 2
0
    def test_language_flavor(self):
        """
        Test that the service ignores files registered as being for non-PGSQL flavors
        """
        # If: I create a new language service
        pgsql_params = LanguageFlavorChangeParams.from_data(
            'file://pguri.sql', 'sql', 'pgsql')
        mssqql_params = LanguageFlavorChangeParams.from_data(
            'file://msuri.sql', 'sql', 'mssql')
        other_params = LanguageFlavorChangeParams.from_data(
            'file://other.doc', 'doc', '')
        service = LanguageService()

        # When: I notify of language preferences
        context: NotificationContext = utils.get_mock_notification_context()

        service.handle_flavor_change(context, pgsql_params)
        service.handle_flavor_change(context, mssqql_params)
        service.handle_flavor_change(context, other_params)

        # Then:
        # ... Only non-PGSQL SQL files should be ignored
        context.send_notification.assert_not_called()
        self.assertFalse(service.is_pgsql_uri(mssqql_params.uri))
        self.assertTrue(service.is_pgsql_uri(pgsql_params.uri))
        self.assertTrue(service.is_pgsql_uri(other_params.uri))

        # When: I change from MSSQL to PGSQL
        mssqql_params = LanguageFlavorChangeParams.from_data(
            'file://msuri.sql', 'sql', 'pgsql')
        service.handle_flavor_change(context, mssqql_params)

        # Then: the service is updated to allow intellisense
        self.assertTrue(service.is_pgsql_uri(mssqql_params.uri))
    def test_mysql_language_flavor(self):
        """
        Test that if provider is MySQL, the service ignores files registered as being for non-MySQL flavors
        """
        # If: I create a new language service
        pgsql_params = LanguageFlavorChangeParams.from_data(
            'file://pguri.sql', 'sql', PG_PROVIDER_NAME)
        mysql_params = LanguageFlavorChangeParams.from_data(
            'file://mysqluri.sql', 'sql', MYSQL_PROVIDER_NAME)
        mssql_params = LanguageFlavorChangeParams.from_data(
            'file://msuri.sql', 'sql', MSSQL_PROVIDER_NAME)
        other_params = LanguageFlavorChangeParams.from_data(
            'file://other.doc', 'doc', '')

        # create a mock mysql service provider
        provider = utils.get_mock_service_provider(
            provider_name=MYSQL_PROVIDER_NAME)
        service = LanguageService()
        service._service_provider = provider

        # When: I notify of language preferences
        context: NotificationContext = utils.get_mock_notification_context()

        service.handle_flavor_change(context, pgsql_params)
        service.handle_flavor_change(context, mssql_params)
        service.handle_flavor_change(context, mysql_params)
        service.handle_flavor_change(context, other_params)

        # Then:
        # ... Only non-MySQL SQL files should be ignored
        context.send_notification.assert_not_called()
        self.assertFalse(service.is_valid_uri(mssql_params.uri))
        self.assertFalse(service.is_valid_uri(pgsql_params.uri))
        self.assertFalse(service.is_valid_uri(other_params.uri))
        self.assertTrue(service.is_valid_uri(mysql_params.uri))

        # When: I change from MSSQL to PGSQL
        mssql_params = LanguageFlavorChangeParams.from_data(
            'file://msuri.sql', 'sql', PG_PROVIDER_NAME)
        service.handle_flavor_change(context, mssql_params)

        # Then: the service is updated to not allow intellisense
        self.assertFalse(service.is_valid_uri(mssql_params.uri))

        # When: I change from PGSQL to MYSQL
        mssql_params = LanguageFlavorChangeParams.from_data(
            'file://msuri.sql', 'sql', MYSQL_PROVIDER_NAME)
        service.handle_flavor_change(context, mssql_params)

        # Then: the service is updated to allow intellisense
        self.assertTrue(service.is_valid_uri(mssql_params.uri))
    def test_handle_did_change_config(self):
        # Setup: Create a workspace service with two mock config change handlers
        ws: WorkspaceService = WorkspaceService()
        ws._logger = utils.get_mock_logger()
        ws._config_change_callbacks = [MagicMock(), MagicMock()]

        # If: The workspace receives a config change notification
        nc: NotificationContext = utils.get_mock_notification_context()
        params: DidChangeConfigurationParams = DidChangeConfigurationParams.from_dict(
            {
                'settings': {
                    'sql': {
                        'intellisense': {
                            'enable_intellisense': False
                        }
                    },
                    'pgsql': {
                        'format': {
                            'keyword_case': 'upper',
                            'identifier_case': 'lower',
                            'strip_comments': True,
                            'reindent': False,
                        }
                    }
                }
            })
        ws._handle_did_change_config(nc, params)

        # Then:
        # ... No notifications should have been sent
        nc.send_notification.assert_not_called()

        # ... The config should have been updated
        self.assertIs(ws.configuration, params.settings)
        self.assertEqual(ws.configuration.pgsql.format.keyword_case, 'upper')
        self.assertEqual(ws.configuration.pgsql.format.identifier_case,
                         'lower')
        self.assertTrue(ws.configuration.pgsql.format.strip_comments)
        self.assertFalse(ws.configuration.pgsql.format.reindent)
        # ... And default values that weren't specified in the notification are preserved
        self.assertTrue(ws.configuration.sql.intellisense.enable_suggestions)

        # ... The mock config change callbacks should have been called
        for callback in ws._config_change_callbacks:
            callback.assert_called_once_with(params.settings)
    def test_handle_text_notification_success(self):
        # Setup:
        # ... Create a workspace service with a mock callback and a workspace that returns a mock script file
        ws: WorkspaceService = WorkspaceService()
        ws._logger = utils.get_mock_logger()
        ws._workspace, sf = self._get_mock_workspace(False)
        ws._text_change_callbacks = [MagicMock()]
        ws._text_open_callbacks = [MagicMock()]
        ws._text_close_callbacks = [MagicMock()]

        # ... Create a mock notification context
        nc: NotificationContext = utils.get_mock_notification_context()

        # ... Create a list of method calls and parameters to call them with
        test_calls = [
            (ws._handle_did_change_text_doc, ws._text_change_callbacks[0],
             self._get_change_text_doc_params(),
             self._test_handle_text_change_helper),
            (ws._handle_did_open_text_doc, ws._text_open_callbacks[0],
             self._get_open_text_doc_params(), None),
            (ws._handle_did_close_text_doc, ws._text_close_callbacks[0],
             self._get_close_text_doc_params(), None)
        ]

        for call in test_calls:
            # If: The workspace service receives a notification
            call[0](nc, call[2])

            # Then:
            # ... The callback should have been called with the script file
            call[1].assert_called_once_with(sf)

            # ... The notification sender should not have not been called
            nc.send_notification.assert_not_called()

            # ... Any additional validation should pass
            if call[3] is not None:
                call[3](call[2], sf)

        # ... Get, Open, and Close file should all have been called
        ws._workspace.get_file.assert_called_once()
        ws._workspace.open_file.assert_called_once()
        ws._workspace.close_file.assert_called_once()
    def test_handle_text_notification_exception(self):
        # Setup:
        # ... Create a workspace service with a workspace that always raises an exception
        ws: WorkspaceService = WorkspaceService()
        ws._logger = utils.get_mock_logger()
        ws._workspace, exp = self._get_mock_workspace(exception=True)

        # ... Create a mock notification context
        nc: NotificationContext = utils.get_mock_notification_context()

        # ... Create a list of method calls and parameters to call them with
        test_calls = [
            (ws._handle_did_change_text_doc,
             self._get_change_text_doc_params()),
            (ws._handle_did_open_text_doc, self._get_open_text_doc_params()),
            (ws._handle_did_close_text_doc, self._get_close_text_doc_params())
        ]

        for call in test_calls:
            # If: The workspace service gets an exception while handling the notification
            call[0](nc, call[1])