示例#1
0
        public void ResultModify_WhenCommandApplied_ValidModifiedLineItemAdded()
        {
            var result = new ReferendumResult();
            CreateReferendumResultCommand cmdCreate = DefaultCreateReferendumResultCommand();

            result.Apply(cmdCreate);
            AddReferendumLineItemsCommand cmdLineItem = DefaultAddReferendumLineItemsCommand(2, cmdCreate.ApplyToResult, result.PollingCentre, result.ResultSender);

            result.Apply(cmdLineItem);
            ConfirmReferendumResultsCommand cmdConfirm = DefaultConfirmPresidentalResultsCommand(3, cmdLineItem.ApplyToResult, result.PollingCentre, result.ResultSender);

            result.Apply(cmdConfirm);
            ModifyReferendumResultsCommand cmd = DefaultModifyReferendumResultsCommand(4, cmdConfirm.ApplyToResult, result.PollingCentre, result.ResultSender);

            //act
            result.Apply(cmd);
            //assert
            Assert.That(result.LineItems.Count(), Is.EqualTo(2));
            Assert.That(result.Id, Is.EqualTo(cmd.ApplyToResult.Id));
            Assert.That(result.Status, Is.EqualTo(ResultStatus.Modified));
            ReferendumResultLineItem lineItem = result.LineItems[1];

            Assert.That(lineItem.Candidate, Is.EqualTo(cmd.ResultDetail[0].Candidate));
            Assert.That(lineItem.ResultCount, Is.EqualTo(cmd.ResultDetail[0].Result));
        }
示例#2
0
 public void Save(ReferendumResult result)
 {
     using (var ctx = new VtsContext(_contextConnection.VtsConnectionString))
     {
         ctx.UpdateGraph(result, map => map.OwnedCollection(n => n.LineItems));
         ctx.SaveChanges();
     }
 }
示例#3
0
        public ReferendumResult GetByPollingCentre(PollingCentreRef pollingCentre)
        {
            using (var ctx = new VtsContext(_contextConnection.VtsConnectionString))
            {
                ReferendumResult results = CtxSetup(ctx.ReferendumResults)
                                           .FirstOrDefault(n => n.PollingCentre.Id == pollingCentre.Id);

                return(results);
            }
        }
示例#4
0
        public ReferendumResult GetById(Guid id)
        {
            using (var ctx = new VtsContext(_contextConnection.VtsConnectionString))
            {
                ReferendumResult results = CtxSetup(ctx.ReferendumResults)
                                           .FirstOrDefault(n => n.Id == id);

                return(results);
            }
        }
示例#5
0
        public void ResultConfirm_WhenCommandApplied_ResultStatusConfirmed()
        {
            var result = new ReferendumResult();
            CreateReferendumResultCommand cmdCreate = DefaultCreateReferendumResultCommand();

            result.Apply(cmdCreate);
            AddReferendumLineItemsCommand cmdLineItem = DefaultAddReferendumLineItemsCommand(2, cmdCreate.ApplyToResult, result.PollingCentre, result.ResultSender);

            result.Apply(cmdLineItem);
            ConfirmReferendumResultsCommand cmd = DefaultConfirmPresidentalResultsCommand(3, cmdLineItem.ApplyToResult, result.PollingCentre, result.ResultSender);

            //act
            result.Apply(cmd);
            //assert
            Assert.That(result.Status, Is.EqualTo(ResultStatus.Confirmed));
        }
示例#6
0
        public void ResultCreation_WhenCommandApplied_ValidNewResult()
        {
            var result = new ReferendumResult();
            CreateReferendumResultCommand cmd = DefaultCreateReferendumResultCommand();

            //act
            result.Apply(cmd);
            //assert
            Assert.That(result.Id, Is.Not.EqualTo(Guid.Empty));
            Assert.That(result.Id, Is.EqualTo(cmd.ApplyToResult.Id));
            Assert.That(result.Status, Is.EqualTo(ResultStatus.New));
            Assert.That(result.ResultReference, Is.EqualTo(cmd.ResultReference));
            Assert.That(result.PollingCentre, Is.EqualTo(cmd.OriginatingPollingCentre));
            Assert.That(result.ResultSender, Is.EqualTo(cmd.CommandGeneratedByUser));
            Assert.That(result.LineItems.Count(), Is.EqualTo(0));
        }
示例#7
0
        public void ResultAddLineItem_WhenCommandApplied_ValidLineItemAdded()
        {
            var result = new ReferendumResult();
            CreateReferendumResultCommand cmdCreate = DefaultCreateReferendumResultCommand();

            result.Apply(cmdCreate);
            AddReferendumLineItemsCommand cmd = DefaultAddReferendumLineItemsCommand(2, cmdCreate.ApplyToResult, result.PollingCentre, result.ResultSender);

            //act
            result.Apply(cmd);
            //assert
            Assert.That(result.LineItems.Count(), Is.EqualTo(1));
            Assert.That(result.Id, Is.EqualTo(cmd.ApplyToResult.Id));
            Assert.That(result.Status, Is.EqualTo(ResultStatus.New));
            ReferendumResultLineItem lineItem = result.LineItems[0];

            Assert.That(lineItem.Candidate, Is.EqualTo(cmd.ResultDetail[0].Candidate));
            Assert.That(lineItem.ResultCount, Is.EqualTo(cmd.ResultDetail[0].Result));
        }
