public void VoteService_VoteExistingAnswer_DoesNotAddAgain() { // Setup var setup = new TestSetup(); var answerVote1 = new AnswerVoteDto() { AnswerId = 1, UserId = "1" }; var answerVote2 = new AnswerVoteDto() { AnswerId = 1, UserId = "1" }; var count = setup.AnswerVotesRepository.Queryable().Where(x => x.UserId == "1").Count(); Assert.Equal(0, count); setup.VoteService.VoteAnswer(answerVote1); count = setup.AnswerVotesRepository.Queryable().Where(x => x.UserId == "1").Count(); Assert.Equal(1, count); setup.VoteService.VoteAnswer(answerVote2); // Verify insert was called only once. count = setup.AnswerVotesRepository.Queryable().Where(x => x.UserId == "1").Count(); Assert.Equal(1, count); }
public void VoteService_CountAnswerVotes_ReturnsAnswerVotes() { // Setup var setup = new TestSetup(); var answerVote1 = new AnswerVoteDto() { Id = 1234, AnswerId = 111, UserId = "1" }; var answerVote2 = new AnswerVoteDto() { Id = 1235, AnswerId = 111, UserId = "2" }; setup.VoteService.VoteAnswer(answerVote1); setup.VoteService.VoteAnswer(answerVote2); var count = setup.VoteService.CountAnswerVotes(111); // Verify that cache has 2 votes. Assert.Equal(2, count); count = setup.VoteService.CountAnswerVotes(0); // Verify that cache has 2 votes. Assert.Equal(0, count); count = setup.VoteService.CountAnswerVotes(112); // Verify that cache does not contain random items. Assert.Equal(0, count); }
public void VoteService_VoteAnswer_AddsVote() { // Setup var setup = new TestSetup(); // Object we will be adding var answerVoteDto = new AnswerVoteDto() { AnswerId = 5, UserId = "D" }; // Call the method we are testing var result = setup.VoteService.VoteAnswer(answerVoteDto); // Check that same Phrase is returned Assert.Equal(result.IntId, answerVoteDto.AnswerId); // Verify cache get was called only once // This test became more complex. Since we are passing user id service will also // update the cache of user votes and when doing that it will call for // description votes cache one more time to get it built. // Changing once to twice. setup.CacheMock.Verify(x => x.Get(CacheConstants.CACHE_KEY_VOTES_DATA), Times.Exactly(2)); // Verify cache add to cache was called only once setup.CacheMock.Verify(x => x.Add(CacheConstants.CACHE_KEY_VOTES_DATA, It.IsAny <KeyIndexedDataSource <AnswerVote> >()), Times.Once()); // Verify repository has the item Assert.NotNull(setup.AnswerVotesRepository.Queryable() .Where(x => x.AnswerId == answerVoteDto.AnswerId).FirstOrDefault()); }
/// <summary> /// Save answer vote /// </summary> /// <param name="voteVote"></param> /// <returns>Id of the answer whos description was voted.</returns> public DataOperationResult VoteAnswer(AnswerVoteDto answerVote) { if (answerVote == null) { throw new ServicesException("Null parameter VoteService.VoteAnswer(answerVote)"); } if (answerVote.AnswerId <= 0) { throw new ServicesException("Unexpected AnswerId in VoteService.VoteAnswer(answerVote)"); } if (answerVote.UserId == null) { throw new ServicesException("Unexpected UserId in VoteService.VoteAnswer(answerVote)"); } var result = new DataOperationResult(); // Find if vote is already there. var existingVote = _answerVoteRepository.Queryable() .FirstOrDefault(x => x.UserId == answerVote.UserId && x.AnswerId == answerVote.AnswerId); // Do not re-add existing vote. if (existingVote != null) { result.IntId = existingVote.AnswerId; return(result); } // Add new answer vote var answerVoteObject = new AnswerVote(); answerVoteObject.FromDto(answerVote); _answerVoteRepository.Insert(answerVoteObject); var task = _answerVoteRepository.SaveChangesAsync(); task.Wait(); // Add to cache. var cachedData = GetVotesCachedData(); cachedData.Insert(answerVoteObject); // Add to user cache if there is a user if (answerVoteObject.UserId != null) { var userCachedData = GetUserVotesCachedData(); userCachedData.Insert(new AnswerVoteUserMask(answerVoteObject)); } result.IntId = answerVoteObject.AnswerId; result.IsNew = true; return(result); }
public void VoteService_VoteExistingAnswer_DoesNotCacheAgain() { // Setup var setup = new TestSetup(); var answerVote1 = new AnswerVoteDto() { AnswerId = 1, UserId = "1" }; var answerVote2 = new AnswerVoteDto() { AnswerId = 2, UserId = "1" }; setup.VoteService.VoteAnswer(answerVote1); setup.VoteService.VoteAnswer(answerVote2); // Verify cache add to cache was called only once setup.CacheMock.Verify(x => x.Add(CacheConstants.CACHE_KEY_VOTES_DATA, It.IsAny <KeyIndexedDataSource <AnswerVote> >()), Times.Once()); }