Example #1
0
        public void VoteService_VoteExistingAnswerDescription_DoesNotAddAgain()
        {
            // Setup
            var setup = new TestSetup();

            var answerDescriptionVote1 = new AnswerDescriptionVoteDto()
            {
                AnswerDescriptionId = 1, UserId = "1"
            };
            var answerDescriptionVote2 = new AnswerDescriptionVoteDto()
            {
                AnswerDescriptionId = 1, UserId = "1"
            };

            var count = setup.AnswerDescriptionVoteRepository.Queryable().Where(x => x.UserId == "1").Count();

            Assert.Equal(0, count);
            setup.VoteService.VoteAnswerDescription(answerDescriptionVote1);
            count = setup.AnswerDescriptionVoteRepository.Queryable().Where(x => x.UserId == "1").Count();
            Assert.Equal(1, count);
            setup.VoteService.VoteAnswerDescription(answerDescriptionVote2);
            // Verify insert was called only once.
            count = setup.AnswerDescriptionVoteRepository.Queryable().Where(x => x.UserId == "1").Count();
            Assert.Equal(1, count);
        }
Example #2
0
        public void VoteService_VoteAnswerDescription_AddsDescription()
        {
            // Setup
            var setup = new TestSetup();
            // Object we will be adding
            var answerDescriptionVoteDto = new AnswerDescriptionVoteDto()
            {
                AnswerDescriptionId = 55, UserId = "D"
            };

            // Call the method we are testing
            var result = setup.VoteService.VoteAnswerDescription(answerDescriptionVoteDto);

            // Check that same Phrase is returned
            Assert.Equal(result.IntId, 15);

            // 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_DESCRIPTION_VOTES_DATA), Times.Exactly(2));
            // Verify cache add to cache was called only once
            setup.CacheMock.Verify(x => x.Add(CacheConstants.CACHE_KEY_DESCRIPTION_VOTES_DATA,
                                              It.IsAny <KeyIndexedDataSource <AnswerDescriptionVote> >()), Times.Once());
            // Verify repository has the item
            Assert.NotNull(setup.AnswerDescriptionVoteRepository.Queryable()
                           .Where(x => x.AnswerDescriptionId == answerDescriptionVoteDto.AnswerDescriptionId).FirstOrDefault());
        }
Example #3
0
        public void VoteService_VoteExistingAnswerDescription_DoesNotCacheAgain()
        {
            // Setup
            var setup = new TestSetup();

            var answerDescriptionVote1 = new AnswerDescriptionVoteDto()
            {
                AnswerDescriptionId = 1, UserId = "1"
            };
            var answerDescriptionVote2 = new AnswerDescriptionVoteDto()
            {
                AnswerDescriptionId = 2, UserId = "1"
            };

            setup.VoteService.VoteAnswerDescription(answerDescriptionVote1);
            setup.VoteService.VoteAnswerDescription(answerDescriptionVote2);

            // Verify cache add to cache was called only once
            setup.CacheMock.Verify(x => x.Add(CacheConstants.CACHE_KEY_DESCRIPTION_VOTES_DATA,
                                              It.IsAny <KeyIndexedDataSource <AnswerDescriptionVote> >()), Times.Once());
        }
Example #4
0
        /// <summary>
        /// Save answer description Vote
        /// </summary>
        /// <param name="answerVote"></param>
        /// <returns>Id of the answer whos description was voted.</returns>
        public DataOperationResult VoteAnswerDescription(AnswerDescriptionVoteDto answerDescriptionVote)
        {
            if (answerDescriptionVote == null)
            {
                throw new ServicesException("Null parameter VoteService.VoteAnswerDescription(answerDescriptionVote)");
            }

            if (answerDescriptionVote.AnswerDescriptionId <= 0)
            {
                throw new ServicesException("Unexpected AnswerDescriptionId in VoteService.VoteAnswerDescription(answerDescriptionVote)");
            }

            if (answerDescriptionVote.UserId == null)
            {
                throw new ServicesException("Unexpected UserId in VoteService.VoteAnswerDescription(answerDescriptionVote)");
            }

            var result = new DataOperationResult();

            // Find if vote is already there.
            var existingVote = _answerDescriptionVoteRepository.Queryable()
                               .FirstOrDefault(x => x.UserId == answerDescriptionVote.UserId && x.AnswerDescriptionId == answerDescriptionVote.AnswerDescriptionId);

            // Do not re-add existing vote.
            if (existingVote != null)
            {
                result.IntId = existingVote.Id;
                return(result);
            }

            // Add new description vote
            var answerDescriptionVoteObject = new AnswerDescriptionVote();

            answerDescriptionVoteObject.FromDto(answerDescriptionVote);

            // Insert
            _answerDescriptionVoteRepository.Insert(answerDescriptionVoteObject);
            var task = _answerDescriptionVoteRepository.SaveChangesAsync();

            task.Wait();

            // Add to cache.
            var cachedData = GetVoteDescriptionsCachedData();

            cachedData.Insert(answerDescriptionVoteObject);

            // Add to user cache if there is a user
            if (answerDescriptionVoteObject.UserId != null)
            {
                var userCachedData = GetUserVoteDescriptionsCachedData();
                userCachedData.Insert(new AnswerDescriptionVoteUserMask(answerDescriptionVoteObject));
            }

            // Find the id of the answer whos description was voted for
            var answerDescriptionDto = _answerDescriptionService
                                       .FindByAnswerDescriptionId(answerDescriptionVote.AnswerDescriptionId);

            result.IntId = answerDescriptionDto.AnswerId;
            result.IsNew = true;
            return(result);
        }