示例#8
0
        public ReferendumResult Confirm(ReferendumResult result, ResultInfo originatingInfo)
        {
            CommandInfo commandInfo = new CommandInfo
            {
                CommandGeneratedByUser   = originatingInfo.CommandGeneratedByUser,
                OriginatingPollingCentre = originatingInfo.OriginatingPollingCentre,
            };
            ConfirmReferendumResultsCommand confirmReferendumResultsCommand = new ConfirmReferendumResultsCommand
            {
                CommandId = Guid.NewGuid(),
                CommandGeneratedByUser   = commandInfo.CommandGeneratedByUser,
                OriginatingPollingCentre = commandInfo.OriginatingPollingCentre,
                ApplyToResult            = result.GetResultRef(),
                CommandExecutionOrder    = result.LastResultCommandExecutedOrder + 1,
            };

            result.Apply(confirmReferendumResultsCommand);
            return(result);
        }
示例#9
0
        public ReferendumResult Modify(ReferendumResult result, ResultInfo originatingInfo,
                                       List <ResultDetail> resultDetails)
        {
            CommandInfo commandInfo = new CommandInfo
            {
                CommandGeneratedByUser   = originatingInfo.CommandGeneratedByUser,
                OriginatingPollingCentre = originatingInfo.OriginatingPollingCentre,
            };

            var command = new ModifyReferendumResultsCommand
            {
                CommandId = Guid.NewGuid(),
                CommandGeneratedByUser   = commandInfo.CommandGeneratedByUser,
                OriginatingPollingCentre = commandInfo.OriginatingPollingCentre,
                ApplyToResult            = result.GetResultRef(),
                CommandExecutionOrder    = result.LastResultCommandExecutedOrder + 1,
                ResultDetail             = resultDetails,
            };

            result.Apply(command);
            return(result);
        }
示例#10
0
        public ReferendumResult Create(ResultInfo originatingInfo, string documentReference)
        {
            CommandInfo commandInfo = new CommandInfo
            {
                CommandGeneratedByUser   = originatingInfo.CommandGeneratedByUser,
                OriginatingPollingCentre = originatingInfo.OriginatingPollingCentre,
                ResultReference          = documentReference
            };
            CreateReferendumResultCommand createReferendumResultCommand = new CreateReferendumResultCommand
            {
                CommandId = Guid.NewGuid(),
                CommandGeneratedByUser   = commandInfo.CommandGeneratedByUser,
                OriginatingPollingCentre = commandInfo.OriginatingPollingCentre,
                ApplyToResult            = new ResultRef(Guid.NewGuid(), ResultType.Referendum),
                ResultReference          = commandInfo.ResultReference,
                ResultDate            = DateTime.Now,
                CommandExecutionOrder = 1
            };
            var result = new ReferendumResult();

            result.Apply(createReferendumResultCommand);
            return(result);
        }
        public Party GetLikeliestParty(Party previousVote, AgeGroup age, Gender gender, ReferendumResult referendum, Region region, SocialGrade socialGrade)
        {
            var likeliestParty = Party.None;
            var highestScore   = 0d;

            foreach (var party in Enum.GetValues(typeof(Party)).Cast <Party>())
            {
                //if (party == Party.SNP && region != Region.Scotland)
                //{
                //    continue;
                //}

                //var isRegionalParty = (region == Region.Scotland && party == Party.SNP) || (region == Region.MidlandsWales && party == Party.PlaidCymru);
                //var regionalMultiplier = isRegionalParty ? 2 : 1;
                //var bonusPreviousParty = previousVote == party ? 2 : 1;

                var score = AgeProbabilities[age].GetVotingLikelihood(party) +
                            GenderProbabilities[gender].GetVotingLikelihood(party) +
                            ReferendumProbabilities[referendum].GetVotingLikelihood(party) +
                            (RegionProbabilities[region].GetVotingLikelihood(party)) +
                            SocialGradeProbabilities[socialGrade].GetVotingLikelihood(party);

                if (previousVote == Party.UKIP && party == Party.UKIP && referendum == ReferendumResult.Leave)
                {
                    var x = true;
                }

                if (PreviousElectionProbabilities.ContainsKey(previousVote))
                {
                    score += PreviousElectionProbabilities[previousVote].GetVotingLikelihood(party);
                    score /= 6;
                }
                else
                {
                    score /= 5;
                }

                if (previousVote == party || region == Region.Scotland || true)
                {
                    score *= 5;

                    Random random = new Random();
                    var    rand   = random.Next(100);
                    if (region == Region.Scotland && rand > 90 && previousVote.IsUnionist() && party.IsUnionist())
                    {
                        score *= 100;
                    }
                    else if (rand > 80 && previousVote == party)// || ())
                    {
                        score *= 100;
                    }
                    else if (rand > 40)
                    {
                        //if (rand > 60 && region == Region.Scotland)
                        //{
                        //    score += RegionProbabilities[region].GetVotingLikelihood(party) * 80;
                        //}
                        //else if (region != Region.Scotland)
                        {
                            if (rand > 68)
                            {
                                score += AgeProbabilities[age].GetVotingLikelihood(party) * 80;
                            }
                            else if (rand > 62)
                            {
                                score += GenderProbabilities[gender].GetVotingLikelihood(party) * 80;
                            }
                            else if (rand > 52)
                            {
                                score += SocialGradeProbabilities[socialGrade].GetVotingLikelihood(party) * 80;
                            }
                            else if (region != Region.Scotland)
                            {
                                score += RegionProbabilities[region].GetVotingLikelihood(party) * 80;
                            }
                        }
                    }
                }

                if (score > highestScore)
                {
                    likeliestParty = party;
                    highestScore   = score;
                }
            }

            if (likeliestParty == Party.None)
            {
                throw new Exception("Couldnt find likeliest party");
            }

            return(likeliestParty);
        }