Example #1
0
    def test_handle_get_database_info_request(self):
        """Test that the database info handler responds with the correct database info"""
        uri = 'test_uri'
        db_name = 'test_db'
        user_name = 'test_user'
        # Set up the request parameters
        params = GetDatabaseInfoParameters()
        params.owner_uri = uri
        request_context = MockRequestContext()

        # Set up a mock connection and cursor for the test
        mock_query_results = [(user_name, )]
        mock_cursor = MockCursor(mock_query_results)
        mock_connection = MockPGServerConnection(mock_cursor, name=db_name)
        self.connection_service.get_connection = mock.Mock(
            return_value=mock_connection)

        # If I send a get_database_info request
        self.admin_service._handle_get_database_info_request(
            request_context, params)

        # Then the service responded with the expected information
        response = request_context.last_response_params
        self.assertIsInstance(response, GetDatabaseInfoResponse)
        expected_info = {'dbname': db_name, 'owner': user_name, 'size': None}
        self.assertEqual(response.database_info.options, expected_info)

        # And the service retrieved the owner name using a query with the database name as a parameter
        owner_query = "SELECT pg_catalog.pg_get_userbyid(db.datdba) FROM pg_catalog.pg_database db WHERE db.datname = '{}'".format(
            db_name)
        mock_cursor.execute.assert_called_once_with(owner_query)
Example #2
0
 def test_metadata_list_request_error(self):
     """Test that the proper error response is sent if there is an error while handling a metadata list request"""
     request_context = MockRequestContext()
     params = MetadataListParameters()
     params.owner_uri = self.test_uri
     self.metadata_service._list_metadata = mock.Mock(side_effect=Exception)
     mock_thread = MockThread()
     with mock.patch('threading.Thread', new=mock.Mock(side_effect=mock_thread.initialize_target)):
         # If I call the metadata list request handler and its execution raises an error
         self.metadata_service._handle_metadata_list_request(request_context, params)
     # Then an error response is sent
     self.assertIsNotNone(request_context.last_error_message)
     self.assertIsNone(request_context.last_notification_method)
     self.assertIsNone(request_context.last_response_params)
    def test_list_databases(self):
        """Test that the list databases handler correctly lists the connection's databases"""
        connection_service = ConnectionService()
        connection_uri = 'someuri'
        request_context = MockRequestContext()
        params = ListDatabasesParams()
        params.owner_uri = connection_uri
        connection = psycopg2.connect(**get_connection_details())
        connection_service.get_connection = mock.Mock(return_value=connection)

        # If I call the list database handler
        connection_service.handle_list_databases(request_context, params)

        # Then a response is returned that lists all the databases
        database_names = request_context.last_response_params.database_names
        self.assertGreater(len(database_names), 0)
        self.assertIn(connection.get_dsn_parameters()['dbname'], database_names)
Example #4
0
    def test_metadata_list_request(self):
        """Test that the metadata list handler properly starts a thread to list metadata and responds with the list"""
        # Set up the parameters and mocks for the request
        expected_metadata = [
            ObjectMetadata(schema='schema1', name='table1', metadata_type=MetadataType.TABLE),
            ObjectMetadata(schema='schema1', name='view1', metadata_type=MetadataType.VIEW),
            ObjectMetadata(schema='schema1', name='function1', metadata_type=MetadataType.FUNCTION),
            ObjectMetadata(schema='schema1', name='table2', metadata_type=MetadataType.TABLE),
            ObjectMetadata(schema='schema2', name='view1', metadata_type=MetadataType.VIEW),
            ObjectMetadata(schema='schema2', name='function1', metadata_type=MetadataType.FUNCTION),
        ]

        metadata_type_to_str_map = {
            MetadataType.TABLE: 't',
            MetadataType.VIEW: 'v',
            MetadataType.FUNCTION: 'f'
        }

        # Query results have schema_name, object_name, and object_type columns in that order
        list_query_result = [(metadata.schema, metadata.name, metadata_type_to_str_map[metadata.metadata_type]) for metadata in expected_metadata]
        mock_cursor = MockCursor(list_query_result)
        mock_connection = MockPGServerConnection(cur=mock_cursor)
        self.connection_service.get_connection = mock.Mock(return_value=mock_connection)
        request_context = MockRequestContext()
        params = MetadataListParameters()
        params.owner_uri = self.test_uri
        mock_thread = MockThread()
        with mock.patch('threading.Thread', new=mock.Mock(side_effect=mock_thread.initialize_target)):
            # If I call the metadata list request handler
            self.metadata_service._handle_metadata_list_request(request_context, params)
            # Then the worker thread was kicked off
            self.assertEqual(mock_thread.target, self.metadata_service._metadata_list_worker)
            mock_thread.start.assert_called_once()
        # And the worker retrieved the correct connection and executed a query on it
        self.connection_service.get_connection.assert_called_once_with(self.test_uri, ConnectionType.DEFAULT)
        mock_cursor.execute.assert_called_once()
        # And the handler responded with the expected results
        self.assertIsNone(request_context.last_error_message)
        self.assertIsNone(request_context.last_notification_method)
        response = request_context.last_response_params
        self.assertIsInstance(response, MetadataListResponse)
        for index, actual_metadata in enumerate(response.metadata):
            self.assertIsInstance(actual_metadata, ObjectMetadata)
            self.assertEqual(actual_metadata.schema, expected_metadata[index].schema)
            self.assertEqual(actual_metadata.name, expected_metadata[index].name)
            self.assertEqual(actual_metadata.metadata_type, expected_metadata[index].metadata_type)
Example #5
0
    def test_get_database_info_request_integration(self):
        # Set up the request parameters
        params = GetDatabaseInfoParameters()
        params.owner_uri = 'test_uri'
        request_context = MockRequestContext()

        # Set up the connection service to return our connection
        connection = psycopg2.connect(**get_connection_details())
        self.connection_service.get_connection = mock.Mock(return_value=connection)

        # If I send a get_database_info request
        self.admin_service._handle_get_database_info_request(request_context, params)

        # Then the service responded with a valid database owner
        owner = request_context.last_response_params.database_info.options['owner']

        cursor = connection.cursor()
        cursor.execute('select usename from pg_catalog.pg_user')
        usernames = [row[0] for row in cursor.fetchall()]
        self.assertIn(owner, usernames)