public void FetchAnswersForQuestions_Always_SetsAttemptCountEqualToCountFromIAttemptRepositoryGetAttemptsForAnswer()
        {
            // arrange
            var questionId = AutoFixture.Create <int>();

            var answerDtos = AutoFixture.CreateMany <AnswerDto>().ToList();

            AutoFixture.Freeze <Mock <IStackExchangeClient> >()
            .Setup(x => x.GetAnswers(It.IsAny <int>()))
            .Returns(answerDtos);

            var attempts = AutoFixture.CreateMany <Attempt>().ToList();

            AutoFixture.Freeze <Mock <IAttemptRepository> >()
            .Setup(x => x.GetAttemptsForAnswer(It.IsAny <int>()))
            .Returns(attempts);

            // act
            var controller = AutoFixture.Create <QuestionController>();

            controller.FetchAnswersForQuestion(questionId);

            // assert
            foreach (var answerDto in answerDtos)
            {
                answerDto.AttemptCount.Should().Be(attempts.Count);
            }
        }
        public void CreateFilter_WhenIRestClientExecuteReturnsResponseWithZeroQuotaRemaining_ReturnsNull()
        {
            // arrange
            var client = new Mock <IRestClient>();
            var data   = AutoFixture.Build <ItemResponseDto <FilterDto> >()
                         .With(x => x.QuotaRemaining, 0)
                         .Create();
            var restResponse = AutoFixture.Build <RestResponse <ItemResponseDto <FilterDto> > >()
                               .With(x => x.Data, data)
                               .Create();

            client
            .Setup(x => x.Execute <ItemResponseDto <FilterDto> >(It.IsAny <IRestRequest>()))
            .Returns(restResponse);
            AutoFixture.Freeze <Mock <IRestSharpWrapper> >()
            .Setup(x => x.CreateRestClient(It.IsAny <string>()))
            .Returns(client.Object);

            // act
            var filterCreator = AutoFixture.Create <StackExchangeFilterCreator>();
            var response      = filterCreator.CreateFilter();

            // assert
            response.Should().BeNull();
        }
Exemplo n.º 3
0
        public void FetchAttemptedQuestion_Always_ReturnsAttemptedQuestion()
        {
            // arrange
            var userId     = AutoFixture.Create <string>();
            var questionId = AutoFixture.Create <int>();

            var attemptDto = AutoFixture.Create <AttemptDto>();

            AutoFixture.Freeze <Mock <IMapper> >()
            .Setup(x => x.Map <AttemptDto>(It.IsAny <Attempt>()))
            .Returns(attemptDto);

            var questionDto = AutoFixture.Create <QuestionDto>();

            AutoFixture.Freeze <Mock <IStackExchangeClient> >()
            .Setup(x => x.GetQuestion(It.IsAny <int>()))
            .Returns(questionDto);

            // act
            var fetcher  = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>();
            var response = fetcher.FetchAttemptedQuestion(userId, questionId);

            // assert
            response.AttemptDto.Should().Be(attemptDto);
            response.QuestionDto.Should().Be(questionDto);
        }
Exemplo n.º 4
0
        public void FetchQuestions_Always_CallsIAttemptedQuestionDtoAssemblerAssembleAttemptedQuestionsOnce()
        {
            // arrange
            var userId = AutoFixture.Create <string>();

            var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList();

            AutoFixture.Freeze <Mock <IStackExchangeClient> >()
            .Setup(x => x.GetQuestions(It.IsAny <List <int> >()))
            .Returns(questionDtos);

            var attemptDtos = AutoFixture.CreateMany <AttemptDto>().ToList();

            AutoFixture.Freeze <Mock <IMapper> >()
            .Setup(x => x.Map <IEnumerable <AttemptDto> >(It.IsAny <IEnumerable <Attempt> >()))
            .Returns(attemptDtos);

            var assemblerMock = AutoFixture.Freeze <Mock <IAttemptedQuestionDtoAssembler> >();

            // act
            var fetcher = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>();

            fetcher.FetchAttemptedQuestions(userId);

            // assert
            assemblerMock.Verify(x => x.AssembleAttemptedQuestions(attemptDtos, questionDtos), Times.Once);
        }
