public async Task ServiceDispose()
        {
            // Setup:
            // ... We need a query service
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);

            // If:
            // ... I execute some bogus query
            var queryParams = new ExecuteDocumentSelectionParams {
                QuerySelection = Common.WholeDocument, OwnerUri = Constants.OwnerUri
            };
            var requestContext = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(queryParams, requestContext.Object);

            await queryService.WorkTask;
            await queryService.ActiveQueries[Constants.OwnerUri].ExecutionTask;

            // ... And it sticks around as an active query
            Assert.Equal(1, queryService.ActiveQueries.Count);

            // ... The query execution service is disposed, like when the service is shutdown
            queryService.Dispose();

            // Then:
            // ... There should no longer be an active query
            Assert.Empty(queryService.ActiveQueries);
        }
        public async Task CancelExecutedQueryTest()
        {
            // If:
            // ... I request a query (doesn't matter what kind) and wait for execution
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams {
                QuerySelection = Common.WholeDocument, OwnerUri = Constants.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);

            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            // ... And then I request to cancel the query
            var cancelParams = new QueryCancelParams {
                OwnerUri = Constants.OwnerUri
            };
            var cancelRequest = new EventFlowValidator <QueryCancelResult>()
                                .AddResultValidation(r =>
            {
                Assert.False(string.IsNullOrWhiteSpace(r.Messages));
            }).Complete();

            await queryService.HandleCancelRequest(cancelParams, cancelRequest.Object);

            // Then:
            // ... The query should not have been disposed
            Assert.NotEmpty(queryService.ActiveQueries);
            cancelRequest.Validate();
        }
Example #3
0
        public async Task CancelInProgressQueryTest()
        {
            // If:
            // ... I request a query (doesn't matter what kind) and execute it
            var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams {
                QuerySelection = Common.WholeDocument, OwnerUri = Common.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);

            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            queryService.ActiveQueries[Common.OwnerUri].HasExecuted = false;    // Fake that it hasn't completed execution

            // ... And then I request to cancel the query
            var cancelParams = new QueryCancelParams {
                OwnerUri = Common.OwnerUri
            };
            var cancelRequest = new EventFlowValidator <QueryCancelResult>()
                                .AddResultValidation(r =>
            {
                Assert.Null(r.Messages);
            }).Complete();
            await queryService.HandleCancelRequest(cancelParams, cancelRequest.Object);

            // Then:
            // ... The query should not have been disposed
            Assert.Equal(1, queryService.ActiveQueries.Count);
            cancelRequest.Validate();
        }
        public async Task QueryExecuteInProgressTest()
        {
            // If:
            // ... I request to execute a query
            var workspaceService = GetDefaultWorkspaceService(Common.StandardQuery);
            var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
            var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Common.OwnerUri, QuerySelection = Common.WholeDocument};

            // Note, we don't care about the results of the first request
            var firstRequestContext = RequestContextMocks.Create<ExecuteRequestResult>(null);
            await Common.AwaitExecution(queryService, queryParams, firstRequestContext.Object);

            // ... And then I request another query without waiting for the first to complete
            queryService.ActiveQueries[Common.OwnerUri].HasExecuted = false; // Simulate query hasn't finished
            var efv = new EventFlowValidator<ExecuteRequestResult>()
                .AddErrorValidation<string>(Assert.NotEmpty)
                .Complete();
            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... All events should have been called as per their flow validator
            efv.Validate();

            // ... There should only be one active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
