public ImportV1Election(TallyJ2dContext db, ImportFile file, XmlDocument xml,
                         Election election,
                         Location location,
                         Action<Ballot> storeBallot,
                         Action<Vote> storeVote,
                         List<Person> people, Action<Person> addPerson,
                         Action<ResultSummary> storeResultSummary,
                         ILogHelper logHelper
   )
   : base(db, file, xml, people, addPerson, logHelper)
 {
   _election = election;
   _location = location;
   _storeBallot = storeBallot;
   _storeVote = storeVote;
   _storeResultSummary = storeResultSummary;
 }
    public void SingleNameElection_1_person()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
                       {
                         ElectionGuid = electionGuid,
                         NumberToElect = 1,
                         NumberExtra = 0
                       };
      var location = new Location
                       {
                         LocationGuid = Guid.NewGuid(),
                         ElectionGuid = electionGuid
                       };
      var personGuid = Guid.NewGuid();
      var ballotGuid = Guid.NewGuid();
      var ballots = new List<Ballot>
                      {
                        new Ballot
                          {LocationGuid = location.LocationGuid, BallotGuid = ballotGuid, StatusCode = BallotStatusEnum.Ok}
                      };

      var votes = new List<vVoteInfo>
                    {
                      new vVoteInfo {SingleNameElectionCount = 33},
                      new vVoteInfo {SingleNameElectionCount = 5},
                      new vVoteInfo {SingleNameElectionCount = 2},
                    };
      foreach (var vVoteInfo in votes)
      {
        vVoteInfo.BallotGuid = ballotGuid;
        vVoteInfo.PersonGuid = personGuid; // all for one person in this test
        vVoteInfo.ElectionGuid = electionGuid;
        vVoteInfo.PersonCombinedInfo = vVoteInfo.PersonCombinedInfoInVote = "zz";
        vVoteInfo.BallotStatusCode = BallotStatusEnum.Ok;
        vVoteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
      }

      var model = new ElectionAnalyzerSingleName(_fakes, election, votes, ballots, SamplePeople);

      model.GenerateResults();

      var results = model.Results;

      results.Count.ShouldEqual(1);

      var result1 = results[0];
      result1.VoteCount.ShouldEqual(33 + 5 + 2);
      result1.Rank.ShouldEqual(1);
      result1.Section.ShouldEqual(ResultHelper.Section.Top);

      result1.CloseToNext.ShouldEqual(false);
      result1.CloseToPrev.ShouldEqual(false);
      result1.ForceShowInOther.ShouldEqual(false);
      result1.IsTieResolved.ShouldEqual(null);
      result1.IsTied.ShouldEqual(false);
      result1.RankInExtra.ShouldEqual(null);
      result1.TieBreakCount.ShouldEqual(null);
      result1.TieBreakGroup.ShouldEqual(null);
      result1.TieBreakRequired.ShouldEqual(false);
    }
    public void SingleNameElection_3_people_with_3_way_Tie()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
                       {
                         ElectionGuid = electionGuid,
                         NumberToElect = 1,
                         NumberExtra = 0
                       };
      var location = new Location
      {
        LocationGuid = Guid.NewGuid(),
        ElectionGuid = electionGuid
      };
      var ballotGuid = Guid.NewGuid();
      var ballots = new List<Ballot>
                      {
                        new Ballot
                          {LocationGuid = location.LocationGuid, BallotGuid = ballotGuid, StatusCode = BallotStatusEnum.Ok}
                      };
      var votes = new List<vVoteInfo>
                    {
                      new vVoteInfo {SingleNameElectionCount = 10, PersonGuid = Guid.NewGuid()},
                      new vVoteInfo {SingleNameElectionCount = 10, PersonGuid = Guid.NewGuid()},
                      new vVoteInfo {SingleNameElectionCount = 10, PersonGuid = Guid.NewGuid()},
                    };
      foreach (var vVoteInfo in votes)
      {
        vVoteInfo.BallotGuid = ballotGuid;
        vVoteInfo.ElectionGuid = electionGuid;
        vVoteInfo.PersonCombinedInfo = vVoteInfo.PersonCombinedInfoInVote = "zz";
        vVoteInfo.BallotStatusCode = BallotStatusEnum.Ok;
        vVoteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
      }

      var model = new ElectionAnalyzerSingleName(_fakes, election, votes, ballots, SamplePeople);

      model.GenerateResults();

      var results = model.Results.OrderByDescending(r => r.VoteCount).ToList();

      results.Count.ShouldEqual(3);

      var result1 = results[0];
      result1.VoteCount.ShouldEqual(10);
      result1.Rank.ShouldEqual(1);
      result1.Section.ShouldEqual(ResultHelper.Section.Top);
      result1.IsTied.ShouldEqual(true);
      result1.TieBreakGroup.ShouldEqual(1);

      var result2 = results[1];
      result2.VoteCount.ShouldEqual(10);
      result2.Rank.ShouldEqual(2);
      result2.Section.ShouldEqual(ResultHelper.Section.Other);
      result2.IsTied.ShouldEqual(true);
      result2.TieBreakGroup.ShouldEqual(1);

      var result3 = results[2];
      result3.VoteCount.ShouldEqual(10);
      result3.Rank.ShouldEqual(3);
      result3.Section.ShouldEqual(ResultHelper.Section.Other);
      result3.IsTied.ShouldEqual(true);
      result3.TieBreakGroup.ShouldEqual(1);
    }
    public void Invalid_People_Do_Not_Affect_Results()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
      {
        ElectionGuid = electionGuid,
        NumberToElect = 1,
        NumberExtra = 0
      };
      var location = new Location
      {
        LocationGuid = Guid.NewGuid(),
        ElectionGuid = electionGuid
      };
      var ballots = new List<Ballot>
                      {
                        new Ballot
                          {LocationGuid = location.LocationGuid, BallotGuid = Guid.NewGuid(), StatusCode = BallotStatusEnum.Ok},
                        new Ballot
                          {LocationGuid = location.LocationGuid, BallotGuid = Guid.NewGuid(), StatusCode = BallotStatusEnum.Ok},
                      };

      var voteInfos = new List<vVoteInfo>
                    {
                      new vVoteInfo {SingleNameElectionCount = 33, BallotGuid = ballots[0].BallotGuid},
                      new vVoteInfo {SingleNameElectionCount = 5, BallotGuid = ballots[0].BallotGuid},
                      new vVoteInfo {SingleNameElectionCount = 5, BallotGuid = ballots[0].BallotGuid},
                      new vVoteInfo {SingleNameElectionCount = 5, BallotGuid = ballots[0].BallotGuid},
                      new vVoteInfo {SingleNameElectionCount = 27, BallotGuid = ballots[1].BallotGuid},
                      new vVoteInfo {SingleNameElectionCount = 27, BallotGuid = ballots[1].BallotGuid},// spoiled
                      new vVoteInfo {SingleNameElectionCount = 27, BallotGuid = ballots[1].BallotGuid},// spoiled
                    };
      var rowId = 1;
      foreach (var voteInfo in voteInfos)
      {
        voteInfo.VoteId = rowId++;
        voteInfo.ElectionGuid = electionGuid;
        voteInfo.PersonCombinedInfo = voteInfo.PersonCombinedInfoInVote = "zz";
        voteInfo.BallotStatusCode = BallotStatusEnum.Ok;
        voteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
        voteInfo.PersonGuid = Guid.NewGuid();
      }
      voteInfos[3].VoteStatusCode = VoteHelper.VoteStatusCode.Changed;
      voteInfos[4].BallotStatusCode = "TooFew"; // will be reset to Okay
      voteInfos[5].PersonCombinedInfo = "different";
      voteInfos[6].PersonIneligibleReasonGuid = Guid.NewGuid();

      var model = new ElectionAnalyzerSingleName(_fakes, election, voteInfos, ballots, SamplePeople);

      model.GenerateResults();

      var results = model.Results.OrderByDescending(r => r.VoteCount).ToList();
      var resultTies = model.ResultTies.OrderBy(rt => rt.TieBreakGroup).ToList();

      resultTies.Count.ShouldEqual(1);
      resultTies[0].TieBreakRequired.ShouldEqual(false);
      resultTies[0].NumToElect.ShouldEqual(0);
      resultTies[0].NumInTie.ShouldEqual(3);


      var summary = model.ResultSummaryAuto;
      summary.BallotsNeedingReview.ShouldEqual(1);
      summary.TotalVotes.ShouldEqual(33 + 5 + 5 + 5 + 27 + 27 + 27);
      summary.SpoiledBallots.ShouldEqual(0);
      summary.SpoiledVotes.ShouldEqual(27+27);

      results.Count.ShouldEqual(5);

      var result1 = results[0];
      result1.VoteCount.ShouldEqual(33);
      result1.Section.ShouldEqual(ResultHelper.Section.Top);
      result1.IsTied.ShouldEqual(false);
      result1.TieBreakRequired = false;

      var result2 = results[1];
      result2.VoteCount.ShouldEqual(27);
      result2.Section.ShouldEqual(ResultHelper.Section.Other);
      result2.IsTied.ShouldEqual(false);
      result2.TieBreakRequired = false;
      result2.ForceShowInOther = false;

      var result3 = results[2];
      result3.VoteCount.ShouldEqual(5);
      result3.Section.ShouldEqual(ResultHelper.Section.Other);
      result3.IsTied.ShouldEqual(true);
      result3.TieBreakRequired = true;
      result3.ForceShowInOther = false;
    }
    public void Invalid_Ballots_Affect_Results()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
      {
        ElectionGuid = electionGuid,
        NumberToElect = 1,
        NumberExtra = 0
      };
      var location = new Location
      {
        LocationGuid = Guid.NewGuid(),
        ElectionGuid = electionGuid
      };

      var ballot1Guid = Guid.NewGuid();
      var ballot2Guid = Guid.NewGuid();
      var ballots = new List<Ballot>
                      {
                        new Ballot
                          {LocationGuid = location.LocationGuid, BallotGuid = ballot1Guid, StatusCode = BallotStatusEnum.Ok},
                        new Ballot
                          {LocationGuid = location.LocationGuid, BallotGuid = ballot2Guid, StatusCode = BallotStatusEnum.Ok}
                      };

      var voteinfos = new List<vVoteInfo>
                    {
                      // TODO 2012-03-24 Glen Little: Needs attention... these test are for normal elections, not single name...
                      new vVoteInfo {SingleNameElectionCount = 33},
                      new vVoteInfo {SingleNameElectionCount = 5},
                      new vVoteInfo {SingleNameElectionCount = 2},
                      new vVoteInfo {SingleNameElectionCount = 4},
                      new vVoteInfo {SingleNameElectionCount = 27},
                      new vVoteInfo {SingleNameElectionCount = 27},
                      new vVoteInfo {SingleNameElectionCount = 27},
                    };
      var rowId = 1;
      foreach (var voteInfo in voteinfos)
      {
        voteInfo.VoteId = rowId++;
        voteInfo.ElectionGuid = electionGuid;
        voteInfo.PersonCombinedInfo = voteInfo.PersonCombinedInfoInVote = "zz";
        voteInfo.BallotStatusCode = BallotStatusEnum.Ok;
        voteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
        voteInfo.BallotGuid = ballot1Guid;
        voteInfo.PersonGuid = Guid.NewGuid();
      }
      voteinfos[3].VoteStatusCode = VoteHelper.VoteStatusCode.Changed;
      voteinfos[4].BallotStatusCode = "TooFew";
      voteinfos[5].PersonCombinedInfo = "different";
      voteinfos[6].PersonIneligibleReasonGuid = Guid.NewGuid();

      var model = new ElectionAnalyzerSingleName(_fakes, election, voteinfos, ballots, SamplePeople);

      model.GenerateResults();

      var results = model.Results.OrderByDescending(r => r.VoteCount).ToList();

      ballots[0].StatusCode.ShouldEqual(BallotStatusEnum.Ok);
      ballots[1].StatusCode.ShouldEqual(BallotStatusEnum.Ok);

      var summary = model.ResultSummaryAuto;
      summary.TotalVotes.ShouldEqual(125);
      summary.SpoiledBallots.ShouldEqual(0);
      summary.SpoiledVotes.ShouldEqual(54);
      summary.BallotsNeedingReview.ShouldEqual(1);

      results.Count.ShouldEqual(5);
    }
    public void Basic_Import_Test()
    {
      var fakeImportFile = new ImportFile();
      var fakes = new ImportFakes();

      var xmlDoc = new XmlDocument();
      var fakeDataContext = new FakeDataContext();

      xmlDoc.LoadXml(Resources.SampleElection);

      var electionGuid = Guid.NewGuid();
      var election = new Election {ElectionGuid = electionGuid};
      var location = new Location {LocationGuid = Guid.NewGuid()};

      var model = new ImportV1Election(fakeDataContext, fakeImportFile, xmlDoc, election, location,
                                       fakes.AddBallotToDb, fakes.AddVoteToDb,
                                       fakes.People, fakes.AddPersonToDb, fakes.AddResultSummaryToDb, fakes.LogHelper);

      model.Process();

      election.Name.ShouldEqual("(Imported) Sample LSA Election");
      election.DateOfElection.ShouldEqual(new DateTime(2011, 4, 20));
      election.ElectionType.ShouldEqual(ElectionTypeEnum.Lsa);
      election.ElectionMode.ShouldEqual(ElectionModeEnum.Normal);
      election.IsSingleNameElection.ShouldEqual(false);
      election.NumberExtra.ShouldEqual(0);
      election.NumberToElect.ShouldEqual(9);
      election.ShowAsTest.ShouldEqual(true, "Imported elections are marked as Test");
      election.TallyStatus.ShouldEqual(ElectionTallyStatusEnum.Reviewing, "Imported elections set to Review mode");

      fakes.ResultSummaries.Count.ShouldEqual(1);
      var resultSummary = fakes.ResultSummaries[0];
      resultSummary.DroppedOffBallots.ShouldEqual(1);
      resultSummary.MailedInBallots.ShouldEqual(10);
      resultSummary.CalledInBallots.ShouldEqual(0);
      resultSummary.InPersonBallots.ShouldEqual(17);
      resultSummary.NumEligibleToVote.ShouldEqual(51);

      fakes.Ballots.Count.ShouldEqual(28);
      var ballot1 = fakes.Ballots[0];
      ballot1.StatusCode.ShouldEqual(BallotStatusEnum.Ok);

      var votes1 = fakes.Votes.Where(v => v.BallotGuid == ballot1.BallotGuid).ToList();
      votes1.Count.ShouldEqual(9);

      var vote1 = votes1[0];
      vote1.StatusCode.ShouldEqual(VoteHelper.VoteStatusCode.Ok);

      var matchingPerson = fakes.People.Where(p => p.PersonGuid == vote1.PersonGuid).ToList();
      matchingPerson.Count.ShouldEqual(1);
      vote1.PersonCombinedInfo.ShouldEqual(matchingPerson[0].CombinedInfo);


      var ballot4 = fakes.Ballots[3];
      ballot4.ComputerCode.ShouldEqual("A");
      ballot4.BallotNumAtComputer.ShouldEqual(4);

      var votes4 = fakes.Votes.Where(v => v.BallotGuid == ballot4.BallotGuid).ToList();
      votes4.Count.ShouldEqual(9);

      var vote4_9 = votes4[8];
      vote4_9.StatusCode.ShouldEqual(VoteHelper.VoteStatusCode.Ok);
      vote4_9.InvalidReasonGuid.ShouldEqual(IneligibleReasonEnum.Unreadable_Writing_illegible);


      var ballot11 = fakes.Ballots[10];
      ballot11.ComputerCode.ShouldEqual("A");
      ballot11.BallotNumAtComputer.ShouldEqual(11);
      ballot11.StatusCode.ShouldEqual(BallotStatusEnum.TooMany);
      var votes11 = fakes.Votes.Where(v => v.BallotGuid == ballot11.BallotGuid).ToList();
      votes11.Count.ShouldEqual(10);
    }
