示例#1
0
        public async Task AddHistoryItem(long testId, TestCaseHistory item)
        {
            var test = await GetTest(testId);

            test.HistoryItems.Add(item);
            await _dbContext.SaveChangesAsync();
        }
示例#2
0
        public async Task <IActionResult> UpdateTestCaseExecutionHistoryAsync([FromBody] List <TestCaseRun> testCaseRuns)
        {
            _backgroundTaskQueue.QueueBackgroundWorkItem(async token =>
            {
                using (var scope = _backgroundTaskQueue.CreateScope())
                {
                    var meissaRepository = scope.ServiceProvider.GetRequiredService <MeissaRepository>();
                    var logger           = scope.ServiceProvider.GetRequiredService <ILogger <TestCaseRunsController> >();

                    try
                    {
                        var existingTestCasesHistory = meissaRepository.GetAllQuery <TestCaseHistory>().Where(x => testCaseRuns.Any(y => y.FullName.Equals(x.FullName))).ToList();
                        var testCaseHistoryEntries   = meissaRepository.GetAllQuery <TestCaseHistoryEntry>();
                        foreach (var testCaseRun in testCaseRuns)
                        {
                            if (existingTestCasesHistory.Any(x => x.FullName.Equals(testCaseRun.FullName)))
                            {
                                var existingTestCaseHistory = existingTestCasesHistory.FirstOrDefault(x => x.FullName.Equals(testCaseRun.FullName));

                                // Creates the new test case history entry for the current run.
                                if (existingTestCaseHistory != null)
                                {
                                    var testCaseHistoryEntry = new TestCaseHistoryEntry
                                    {
                                        AvgDuration       = testCaseRun.Duration,
                                        TestCaseHistoryId = existingTestCaseHistory.TestCaseHistoryId,
                                    };
                                    meissaRepository.Insert(testCaseHistoryEntry);

                                    // Get all previous runs for the test and add to the list the new entry.
                                    var allCurrentTestCaseHistoryEntries = testCaseHistoryEntries.Where(x => x.TestCaseHistoryId.Equals(existingTestCaseHistory.TestCaseHistoryId)).ToList();
                                    allCurrentTestCaseHistoryEntries.Add(testCaseHistoryEntry);
                                }

                                // Calculate the new average duration for the current tests based on the new entry.
                                double newAverageDurationTicks = testCaseHistoryEntries.Average(x => x.AvgDuration.Ticks);
                                var newAverageDuration         = new TimeSpan(Convert.ToInt64(newAverageDurationTicks));

                                // Update the test case history info.
                                if (existingTestCaseHistory != null)
                                {
                                    existingTestCaseHistory.AvgDuration     = newAverageDuration;
                                    existingTestCaseHistory.LastUpdatedTime = DateTime.Now;

                                    meissaRepository.Update(existingTestCaseHistory);
                                }
                            }
                            else
                            {
                                // If no entries exist, we create the history test case and a new history entry.
                                var testCaseHistoryDto = new TestCaseHistory()
                                {
                                    FullName        = testCaseRun.FullName,
                                    LastUpdatedTime = DateTime.Now,
                                    AvgDuration     = testCaseRun.Duration,
                                };
                                testCaseHistoryDto = await meissaRepository.InsertWithSaveAsync(testCaseHistoryDto);

                                var testCaseHistoryEntry = new TestCaseHistoryEntry
                                {
                                    AvgDuration       = testCaseRun.Duration,
                                    TestCaseHistoryId = testCaseHistoryDto.TestCaseHistoryId,
                                };
                                meissaRepository.Insert(testCaseHistoryEntry);
                            }
                        }

                        await meissaRepository.SaveAsync();
                    }
                    catch (Exception ex)
                    {
                        logger.LogCritical("Exception while updating test cases execution history.", ex);
                    }
                }
            });

            return(Ok());
        }
        public async Task <IActionResult> UpdateTestCaseExecutionHistoryAsync([FromBody] List <TestCaseRun> testCaseRuns)
        {
            try
            {
                Debug.WriteLine("##### Start UpdateTestCaseExecutionHistory");
                DateTime startTime = DateTime.Now;
                var      existingTestCasesHistory = _meissaRepository.GetAllQuery <TestCaseHistory>().Where(x => testCaseRuns.Any(y => y.FullName.Equals(x.FullName))).ToList();
                var      testCaseHistoryEntries   = _meissaRepository.GetAllQuery <TestCaseHistoryEntry>();
                foreach (var testCaseRun in testCaseRuns)
                {
                    if (existingTestCasesHistory.Any(x => x.FullName.Equals(testCaseRun.FullName)))
                    {
                        var existingTestCaseHistory = existingTestCasesHistory.FirstOrDefault(x => x.FullName.Equals(testCaseRun.FullName));

                        // Creates the new test case history entry for the current run.
                        if (existingTestCaseHistory != null)
                        {
                            var testCaseHistoryEntry = new TestCaseHistoryEntry
                            {
                                AvgDuration       = testCaseRun.Duration,
                                TestCaseHistoryId = existingTestCaseHistory.TestCaseHistoryId,
                            };
                            _meissaRepository.Insert(testCaseHistoryEntry);

                            // Get all previous runs for the test and add to the list the new entry.
                            var allCurrentTestCaseHistoryEntries = testCaseHistoryEntries.Where(x => x.TestCaseHistoryId.Equals(existingTestCaseHistory.TestCaseHistoryId)).ToList();
                            allCurrentTestCaseHistoryEntries.Add(testCaseHistoryEntry);
                        }

                        // Calculate the new average duration for the current tests based on the new entry.
                        double newAverageDurationTicks = testCaseHistoryEntries.Average(x => x.AvgDuration.Ticks);
                        var    newAverageDuration      = new TimeSpan(Convert.ToInt64(newAverageDurationTicks));

                        // Update the test case history info.
                        if (existingTestCaseHistory != null)
                        {
                            existingTestCaseHistory.AvgDuration     = newAverageDuration;
                            existingTestCaseHistory.LastUpdatedTime = DateTime.Now;

                            _meissaRepository.Update(existingTestCaseHistory);
                        }
                    }
                    else
                    {
                        // If no entries exist, we create the history test case and a new history entry.
                        var testCaseHistoryDto = new TestCaseHistory()
                        {
                            FullName        = testCaseRun.FullName,
                            LastUpdatedTime = DateTime.Now,
                            AvgDuration     = testCaseRun.Duration,
                        };
                        testCaseHistoryDto = await _meissaRepository.InsertWithSaveAsync(testCaseHistoryDto);

                        var testCaseHistoryEntry = new TestCaseHistoryEntry
                        {
                            AvgDuration       = testCaseRun.Duration,
                            TestCaseHistoryId = testCaseHistoryDto.TestCaseHistoryId,
                        };
                        _meissaRepository.Insert(testCaseHistoryEntry);
                    }
                }

                await _meissaRepository.SaveAsync();

                DateTime endTime = DateTime.Now;
                Debug.WriteLine($"##### End UpdateTestCaseExecutionHistory for {endTime - startTime}");

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Exception while updating test cases execution history.", ex);
                return(StatusCode(500, "A problem happened while handling your request."));
            }
        }