Example #5
0
        public async Task SubsetServiceOutOfRangeSubsetTest()
        {
            // If:
            // ... I have a query that doesn't have any result sets
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Constants.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            // ... And I then ask for a set of results from it
            var subsetParams = new SubsetParams {
                OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0
            };
            var subsetRequest = new EventFlowValidator <SubsetResult>()
                                .AddStandardErrorValidation()
                                .Complete();
            await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);

            subsetRequest.Validate();
        }
        private async Task QueryExecuteAllBatchesNoOp()
        {
            // If:
            // ... I request to execute a valid query with all batches as no op
            var workspaceService = GetDefaultWorkspaceService(string.Format("{0}\r\nGO\r\n{0}", Common.NoOpQuery));
            var queryService     = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);
            var queryParams      = new ExecuteDocumentSelectionParams {
                QuerySelection = Common.WholeDocument, OwnerUri = Constants.OwnerUri
            };

            var efv = new EventFlowValidator <ExecuteRequestResult>()
                      .AddStandardQueryResultValidator()
                      .AddStandardBatchStartValidator()
                      .AddStandardMessageValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardMessageValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddEventValidation(QueryCompleteEvent.Type, p =>
            {
                // Validate OwnerURI matches
                Assert.Equal(Constants.OwnerUri, p.OwnerUri);
                Assert.NotNull(p.BatchSummaries);
                Assert.Equal(2, p.BatchSummaries.Length);
                Assert.All(p.BatchSummaries, bs => Assert.Equal(0, bs.ResultSetSummaries.Length));
            }).Complete();
            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... All events should have been called as per their flow validator
            efv.Validate();

            // ... There should be one active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
        public async Task QueryExecuteCompletedTest()
        {
            // If:
            // ... I request to execute a query
            var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);
            var queryParams      = new ExecuteDocumentSelectionParams {
                OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument
            };

            // Note, we don't care about the results of the first request
            var firstRequestContext = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await Common.AwaitExecution(queryService, queryParams, firstRequestContext.Object);

            // ... And then I request another query after waiting for the first to complete
            var efv = new EventFlowValidator <ExecuteRequestResult>()
                      .AddStandardQueryResultValidator()
                      .AddStandardBatchStartValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardQueryCompleteValidator(1)
                      .Complete();

            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... All events should have been called as per their flow validator
            efv.Validate();

            // ... There should only be one active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
        public async Task QueryExecuteSingleBatchNoResultsTest()
        {
            // If:
            // ... I request to execute a valid query with no results
            var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);
            var queryParams      = new ExecuteDocumentSelectionParams {
                QuerySelection = Common.WholeDocument, OwnerUri = Constants.OwnerUri
            };

            var efv = new EventFlowValidator <ExecuteRequestResult>()
                      .AddStandardQueryResultValidator()
                      .AddStandardBatchStartValidator()
                      .AddStandardMessageValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardQueryCompleteValidator(1)
                      .Complete();

            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... All events should have been called as per their flow validator
            efv.Validate();

            // ... There should be one active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
        public async Task QueryExecuteMultipleBatchSingleResultTest()
        {
            // If:
            // ... I request a to execute a valid query with multiple batches
            var workspaceService = GetDefaultWorkspaceService(string.Format("{0}\r\nGO\r\n{0}", Constants.StandardQuery));
            var queryService     = Common.GetPrimedExecutionService(Common.StandardTestDataSet, true, false, false, workspaceService);
            var queryParams      = new ExecuteDocumentSelectionParams {
                OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument
            };

            var efv = new EventFlowValidator <ExecuteRequestResult>()
                      .AddStandardQueryResultValidator()
                      .AddStandardBatchStartValidator()
                      .AddStandardResultSetValidator()
                      .AddStandardMessageValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardResultSetValidator()
                      .AddStandardMessageValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardQueryCompleteValidator(2)
                      .Complete();
            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... All events should have been called as per their flow validator
            efv.Validate();

            // ... There should be one active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
Example #10
0
        public async Task SubsetServiceValidTest()
        {
            // If:
            // ... I have a query that has results (doesn't matter what)
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(Common.ExecutionPlanTestDataSet, true, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Constants.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            // ... And I then ask for a valid set of results from it
            var subsetParams = new SubsetParams {
                OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0
            };
            var subsetRequest = new EventFlowValidator <SubsetResult>()
                                .AddResultValidation(r =>
            {
                // Then: Subset should not be null
                Assert.NotNull(r.ResultSubset);
            }).Complete();
            await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);

            subsetRequest.Validate();
        }
Example #11
0
        public async Task SubsetServiceUnexecutedQueryTest()
        {
            // If:
            // ... I have a query that hasn't finished executing (doesn't matter what)
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(Common.StandardTestDataSet, true, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Constants.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

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

            // ... And I then ask for a valid set of results from it
            var subsetParams = new SubsetParams {
                OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0
            };
            var subsetRequest = new EventFlowValidator <SubsetResult>()
                                .AddStandardErrorValidation()
                                .Complete();
            await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);

            subsetRequest.Validate();
        }
        public async Task ExecutionPlanServiceOutOfRangeSubsetTest()
        {
            // If:
            // ... I have a query that doesn't have any result sets
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams
            {
                QuerySelection       = null,
                OwnerUri             = Constants.OwnerUri,
                ExecutionPlanOptions = new ExecutionPlanOptions
                {
                    IncludeActualExecutionPlanXml    = false,
                    IncludeEstimatedExecutionPlanXml = true
                }
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            // ... And I then ask for an execution plan from a result set
            var executionPlanParams = new QueryExecutionPlanParams {
                OwnerUri = Constants.OwnerUri, ResultSetIndex = 0, BatchIndex = 0
            };
            var executionPlanRequest = new EventFlowValidator <QueryExecutionPlanResult>()
                                       .AddStandardErrorValidation()
                                       .Complete();
            await queryService.HandleExecutionPlanRequest(executionPlanParams, executionPlanRequest.Object);

            executionPlanRequest.Validate();
        }
Example #13
0
        // Internal for testing purposes
        internal string GetSqlText(ExecuteRequestParamsBase request)
        {
            // If it is a document selection, we'll retrieve the text from the document
            ExecuteDocumentSelectionParams docRequest = request as ExecuteDocumentSelectionParams;

            if (docRequest != null)
            {
                return(GetSqlTextFromSelectionData(docRequest.OwnerUri, docRequest.QuerySelection));
            }

            // If it is a document statement, we'll retrieve the text from the document
            ExecuteDocumentStatementParams stmtRequest = request as ExecuteDocumentStatementParams;

            if (stmtRequest != null)
            {
                return(GetSqlStatementAtPosition(stmtRequest.OwnerUri, stmtRequest.Line, stmtRequest.Column));
            }

            // If it is an ExecuteStringParams, return the text as is
            ExecuteStringParams stringRequest = request as ExecuteStringParams;

            if (stringRequest != null)
            {
                return(stringRequest.Query);
            }

            // Note, this shouldn't be possible due to inheritance rules
            throw new InvalidCastException("Invalid request type");
        }
        public async Task ExecutionPlanServiceUnexecutedQueryTest()
        {
            // If:
            // ... I have a query that hasn't finished executing (doesn't matter what)
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(Common.ExecutionPlanTestDataSet, true, false, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams
            {
                QuerySelection       = null,
                OwnerUri             = Constants.OwnerUri,
                ExecutionPlanOptions = new ExecutionPlanOptions
                {
                    IncludeActualExecutionPlanXml    = false,
                    IncludeEstimatedExecutionPlanXml = true
                }
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

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

            // ... And I then ask for a valid execution plan from it
            var executionPlanParams = new QueryExecutionPlanParams {
                OwnerUri = Constants.OwnerUri, ResultSetIndex = 0, BatchIndex = 0
            };
            var executionPlanRequest = new EventFlowValidator <QueryExecutionPlanResult>()
                                       .AddStandardErrorValidation()
                                       .Complete();
            await queryService.HandleExecutionPlanRequest(executionPlanParams, executionPlanRequest.Object);

            executionPlanRequest.Validate();
        }
        public async Task ExecutionPlanServiceValidTest()
        {
            // If:
            // ... I have a query that has results in the form of an execution plan
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(Common.ExecutionPlanTestDataSet, true, false, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams
            {
                QuerySelection       = null,
                OwnerUri             = Constants.OwnerUri,
                ExecutionPlanOptions = new ExecutionPlanOptions
                {
                    IncludeActualExecutionPlanXml    = false,
                    IncludeEstimatedExecutionPlanXml = true
                }
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            // ... And I then ask for a valid execution plan
            var executionPlanParams = new QueryExecutionPlanParams {
                OwnerUri = Constants.OwnerUri, BatchIndex = 0, ResultSetIndex = 0
            };
            var executionPlanRequest = new EventFlowValidator <QueryExecutionPlanResult>()
                                       .AddResultValidation(r =>
            {
                // Then: Messages should be null and execution plan should not be null
                Assert.NotNull(r.ExecutionPlan);
            }).Complete();
            await queryService.HandleExecutionPlanRequest(executionPlanParams, executionPlanRequest.Object);

            executionPlanRequest.Validate();
        }
        public async Task QueryExecuteSingleBatchMultipleResultTest()
        {
            // If:
            // ... I request to execute a valid query with one batch and multiple result sets
            var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
            var dataset          = new[] { Common.StandardTestResultSet, Common.StandardTestResultSet };
            var queryService     = Common.GetPrimedExecutionService(dataset, true, false, false, workspaceService);
            var queryParams      = new ExecuteDocumentSelectionParams {
                OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument
            };

            List <ResultSetEventParams> collectedResultSetEventParams = new List <ResultSetEventParams>();
            var efv = new EventFlowValidator <ExecuteRequestResult>()
                      .AddStandardQueryResultValidator()
                      .AddStandardBatchStartValidator()
                      .AddResultSetValidator(ResultSetAvailableEvent.Type, collectedResultSetEventParams)
                      .AddResultSetValidator(ResultSetUpdatedEvent.Type, collectedResultSetEventParams)
                      .AddResultSetValidator(ResultSetCompleteEvent.Type, collectedResultSetEventParams)
                      .AddStandardMessageValidator()
                      .AddStandardQueryCompleteValidator(1)
                      .Complete();
            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... All events should have been called as per their flow validator
            efv.Validate();

            // ... There should be one active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
        public async Task QueryExecuteInvalidQueryTest()
        {
            // If:
            // ... I request to execute a query that is invalid
            var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, true, false, workspaceService);
            var queryParams      = new ExecuteDocumentSelectionParams {
                OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument
            };

            var efv = new EventFlowValidator <ExecuteRequestResult>()
                      .AddStandardQueryResultValidator()
                      .AddStandardBatchStartValidator()
                      .AddStandardBatchCompleteValidator()
                      .AddStandardQueryCompleteValidator(1)
                      .Complete();
            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... Am error should have been sent
            efv.Validate();

            // ... There should not be an active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
        public async Task DisposeExecutedQuery()
        {
            // If:
            // ... I request a query (doesn't matter what kind)
            var workspaceService = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            var queryService     = Common.GetPrimedExecutionService(null, true, false, false, workspaceService);
            var executeParams    = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Constants.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);

            await queryService.WorkTask;
            await queryService.ActiveQueries[Constants.OwnerUri].ExecutionTask;

            // ... And then I dispose of the query
            var disposeParams = new QueryDisposeParams {
                OwnerUri = Constants.OwnerUri
            };
            var disposeRequest = new EventFlowValidator <QueryDisposeResult>()
                                 .AddStandardQueryDisposeValidator()
                                 .Complete();
            await queryService.HandleDisposeRequest(disposeParams, disposeRequest.Object);

            // Then:
            // ... And the active queries should be empty
            disposeRequest.Validate();
            Assert.Empty(queryService.ActiveQueries);
        }
Example #19
0
        public static async Task AwaitExecution(QueryExecutionService service, ExecuteDocumentSelectionParams qeParams,
                                                HostingProtocol.RequestContext <ExecuteRequestResult> requestContext)
        {
            await service.HandleExecuteRequest(qeParams, requestContext);

            if (service.ActiveQueries.ContainsKey(qeParams.OwnerUri) && service.ActiveQueries[qeParams.OwnerUri].ExecutionTask != null)
            {
                await service.ActiveQueries[qeParams.OwnerUri].ExecutionTask;
            }
        }
        /// <summary>
        /// Run a query using a given connection bound to a URI. This method only waits for the initial response from query
        /// execution (QueryExecuteResult). It is up to the caller to wait for the QueryCompleteEvent if they are interested.
        /// </summary>
        public async Task <ExecuteRequestResult> RunQueryAsync(string ownerUri, string query, int timeoutMilliseconds = 5000)
        {
            WriteToFile(ownerUri, query);

            var queryParams = new ExecuteDocumentSelectionParams
            {
                OwnerUri       = ownerUri,
                QuerySelection = null
            };

            return(await Driver.SendRequest(ExecuteDocumentSelectionRequest.Type, queryParams));
        }
Example #21
0
        public async Task SaveResultAsCsvFailure()
        {
            // Given:
            // ... A working query and workspace service
            WorkspaceService <SqlToolsSettings> ws = Common.GetPrimedWorkspaceService(Common.StandardQuery);
            Dictionary <string, byte[]>         storage;
            QueryExecutionService qes = Common.GetPrimedExecutionService(Common.ExecutionPlanTestDataSet, true, false, ws, out storage);

            // ... The query execution service has executed a query with results
            var executeParams = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Common.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await qes.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            // If: I attempt to save a result set and get it to throw because of invalid column selection
            SaveResultsAsCsvRequestParams saveParams = new SaveResultsAsCsvRequestParams
            {
                BatchIndex       = 0,
                FilePath         = "qqq",
                OwnerUri         = Common.OwnerUri,
                ResultSetIndex   = 0,
                ColumnStartIndex = -1,
                ColumnEndIndex   = 100,
                RowStartIndex    = 0,
                RowEndIndex      = 5
            };

            qes.CsvFileFactory = GetCsvStreamFactory(storage, saveParams);
            object error          = null;
            var    requestContext = RequestContextMocks.Create <SaveResultRequestResult>(null)
                                    .AddErrorHandling(e => error = e);
            await qes.HandleSaveResultsAsCsvRequest(saveParams, requestContext.Object);

            await qes.ActiveQueries[saveParams.OwnerUri]
            .Batches[saveParams.BatchIndex]
            .ResultSets[saveParams.ResultSetIndex]
            .SaveTasks[saveParams.FilePath];

            // Then:
            // ... An error event should have been fired
            // ... No success event should have been fired
            VerifyResponseCalls(requestContext, false, true);
            Assert.IsType <SaveResultRequestError>(error);
            Assert.NotNull(error);
            Assert.NotNull(((SaveResultRequestError)error).message);
        }
        public void GetSqlTextFromDocumentRequestPartial()
        {
            // Setup:
            // ... Create a workspace service with a multi-line constructed query
            string query = string.Format("{0}{1}GO{1}{0}", Common.StandardQuery, Environment.NewLine);
            var workspaceService = GetDefaultWorkspaceService(query);
            var queryService = new QueryExecutionService(null, workspaceService);

            // If: I attempt to get query text from execute document params (partial document)
            var queryParams = new ExecuteDocumentSelectionParams {OwnerUri = Common.OwnerUri, QuerySelection = Common.SubsectionDocument};
            var queryText = queryService.GetSqlText(queryParams);

            // Then: The text should be a subset of the constructed query
            Assert.Contains(queryText, query);
        }
        public async Task SaveResultAsExcelFailure()
        {
            // Given:
            // ... A working query and workspace service
            WorkspaceService <SqlToolsSettings>   ws = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            ConcurrentDictionary <string, byte[]> storage;
            QueryExecutionService qes = Common.GetPrimedExecutionService(Common.StandardTestDataSet, true, false, false, ws, out storage);

            // ... The query execution service has executed a query with results
            var executeParams = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Constants.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await qes.HandleExecuteRequest(executeParams, executeRequest.Object);

            await qes.WorkTask;
            await qes.ActiveQueries[Constants.OwnerUri].ExecutionTask;

            // If: I attempt to save a result set and get it to throw because of invalid column selection
            SaveResultsAsExcelRequestParams saveParams = new SaveResultsAsExcelRequestParams
            {
                BatchIndex       = 0,
                FilePath         = "qqq",
                OwnerUri         = Constants.OwnerUri,
                ResultSetIndex   = 0,
                ColumnStartIndex = -1,
                ColumnEndIndex   = 100,
                RowStartIndex    = 0,
                RowEndIndex      = 5
            };

            qes.JsonFileFactory = GetExcelStreamFactory(storage, saveParams);
            var efv = new EventFlowValidator <SaveResultRequestResult>()
                      .AddStandardErrorValidation()
                      .Complete();
            await qes.HandleSaveResultsAsExcelRequest(saveParams, efv.Object);

            await qes.ActiveQueries[saveParams.OwnerUri]
            .Batches[saveParams.BatchIndex]
            .ResultSets[saveParams.ResultSetIndex]
            .SaveTasks[saveParams.FilePath];

            // Then:
            // ... An error event should have been fired
            // ... No success event should have been fired
            efv.Validate();
        }
        public void GetSqlTextFromDocumentRequestFull()
        {
            // Setup:
            // ... Create a workspace service with a multi-line constructed query
            // ... Create a query execution service without a connection service (we won't be
            //     executing queries), and the previously created workspace service
            string query = string.Format("{0}{1}GO{1}{0}", Common.StandardQuery, Environment.NewLine);
            var workspaceService = GetDefaultWorkspaceService(query);
            var queryService = new QueryExecutionService(null, workspaceService);

            // If: I attempt to get query text from execute document params (entire document)
            var queryParams = new ExecuteDocumentSelectionParams {OwnerUri = Common.OwnerUri, QuerySelection = Common.WholeDocument};
            var queryText = queryService.GetSqlText(queryParams);

            // Then: The text should match the constructed query
            Assert.Equal(query, queryText);
        }
        public async Task SaveResultsAsXmlSuccess()
        {
            // Given:
            // ... A working query and workspace service
            WorkspaceService <SqlToolsSettings>   ws = Common.GetPrimedWorkspaceService(Constants.StandardQuery);
            ConcurrentDictionary <string, byte[]> storage;
            QueryExecutionService qes = Common.GetPrimedExecutionService(Common.StandardTestDataSet, true, false, false, ws, out storage);

            // ... The query execution service has executed a query with results
            var executeParams = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Constants.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await qes.HandleExecuteRequest(executeParams, executeRequest.Object);

            await qes.WorkTask;
            await qes.ActiveQueries[Constants.OwnerUri].ExecutionTask;

            // If: I attempt to save a result set from a query
            SaveResultsAsXmlRequestParams saveParams = new SaveResultsAsXmlRequestParams
            {
                OwnerUri       = Constants.OwnerUri,
                FilePath       = "qqq",
                BatchIndex     = 0,
                ResultSetIndex = 0,
                Formatted      = true
            };

            qes.XmlFileFactory = GetXmlStreamFactory(storage, saveParams);

            var efv = new EventFlowValidator <SaveResultRequestResult>()
                      .AddStandardResultValidator()
                      .Complete();
            await qes.HandleSaveResultsAsXmlRequest(saveParams, efv.Object);

            await qes.ActiveQueries[saveParams.OwnerUri]
            .Batches[saveParams.BatchIndex]
            .ResultSets[saveParams.ResultSetIndex]
            .SaveTasks[saveParams.FilePath];

            // Then:
            // ... I should have a successful result
            // ... There should not have been an error
            efv.Validate();
        }
        // Internal for testing purposes
        internal string GetSqlText(ExecuteRequestParamsBase request)
        {
            // If it is a document selection, we'll retrieve the text from the document
            ExecuteDocumentSelectionParams docRequest = request as ExecuteDocumentSelectionParams;

            if (docRequest != null)
            {
                // Get the document from the parameters
                ScriptFile queryFile = WorkspaceService.Workspace.GetFile(docRequest.OwnerUri);

                // If a selection was not provided, use the entire document
                if (docRequest.QuerySelection == null)
                {
                    return(queryFile.Contents);
                }

                // A selection was provided, so get the lines in the selected range
                string[] queryTextArray = queryFile.GetLinesInRange(
                    new BufferRange(
                        new BufferPosition(
                            docRequest.QuerySelection.StartLine + 1,
                            docRequest.QuerySelection.StartColumn + 1
                            ),
                        new BufferPosition(
                            docRequest.QuerySelection.EndLine + 1,
                            docRequest.QuerySelection.EndColumn + 1
                            )
                        )
                    );
                return(string.Join(Environment.NewLine, queryTextArray));
            }

            // If it is an ExecuteStringParams, return the text as is
            ExecuteStringParams stringRequest = request as ExecuteStringParams;

            if (stringRequest != null)
            {
                return(stringRequest.Query);
            }

            // Note, this shouldn't be possible due to inheritance rules
            throw new InvalidCastException("Invalid request type");
        }
Example #27
0
        public async Task SaveResultsAsJsonSuccess()
        {
            // Given:
            // ... A working query and workspace service
            WorkspaceService <SqlToolsSettings> ws = Common.GetPrimedWorkspaceService(Common.StandardQuery);
            Dictionary <string, byte[]>         storage;
            QueryExecutionService qes = Common.GetPrimedExecutionService(Common.StandardTestDataSet, true, false, ws, out storage);

            // ... The query execution service has executed a query with results
            var executeParams = new ExecuteDocumentSelectionParams {
                QuerySelection = null, OwnerUri = Common.OwnerUri
            };
            var executeRequest = RequestContextMocks.Create <ExecuteRequestResult>(null);
            await qes.HandleExecuteRequest(executeParams, executeRequest.Object);

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

            // If: I attempt to save a result set from a query
            SaveResultsAsJsonRequestParams saveParams = new SaveResultsAsJsonRequestParams
            {
                OwnerUri       = Common.OwnerUri,
                FilePath       = "qqq",
                BatchIndex     = 0,
                ResultSetIndex = 0
            };

            qes.JsonFileFactory = GetJsonStreamFactory(storage, saveParams);
            SaveResultRequestResult result = null;
            var requestContext             = RequestContextMocks.Create <SaveResultRequestResult>(r => result = r);
            await qes.HandleSaveResultsAsJsonRequest(saveParams, requestContext.Object);

            await qes.ActiveQueries[saveParams.OwnerUri]
            .Batches[saveParams.BatchIndex]
            .ResultSets[saveParams.ResultSetIndex]
            .SaveTasks[saveParams.FilePath];

            // Then:
            // ... I should have a successful result
            // ... There should not have been an error
            VerifyResponseCalls(requestContext, true, false);
            Assert.NotNull(result);
            Assert.Null(result.Messages);
        }
        /// <summary>
        /// Run a query using a given connection bound to a URI
        /// </summary>
        public async Task <BatchEventParams> RunQueryAndWaitToStart(string ownerUri, int timeoutMilliseconds = 5000)
        {
            var queryParams = new ExecuteDocumentSelectionParams
            {
                OwnerUri       = ownerUri,
                QuerySelection = null
            };

            var result = await Driver.SendRequest(ExecuteDocumentSelectionRequest.Type, queryParams);

            if (result != null)
            {
                var eventResult = await Driver.WaitForEvent(BatchStartEvent.Type, timeoutMilliseconds);

                return(eventResult);
            }
            else
            {
                return(null);
            }
        }
        public async Task QueryExecuteUnconnectedUriTest()
        {
            // Given:
            // If:
            // ... I request to execute a query using a file URI that isn't connected
            var workspaceService = GetDefaultWorkspaceService(Common.StandardQuery);
            var queryService = Common.GetPrimedExecutionService(null, false, false, workspaceService);
            var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = "notConnected", QuerySelection = Common.WholeDocument};

            var efv = new EventFlowValidator<ExecuteRequestResult>()
                .AddErrorValidation<string>(Assert.NotEmpty)
                .Complete();
            await Common.AwaitExecution(queryService, queryParams, efv.Object);

            // Then:
            // ... All events should have been called as per their flow validator
            efv.Validate();

            // ... There should be no active queries
            Assert.Empty(queryService.ActiveQueries);
        }
        public async Task QueryExecuteMissingSelectionTest()
        {
            // Given:
            // ... A workspace with a standard query is configured
            var workspaceService = Common.GetPrimedWorkspaceService(string.Empty);

            // If:
            // ... I request to execute a query with a missing query string
            var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
            var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Common.OwnerUri, QuerySelection = null};

            var efv = new EventFlowValidator<ExecuteRequestResult>()
                .AddErrorValidation<string>(Assert.NotEmpty)
                .Complete();
            await queryService.HandleExecuteRequest(queryParams, efv.Object);

            // Then:
            // ... Am error should have been sent
            efv.Validate();

            // ... There should not be an active query
            Assert.Empty(queryService.ActiveQueries);
        }