Ejemplo n.º 7
0
    public JsonResult Create()
    {
      if (UserSession.IsGuestTeller)
      {
        return new
                 {
                   Success = false,
                   Message = "Not authorized"
                 }.AsJsonResult();
      }

      // create an election for this ID
      // create a default Location
      // assign all of these to this person and computer

      var election = new Election
                       {
                         Convenor = "[Convenor]",
                         ElectionGuid = Guid.NewGuid(),
                         Name = "[New Election]",
                         ElectionType = "LSA",
                         ElectionMode = ElectionMode.Normal,
                         TallyStatus = ElectionTallyStatusEnum.NotStarted,
                         NumberToElect = 9,
                         NumberExtra = 0,
                         CanVote = CanVoteOrReceive.All,
                         CanReceive = CanVoteOrReceive.All
                       };
      Db.Elections.Add(election);
      Db.SaveChanges();

      new ElectionStatusSharer().SetStateFor(election);

      var join = new JoinElectionUser
                   {
                     ElectionGuid = election.ElectionGuid,
                     UserId = UserSession.UserGuid
                   };
      Db.JoinElectionUsers.Add(join);


      var mainLocation = new Location
                           {
                             Name = "Main Location",
                             LocationGuid = Guid.NewGuid(),
                             ElectionGuid = election.ElectionGuid,
                             SortOrder = 1
                           };
      Db.Locations.Add(mainLocation);

      Db.SaveChanges();

      UserSession.CurrentElection = election;

      var computerModel = new ComputerModel();
      computerModel.AddCurrentComputerIntoElection(election.ElectionGuid);
      computerModel.AddCurrentComputerIntoLocation(mainLocation.C_RowId);

      return new
               {
                 Success = true
               }.AsJsonResult();
    }
    public void ElectionTieSpanningTopExtraOther()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
        {
          ElectionGuid = electionGuid,
          NumberToElect = 2,
          NumberExtra = 2
        };
      var location = new Location
        {
          LocationGuid = Guid.NewGuid(),
          ElectionGuid = electionGuid
        };

      var ballots = new List<Ballot>
        {
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = Guid.NewGuid(), StatusCode = BallotStatusEnum.Ok},
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = Guid.NewGuid(), StatusCode = BallotStatusEnum.Ok},
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = Guid.NewGuid(), StatusCode = BallotStatusEnum.Ok},
        };

      // results wanted:
      //  person 0 = 2 votes
      //  person 1 = 1
      // ---
      //  person 2 = 1
      //  person 3 = 1
      // --
      //  person 4 = 1
      var votes = new List<vVoteInfo>
        {
          new vVoteInfo {PersonGuid = SamplePeople[0].PersonGuid, BallotGuid = ballots[0].BallotGuid},
          new vVoteInfo {PersonGuid = SamplePeople[1].PersonGuid, BallotGuid = ballots[0].BallotGuid},
          new vVoteInfo {PersonGuid = SamplePeople[0].PersonGuid, BallotGuid = ballots[1].BallotGuid},
          new vVoteInfo {PersonGuid = SamplePeople[2].PersonGuid, BallotGuid = ballots[1].BallotGuid},
          new vVoteInfo {PersonGuid = SamplePeople[3].PersonGuid, BallotGuid = ballots[2].BallotGuid},
          new vVoteInfo {PersonGuid = SamplePeople[4].PersonGuid, BallotGuid = ballots[2].BallotGuid},
        };
      foreach (var vVoteInfo in votes)
      {
        vVoteInfo.ElectionGuid = electionGuid;
        vVoteInfo.PersonCombinedInfo = vVoteInfo.PersonCombinedInfoInVote = "zz";
        vVoteInfo.BallotStatusCode = BallotStatusEnum.Ok;
        vVoteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
      }

      var model = new ElectionAnalyzerNormal(_fakes, election, votes, ballots, SamplePeople);

      model.GenerateResults();

      var summary = model.ResultSummaryAuto;
      summary.BallotsReceived.ShouldEqual(3);
      summary.TotalVotes.ShouldEqual(6);
      summary.SpoiledBallots.ShouldEqual(0);
      summary.SpoiledVotes.ShouldEqual(0);
      summary.BallotsNeedingReview.ShouldEqual(0);

      var results = model.Results.OrderByDescending(r => r.VoteCount).ToList();
      var resultTies = model.ResultTies.OrderBy(rt => rt.TieBreakGroup).ToList();

      resultTies.Count.ShouldEqual(1);
      resultTies[0].NumToElect.ShouldEqual(3);
      resultTies[0].NumInTie.ShouldEqual(4);
      resultTies[0].TieBreakRequired.ShouldEqual(true);

      results.Count.ShouldEqual(5);

      results[0].IsTied.ShouldEqual(false);
      results[0].CloseToPrev.ShouldEqual(false);
      results[0].CloseToNext.ShouldEqual(true);
      results[0].Section.ShouldEqual(ResultHelper.Section.Top);
      results[0].TieBreakRequired.ShouldEqual(false);
      results[0].ForceShowInOther.ShouldEqual(false);

      results[1].IsTied.ShouldEqual(true);
      results[1].TieBreakGroup.ShouldEqual(1);
      results[1].CloseToPrev.ShouldEqual(true);
      results[1].CloseToNext.ShouldEqual(true);
      results[1].Section.ShouldEqual(ResultHelper.Section.Top);
      results[1].TieBreakRequired.ShouldEqual(true);
      results[1].ForceShowInOther.ShouldEqual(false);

      results[2].IsTied.ShouldEqual(true);
      results[2].TieBreakGroup.ShouldEqual(1);
      results[2].CloseToPrev.ShouldEqual(true);
      results[2].CloseToNext.ShouldEqual(true);
      results[2].Section.ShouldEqual(ResultHelper.Section.Extra);
      results[2].TieBreakRequired.ShouldEqual(true);
      results[2].ForceShowInOther.ShouldEqual(false);

      results[3].IsTied.ShouldEqual(true);
      results[3].TieBreakGroup.ShouldEqual(1);
      results[3].CloseToPrev.ShouldEqual(true);
      results[3].CloseToNext.ShouldEqual(true);
      results[3].Section.ShouldEqual(ResultHelper.Section.Extra);
      results[3].TieBreakRequired.ShouldEqual(true);
      results[3].ForceShowInOther.ShouldEqual(false);

      results[4].IsTied.ShouldEqual(true);
      results[4].TieBreakGroup.ShouldEqual(1);
      results[4].CloseToPrev.ShouldEqual(true);
      results[4].CloseToNext.ShouldEqual(false);
      results[4].Section.ShouldEqual(ResultHelper.Section.Other);
      results[4].ForceShowInOther.ShouldEqual(true);
      results[4].TieBreakRequired.ShouldEqual(true);
      results[4].ForceShowInOther.ShouldEqual(true);
    }
    public void Election_3_people_with_Tie_Not_Required()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
        {
          ElectionGuid = electionGuid,
          NumberToElect = 3,
          NumberExtra = 0
        };
      var location = new Location
        {
          LocationGuid = Guid.NewGuid(),
          ElectionGuid = electionGuid
        };

      var ballot1Guid = Guid.NewGuid();
      var ballot2Guid = Guid.NewGuid();
      var ballot3Guid = Guid.NewGuid();
      var ballots = new List<Ballot>
        {
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = ballot1Guid, StatusCode = BallotStatusEnum.Ok},
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = ballot2Guid, StatusCode = BallotStatusEnum.Ok},
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = ballot3Guid, StatusCode = BallotStatusEnum.Ok}
        };

      var person1Guid = Guid.NewGuid();
      var person2Guid = Guid.NewGuid();
      var person3Guid = Guid.NewGuid();
      var votes = new List<vVoteInfo>
        {
          new vVoteInfo {PersonGuid = person1Guid, BallotGuid = ballot1Guid},
          new vVoteInfo {PersonGuid = person1Guid, BallotGuid = ballot2Guid},
          new vVoteInfo {PersonGuid = person1Guid, BallotGuid = ballot3Guid},
          new vVoteInfo {PersonGuid = person2Guid, BallotGuid = ballot1Guid},
          new vVoteInfo {PersonGuid = person2Guid, BallotGuid = ballot2Guid},
          new vVoteInfo {PersonGuid = person2Guid, BallotGuid = ballot3Guid},
          new vVoteInfo {PersonGuid = person3Guid, BallotGuid = ballot1Guid},
          new vVoteInfo {PersonGuid = person3Guid, BallotGuid = ballot2Guid},
          new vVoteInfo {PersonGuid = person3Guid, BallotGuid = ballot3Guid},
        };
      foreach (var vVoteInfo in votes)
      {
        vVoteInfo.ElectionGuid = electionGuid;
        vVoteInfo.PersonCombinedInfo = vVoteInfo.PersonCombinedInfoInVote = "zz";
        vVoteInfo.BallotStatusCode = BallotStatusEnum.Ok;
        vVoteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
      }

      var model = new ElectionAnalyzerNormal(_fakes, election, votes, ballots, SamplePeople);

      model.GenerateResults();

      var results = model.Results.OrderByDescending(r => r.VoteCount).ToList();
      var resultTies = model.ResultTies.OrderBy(rt => rt.TieBreakGroup).ToList();

      resultTies.Count.ShouldEqual(1);
      resultTies[0].NumToElect.ShouldEqual(0);
      resultTies[0].NumInTie.ShouldEqual(3);
      resultTies[0].TieBreakRequired.ShouldEqual(false);

      results.Count.ShouldEqual(3);

      var result1 = results[0];
      result1.VoteCount.ShouldEqual(3);
      result1.Rank.ShouldEqual(1);
      result1.Section.ShouldEqual(ResultHelper.Section.Top);
      result1.IsTied.ShouldEqual(true);
      result1.TieBreakGroup.ShouldEqual(1);
      result1.TieBreakRequired.ShouldEqual(false);
      result1.ForceShowInOther.ShouldEqual(false);

      var result2 = results[1];
      result2.VoteCount.ShouldEqual(3);
      result2.Rank.ShouldEqual(2);
      result2.Section.ShouldEqual(ResultHelper.Section.Top);
      result2.IsTied.ShouldEqual(true);
      result2.TieBreakGroup.ShouldEqual(1);
      result2.ForceShowInOther.ShouldEqual(false);
      result2.TieBreakRequired.ShouldEqual(false);

      var result3 = results[2];
      result3.VoteCount.ShouldEqual(3);
      result3.Rank.ShouldEqual(3);
      result3.Section.ShouldEqual(ResultHelper.Section.Top);
      result3.IsTied.ShouldEqual(true);
      result3.TieBreakGroup.ShouldEqual(1);
      result3.ForceShowInOther.ShouldEqual(false);
      result3.TieBreakRequired.ShouldEqual(false);
    }
    public void Election_3_people()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
        {
          ElectionGuid = electionGuid,
          NumberToElect = 1,
          NumberExtra = 0
        };
      var location = new Location
        {
          LocationGuid = Guid.NewGuid(),
          ElectionGuid = electionGuid
        };

      var ballot1Guid = Guid.NewGuid();
      var ballot2Guid = Guid.NewGuid();
      var ballot3Guid = Guid.NewGuid();
      var ballots = new List<Ballot>
        {
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = ballot1Guid, StatusCode = BallotStatusEnum.Ok},
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = ballot2Guid, StatusCode = BallotStatusEnum.Ok},
          new Ballot
            {LocationGuid = location.LocationGuid, BallotGuid = ballot3Guid, StatusCode = BallotStatusEnum.Ok}
        };

      var person1Guid = Guid.NewGuid();
      var votes = new List<vVoteInfo>
        {
          new vVoteInfo {SingleNameElectionCount = 33, PersonGuid = person1Guid, BallotGuid = ballot1Guid},
          new vVoteInfo {SingleNameElectionCount = 5, PersonGuid = person1Guid, BallotGuid = ballot2Guid},
          new vVoteInfo {SingleNameElectionCount = 2, PersonGuid = Guid.NewGuid(), BallotGuid = ballot3Guid},
        };
      foreach (var vVoteInfo in votes)
      {
        vVoteInfo.ElectionGuid = electionGuid;
        vVoteInfo.PersonCombinedInfo = vVoteInfo.PersonCombinedInfoInVote = "zz";
        vVoteInfo.BallotStatusCode = ballots.Single(b => b.BallotGuid == vVoteInfo.BallotGuid).StatusCode;
        vVoteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
      }

      var model = new ElectionAnalyzerNormal(_fakes, election, votes, ballots, SamplePeople);

      model.GenerateResults();

      var results = model.Results.OrderByDescending(r => r.VoteCount).ToList();

      results.Count.ShouldEqual(2);

      var result1 = results[0];
      result1.VoteCount.ShouldEqual(2);
      result1.Rank.ShouldEqual(1);
      result1.Section.ShouldEqual(ResultHelper.Section.Top);
      result1.IsTied.ShouldEqual(false);

      var result2 = results[1];
      result2.VoteCount.ShouldEqual(1);
      result2.Rank.ShouldEqual(2);
      result2.Section.ShouldEqual(ResultHelper.Section.Other);
      result2.IsTied.ShouldEqual(false);
    }
