/// <summary>
 /// Gets the test run with the specified identifier.
 /// </summary>
 /// <param name="id">The identifier of the test run within the database.</param>
 /// <returns>The test run with the specified identifier, or <c>null</c> if no such test run exists.</returns>
 public TestRun GetByID(Int64 id)
 {
     using (var testRunContext = new TestRunContext())
     {
         return testRunContext.TestRuns.Where(x => x.ID == id).SingleOrDefault();
     }
 }
        /// <summary>
        /// Creates a new test run and places it into pending status.
        /// </summary>
        /// <param name="workingDirectory">The current working directory for the build agent.</param>
        /// <returns>The identifier of the test run within the database.</returns>
        public Int64 CreateTestRun(String workingDirectory)
        {
            using (var testRunContext = new TestRunContext())
            {
                var run = new TestRun() { Status = TestRunStatus.Pending, WorkingDirectory = workingDirectory };

                testRunContext.TestRuns.Add(run);
                testRunContext.SaveChanges();

                return run.ID;
            }
        }
        /// <summary>
        /// Gets a value which represents the overall status of the most recent test runs in all of the specified working directories.
        /// </summary>
        /// <param name="workingDirectories">A collection of working directories representing the test runs to evaluate.</param>
        /// <returns>A <see cref="TestRunStatus"/> value that specifies the overall status of the most recent test runs in the specified working directories.</returns>
        public TestRunStatus GetMostRecentStatusByWorkingDirectories(IEnumerable<String> workingDirectories)
        {
            if (workingDirectories == null)
                return TestRunStatus.Failed;

            using (var testRunContext = new TestRunContext())
            {
                var statuses = testRunContext.TestRuns.GroupBy(x => x.WorkingDirectory).Where(x => workingDirectories.Contains(x.Key))
                    .Select(x => x.OrderByDescending(y => y.ID).Select(y => y.Status).FirstOrDefault()).ToList();

                if (statuses.Any(x => x == TestRunStatus.Failed))
                    return TestRunStatus.Failed;

                if (statuses.Any(x => x == TestRunStatus.Running))
                    return TestRunStatus.Running;

                if (statuses.Any(x => x == TestRunStatus.Pending))
                    return TestRunStatus.Pending;

                return TestRunStatus.Succeeded;
            }
        }
        /// <summary>
        /// Gets the status of the most recent test run in the specified working directory.
        /// </summary>
        /// <param name="workingDirectory">The working directory of the test runs to evaluate.</param>
        /// <returns>A <see cref="TestRunStatus"/> value that specifies the status of the most recent test run in the specified working directory.</returns>
        public TestRunStatus GetMostRecentStatusByWorkingDirectory(String workingDirectory)
        {
            if (String.IsNullOrEmpty(workingDirectory))
                return TestRunStatus.Failed;

            using (var testRunContext = new TestRunContext())
            {
                return testRunContext.TestRuns.Where(x => x.WorkingDirectory == workingDirectory).OrderByDescending(x => x.ID).Take(1).Select(x => x.Status).SingleOrDefault();
            }
        }
        /// <summary>
        /// Updates the status of the specified test run.
        /// </summary>
        /// <param name="id">The identifier of the test run within the database.</param>
        /// <param name="status">The status to set for the specified test run.</param>
        /// <returns>A <see cref="TestRunStatus"/> value specifying the test run's previous status.</returns>
        public TestRunStatus UpdateTestRunStatus(Int64 id, TestRunStatus status)
        {
            using (var testRunContext = new TestRunContext())
            {
                var run = testRunContext.TestRuns.Where(x => x.ID == id).Single();
                var previousStatus = run.Status;
                run.Status = status;
                testRunContext.SaveChanges();

                return previousStatus;
            }
        }
 /// <summary>
 /// Gets the status of the specified test run.
 /// </summary>
 /// <param name="id">The identifier of the test run within the database.</param>
 /// <returns>A <see cref="TestRunStatus"/> value specifying the test run's current status.</returns>
 public TestRunStatus GetTestRunStatus(Int64 id)
 {
     using (var testRunContext = new TestRunContext())
     {
         var run = testRunContext.TestRuns.Where(x => x.ID == id).Single();
         return run.Status;
     }
 }