Ejemplo n.º 1
0
    public static async Task SeedSampleData(SurveyContext context)
    {
        var testOwner = new User(NonEmptyString.Create("Test User"), NonEmptyString.Create("*****@*****.**"), NonEmptyString.Create("test-id"));

        var survey1 = new Survey(testOwner, NonEmptyString.Create("Test Topic 1"), 10, NonEmptyString.Create("Testers"));
        var survey2 = new Survey(testOwner, NonEmptyString.Create("Test Topic 2"), 20, NonEmptyString.Create("More Testers"));
        var survey3 = new Survey(testOwner, NonEmptyString.Create("Test Topic 3"), 30, NonEmptyString.Create("Even More Testers"));

        survey1.AddSurveyOption(NonEmptyString.Create("Test Option 1"));

        survey2.AddSurveyOption(NonEmptyString.Create("Test Option 2"));
        survey2.AddSurveyOption(NonEmptyString.Create("Test Option 3"));

        survey3.AddSurveyOption(NonEmptyString.Create("Test Option 4"));
        survey3.AddSurveyOption(NonEmptyString.Create("Test Option 5"));
        survey3.AddSurveyOption(NonEmptyString.Create("Test Option 6"));

        await context.Users.AddAsync(testOwner);

        await context.Surveys.AddRangeAsync(new List <Survey> {
            survey1, survey2, survey3
        });

        await context.SaveChangesAsync();
    }
        public async Task <SurveyModel> Handle(CreateSurveyCommand request, CancellationToken cancellationToken)
        {
            var survey = new Survey(request.SurveyTopic, request.NumberOfRespondents, request.RespondentType);

            foreach (var option in request.SurveyOptions)
            {
                if (option.PreferredNumberOfVotes.HasValue)
                {
                    survey.AddSurveyOption(option.OptionText, option.PreferredNumberOfVotes.Value);
                }
                else
                {
                    survey.AddSurveyOption(option.OptionText);
                }
            }

            IVoteDistribution voteDistribution;

            if (request.SurveyOptions.Any(option => option.PreferredNumberOfVotes > 0))
            {
                voteDistribution = new FixedVoteDistribution();
            }
            else
            {
                voteDistribution = new RandomVoteDistribution();
            }

            var result = survey.CalculateOutcome(voteDistribution);

            _surveyRepository.Add(result);

            await _surveyRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            return(_mapper.Map <SurveyModel>(result));
        }
Ejemplo n.º 3
0
        private async Task CreateSurvey(CancellationToken stoppingToken)
        {
            using var scope = _serviceScopeFactory.CreateScope();

            var surveyRepository = scope.ServiceProvider.GetService <ISurveyRepository>();

            var survey = new Survey("Test Topic", 100, "Testers");

            survey.AddSurveyOption("Option 1");
            survey.AddSurveyOption("Option 2");
            survey.AddSurveyOption("Option 3");
            survey.AddSurveyOption("Option 4");

            IVoteDistribution voteDistribution = new RandomVoteDistribution();

            var result = survey.CalculateOutcome(voteDistribution);

            surveyRepository.Add(result);

            await surveyRepository.UnitOfWork.SaveChangesAsync(stoppingToken);

            var winningOption = result.Options.OrderByDescending(option => option.NumberOfVotes).First();

            _logger.LogInformation($"Added Survey with topic: {result.Topic} asking: {result.NumberOfRespondents} {result.RespondentType}. Winning option: {winningOption.OptionText} with: {winningOption.NumberOfVotes} votes");
        }
Ejemplo n.º 4
0
        public void Should_Be_Able_To_Calculate_Results_Of_Survey_With_One_Sided_Outcome()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1000;
            const string respondentType      = "Developers";

            var survey = new Survey(null, NonEmptyString.Create(topic), numberOfRespondents, NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"));
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"));

            survey.CalculateOneSidedOutcome();

            Assert.Equal(numberOfRespondents, survey.Options.Max(option => option.NumberOfVotes));
        }
Ejemplo n.º 5
0
        GivenSurveyWithOptionsWhereCombinedPreferredNumberOfVotesExceedTotalRespondents_WhenAddingOptionToSurvey_ThenSurveyDomainExceptionShouldBeThrown()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1000;
            const string respondentType      = "Developers";

            var survey = new Survey(_testUser, NonEmptyString.Create(topic), numberOfRespondents,
                                    NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"), 500);

            Action act = () => { survey.AddSurveyOption(NonEmptyString.Create("Spaces"), 501); };

            act.Should().ThrowExactly <SurveyDomainException>();
        }