Ejemplo n.º 11
0
    public JsonResult EditLocation(int id, string text)
    {
      var location = Locations.SingleOrDefault(l => l.C_RowId == id);

      if (location == null)
      {
        location = new Location { ElectionGuid = UserSession.CurrentElectionGuid, LocationGuid = Guid.NewGuid() };
        Db.Locations.Add(location);
      }

      int locationId;
      string locationText;
      string status;
      var success = false;

      if (text.HasNoContent() && location.C_RowId != 0)
      {
        if (Locations.Count() > 1)
        {
          // delete existing if we can
          var used = Db.Ballots.Any(b => b.LocationGuid == location.LocationGuid);
          if (!used)
          {
            Db.Locations.Remove(location);
            Db.SaveChanges();

            status = "Deleted";
            success = true;
            locationId = 0;
            locationText = "";
          }
          else
          {
            status = "Cannot deleted this location because it has Ballots recorded in it";
            locationId = location.C_RowId;
            locationText = location.Name;
          }
        }
        else
        { // only one
          status = "At least one location is required";
          locationId = location.C_RowId;
          locationText = location.Name;
        }
      }
      else if (text.HasContent())
      {
        location.Name = text;
        Db.SaveChanges();

        status = "Saved";
        locationId = location.C_RowId;
        locationText = location.Name;
        success = true;
      }
      else
      {
        status= "Nothing to save";
        locationId = 0;
        success = true;
        locationText = "";
      }

      return new
               {
                 // returns 0 if deleted or not created
                 Id = locationId,
                 Text = locationText,
                 Success = success,
                 Status = status
               }.AsJsonResult();
    }
Ejemplo n.º 12
0
    public object LocationInfoForJson(Location location)
    {
      var isSingleName = UserSession.CurrentElection.IsSingleNameElection;
      var sum = isSingleName
                  ? Db.vVoteInfoes.Where(vi => vi.LocationId == location.C_RowId).Sum(vi => vi.SingleNameElectionCount)
                  : Db.vBallotInfoes.Count(b => b.LocationId == location.C_RowId);

      return new
               {
                 Id = location.C_RowId,
                 TallyStatus = LocationStatusEnum.TextFor(location.TallyStatus),
                 TallyStatusCode = location.TallyStatus,
                 location.ContactInfo,
                 location.BallotsCollected,
                 location.Name,
                 BallotsEntered = sum
               };
    }