public ElectionAnalyzerNormal(IAnalyzerFakes fakes, Election election,
                               List<vVoteInfo> voteinfos, List<Ballot> ballots,
                               List<Person> people)
   : base(fakes, election, people, ballots, voteinfos)
 {
   //_locationInfos = locationInfos;
 }
        public ResultsModel(Election election = null)
        {
            _election = election;

            _analyzer = CurrentElection.IsSingleNameElection
                            ? new ElectionAnalyzerSingleName(election) as IElectionAnalyzer
                            : new ElectionAnalyzerNormal(election);
        }
        public Exporter Export()
        {
            // collect all the info
            _election = Db.Elections.SingleOrDefault(e => e.ElectionGuid == _electionGuid);

            var logger = new LogHelper(_electionGuid);
            logger.Add("Export to file started");

            if (_election == null) return null;

            var locations = Db.Locations.Where(l => l.ElectionGuid == _electionGuid);
            var computers = Db.Computers.Where(c => c.ElectionGuid == _electionGuid);

            var people = Db.People.Where(p => p.ElectionGuid == _electionGuid);
            var tellers = Db.Tellers.Where(t => t.ElectionGuid == _electionGuid);
            var results = Db.Results.Where(r => r.ElectionGuid == _electionGuid);
            var resultSummaries = Db.ResultSummaries.Where(r => r.ElectionGuid == _electionGuid);
            var resultTies = Db.ResultTies.Where(r => r.ElectionGuid == _electionGuid);
            var logs = Db.C_Log.Where(log => log.ElectionGuid == _electionGuid);

            var joinElectionUsers = Db.JoinElectionUsers.Where(j => j.ElectionGuid == _electionGuid);
            var users = Db.Users.Where(u => joinElectionUsers.Select(j => j.UserId).Contains(u.UserId));

            var ballots = Db.Ballots.Where(b => locations.Select(l => l.LocationGuid).Contains(b.LocationGuid));
            var votes = Db.Votes.Where(v => ballots.Select(b => b.BallotGuid).Contains(v.BallotGuid));

            var site = new SiteInfo();

            var blob = new
                {
                    Exported = DateTime.Now.ToString("o"),
                    ByUser = UserSession.MemberName,
                    UserEmail = UserSession.MemberEmail,
                    Server = site.ServerName,
                    Environment = site.CurrentEnvironment,
                    // elements
                    election = ExportElection(_election),
                    resultSummary = ExportResultSummaries(resultSummaries),
                    result = ExportResults(results),
                    resultTie = ExportResultTies(resultTies),
                    teller = ExportTellers(tellers),
                    user = ExportUsers(users),
                    location = ExportLocationComputerBallotVote(locations, computers, ballots, votes, logs),
                    person = ExportPeople(people),
                    reason = ExportReasons(),
                    //log = ExportLogs(logs)
                };

            var exportName = string.Format("{0} {1}.TallyJ",
                                           _election.DateOfElection.GetValueOrDefault(DateTime.Today)
                                                    .ToString("yyyy-MM-dd"),
                                           _election.Name);

            return new Exporter(blob, "TallyJ2", exportName);
        }
 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;
 }
 protected ElectionAnalyzerCore(IAnalyzerFakes fakes, Election election, List<Person> people,
                                List<Ballot> ballots,
                                List<vVoteInfo> voteinfos)
 {
     _election = election;
     _resultSummary = fakes.ResultSummary;
     _resultTies = new List<ResultTie>();
     _results = new List<Result>();
     _people = people;
     _ballots = ballots;
     _voteinfos = voteinfos;
     _votes = voteinfos.Select(vi => new Vote {C_RowId = vi.VoteId}).ToList();
     _deleteResult = fakes.RemoveResult;
     _addResult = fakes.AddResult;
     _saveChanges = fakes.SaveChanges;
     _deleteResultTie = fakes.RemoveResultTie;
     _addResultTie = fakes.AddResultTie;
 }