示例#4
0
        public async Task <IActionResult> SwitchState(int id, int statusId, string returnurl)
        {
            var test = await _testLabProvider.GetTest(id);

            var who = await _usersProvider.GetUser(User.GetUserId());

            var statusType  = (Status)statusId;
            var historyItem = new TestCaseHistory
            {
                Author  = who,
                Message = $@"changed status from <strong>{test.Status.GetDescription()}</strong> to <strong>{statusType.GetDescription()}</strong>"
            };

            switch (statusType)
            {
            case Status.New:
                break;

            case Status.ReadyForAutomation:
                break;

            case Status.InProgress:
                test.Assignee  = who;
                test.StartedOn = DateTime.Now;
                if (!string.IsNullOrEmpty(who.GitHubToken))
                {
                    var gitClient = new GithubApiClient(who.GitHubAccount, who.GitHubToken);
                    gitClient.AddAssignees(test.GitHubId, new [] { who.GitHubAccount });
                }
                break;

            case Status.ToCrossPlatform:
                break;

            case Status.ReadyForReview:
                return(RedirectToAction("SubmitForReview", "TestLab", new { @id = id }));

            case Status.ReviewStarted:
                test.Reviewer = who;
                if (!string.IsNullOrEmpty(who.GitHubToken))
                {
                    var gitClient = new GithubApiClient(who.GitHubAccount, who.GitHubToken);
                    gitClient.AddReviewer(test.PullRequestId, new [] { who.GitHubAccount });
                }
                break;

            case Status.CannotAutomate:
                break;

            case Status.Finished:
                test.FinishedOn = DateTime.Now;
                break;

            case Status.ChangesRequested:
                return(RedirectToAction("RequestChange", "TestLab", new { @id = id }));

            case Status.Fixed:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            test.Status = statusType;
            test.HistoryItems.Add(historyItem);
            await _testLabProvider.UpdateTest(test);

            if (string.IsNullOrEmpty(returnurl))
            {
                return(Redirect(Url.RouteUrl(new { controller = "TestLab", action = "TestSuite" }) + "/" +
                                test.TestSuite.Id + "#test-" + test.Id));
            }
            else
            {
                return(LocalRedirect(returnurl));
            }
        }
        public async Task <IActionResult> UpdateTestCaseExecutionHistoryAsync([FromBody] List <TestCaseRun> testCaseRuns)
        {
            try
            {
                var existingTestCasesHistory = _meissaRepository.GetAllQuery <TestCaseHistory>().AsEnumerable().Where(x => testCaseRuns.Any(y => y.FullName.Equals(x.FullName))).ToList();
                var testCaseHistoryEntries   = _meissaRepository.GetAllQuery <TestCaseHistoryEntry>();
                foreach (var testCaseRun in testCaseRuns)
                {
                    if (existingTestCasesHistory.Any(x => x.FullName.Equals(testCaseRun.FullName)))
                    {
                        var existingTestCaseHistory = existingTestCasesHistory.FirstOrDefault(x => x.FullName.Equals(testCaseRun.FullName));

                        // Creates the new test case history entry for the current run.
                        if (existingTestCaseHistory != null)
                        {
                            var testCaseHistoryEntry = new TestCaseHistoryEntry
                            {
                                AvgDuration       = testCaseRun.Duration,
                                TestCaseHistoryId = existingTestCaseHistory.TestCaseHistoryId,
                            };
                            _meissaRepository.Insert(testCaseHistoryEntry);

                            // Get all previous runs for the test and add to the list the new entry.
                            var allCurrentTestCaseHistoryEntries = testCaseHistoryEntries.Where(x => x.TestCaseHistoryId.Equals(existingTestCaseHistory.TestCaseHistoryId)).ToList();
                            allCurrentTestCaseHistoryEntries.Add(testCaseHistoryEntry);
                        }

                        // Calculate the new average duration for the current tests based on the new entry.
                        double newAverageDurationTicks = testCaseHistoryEntries.Average(x => x.AvgDuration.Ticks);
                        var    newAverageDuration      = new TimeSpan(Convert.ToInt64(newAverageDurationTicks));

                        // Update the test case history info.
                        if (existingTestCaseHistory != null)
                        {
                            existingTestCaseHistory.AvgDuration     = newAverageDuration;
                            existingTestCaseHistory.LastUpdatedTime = DateTime.Now;

                            _meissaRepository.Update(existingTestCaseHistory);
                        }
                    }
                    else
                    {
                        // If no entries exist, we create the history test case and a new history entry.
                        var testCaseHistoryDto = new TestCaseHistory()
                        {
                            FullName        = testCaseRun.FullName,
                            LastUpdatedTime = DateTime.Now,
                            AvgDuration     = testCaseRun.Duration,
                        };
                        testCaseHistoryDto = await _meissaRepository.InsertWithSaveAsync(testCaseHistoryDto).ConfigureAwait(false);

                        var testCaseHistoryEntry = new TestCaseHistoryEntry
                        {
                            AvgDuration       = testCaseRun.Duration,
                            TestCaseHistoryId = testCaseHistoryDto.TestCaseHistoryId,
                        };
                        _meissaRepository.Insert(testCaseHistoryEntry);
                    }
                }

                await _meissaRepository.SaveAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"Exception while updating test cases execution history. {ex.Message} {ex.InnerException?.StackTrace} {ex.InnerException?.Message}", ex);
            }

            return(Ok());
        }