Exemplo n.º 5
0
        public void FetchQuestions_Always_CallsIQuestionFilterFilterQuestionsOnce()
        {
            // arrange
            var userId = AutoFixture.Create <string>();

            var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList();

            AutoFixture.Freeze <Mock <IStackExchangeClient> >()
            .Setup(x => x.GetLatestQuestions(It.IsAny <int>()))
            .Returns(questionDtos);

            var filteredQuestions = AutoFixture.CreateMany <QuestionDto>(20).ToList();
            var filterMock        = AutoFixture.Freeze <Mock <IQuestionFilter> >();

            filterMock
            .Setup(x => x.FilterQuestions(It.IsAny <IEnumerable <QuestionDto> >()))
            .Returns(filteredQuestions);

            // act
            var fetcher = AutoFixture.Create <FilteredLatestQuestionsFetcher>();

            fetcher.FetchQuestions(userId);

            // assert
            filterMock.Verify(x => x.FilterQuestions(questionDtos), Times.Once);
        }
Exemplo n.º 6
0
        public void FetchQuestions_WhenAttemptRepositoryReturnsQuestionIds_ExcludesAttemptedQuestionIdsFromResult()
        {
            // arrange
            var userId     = AutoFixture.Create <string>();
            var questionId = AutoFixture.Create <int>();

            var attempts = AutoFixture.Build <Attempt>()
                           .With(x => x.QuestionId, questionId)
                           .CreateMany()
                           .ToList();

            AutoFixture.Freeze <Mock <IAttemptRepository> >()
            .Setup(x => x.GetAttempts(It.IsAny <string>()))
            .Returns(attempts);

            var previouslyAttemptedQuestions = AutoFixture.Build <QuestionDto>()
                                               .With(x => x.QuestionId, questionId)
                                               .CreateMany()
                                               .ToList();
            var notPreviouslyAttemptedQuestions = AutoFixture.CreateMany <QuestionDto>(20).ToList();

            previouslyAttemptedQuestions.InsertRange(RandomGenerator.Next(previouslyAttemptedQuestions.Count), notPreviouslyAttemptedQuestions);
            AutoFixture.Freeze <Mock <IQuestionFilter> >()
            .Setup(x => x.FilterQuestions(It.IsAny <IEnumerable <QuestionDto> >()))
            .Returns(previouslyAttemptedQuestions);

            // act
            var fetcher  = AutoFixture.Create <FilteredLatestQuestionsFetcher>();
            var response = fetcher.FetchQuestions(userId);

            // assert
            response.Should().BeEquivalentTo(notPreviouslyAttemptedQuestions);
        }
        public void SubmitAttempt_WhenExistingAttemptCheckReturnsNull_AssignsUserIdAndScore()
        {
            // arrange
            var attemptDto = AutoFixture.Create <AttemptDto>();
            var userId     = AutoFixture.Create <string>();

            AutoFixture.Freeze <Mock <IAttemptRepository> >()
            .Setup(x => x.GetAttempt(It.IsAny <int>(), It.IsAny <string>()))
            .Returns((Attempt)null);

            var attempt = AutoFixture.Create <Attempt>();

            AutoFixture.Freeze <Mock <IMapper> >()
            .Setup(x => x.Map <Attempt>(It.IsAny <AttemptDto>()))
            .Returns(attempt);

            var score = AutoFixture.Create <int>();

            AutoFixture.Freeze <Mock <IAttemptScoreCalculator> >()
            .Setup(x => x.CalculateScore(It.IsAny <Attempt>()))
            .Returns(score);

            var repositoryMock = AutoFixture.Freeze <Mock <IAttemptRepository> >();

            // act
            var manager = AutoFixture.Create <AttemptSubmissionManager>();

            manager.SubmitAttempt(attemptDto, userId);

            //  assert
            repositoryMock.Verify(x => x.InsertAttempt(It.Is <Attempt>(param => param.UserId == userId && param.Score == score)));
        }
        public void CalculateScore_GivenIncorrectAnswer_ReturnsRatioOfSameAttemptsDividedByTotalAttemptsOnQuestion()
        {
            // arrange
            var attempt = AutoFixture.Create <Attempt>();

            var matchingAttempts = AutoFixture.Build <Attempt>()
                                   .With(x => x.AnswerId, attempt.AnswerId)
                                   .CreateMany()
                                   .ToList();
            var nonMatchingAttempts = AutoFixture.CreateMany <Attempt>().ToList();

            nonMatchingAttempts.InsertRange(RandomGenerator.Next(nonMatchingAttempts.Count), matchingAttempts);
            AutoFixture.Freeze <Mock <IAttemptRepository> >()
            .Setup(x => x.GetAttemptsForQuestion(It.IsAny <int>()))
            .Returns(nonMatchingAttempts);

            // act
            var calculator = AutoFixture.Create <AttemptScoreCalculator>();
            var response   = calculator.CalculateScore(attempt);

            //  assert
            var score = (double)(matchingAttempts.Count - 1) / nonMatchingAttempts.Count * 100;

            response.Should().Be((int)Math.Round(score));
        }
        public void SubmitAttempt_WhenExistingAttemptCheckReturnsNull_CallsIAttemptScoreCalculatorCalculateScoreOnce()
        {
            // arrange
            var attemptDto = AutoFixture.Create <AttemptDto>();
            var userId     = AutoFixture.Create <string>();

            AutoFixture.Freeze <Mock <IAttemptRepository> >()
            .Setup(x => x.GetAttempt(It.IsAny <int>(), It.IsAny <string>()))
            .Returns((Attempt)null);

            var attempt = AutoFixture.Create <Attempt>();

            AutoFixture.Freeze <Mock <IMapper> >()
            .Setup(x => x.Map <Attempt>(It.IsAny <AttemptDto>()))
            .Returns(attempt);

            var calculatorMock = AutoFixture.Freeze <Mock <IAttemptScoreCalculator> >();

            // act
            var manager = AutoFixture.Create <AttemptSubmissionManager>();

            manager.SubmitAttempt(attemptDto, userId);

            //  assert
            calculatorMock.Verify(x => x.CalculateScore(attempt), Times.Once);
        }
        public void ValidationResults_NotNullAndEmptyAfterCreation()
        {
            var target = AutoFixture.Freeze <DefaultOptionSetValidator>();

            Assert.That(target.ValidationResults, Is.Not.Null);
            Assert.That(target.ValidationResults, Is.Empty);
        }
        public void CreateFilter_WhenIRestClientExecuteReturnsValidResponse_ReturnsResponseData()
        {
            // arrange
            var client = new Mock <IRestClient>();
            var data   = AutoFixture.Build <ItemResponseDto <FilterDto> >()
                         .With(x => x.QuotaRemaining, RandomGenerator.Next(1, 100))
                         .Create();
            var restResponse = AutoFixture.Build <RestResponse <ItemResponseDto <FilterDto> > >()
                               .With(x => x.Data, data)
                               .Create();

            client
            .Setup(x => x.Execute <ItemResponseDto <FilterDto> >(It.IsAny <IRestRequest>()))
            .Returns(restResponse);
            AutoFixture.Freeze <Mock <IRestSharpWrapper> >()
            .Setup(x => x.CreateRestClient(It.IsAny <string>()))
            .Returns(client.Object);

            // act
            var filterCreator = AutoFixture.Create <StackExchangeFilterCreator>();
            var response      = filterCreator.CreateFilter();

            // assert
            response.Should().BeEquivalentTo(data.Items.FirstOrDefault()?.Filter);
        }
