public async void SubsetServiceOutOfRangeSubsetTest()
        {
            // If:
            // ... I have a query that doesn't have any result sets
            var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, workspaceService);
            var executeParams    = new QueryExecuteParams {
                QuerySelection = null, OwnerUri = Common.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <QueryExecuteResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

            await queryService.ActiveQueries[Common.OwnerUri].ExecutionTask;

            // ... And I then ask for a set of results from it
            var subsetParams = new QueryExecuteSubsetParams {
                OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0
            };
            QueryExecuteSubsetResult result = null;
            var subsetRequest = GetQuerySubsetResultContextMock(qesr => result = qesr, null);

            queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object).Wait();

            // Then:
            // ... I should get an error result
            // ... There should not be rows
            // ... There should not be any error calls
            VerifyQuerySubsetCallCount(subsetRequest, Times.Once(), Times.Never());
            Assert.NotNull(result.Message);
            Assert.Null(result.ResultSubset);
        }
        public async Task SubsetServiceUnexecutedQueryTest()
        {
            // If:
            // ... I have a query that hasn't finished executing (doesn't matter what)
            var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(new[] { Common.StandardTestData }, true, false, workspaceService);
            var executeParams    = new QueryExecuteParams {
                QuerySelection = null, OwnerUri = Common.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <QueryExecuteResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

            await queryService.ActiveQueries[Common.OwnerUri].ExecutionTask;

            queryService.ActiveQueries[Common.OwnerUri].Batches[0].ResultSets[0].hasBeenRead = false;

            // ... And I then ask for a valid set of results from it
            var subsetParams = new QueryExecuteSubsetParams {
                OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0
            };
            QueryExecuteSubsetResult result = null;
            var subsetRequest = GetQuerySubsetResultContextMock(qesr => result = qesr, null);
            await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);

            // Then:
            // ... I should get an error result
            // ... There should not be rows
            // ... There should not be any error calls
            VerifyQuerySubsetCallCount(subsetRequest, Times.Once(), Times.Never());
            Assert.NotNull(result.Message);
            Assert.Null(result.ResultSubset);
        }
        internal static async Task ExecuteQuery(string query)
        {
            // create a temporary "workspace" file
            using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
                // create the client helper which wraps the client driver objects
                using (ClientHelper testHelper = new ClientHelper())
                {
                    // connnection details
                    ConnectParams connectParams = new ConnectParams();
                    connectParams.Connection                    = new ConnectionDetails();
                    connectParams.Connection.ServerName         = "localhost";
                    connectParams.Connection.DatabaseName       = "master";
                    connectParams.Connection.AuthenticationType = "Integrated";

                    // connect to the database
                    await testHelper.Connect(queryTempFile.FilePath, connectParams);

                    // execute the query
                    QueryExecuteCompleteParams queryComplete =
                        await testHelper.RunQuery(queryTempFile.FilePath, query);

                    if (queryComplete.BatchSummaries != null && queryComplete.BatchSummaries.Length > 0)
                    {
                        var batch = queryComplete.BatchSummaries[0];
                        if (batch.ResultSetSummaries != null && batch.ResultSetSummaries.Length > 0)
                        {
                            var resultSet = batch.ResultSetSummaries[0];

                            // retrive the results
                            QueryExecuteSubsetResult querySubset = await testHelper.ExecuteSubset(
                                queryTempFile.FilePath, batch.Id,
                                resultSet.Id, 0, (int)resultSet.RowCount);

                            // print the header
                            foreach (var column in resultSet.ColumnInfo)
                            {
                                Console.Write(column.ColumnName + ", ");
                            }
                            Console.Write(Environment.NewLine);

                            // print the rows
                            foreach (var row in querySubset.ResultSubset.Rows)
                            {
                                for (int i = 0; i < resultSet.ColumnInfo.Length; ++i)
                                {
                                    Console.Write(row.GetValue(i) + ", ");
                                }
                                Console.Write(Environment.NewLine);
                            }
                        }
                    }

                    // close database connection
                    await testHelper.Disconnect(queryTempFile.FilePath);
                }
        }
        public async Task HandleResultSubsetRequest(QueryExecuteSubsetParams subsetParams,
                                                    RequestContext <QueryExecuteSubsetResult> requestContext)
        {
            try
            {
                // Attempt to load the query
                Query query;
                if (!ActiveQueries.TryGetValue(subsetParams.OwnerUri, out query))
                {
                    await requestContext.SendResult(new QueryExecuteSubsetResult
                    {
                        Message = SR.QueryServiceRequestsNoQuery
                    });

                    return;
                }

                // Retrieve the requested subset and return it
                var result = new QueryExecuteSubsetResult
                {
                    Message      = null,
                    ResultSubset = await query.GetSubset(subsetParams.BatchIndex,
                                                         subsetParams.ResultSetIndex, subsetParams.RowsStartIndex, subsetParams.RowsCount)
                };
                await requestContext.SendResult(result);
            }
            catch (InvalidOperationException ioe)
            {
                // Return the error as a result
                await requestContext.SendResult(new QueryExecuteSubsetResult
                {
                    Message = ioe.Message
                });
            }
            catch (ArgumentOutOfRangeException aoore)
            {
                // Return the error as a result
                await requestContext.SendResult(new QueryExecuteSubsetResult
                {
                    Message = aoore.Message
                });
            }
            catch (Exception e)
            {
                // This was unexpected, so send back as error
                await requestContext.SendError(e.Message);
            }
        }