Ejemplo n.º 6
0
        public void Should_Not_Be_Able_To_Add_Preferred_Number_Of_Votes_If_Total_Exceeds_Respondents()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1000;
            const string respondentType      = "Developers";

            var survey = new Survey(null, NonEmptyString.Create(topic), numberOfRespondents, NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"), 500);

            Assert.Throws <SurveyDomainException>(() =>
            {
                survey.AddSurveyOption(NonEmptyString.Create("Spaces"), 501);
            });
        }
Ejemplo n.º 7
0
        public void Should_Be_Able_To_Add_Options_To_Survey()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1;
            const string respondentType      = "Developers";

            var survey = new Survey(null, NonEmptyString.Create(topic), numberOfRespondents, NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"));
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"));

            Assert.Equal(2, survey.Options.Count);
            Assert.Equal("Tabs", survey.Options.First().OptionText);
            Assert.Equal("Spaces", survey.Options.Last().OptionText);
        }
Ejemplo n.º 8
0
        public void Should_Be_Able_To_Add_Options_To_Survey()
        {
            var topic = "Tabs or spaces?";
            var numberOfRespondents = 1;
            var respondentType      = "Developers";

            var survey = new Survey(topic, numberOfRespondents, respondentType);

            survey.AddSurveyOption("Tabs");
            survey.AddSurveyOption("Spaces");

            Assert.Equal(2, survey.Options.Count);
            Assert.Equal("Tabs", survey.Options.First().OptionText);
            Assert.Equal("Spaces", survey.Options.Last().OptionText);
        }
Ejemplo n.º 9
0
        public void Should_Not_Be_Able_To_Add_Preferred_Number_Of_Votes_If_Total_Exceeds_Respondents()
        {
            var topic = "Tabs or spaces?";
            var numberOfRespondents = 1000;
            var respondentType      = "Developers";

            var survey = new Survey(topic, numberOfRespondents, respondentType);

            survey.AddSurveyOption("Tabs", 500);

            Assert.Throws <SurveyDomainException>(() =>
            {
                survey.AddSurveyOption("Spaces", 501);
            });
        }
Ejemplo n.º 10
0
        public void GivenValidOption_WhenAddingOptionToSurvey_ShouldAddOptionToSurveyOptionsList()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1;
            const string respondentType      = "Developers";

            var survey = new Survey(_testUser, NonEmptyString.Create(topic), numberOfRespondents,
                                    NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"));
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"));

            survey.Options.Count.Should().Be(2);
            survey.Options.First().OptionText.Value.Should().Be("Tabs");
            survey.Options.Last().OptionText.Value.Should().Be("Spaces");
        }
Ejemplo n.º 11
0
        public void GivenSurveyWithOptions_WhenCalculatingOneSidedOutcome_ThenOneOptionShouldReceiveAllVotes()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1000;
            const string respondentType      = "Developers";

            var survey = new Survey(_testUser, NonEmptyString.Create(topic), numberOfRespondents,
                                    NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"));
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"));

            survey.CalculateOneSidedOutcome();

            survey.Options.Max(option => option.NumberOfVotes).Should().Be(numberOfRespondents);
        }
Ejemplo n.º 12
0
        public void Creating_Survey_Should_Add_SurveyCreated_Event_To_DomainEvents()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1;
            const string respondentType      = "Developers";

            var survey = new Survey(null, NonEmptyString.Create(topic), numberOfRespondents, NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"));
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"));


            var surveyCreatedEvent = survey.DomainEvents.First() as SurveyCreatedDomainEvent;

            Assert.True(surveyCreatedEvent?.Survey == survey);
        }
Ejemplo n.º 13
0
        public void Should_Be_Able_To_Calculate_Results_Of_Survey_With_Fixed_Outcome()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1000;
            const string respondentType      = "Developers";

            var survey = new Survey(null, NonEmptyString.Create(topic), numberOfRespondents, NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"), 600);
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"), 400);

            survey.CalculateOutcome();

            Assert.Equal(600, survey.Options.First().NumberOfVotes);
            Assert.Equal(400, survey.Options.Last().NumberOfVotes);
        }
Ejemplo n.º 14
0
        public void GivenSurveyWithOptions_WhenCalculatingOutcome_ThenOptionNumberOfVotesShouldBeDistributedEvenly()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1000;
            const string respondentType      = "Developers";

            var survey = new Survey(_testUser, NonEmptyString.Create(topic), numberOfRespondents,
                                    NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"));
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"));

            survey.CalculateOutcome();

            survey.Options.Sum(option => option.NumberOfVotes).Should().Be(numberOfRespondents);
            survey.Options.All(option => option.NumberOfVotes > 0).Should().BeTrue();
        }