Exemplo n.º 12
0
 public override void SetupTest()
 {
     base.SetupTest();
     _mapper = AutoFixture.Freeze <Mock <IMappingEngine> >();
     _mapper.DefaultValue = DefaultValue.Mock;
     _repository          = AutoFixture.Freeze <Mock <IRepository> >();
     _sut = AutoFixture.Create <WorkerRegistry>();
     _mapper.Setup(s => s.Map <RegisterWorkerRequest, WorkerRegistration>(It.IsAny <RegisterWorkerRequest>())).Returns(AutoFixture.Create <WorkerRegistration>());
 }
        public void AddError_WhenWePassEmptyMessage_ThrowException()
        {
            var ex = Assert.Throws <ArgumentNullException>(() =>
            {
                var target = AutoFixture.Freeze <DefaultOptionSetValidator>();
                target.AddError(null, null);
            });

            Assert.That(ex.ParamName, Is.EqualTo("message"));
        }
Exemplo n.º 14
0
        public override void SetupTest()
        {
            base.SetupTest();

            _fileSystemMock = AutoFixture.Freeze <Mock <IFileSystem> >();
            _processRunner  = AutoFixture.Freeze <Mock <IProcessRunner> >();
            _serverChannel  = AutoFixture.Freeze <Mock <IServerChannel> >();
            _options        = AutoFixture.Freeze <TaskExecutionOptions>();

            _serverChannel.Setup(s => s.WorkComplete(It.IsAny <TaskExecutionResult>())).Returns(new Response(false, ""));
            _taskExecutor = AutoFixture.Create <TaskExecutor>();
        }
        public void FetchLatestQuestions_Always_CallsIFilteredLatestQuestionsFetcherFetchQuestionsOnce()
        {
            // arrange
            var questionFetcherMock = AutoFixture.Freeze <Mock <IFilteredLatestQuestionsFetcher> >();

            // act
            var controller = AutoFixture.Create <QuestionController>();

            controller.FetchLatestQuestions();

            //  assert
            questionFetcherMock.Verify(x => x.FetchQuestions(It.IsAny <string>()), Times.Once);
        }
        public void CreateFilter_Always_CallsIStackExchangeResourceFactoryFetchResourceOnce()
        {
            // arrange
            var resourceFactoryMock = AutoFixture.Freeze <Mock <IStackExchangeResourceFactory> >();

            // act
            var filterCreator = AutoFixture.Create <StackExchangeFilterCreator>();

            filterCreator.CreateFilter();

            // assert
            resourceFactoryMock.Verify(x => x.FetchResource(StackExchangeResourceEnum.CreateFilter), Times.Once);
        }
        public void FetchPreviousQuestions_Always_CallsIPreviouslyAttemptedQuestionFetcherFetchQuestionsOnce()
        {
            // arrange
            var questionFetcherMock = AutoFixture.Freeze <Mock <IPreviouslyAttemptedQuestionFetcher> >();

            // act
            var controller = AutoFixture.Create <AttemptController>();

            controller.FetchPreviousQuestions();

            //  assert
            questionFetcherMock.Verify(x => x.FetchAttemptedQuestions(It.IsAny <string>()), Times.Once);
        }
        public void CreateFilter_Always_CallsIRestSharpWrapperCreateRestClientOnce()
        {
            // arrange
            var restSharpWrapperMock = AutoFixture.Freeze <Mock <IRestSharpWrapper> >();

            // act
            var filterCreator = AutoFixture.Create <StackExchangeFilterCreator>();

            filterCreator.CreateFilter();

            // assert
            restSharpWrapperMock.Verify(x => x.CreateRestClient("https://api.stackexchange.com/2.2"), Times.Once);
        }