Example #6
0
        public VoteInfo(Vote vote, Election election, Ballot ballot, Location location, Person person)
        {
            VoteId = vote.C_RowId;
            SingleNameElectionCount  = vote.SingleNameElectionCount;
            PositionOnBallot         = vote.PositionOnBallot;
            PersonCombinedInfoInVote = vote.PersonCombinedInfo;
            VoteIneligibleReasonGuid = vote.InvalidReasonGuid;
            VoteStatusCode           = vote.StatusCode;
            OnlineVoteRaw            = vote.OnlineVoteRaw;

            //ballot
            BallotGuid       = ballot.BallotGuid;
            BallotId         = ballot.C_RowId;
            BallotStatusCode = ballot.StatusCode;
            C_BallotCode     = ballot.C_BallotCode;

            //Location
            LocationId = location == null ? 0 : location.C_RowId;

            ElectionGuid = election.ElectionGuid;

            if (person != null)
            {
                AssertAtRuntime.That(person.PersonGuid == vote.PersonGuid);

                new PeopleModel().ApplyVoteReasonFlags(person);

                PersonId                   = person.C_RowId;
                PersonFullNameFL           = person.FullNameAndArea;
                PersonFullName             = person.C_FullName;
                PersonCombinedInfo         = person.CombinedInfo;
                PersonIneligibleReasonGuid = person.IneligibleReasonGuid;
                PersonCanReceiveVotes      = person.CanReceiveVotes.AsBoolean();
                PersonGuid                 = person.PersonGuid;
            }
            else
            {
                PersonCanReceiveVotes = false;
            }
        }
    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);
    }
 /// <Summary>Totally erase all tally information</Summary>
 public void EraseElectionContents(Election election)
 {
   Db.EraseElectionContents(election.ElectionGuid, true, UserSession.LoginId);
 }
    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_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 SetStateFor(Election election)
 {
   SetStateFor(election.ElectionGuid, election.TallyStatus);
 }
    /// <Summary>Saves changes to this electoin</Summary>
    public JsonResult SaveElection(Election electionFromBrowser)
    {
      var election = Db.Elections.SingleOrDefault(e => e.C_RowId == electionFromBrowser.C_RowId);
      if (election != null)
      {
        var currentType = election.ElectionType;
        var currentMode = election.ElectionMode;
        var currentCan = election.CanVote;
        var currentReceive = election.CanReceive;

        // List of fields to allow edit from setup page
        var editableFields = new
                               {
                                 election.Name,
                                 election.DateOfElection,
                                 election.Convenor,
                                 election.ElectionType,
                                 election.ElectionMode,
                                 election.NumberToElect,
                                 election.NumberExtra,
                                 election.CanVote,
                                 election.CanReceive,
                                 election.ListForPublic,
                                 election.ShowAsTest,
                                 election.ElectionPasscode
                               }.GetAllPropertyInfos().Select(pi => pi.Name).ToArray();


        var changed = electionFromBrowser.CopyPropertyValuesTo(election, editableFields);

        //var isSingleNameElection = election.NumberToElect.AsInt() == 1;
        //if (election.IsSingleNameElection != isSingleNameElection)
        //{
        //  election.IsSingleNameElection = isSingleNameElection;
        //  changed = true;
        //}

        if (changed)
        {
          Db.SaveChanges();
          UserSession.CurrentElection = election;
        }

        if (currentMode != election.ElectionMode
            || currentType != election.ElectionType
            || currentCan != election.CanVote
            || currentReceive != election.CanReceive
          )
        {
          // reset flags
          new PeopleModel().ResetInvolvementFlags();
          Db.SaveChanges();

          // update analysis
          new ResultsModel().GenerateResults();
        }

        return new
                 {
                   Status = "Saved",
                   // TODO 2011-11-20 Glen Little: Return entire election?
                   Election = election
                 }.AsJsonResult();
      }

      return new
               {
                 Status = "Unknown ID"
               }.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 Ballot_TwoPeople()
    {
      var electionGuid = Guid.NewGuid();
      var election = new Election
        {
          ElectionGuid = electionGuid,
          NumberToElect = 2,
          NumberExtra = 0,
          CanReceive = ElectionModel.CanVoteOrReceive.All
        };

      var personGuid = Guid.NewGuid();

      var ballots = new List<Ballot>
        {
          new Ballot {BallotGuid = Guid.NewGuid(), StatusCode = BallotStatusEnum.Ok}
        };
      var votes = new List<vVoteInfo>
        {
          new vVoteInfo {PersonGuid = personGuid},
          new vVoteInfo {PersonGuid = Guid.NewGuid()},
        };
      foreach (var vVoteInfo in votes)
      {
        //vVoteInfo.PersonGuid = personGuid; // all for one person in this test
        vVoteInfo.ElectionGuid = electionGuid;
        vVoteInfo.PersonCombinedInfo = vVoteInfo.PersonCombinedInfoInVote = "zz";
        vVoteInfo.BallotGuid = ballots.Select(b => b.BallotGuid).First();
        vVoteInfo.BallotStatusCode = BallotStatusEnum.Ok;
        vVoteInfo.VoteStatusCode = VoteHelper.VoteStatusCode.Ok;
      }

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

      model.GenerateResults();

      var results = model.Results;

      results.Count.ShouldEqual(2);

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

      var result2 = results[1];
      result2.VoteCount.ShouldEqual(1);
      result2.Rank.ShouldEqual(2);
      result2.Section.ShouldEqual(ResultHelper.Section.Top);

      var resultSummaryAuto = model.ResultSummaryAuto;
      resultSummaryAuto.BallotsNeedingReview.ShouldEqual(0);
      resultSummaryAuto.BallotsReceived.ShouldEqual(1);

      resultSummaryAuto.DroppedOffBallots.ShouldEqual(0);
      resultSummaryAuto.InPersonBallots.ShouldEqual(1);
      resultSummaryAuto.MailedInBallots.ShouldEqual(0);
      resultSummaryAuto.CalledInBallots.ShouldEqual(0);
      resultSummaryAuto.NumEligibleToVote.ShouldEqual(5);
      resultSummaryAuto.NumVoters.ShouldEqual(1);
      resultSummaryAuto.ResultType.ShouldEqual(ResultType.Automatic);
    }
    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);
    }
 private object ExportElection(Election election)
 {
     return new
         {
             election.TallyStatus,
             election.ElectionType,
             election.ElectionMode,
             election.DateOfElection,
             election.NumberToElect,
             election.NumberExtra,
             election.Name,
             election.Convenor,
             ShowAsTest = election.ShowAsTest.OnlyIfTrue(),
             ListForPublic = election.ListForPublic.OnlyIfTrue(),
             ListedForPublicAsOf = election.ListedForPublicAsOf.AsString("o"),
             election.ElectionPasscode,
             election.CanVote,
             election.CanReceive,
             election.LastEnvNum,
             election.OwnerLoginId,
             ShowFullReport = election.ShowFullReport.OnlyIfTrue()
         };
 }
    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 JsonResult SaveElection(Election election)
 {
   return new ElectionModel().SaveElection(election);
 }
 public BallotAnalyzer(Election election, Func<int> saveChangesToDatastore)
 {
   IsSingleNameElection = election.IsSingleNameElection;
   VotesNeededOnBallot = election.NumberToElect.AsInt();
   SaveChangesToDatastore = saveChangesToDatastore;
 }
    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;
    }
        private static void DetermineSection(Result result, Election election, int rank)
        {
            string section;

            if (rank <= election.NumberToElect)
            {
                section = ResultHelper.Section.Top;
            }
            else if (rank <= (election.NumberToElect + election.NumberExtra))
            {
                section = ResultHelper.Section.Extra;
            }
            else
            {
                section = ResultHelper.Section.Other;
            }

            result.Section = section;
        }
    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);
    }
 protected ElectionAnalyzerCore(Election election)
 {
     _election = election;
 }
 public ElectionAnalyzerNormal(Election election) : base(election)
 {
 }
 public PeopleModel(Election election)
 {
   _election = election;
 }