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();
        }
        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);
        }
        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 async Task can_get_snapshot()
        {
            //Given
            var aggregate = new Reviews.Domain.Review();

            aggregate.Apple(AutoFixture.Build <Domain.Events.V1.ReviewCreated>()
                            .With(e => e.Caption, Caption)
                            .With(e => e.Content, Content)
                            .With(e => e.ProductId, ProductId)
                            .With(e => e.Id, AggregateId).Create());
            aggregate.Apple(AutoFixture.Build <Domain.Events.V1.ReviewPublished>().With(e => e.Id, AggregateId).Create());
            aggregate.Apple(AutoFixture.Build <Domain.Events.V1.ReviewApproved>().With(e => e.Id, AggregateId).Create());

            var sut  = new GesSnapshotStore(Connection, null);
            var snap = aggregate.TakeSnapshot();
            await sut.SaveSnapshotAsync <Review>(snap);

            //When
            var result = await sut.GetSnapshotAsync <Review>(AggregateId);

            //Then
            outputHelper.WriteLine($"Snapshot result last Version:{result.Version}");
            result.Should().BeEquivalentTo(new ReviewSnapshot(Guid.Empty, AggregateId, -1, Caption, Content, Status.Approved, ProductId),
                                           o => o.ExcludingFields().Excluding(q => q.Id).Excluding(q => q.CurrentStatus).Excluding(q => q.ProductId));
        }
Ejemplo n.º 5
0
        public void WhenGet()
        {
            //Arrange
            var serviceCollections = new ServiceCollection();
            var roundName          = AutoFixture.Create <string>();
            var score = AutoFixture.Create <int>();
            var queryStringParameters = new Dictionary <string, string>();

            queryStringParameters.Add("Score", score.ToString());
            queryStringParameters.Add("RoundName", roundName);

            var request       = AutoFixture.Build <APIGatewayProxyRequest>().With(rq => rq.QueryStringParameters, queryStringParameters).Create();
            var proxyResponse = AutoFixture.Create <APIGatewayProxyResponse>();
            var getClassificationFromScoreMock = new Mock <IGetClassificationFromScore>();

            getClassificationFromScoreMock.Setup(sp => sp.GetClassification(score, roundName)).Returns(proxyResponse);
            serviceCollections.AddScoped(provider => getClassificationFromScoreMock.Object);
            var serviceProvider = serviceCollections.BuildServiceProvider();

            var subject = new ScoreClassificationService(serviceProvider);
            //Act
            var result = subject.GetClassification(request);

            //Assert
            result.Should().Be(proxyResponse);
        }
        public void CreateFilter_WhenIRestClientExecuteReturnsResponseWithNullData_ReturnsNull()
        {
            // arrange
            var resourceEnum = AutoFixture.Create <StackExchangeResourceEnum>();
            var parameters   = AutoFixture.CreateMany <object>().ToList();
            var data         = AutoFixture.Create <Dictionary <string, object> >();

            var client       = new Mock <IRestClient>();
            var restResponse = AutoFixture.Build <RestResponse <ItemResponseDto <QuestionDto> > >()
                               .Without(x => x.Data)
                               .Create();

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

            // act
            var requestHandler = AutoFixture.Create <StackExchangeRequestHandler>();
            var response       = requestHandler.Execute <QuestionDto>(resourceEnum, parameters, data);

            // assert
            response.Should().BeNull();
        }
        public void CreateFilter_WhenIRestClientExecuteReturnsValidResponse_ReturnsResponseData()
        {
            // arrange
            var resourceEnum = AutoFixture.Create <StackExchangeResourceEnum>();
            var parameters   = AutoFixture.CreateMany <object>().ToList();
            var data         = AutoFixture.Create <Dictionary <string, object> >();

            var client       = new Mock <IRestClient>();
            var responseData = AutoFixture.Build <ItemResponseDto <QuestionDto> >()
                               .With(x => x.QuotaRemaining, RandomGenerator.Next(1, 100))
                               .Create();
            var restResponse = AutoFixture.Build <RestResponse <ItemResponseDto <QuestionDto> > >()
                               .With(x => x.Data, responseData)
                               .Create();

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

            // act
            var requestHandler = AutoFixture.Create <StackExchangeRequestHandler>();
            var response       = requestHandler.Execute <QuestionDto>(resourceEnum, parameters, data);

            // assert
            response.Should().BeEquivalentTo(responseData.Items);
        }