Exemplo n.º 19
0
        public async Task TestEmitBatchAsync_WithEnabledTrue_ShouldInsertLogEvents(IEnumerable <LogEvent> logEvents)
        {
            //Arrange
            AutoFixture.Customize <BigQuerySinkOptions>(opt => opt.With(p => p.IsEnabled, true));
            var connection = AutoFixture.Freeze <Mock <IConnection> >();

            //Act
            var sut = AutoFixture.Create <SinkTest>();
            await sut.TestEmitBatchAsync(logEvents);

            //Assert
            connection.Verify(mock => mock.CreateLogTableAsync(), Times.Once);
            connection.Verify(mock => mock.InsertLogEventsAsync(It.IsAny <IEnumerable <BigQueryLogEvent> >()), Times.Once);
        }
        public void CreateAttempt_Always_CallsIAttemptSubmissionManagerSubmitAttemptOnce()
        {
            // arrange
            var attemptDto = AutoFixture.Create <AttemptDto>();

            var managerMock = AutoFixture.Freeze <Mock <IAttemptSubmissionManager> >();

            // act
            var controller = AutoFixture.Create <AttemptController>();

            controller.CreateAttempt(attemptDto);

            //  assert
            managerMock.Verify(x => x.SubmitAttempt(attemptDto, It.IsAny <string>()), Times.Once);
        }
        public void AddError_WhenCallAddError_MessagePersistsAmongValidationResults()
        {
            var message     = AutoFixture.Create <string>();
            var xmlLineInfo = AutoFixture.Create <Mock <IXmlLineInfo> >()
                              .Object;

            var target = AutoFixture.Freeze <DefaultOptionSetValidator>();

            target.AddError(message, xmlLineInfo);

            Assert.That(target.ValidationResults, Is.Not.Null);
            Assert.That(target.ValidationResults.Count, Is.EqualTo(1));
            Assert.That(target.ValidationResults[0].Key, Is.EqualTo(message));
            Assert.That(target.ValidationResults[0].Value, Is.SameAs(xmlLineInfo));
        }
        public void FetchAnswersForQuestion_Always_CallsIStackExchangeClientFetchAnswersForQuestionOnce()
        {
            // arrange
            var questionId = AutoFixture.Create <int>();

            var clientMock = AutoFixture.Freeze <Mock <IStackExchangeClient> >();

            // act
            var controller = AutoFixture.Create <QuestionController>();

            controller.FetchAnswersForQuestion(questionId);

            //  assert
            clientMock.Verify(x => x.GetAnswers(questionId), Times.Once);
        }
        public void GetAttemptsForQuestion_Always_CallsIAttemptRepositoryGetAttemptsForQuestion()
        {
            // arrange
            var questionId = AutoFixture.Create <int>();

            var repositoryMock = AutoFixture.Freeze <Mock <IAttemptRepository> >();

            // act
            var controller = AutoFixture.Create <QuestionController>();

            controller.GetAttemptsForQuestion(questionId);

            //  assert
            repositoryMock.Verify(x => x.GetAttemptsForQuestion(questionId), Times.Once);
        }
        public void CalculateScore_GivenIncorrectAnswer_CallsIAttemptRepositoryGetAttemptsForQuestionOnce()
        {
            // arrange
            var attempt = AutoFixture.Create <Attempt>();

            var attemptRepositoryMock = AutoFixture.Freeze <Mock <IAttemptRepository> >();

            // act
            var calculator = AutoFixture.Create <AttemptScoreCalculator>();

            calculator.CalculateScore(attempt);

            //  assert
            attemptRepositoryMock.Verify(x => x.GetAttemptsForQuestion(attempt.QuestionId), Times.Once);
        }
