public async void QueryExecuteValidNoResultsTest()
        {
            // Given:
            // ... Default settings are stored in the workspace service
            WorkspaceService <SqlToolsSettings> .Instance.CurrentSettings = new SqlToolsSettings();

            // 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 request to execute a valid query with no results
            var queryService = await Common.GetPrimedExecutionService(Common.CreateMockFactory(null, false), true, workspaceService.Object);

            var queryParams = new QueryExecuteParams {
                QuerySelection = Common.WholeDocument, OwnerUri = Common.OwnerUri
            };

            QueryExecuteResult         result         = null;
            QueryExecuteCompleteParams completeParams = null;
            var requestContext =
                RequestContextMocks.SetupRequestContextMock <QueryExecuteResult, QueryExecuteCompleteParams>(
                    resultCallback: qer => result = qer,
                    expectedEvent: QueryExecuteCompleteEvent.Type,
                    eventCallback: (et, cp) => completeParams = cp,
                    errorCallback: null);

            await AwaitExecution(queryService, queryParams, requestContext.Object);

            // Then:
            // ... No Errors should have been sent
            // ... A successful result should have been sent with messages on the first batch
            // ... A completion event should have been fired with empty results
            VerifyQueryExecuteCallCount(requestContext, Times.Once(), Times.Once(), Times.Never());
            Assert.Null(result.Messages);
            Assert.Equal(1, completeParams.BatchSummaries.Length);
            Assert.Empty(completeParams.BatchSummaries[0].ResultSetSummaries);
            Assert.NotEmpty(completeParams.BatchSummaries[0].Messages);

            // ... There should be one active query
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
Beispiel #2
0
        public async void CancelInProgressQueryTest()
        {
            // Set up file for returning the query
            var fileMock = new Mock <ScriptFile>();

            fileMock.Setup(file => file.GetLinesInRange(It.IsAny <BufferRange>()))
            .Returns(new[] { 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 request a query (doesn't matter what kind) and execute it
            var queryService = await Common.GetPrimedExecutionService(Common.CreateMockFactory(null, false), true, workspaceService.Object);

            var executeParams = new QueryExecuteParams {
                QuerySelection = Common.GetSubSectionDocument(), OwnerUri = Common.OwnerUri
            };
            var executeRequest =
                RequestContextMocks.SetupRequestContextMock <QueryExecuteResult, QueryExecuteCompleteParams>(null, QueryExecuteCompleteEvent.Type, null, 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
            };
            QueryCancelResult result = null;
            var cancelRequest        = GetQueryCancelResultContextMock(qcr => result = qcr, null);

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

            // Then:
            // ... I should have seen a successful event (no messages)
            VerifyQueryCancelCallCount(cancelRequest, Times.Once(), Times.Never());
            Assert.Null(result.Messages);

            // ... The query should not have been disposed
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
        public async void QueryExecuteCompletedTest()
        {
            // 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 request to execute a query
            var queryService = await Common.GetPrimedExecutionService(Common.CreateMockFactory(null, false), true, workspaceService.Object);

            var queryParams = new QueryExecuteParams {
                OwnerUri = Common.OwnerUri, QuerySelection = Common.WholeDocument
            };

            // Note, we don't care about the results of the first request
            var firstRequestContext = RequestContextMocks.SetupRequestContextMock <QueryExecuteResult, QueryExecuteCompleteParams>(null, QueryExecuteCompleteEvent.Type, null, null);

            await AwaitExecution(queryService, queryParams, firstRequestContext.Object);

            // ... And then I request another query after waiting for the first to complete
            QueryExecuteResult         result   = null;
            QueryExecuteCompleteParams complete = null;
            var secondRequestContext            =
                RequestContextMocks.SetupRequestContextMock <QueryExecuteResult, QueryExecuteCompleteParams>(qer => result = qer, QueryExecuteCompleteEvent.Type, (et, qecp) => complete = qecp, null);

            await AwaitExecution(queryService, queryParams, secondRequestContext.Object);

            // Then:
            // ... No errors should have been sent
            // ... A result should have been sent with no errors
            // ... There should only be one active query
            VerifyQueryExecuteCallCount(secondRequestContext, Times.Once(), Times.Once(), Times.Never());
            Assert.Null(result.Messages);
            Assert.False(complete.BatchSummaries.Any(b => b.HasError));
            Assert.Equal(1, queryService.ActiveQueries.Count);
        }
        public async void DisposeExecutedQuery()
        {
            // Set up file for returning the query
            var fileMock = new Mock <ScriptFile>();

            fileMock.SetupGet(file => file.Contents).Returns("doesn't matter");
            // Set up workspace mock
            var workspaceService = new Mock <WorkspaceService <SqlToolsSettings> >();

            workspaceService.Setup(service => service.Workspace.GetFile(It.IsAny <string>()))
            .Returns(fileMock.Object);
            // If:
            // ... I request a query (doesn't matter what kind)
            var queryService = await Common.GetPrimedExecutionService(Common.CreateMockFactory(null, false), true, workspaceService.Object);

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

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

            // ... And then I dispose of the query
            var disposeParams = new QueryDisposeParams {
                OwnerUri = Common.OwnerUri
            };
            QueryDisposeResult result = null;
            var disposeRequest        = GetQueryDisposeResultContextMock(qdr => {
                result = qdr;
            }, null);

            queryService.HandleDisposeRequest(disposeParams, disposeRequest.Object).Wait();

            // Then:
            // ... I should have seen a successful result
            // ... And the active queries should be empty
            VerifyQueryDisposeCallCount(disposeRequest, Times.Once(), Times.Never());
            Assert.Null(result.Messages);
            Assert.Empty(queryService.ActiveQueries);
        }
Beispiel #5
0
        public async void CancelExecutedQueryTest()
        {
            // 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 request a query (doesn't matter what kind) and wait for execution
            var queryService = await Common.GetPrimedExecutionService(Common.CreateMockFactory(null, false), true, workspaceService.Object);

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

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

            // ... And then I request to cancel the query
            var cancelParams = new QueryCancelParams {
                OwnerUri = Common.OwnerUri
            };
            QueryCancelResult result = null;
            var cancelRequest        = GetQueryCancelResultContextMock(qcr => result = qcr, null);
            await queryService.HandleCancelRequest(cancelParams, cancelRequest.Object);

            // Then:
            // ... I should have seen a result event with an error message
            VerifyQueryCancelCallCount(cancelRequest, Times.Once(), Times.Never());
            Assert.NotNull(result.Messages);

            // ... The query should not have been disposed
            Assert.NotEmpty(queryService.ActiveQueries);
        }
        public async void QueryExecuteInvalidQueryTest()
        {
            // 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 request to execute a query that is invalid
            var queryService = await Common.GetPrimedExecutionService(Common.CreateMockFactory(null, true), true, workspaceService.Object);

            var queryParams = new QueryExecuteParams {
                OwnerUri = Common.OwnerUri, QuerySelection = Common.WholeDocument
            };

            QueryExecuteResult         result   = null;
            QueryExecuteCompleteParams complete = null;
            var requestContext =
                RequestContextMocks.SetupRequestContextMock <QueryExecuteResult, QueryExecuteCompleteParams>(qer => result = qer, QueryExecuteCompleteEvent.Type, (et, qecp) => complete = qecp, null);

            await AwaitExecution(queryService, queryParams, requestContext.Object);

            // Then:
            // ... No errors should have been sent
            // ... A result should have been sent with success (we successfully started the query)
            // ... A completion event should have been sent with error
            VerifyQueryExecuteCallCount(requestContext, Times.Once(), Times.Once(), Times.Never());
            Assert.Null(result.Messages);
            Assert.Equal(1, complete.BatchSummaries.Length);
            Assert.True(complete.BatchSummaries[0].HasError);
            Assert.NotEmpty(complete.BatchSummaries[0].Messages);
        }