public void No_Match_Returns_Empty_Collection() { using (var database = new DbCodeReview()) { database.Commits.AddRange(new Commit[] { new Commit() { Author = "daffy", Message = "A message", Timestamp = new DateTime(2000, 1, 1), }, new Commit() { Author = "bugs", Message = "Another message", Timestamp = new DateTime(2002, 1, 2) }, }); database.SaveChanges(); } using (var database = new DbCodeReview()) { var sut = new CommitQuery(database.Commits) { IncludeAuthor = "donald" }; Assert.AreEqual(0, sut.Execute().Count <Commit>()); var resultList = sut.Execute().ToList(); for (int i = 1; i < resultList.Count; i++) { Assert.IsTrue(resultList[i - 1].Timestamp < resultList[i].Timestamp); } } }
public IHttpActionResult GetCommits() { CommitsModel vm; UnitOfWork unitOfWork = null; System.Diagnostics.Trace.TraceInformation("Get Commits"); try { unitOfWork = new UnitOfWork(); var parameters = this.Request.GetQueryNameValuePairs(); CommitQuery query = new CommitQuery(unitOfWork.Context.Commits); var keyword = parameters.FirstOrDefault(p => p.Key == "keyword").Value; if (string.IsNullOrEmpty(keyword) == false) { query.Keyword = parameters.FirstOrDefault(p => p.Key == "keyword").Value; } query.ExcludeApproved = String.Equals("true", parameters.FirstOrDefault(p => p.Key == "excludeApproved").Value, StringComparison.InvariantCulture); query.Max = Parser.ParseNullable <int>(parameters.FirstOrDefault(p => p.Key == "max").Value); query.IncludeAuthor = parameters.FirstOrDefault(p => p.Key == "include").Value; query.ExcludeAuthor = parameters.FirstOrDefault(p => p.Key == "exclude").Value; query.Project = Parser.ParseNullable <int>(parameters.FirstOrDefault(p => p.Key == "project").Value); var list = new List <Tuple <Commit, CommitStats> >(); foreach (var commit in query.Execute()) { var comments = unitOfWork.Context.Comments.Where(c => c.Revision == commit.Revision).ToList(); var replies = comments.Where(c => c.User == commit.Author && c.IsLike == false); var reviews = comments.Where(c => c.User != commit.Author && c.IsLike == false); var likes = comments.Where(c => c.IsLike == true); var stats = new CommitStats( reviews.Select(c => c.User).Distinct().Count(), reviews.Count(), replies.Count(), likes.Count()); list.Add(new Tuple <Commit, CommitStats>(commit, stats)); } vm = new CommitsModel(list); return(Ok(vm)); } catch (Exception ex) { System.Diagnostics.Trace.TraceError("GetRevision: " + ex); return(InternalServerError(ex)); } finally { if (unitOfWork != null) { unitOfWork.Dispose(); } } }
public void By_KeyWord() { using (var database = new DbCodeReview()) { database.Commits.AddRange(new Commit[] { new Commit() { Author = "daffy", Message = "A message", Timestamp = new DateTime(2000, 1, 1) }, new Commit() { Author = "bugs", Message = "Another message", Timestamp = new DateTime(2002, 1, 2) }, new Commit() { Author = "mickey", Message = "Yet another", Timestamp = new DateTime(2000, 2, 3) }, }); database.SaveChanges(); } using (var database = new DbCodeReview()) { var sut = new CommitQuery(database.Commits) { Keyword = "m" }; var result = sut.Execute(); Assert.AreEqual(2, result.Count <Commit>()); Assert.AreEqual(2, result.Where(c => c.Author == "daffy" || c.Author == "bugs").Count <Commit>()); } }
public void By_All_Criteria() { using (var database = new DbCodeReview()) { database.Commits.AddRange(new Commit[] { new Commit() { Author = "daffy", Message = "A message", Timestamp = new DateTime(2000, 1, 1), ApprovedBy = "bugs" }, new Commit() { Author = "bugs", Message = "Another message", Timestamp = new DateTime(2002, 1, 2) }, new Commit() { Author = "mickey", Message = "Yet another", Timestamp = new DateTime(2000, 2, 3) }, new Commit() { Author = "mickey", Message = "Yet another message", Timestamp = new DateTime(2000, 2, 3) }, new Commit() { Author = "mickey", Message = "Mickey mouse message", Timestamp = new DateTime(2003, 2, 3) } }); database.SaveChanges(); } using (var database = new DbCodeReview()) { var sut = new CommitQuery(database.Commits) { ExcludeApproved = true, ExcludeAuthor = "daffy", IncludeAuthor = "mickey", Keyword = "message", Max = 2 }; var result = sut.Execute(); Assert.AreEqual(2, result.Count <Commit>()); Assert.AreEqual(2, result.Where(c => c.Author == "mickey" && c.Message.Contains("message") && c.ApprovedBy == null).Count <Commit>()); } }
public void Take_Max() { using (var database = new DbCodeReview()) { database.Commits.AddRange(new Commit[] { new Commit() { Author = "daffy", Message = "A message", Timestamp = new DateTime(2000, 1, 1) }, new Commit() { Author = "bugs", Message = "Another message", Timestamp = new DateTime(2002, 1, 2) }, new Commit() { Author = "mickey", Message = "Yet another", Timestamp = new DateTime(2000, 2, 3) }, new Commit() { Author = "mickey", Message = "Yet another mickey message", Timestamp = new DateTime(2000, 2, 1) }, new Commit() { Author = "mickey", Message = "Yet another mickey mouse message", Timestamp = new DateTime(2000, 2, 4) }, }); database.SaveChanges(); } using (var database = new DbCodeReview()) { var sut = new CommitQuery(database.Commits) { Max = 2 }; var result = sut.Execute(); Assert.AreEqual(2, result.Count <Commit>()); var resultList = result.ToList(); Assert.IsTrue(resultList[0].Timestamp <= resultList[1].Timestamp); } }
public void By_ExcludeApproved() { using (var database = new DbCodeReview()) { database.Commits.AddRange(new Commit[] { new Commit() { Author = "daffy", Message = "A message", Timestamp = new DateTime(2000, 1, 1), ApprovedBy = "bugs" }, new Commit() { Author = "bugs", Message = "Another message", Timestamp = new DateTime(2002, 1, 2) }, new Commit() { Author = "mickey", Message = "Yet another", Timestamp = new DateTime(2000, 2, 3) }, }); database.SaveChanges(); } using (var database = new DbCodeReview()) { var sut = new CommitQuery(database.Commits) { ExcludeApproved = true }; var result = sut.Execute(); Assert.AreEqual(2, result.Count <Commit>()); Assert.AreEqual(0, result.Where(c => c.Author == "daffy").Count <Commit>()); sut = new CommitQuery(database.Commits) { ExcludeApproved = false }; result = sut.Execute(); Assert.AreEqual(3, result.Count <Commit>()); } }
public void Ordered_By_Timestamp_Ascending() { using (var database = new DbCodeReview()) { database.Commits.AddRange(new Commit[] { new Commit() { Author = "daffy", Message = "A message", Timestamp = new DateTime(2000, 1, 1), ApprovedBy = "bugs" }, new Commit() { Author = "bugs", Message = "Another message", Timestamp = new DateTime(2002, 1, 2) }, new Commit() { Author = "mickey", Message = "Yet another", Timestamp = new DateTime(2000, 2, 3) }, new Commit() { Author = "mickey", Message = "Yet another message", Timestamp = new DateTime(2000, 2, 3) }, new Commit() { Author = "mickey", Message = "Mickey mouse message", Timestamp = new DateTime(2003, 2, 3) } }); database.SaveChanges(); } using (var database = new DbCodeReview()) { var sut = new CommitQuery(database.Commits); var resultList = sut.Execute().ToList(); for (int i = 1; i < resultList.Count; i++) { Assert.IsTrue(resultList[i - 1].Timestamp <= resultList[i].Timestamp); } } }