Exemplo n.º 25
0
        public void FetchQuestions_Always_CallsIAttemptRepositoryGetAttemptsOnce()
        {
            // arrange
            var userId = AutoFixture.Create <string>();

            var repositoryMock = AutoFixture.Freeze <Mock <IAttemptRepository> >();

            // act
            var fetcher = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>();

            fetcher.FetchAttemptedQuestions(userId);

            // assert
            repositoryMock.Verify(x => x.GetAttempts(userId), Times.Once);
        }
        public void FetchPreviousQuestions_Always_ReturnsResponseFromIPreviouslyAttemptedQuestionFetcher()
        {
            // arrange
            var attemptQuestionDtos = AutoFixture.CreateMany <AttemptedQuestionDto>().ToList();

            AutoFixture.Freeze <Mock <IPreviouslyAttemptedQuestionFetcher> >()
            .Setup(x => x.FetchAttemptedQuestions(It.IsAny <string>()))
            .Returns(attemptQuestionDtos);

            // act
            var controller = AutoFixture.Create <AttemptController>();
            var response   = controller.FetchPreviousQuestions();

            //  assert
            response.Should().BeEquivalentTo(attemptQuestionDtos);
        }
        public void FetchLatestQuestions_Always_ReturnsResponseFromIFilteredLatestQuestionsFetcher()
        {
            // arrange
            var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList();

            AutoFixture.Freeze <Mock <IFilteredLatestQuestionsFetcher> >()
            .Setup(x => x.FetchQuestions(It.IsAny <string>()))
            .Returns(questionDtos);

            // act
            var controller = AutoFixture.Create <QuestionController>();
            var response   = controller.FetchLatestQuestions();

            //  assert
            response.Should().BeEquivalentTo(questionDtos);
        }
        public void SubmitAttempt_Always_CallsIAttemptRepositoryGetAttemptOnce()
        {
            // arrange
            var attemptDto = AutoFixture.Create <AttemptDto>();
            var userId     = AutoFixture.Create <string>();

            var repositoryMock = AutoFixture.Freeze <Mock <IAttemptRepository> >();

            // act
            var manager = AutoFixture.Create <AttemptSubmissionManager>();

            manager.SubmitAttempt(attemptDto, userId);

            //  assert
            repositoryMock.Verify(x => x.GetAttempt(attemptDto.QuestionId, userId), Times.Once);
        }
Exemplo n.º 29
0
        public void FetchAttemptedQuestion_Always_CallsIStackExchangeClientGetQuestionOnce()
        {
            // arrange
            var userId     = AutoFixture.Create <string>();
            var questionId = AutoFixture.Create <int>();

            var clientMock = AutoFixture.Freeze <Mock <IStackExchangeClient> >();

            // act
            var fetcher = AutoFixture.Create <PreviouslyAttemptedQuestionFetcher>();

            fetcher.FetchAttemptedQuestion(userId, questionId);

            // assert
            clientMock.Verify(x => x.GetQuestion(questionId), Times.Once);
        }
Exemplo n.º 30
0
        public void AssembleAttemptedQuestions_WhenQuestionFilterReturnsNull_ReturnsEmptyList()
        {
            // arrange
            var attemptDtos  = AutoFixture.CreateMany <AttemptDto>().ToList();
            var questionDtos = AutoFixture.CreateMany <QuestionDto>().ToList();

            AutoFixture.Freeze <Mock <IQuestionFilter> >()
            .Setup(x => x.GetQuestionDtoById(It.IsAny <IEnumerable <QuestionDto> >(), It.IsAny <int>()))
            .Returns((QuestionDto)null);

            // act
            var assembler = AutoFixture.Create <AttemptedQuestionDtoAssembler>();
            var response  = assembler.AssembleAttemptedQuestions(attemptDtos, questionDtos);

            // assert
            response.Should().BeEmpty();
        }