コード例 #1
0
        public void FlagService_FlagExistingAnswer_DoesNotAddAgain()
        {
            // Setup
            var setup = new TestSetup();

            var answerFlag1 = new AnswerFlagDto()
            {
                AnswerId = 1, UserId = "1"
            };
            var answerFlag2 = new AnswerFlagDto()
            {
                AnswerId = 1, UserId = "1"
            };

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

            Assert.Equal(0, count);
            setup.FlagService.FlagAnswer(answerFlag1);
            count = setup.AnswerFlagsRepository.Queryable().Where(x => x.UserId == "1").Count();
            Assert.Equal(1, count);
            setup.FlagService.FlagAnswer(answerFlag2);
            // Verify insert was called for every flag save. Flags work differently than votes.
            // Double flags are OK.
            count = setup.AnswerFlagsRepository.Queryable().Where(x => x.UserId == "1").Count();
            Assert.Equal(2, count);
        }
コード例 #2
0
        public void FlagService_CountAnswerFlags_ReturnsAnswerFlags()
        {
            // Setup
            var setup = new TestSetup();

            var answerFlag1 = new AnswerFlagDto()
            {
                Id = 1234, AnswerId = 111, UserId = "1"
            };
            var answerFlag2 = new AnswerFlagDto()
            {
                Id = 1235, AnswerId = 111, UserId = "2"
            };

            setup.FlagService.FlagAnswer(answerFlag1);
            setup.FlagService.FlagAnswer(answerFlag2);

            //var count = setup.FlagService.CountAnswerFlags(111);
            //// Verify that cache has 2 Flags.
            //Assert.Equal(2, count);

            //count = setup.FlagService.CountAnswerFlags(0);
            //// Verify that cache has 2 Flags.
            //Assert.Equal(0, count);

            //count = setup.FlagService.CountAnswerFlags(112);
            //// Verify that cache does not contain random items.
            //Assert.Equal(0, count);
        }
コード例 #3
0
        public void FlagService_FlagAnswer_AddsFlag()
        {
            // Setup
            var setup = new TestSetup();
            // Object we will be adding
            var answerFlagDto = new AnswerFlagDto()
            {
                Id = 123, AnswerId = 5, UserId = "D"
            };

            // Call the method we are testing
            var result = setup.FlagService.FlagAnswer(answerFlagDto);

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

            // Verify repository has the item
            Assert.NotNull(setup.AnswerFlagsRepository.Queryable()
                           .Where(x => x.AnswerId == answerFlagDto.AnswerId).FirstOrDefault());
        }
コード例 #4
0
        public void FlagService_FlagExistingAnswer_DoesNotCacheAgain()
        {
            // Setup
            var setup = new TestSetup();

            var answerFlag1 = new AnswerFlagDto()
            {
                AnswerId = 1, UserId = "1"
            };
            var answerFlag2 = new AnswerFlagDto()
            {
                AnswerId = 2, UserId = "1"
            };

            setup.FlagService.FlagAnswer(answerFlag1);
            setup.FlagService.FlagAnswer(answerFlag2);

            // Verify cache add to cache was called only once
            //setup.CacheMock.Verify(x => x.Add(CacheConstants.CACHE_KEY_FlagS_DATA,
            //    It.IsAny<KeyIndexedDataSource<AnswerFlag>>()), Times.Once());
        }
コード例 #5
0
        /// <summary>
        /// Save answer flag
        /// </summary>
        /// <param name="answerFlag"></param>
        /// <returns></returns>
        /// <remarks>Flag is always considered new</remarks>
        public DataOperationResult FlagAnswer(AnswerFlagDto answerFlag)
        {
            if (answerFlag == null)
            {
                throw new ServicesException("Null parameter FlagService.FlagAnswer(answerFlag)");
            }

            if (answerFlag.AnswerId <= 0)
            {
                throw new ServicesException("Unexpected AnswerId in FlagService.FlagAnswer(answerFlag)");
            }

            if (answerFlag.UserId == null)
            {
                throw new ServicesException("Unexpected UserId in FlagService.FlagAnswer(answerFlag)");
            }

            var answerFlagObject = new AnswerFlag();

            answerFlagObject.FromDto(answerFlag);

            _answerFlagRepository.Insert(answerFlagObject);

            var task = _answerFlagRepository.SaveChangesAsync();

            task.Wait();

            // Add to user cache if there is a user
            if (answerFlagObject.UserId != null)
            {
                var userCachedData = GetUserFlagsCachedData();
                userCachedData.Insert(answerFlagObject);
            }

            return(new DataOperationResult()
            {
                IntId = answerFlagObject.Id, IsNew = true
            });
        }