Exemplo n.º 1
0
    def setUp(self):
        """Set up the test by creating a query with multiple batches"""
        self.statement_list = statement_list = [
            'select version;', 'select * from t1;'
        ]
        self.statement_str = ''.join(statement_list)
        self.query_uri = 'test_uri'
        self.query = Query(
            self.query_uri, self.statement_str,
            QueryExecutionSettings(ExecutionPlanOptions(),
                                   ResultSetStorageType.FILE_STORAGE),
            QueryEvents())

        self.mock_query_results = [('Id1', 'Value1'), ('Id2', 'Value2')]
        self.cursor = utils.MockCursor(self.mock_query_results)
        self.connection = utils.MockConnection(cursor=self.cursor)

        self.columns_info = []
        db_column_id = DbColumn()
        db_column_id.data_type = 'text'
        db_column_id.column_name = 'Id'
        db_column_value = DbColumn()
        db_column_value.data_type = 'text'
        db_column_value.column_name = 'Value'
        self.columns_info = [db_column_id, db_column_value]
        self.get_columns_info_mock = mock.Mock(return_value=self.columns_info)
Exemplo n.º 2
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)
    def setUp(self):
        self._service_under_test = EditDataService()
        self._mock_connection = mock.MagicMock()
        self._service_provider = ServiceProviderMock({
            'query_execution': {},
            'connection':
            self._mock_connection
        })

        self.cursor = utils.MockCursor(None)
        self.connection = utils.MockConnection(cursor=self.cursor)
        self.cursor.connection = self.connection
        self.connection_service = ConnectionService()
        self.connection_service.get_connection = mock.Mock(
            return_value=self.connection)
        self.query_execution_service = QueryExecutionService()
        self._service_provider._services = {
            constants.CONNECTION_SERVICE_NAME: self.connection_service,
            constants.QUERY_EXECUTION_SERVICE_NAME:
            self.query_execution_service
        }
        self._service_provider._is_initialized = True

        self._service_under_test.register(self._service_provider)

        # self._connection = MockConnection({"port": "8080", "host": "test", "dbname": "test"})
        self._initialize_edit_request = InitializeEditParams()

        self._initialize_edit_request.schema_name = 'public'
        self._initialize_edit_request.object_name = 'Employee'
        self._initialize_edit_request.object_type = 'Table'
        self._initialize_edit_request.owner_uri = 'testuri'
Exemplo n.º 4
0
    def test_handle_create_session_successful(self):
        # Setup:
        # ... Create OE service with mock connection service that returns a successful connection response
        mock_connection = utils.MockConnection({
            'host': 'myserver',
            'dbname': 'postgres',
            'user': '******',
            'port': 123
        })
        cs = ConnectionService()
        cs.connect = mock.MagicMock(return_value=ConnectionCompleteParams())
        cs.get_connection = mock.MagicMock(return_value=mock_connection)
        oe = ObjectExplorerService()
        oe._service_provider = utils.get_mock_service_provider(
            {constants.CONNECTION_SERVICE_NAME: cs})

        # ... Create parameters, session, request context validator
        params, session_uri = _connection_details()

        # ... Create validation of success notification
        def validate_success_notification(response: SessionCreatedParameters):
            self.assertTrue(response.success)
            self.assertEqual(response.session_id, session_uri)
            self.assertIsNone(response.error_message)

            self.assertIsInstance(response.root_node, NodeInfo)
            self.assertEqual(response.root_node.label, TEST_DBNAME)
            self.assertEqual(response.root_node.node_path, session_uri)
            self.assertEqual(response.root_node.node_type, 'Database')
            self.assertIsInstance(response.root_node.metadata, ObjectMetadata)
            self.assertEqual(response.root_node.metadata.urn,
                             oe._session_map[session_uri].server.urn_base)
            self.assertEqual(
                response.root_node.metadata.name,
                oe._session_map[session_uri].server.maintenance_db_name)
            self.assertEqual(response.root_node.metadata.metadata_type_name,
                             'Database')
            self.assertFalse(response.root_node.is_leaf)

        rc = RequestFlowValidator()
        rc.add_expected_response(
            CreateSessionResponse,
            lambda param: self.assertEqual(param.session_id, session_uri))
        rc.add_expected_notification(SessionCreatedParameters,
                                     SESSION_CREATED_METHOD,
                                     validate_success_notification)

        # If: I create a session
        oe._handle_create_session_request(rc.request_context, params)
        oe._session_map[session_uri].init_task.join()

        # Then:
        # ... Error notification should have been returned, session should be cleaned up from OE service
        rc.validate()

        # ... The session should still exist and should have connection and server setup
        self.assertIn(session_uri, oe._session_map)
        self.assertIsInstance(oe._session_map[session_uri].server, Server)
        self.assertTrue(oe._session_map[session_uri].is_ready)
Exemplo n.º 5
0
 def setUp(self):
     self._cursor = utils.MockCursor(None)
     self._connection = utils.MockConnection(cursor=self._cursor)
     self._batch_text = 'Select * from t1'
     self._batch_id = 1
     self._batch_events = BatchEvents()
     self._selection_data = SelectionData()
     self._result_set = mock.MagicMock()
Exemplo n.º 6
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()
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
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())
Exemplo n.º 10
0
    def setUp(self):
        self._rows = [(
            1,
            'int4',
        ), (2, 'bool')]
        self._cursor = utils.MockCursor(self._rows)

        column = namedtuple('Column', [
            'name', 'type_code', 'display_size', 'internal_size', 'precision',
            'scale', 'null_ok'
        ])

        self._cursor.description = [
            column('id', 1, None, None, None, None, True),
            column('is_valid', 2, None, None, None, None, True)
        ]
        self._connection = utils.MockConnection(cursor=self._cursor)
Exemplo n.º 11
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)