public void GetByIdTest_ValidId_ShouldReturnValidEntity() { Vote entity = new Vote() { Value = 1 }; this.voteRepository.Add(entity); Vote foundEntity = this.voteEntities.Find(entity.Id); Assert.AreSame(entity, foundEntity); }
public void AddTest_EntityIsValid_ShouldBeAddedSuccessfuly() { Vote entity = new Vote() { Value = 1 }; this.voteRepository.Add(entity); Vote foundEntity = this.voteEntities.Find(entity.Id); Assert.IsTrue(entity.Id != 0); Assert.IsNotNull(foundEntity); Assert.AreSame(entity, foundEntity); }
public HttpResponseMessage VoteForPost([FromBody]VoteModel voteModel, [FromUri]int postId, string sessionKey) { var response = this.PerformOperationAndHandleExceptions(() => { var user = this.userRepository.GetAll() .Where(usr => usr.SessionKey == sessionKey).FirstOrDefault(); if (user == null) { throw new InvalidOperationException("No logged user or invalid sessionKey"); } var post = this.postRepository.GetById(postId); if (post == null) { throw new ArgumentException(string.Format("No post with id = {0}", postId)); } if (voteModel.Value < 0 || voteModel.Value > 5) { throw new ArgumentException("The vote value cannot be less than 0 and larger than 5"); } var vote = new Vote() { Value = voteModel.Value, User = user, Post = post }; post.Votes.Add(vote); this.voteRepository.Add(vote); var responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, new { id = vote.Id, votedBy = user.Nickname, value = vote.Value }); return responseMessage; }); return response; }
public void UpdateTest_InvalidEntityId() { int invalidId = -1; Vote entity = new Vote() { Value = 1 }; this.voteRepository.Add(entity); Vote voteToUpdateWith = new Vote() { Value = 2 }; Vote updatedVote = this.voteRepository.Update(invalidId, voteToUpdateWith); Assert.IsNull(updatedVote); }
public void UpdateTest_ValidUpdate() { Vote entity = new Vote() { Value = 1 }; this.voteRepository.Add(entity); Vote voteToUpdateWith = new Vote() { Value = 2 }; Vote updatedVote = this.voteRepository.Update(entity.Id, voteToUpdateWith); Assert.AreSame(entity, updatedVote); Assert.AreEqual(updatedVote.Value, voteToUpdateWith.Value); }
public void UpdateTest_InvalidUpdateEntity_ShouldThrowArgumentNullException() { Vote entity = new Vote() { Value = 1 }; this.voteRepository.Add(entity); Vote voteToUpdateWith = null; Vote updatedUser = this.voteRepository.Update(entity.Id, voteToUpdateWith); }