Ejemplo n.º 8
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 async Task can_load_aggregate()
        {
            var aggregate = new Reviews.Domain.Review();

            aggregate.Apple(AutoFixture.Build <Domain.Events.V1.ReviewCreated>().With(e => e.Id, AggregateId).Create());
            aggregate.Apple(AutoFixture.Build <Domain.Events.V1.ReviewApproved>().With(e => e.Id, AggregateId).Create());


            var sut = new GesAggrigateStore(Connection, Serializer, EventTypeMapper, (a, b) => $"{a}-{b}", null);

            var saveResult = await sut.Save(aggregate);

            var result = await sut.Load <Domain.Review>(AggregateId.ToString());

            result.Id.Should().Be(AggregateId);
        }
        public void CalculateScore_GivenCorrectAnswer_Returns100()
        {
            // arrange
            var correctAnswer = AutoFixture.Create <int>();
            var attempt       = AutoFixture.Build <Attempt>()
                                .With(x => x.AnswerId, correctAnswer)
                                .With(x => x.AcceptedAnswerId, correctAnswer)
                                .Create();

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

            //  assert
            response.Should().Be(100);
        }
Ejemplo n.º 11
0
        public WhenApprovingCohortFixture AddDraftApprenticeship(bool isIncompleteForEmployer = false, bool isIncompleteForProvider = false)
        {
            var draftApprenticeshipDetailsComposer = AutoFixture.Build <DraftApprenticeshipDetails>().WithAutoProperties();

            if (isIncompleteForEmployer)
            {
                draftApprenticeshipDetailsComposer = draftApprenticeshipDetailsComposer.Without(d => d.FirstName);
            }

            if (isIncompleteForProvider)
            {
                draftApprenticeshipDetailsComposer = draftApprenticeshipDetailsComposer.Without(d => d.Uln);
            }

            ApprenticeshipBase apprenticeship = new DraftApprenticeship(draftApprenticeshipDetailsComposer.Create(), Party.Provider);

            Cohort.Add(c => c.Apprenticeships, apprenticeship);
            return(this);
        }
        public void GetQuestionDtoById_Always_ReturnsQuestionsWhereAnswerCountGreaterThan1AndAcceptedAnswerIdHasValue()
        {
            // arrange
            var questionId           = AutoFixture.Create <int>();
            var matchingQuestionDtos = AutoFixture.Build <QuestionDto>()
                                       .With(x => x.QuestionId, questionId)
                                       .Create();
            var nonMatchingQuestionDtos = AutoFixture.CreateMany <QuestionDto>()
                                          .ToList();

            nonMatchingQuestionDtos.Insert(RandomGenerator.Next(nonMatchingQuestionDtos.Count), matchingQuestionDtos);

            // act
            var filter   = AutoFixture.Create <QuestionFilter>();
            var response = filter.GetQuestionDtoById(nonMatchingQuestionDtos, questionId);

            // assert
            response.Should().Be(matchingQuestionDtos);
        }
        public ProcessFullyApprovedCohortCommandFixture SetApprovedApprenticeships(bool isFundedByTransfer)
        {
            var provider = new Provider {
                Name = "Test Provider"
            };
            var account            = new Account(1, "", "", "", DateTime.UtcNow);
            var accountLegalEntity = new AccountLegalEntity(account, 1, 1, "", "", "Test Employer", OrganisationType.Charities, "", DateTime.UtcNow);

            AutoFixture.Inject(account);

            var cohortBuilder = AutoFixture.Build <Cohort>()
                                .Without(c => c.Apprenticeships)
                                .With(c => c.AccountLegalEntity, accountLegalEntity)
                                .With(c => c.Provider, provider)
                                .With(x => x.IsDeleted, false);

            if (!isFundedByTransfer)
            {
                cohortBuilder.Without(c => c.TransferSenderId).Without(c => c.TransferApprovalActionedOn);
            }

            var apprenticeshipBuilder = AutoFixture.Build <Apprenticeship>().Without(a => a.DataLockStatus).Without(a => a.EpaOrg).Without(a => a.ApprenticeshipUpdate).Without(a => a.Continuation).Without(a => a.PreviousApprenticeship);
            var cohort1 = cohortBuilder.With(c => c.Id, Command.CohortId).Create();
            var cohort2 = cohortBuilder.Create();

            var apprenticeship1 = apprenticeshipBuilder.With(a => a.Cohort, cohort1).Create();
            var apprenticeship2 = apprenticeshipBuilder.With(a => a.Cohort, cohort1).Create();
            var apprenticeship3 = apprenticeshipBuilder.With(a => a.Cohort, cohort2).Create();

            var apprenticeships1 = new[] { apprenticeship1, apprenticeship2 };
            var apprenticeships2 = new[] { apprenticeship1, apprenticeship2, apprenticeship3 };

            Apprenticeships.AddRange(apprenticeships1);
            Db.Object.AccountLegalEntities.Add(accountLegalEntity);
            Db.Object.Providers.Add(provider);
            Db.Object.Apprenticeships.AddRange(apprenticeships2);

            Db.Object.SaveChanges();

            return(this);
        }
        public void WhenHashAndVerify()
        {
            // Arrange
            var password = AutoFixture.Create <string>();
            var security = AutoFixture.Build <SecurityConfig>()
                           .With(sec => sec.SaltSize, AutoFixture.Create <int>() + 8)
                           .Create();

            Mocker.GetMock <IOptions <SecurityConfig> >()
            .Setup(cfg => cfg.Value)
            .Returns(security);

            var subject = Mocker.CreateInstance <PasswordManager>();

            // Act
            var result = subject.Generate(password);

            // Assert
            result.Should().NotBe(password);
            subject.Verify(result.PasswordHash, result.HashSalt, password).Should().BeTrue();
        }
        public void FilterQuestions_Always_ReturnsQuestionsWhereAnswerCountGreaterThan1AndAcceptedAnswerIdHasValue()
        {
            // arrange
            var matchingQuestionDtos = AutoFixture.Build <QuestionDto>()
                                       .With(x => x.AnswerCount, RandomGenerator.Next(2, int.MaxValue))
                                       .With(x => x.AcceptedAnswerId)
                                       .CreateMany()
                                       .ToList();
            var nonMatchingQuestionDtos = AutoFixture.Build <QuestionDto>()
                                          .With(x => x.AnswerCount, RandomGenerator.Next(0, 1))
                                          .Without(x => x.AcceptedAnswerId)
                                          .CreateMany()
                                          .ToList();

            nonMatchingQuestionDtos.InsertRange(RandomGenerator.Next(nonMatchingQuestionDtos.Count), matchingQuestionDtos);

            // act
            var filter   = AutoFixture.Create <QuestionFilter>();
            var response = filter.FilterQuestions(nonMatchingQuestionDtos);

            // assert
            response.Should().BeEquivalentTo(matchingQuestionDtos);
        }
Ejemplo n.º 16
0
 public IEnumerable <T> FakeMany <T>(Func <ICustomizationComposer <T>, IPostprocessComposer <T> > builder)
 {
     return(builder(AutoFixture.Build <T>()).CreateMany());
 }
Ejemplo n.º 17
0
 public T Fake <T>(Func <ICustomizationComposer <T>, IPostprocessComposer <T> > builder)
 {
     return(builder(AutoFixture.Build <T>()).Create());
 }