def test_stream_closes_during_read_and_write(self):
        """
            Assert expected exception on attempt to read and write to a closed stream.
        """
        test_stream = io.BytesIO()
        json_rpc_writer = jsonrpc.JsonRpcWriter(test_stream)
        json_rpc_writer.send_request(method=u'testMethod/DoThis',
                                     params={u'Key': u'Value'},
                                     request_id=1)

        # reset the stream.
        test_stream.seek(0)
        json_rpc_reader = jsonrpc.JsonRpcReader(test_stream)

        test_stream.close()
        with self.assertRaises(ValueError):
            json_rpc_reader.read_response()

        test_stream = io.BytesIO()
        json_rpc_writer = jsonrpc.JsonRpcWriter(test_stream)
        test_stream.close()

        with self.assertRaises(ValueError):
            json_rpc_writer.send_request(method=u'testMethod/DoThis',
                                         params={u'Key': u'Value'},
                                         request_id=1)
    def test_nested_request(self):
        """
            Verify submission of a valid nested request.
        """
        test_stream = io.BytesIO()
        json_rpc_writer = jsonrpc.JsonRpcWriter(test_stream)
        json_rpc_writer.send_request(method=u'testMethod/DoThis',
                                     params={
                                         u'Key': u'Value',
                                         u'key2': {
                                             u'key3': u'value3',
                                             u'key4': u'value4'
                                         }
                                     },
                                     request_id=1)

        test_stream.seek(0)
        json_rpc_reader = jsonrpc.JsonRpcReader(test_stream)
        response = json_rpc_reader.read_response()
        baseline = {
            u'jsonrpc': u'2.0',
            u'params': {
                u'Key': u'Value',
                u'key2': {
                    u'key3': u'value3',
                    u'key4': u'value4'
                }
            },
            u'method': u'testMethod/DoThis',
            u'id': 1
        }
        self.assertEqual(response, baseline)

        json_rpc_reader.close()
        self.assertTrue(test_stream.closed)
    def test_basic_request(self):
        """
            Verify json rpc writer submits a valid request.
        """
        test_stream = io.BytesIO()
        json_rpc_writer = jsonrpc.JsonRpcWriter(test_stream)
        json_rpc_writer.send_request(method=u'testMethod/DoThis',
                                     params={u'Key': u'Value'},
                                     request_id=1)

        # Use JSON RPC reader to read request.
        test_stream.seek(0)
        json_rpc_reader = jsonrpc.JsonRpcReader(test_stream)
        response = json_rpc_reader.read_response()
        baseline = {
            u'jsonrpc': u'2.0',
            u'params': {
                u'Key': u'Value'
            },
            u'method': u'testMethod/DoThis',
            u'id': 1
        }
        self.assertEqual(response, baseline)

        json_rpc_reader.close()
        self.assertTrue(test_stream.closed)
Exemple #4
0
def generate_query_baseline(file_name):
    sqltoolsservice_args = [mssqltoolsservice.get_executable_path()]
    with io.open(file_name, 'wb') as baseline:
        tools_service_process = subprocess.Popen(sqltoolsservice_args,
                                                 bufsize=0,
                                                 stdin=subprocess.PIPE,
                                                 stdout=baseline)

        # Before running this script, enter a real server name.
        parameters = {
            u'OwnerUri': u'connectionservicetest',
            u'Connection': {
                u'ServerName': u'*',
                u'DatabaseName': u'AdventureWorks2014',
                u'UserName': u'*',
                u'Password': u'*',
                u'AuthenticationType': u'Integrated'
            }
        }

        writer = json_rpc_client.JsonRpcWriter(tools_service_process.stdin)
        writer.send_request(u'connection/connect', parameters, id=1)

        time.sleep(2)

        parameters = {
            u'OwnerUri': u'connectionservicetest',
            u'Query': "select * from HumanResources.Department"
        }
        writer.send_request(u'query/executeString', parameters, id=2)

        time.sleep(5)
        parameters = {
            u'OwnerUri': u'connectionservicetest',
            u'BatchIndex': 0,
            u'ResultSetIndex': 0,
            u'RowsStartIndex': 0,
            u'RowsCount': 16
        }

        writer.send_request(u'query/subset', parameters, id=3)
        # submit raw request.
        time.sleep(5)
        tools_service_process.kill()