Beispiel #5
0
        public async void SubsetServiceUnexecutedQueryTest()
        {
            // Set up file for returning the query
            var fileMock = new Mock <ScriptFile>();

            fileMock.SetupGet(file => file.Contents).Returns(Common.StandardQuery);
            // Set up workspace mock
            var workspaceService = new Mock <WorkspaceService <SqlToolsSettings> >();

            workspaceService.Setup(service => service.Workspace.GetFile(It.IsAny <string>()))
            .Returns(fileMock.Object);
            // If:
            // ... I have a query that hasn't finished executing (doesn't matter what)
            var queryService = await Common.GetPrimedExecutionService(
                Common.CreateMockFactory(new[] { Common.StandardTestData }, false), true,
                workspaceService.Object);

            var executeParams = new QueryExecuteParams {
                QuerySelection = null, OwnerUri = Common.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <QueryExecuteResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

            await queryService.ActiveQueries[Common.OwnerUri].ExecutionTask;

            queryService.ActiveQueries[Common.OwnerUri].HasExecuted = false;

            // ... And I then ask for a valid set of results from it
            var subsetParams = new QueryExecuteSubsetParams {
                OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0
            };
            QueryExecuteSubsetResult result = null;
            var subsetRequest = GetQuerySubsetResultContextMock(qesr => result = qesr, null);

            queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object).Wait();

            // Then:
            // ... I should get an error result
            // ... There should not be rows
            // ... There should not be any error calls
            VerifyQuerySubsetCallCount(subsetRequest, Times.Once(), Times.Never());
            Assert.NotNull(result.Message);
            Assert.Null(result.ResultSubset);
        }
        public async Task SubsetServiceMissingQueryTest()
        {
            // If:
            // ... I ask for a set of results for a file that hasn't executed a query
            var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, workspaceService);
            var subsetParams     = new QueryExecuteSubsetParams {
                OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0
            };
            QueryExecuteSubsetResult result = null;
            var subsetRequest = GetQuerySubsetResultContextMock(qesr => result = qesr, null);
            await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);

            // Then:
            // ... I should have an error result
            // ... There should be no rows in the result set
            // ... There should not be any error calls
            VerifyQuerySubsetCallCount(subsetRequest, Times.Once(), Times.Never());
            Assert.NotNull(result.Message);
            Assert.Null(result.ResultSubset);
        }