Ejemplo n.º 15
0
        GivenSurveyWithOptionsHavingPreferredNumberOfVotes_WhenCalculatingOutcome_ThenEachOptionShouldHaveExpectedPreferredNumberOfVotes()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1000;
            const string respondentType      = "Developers";

            var survey = new Survey(_testUser, NonEmptyString.Create(topic), numberOfRespondents,
                                    NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"), 600);
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"), 400);

            survey.CalculateOutcome();

            survey.Options.First().NumberOfVotes.Should().Be(600);
            survey.Options.Last().NumberOfVotes.Should().Be(400);
        }
Ejemplo n.º 16
0
        public void Should_Be_Able_To_Calculate_Results_Of_Survey_With_One_Sided_Outcome()
        {
            var topic = "Tabs or spaces?";
            var numberOfRespondents = 1000;
            var respondentType      = "Developers";

            var survey = new Survey(topic, numberOfRespondents, respondentType);

            survey.AddSurveyOption("Tabs");
            survey.AddSurveyOption("Spaces");

            var voteDistributionStrategy = new OneSidedVoteDistribution();

            var result = survey.CalculateOutcome(voteDistributionStrategy);

            Assert.Equal(numberOfRespondents, result.Options.Max(option => option.NumberOfVotes));
        }
Ejemplo n.º 17
0
        public void Should_Be_Able_To_Calculate_Results_Of_Survey_With_Fixed_Outcome()
        {
            var topic = "Tabs or spaces?";
            var numberOfRespondents = 1000;
            var respondentType      = "Developers";

            var survey = new Survey(topic, numberOfRespondents, respondentType);

            survey.AddSurveyOption("Tabs", 600);
            survey.AddSurveyOption("Spaces", 400);

            var voteDistribution = new FixedVoteDistribution();

            var result = survey.CalculateOutcome(voteDistribution);

            Assert.Equal(600, result.Options.First().NumberOfVotes);
            Assert.Equal(400, result.Options.Last().NumberOfVotes);
        }
Ejemplo n.º 18
0
        GivenValidSurveyDetail_WhenCreatingSurvey_ThenValidSurveyIsCreatedWithSurveyCreatedEventAddedToDomainEvents()
        {
            const string topic = "Tabs or spaces?";
            const int    numberOfRespondents = 1;
            const string respondentType      = "Developers";

            var survey = new Survey(_testUser, NonEmptyString.Create(topic), numberOfRespondents,
                                    NonEmptyString.Create(respondentType));

            survey.AddSurveyOption(NonEmptyString.Create("Tabs"));
            survey.AddSurveyOption(NonEmptyString.Create("Spaces"));

            survey.DomainEvents.Should().AllBeOfType <SurveyCreatedDomainEvent>();

            var surveyCreatedEvent = (SurveyCreatedDomainEvent)survey.DomainEvents.First();

            surveyCreatedEvent.Survey.Should().Be(survey);
        }
Ejemplo n.º 19
0
        public void Should_Not_Be_Able_To_Add_Empty_Options_To_Survey()
        {
            var topic = "To be, or not to be?";
            var numberOfRespondents = 2;
            var respondentType      = "Writers";

            Assert.Throws <SurveyDomainException>(() =>
            {
                var survey = new Survey(topic, numberOfRespondents, respondentType);

                survey.AddSurveyOption("");
            });
        }
Ejemplo n.º 20
0
        public void Should_Not_Be_Able_To_Add_Empty_Options_To_Survey()
        {
            const string topic = "To be, or not to be?";
            const int    numberOfRespondents = 2;
            const string respondentType      = "Writers";

            Assert.ThrowsAny <Exception>(() =>
            {
                var survey = new Survey(null, NonEmptyString.Create(topic), numberOfRespondents, NonEmptyString.Create(respondentType));

                survey.AddSurveyOption(NonEmptyString.Create(""));
            });
        }
Ejemplo n.º 21
0
        public void GivenEmptyOption_WhenAddingOptionToSurvey_ThenExceptionShouldBeThrown()
        {
            const string topic = "To be, or not to be?";
            const int    numberOfRespondents = 2;
            const string respondentType      = "Writers";

            Action act = () =>
            {
                var survey = new Survey(_testUser, NonEmptyString.Create(topic), numberOfRespondents,
                                        NonEmptyString.Create(respondentType));

                survey.AddSurveyOption(NonEmptyString.Create(""));
            };

            act.Should().Throw <Exception>();
        }