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);
    }
Exemplo n.º 2
0
    public JsonResult Import(int rowId)
    {
      var currentElectionGuid = UserSession.CurrentElectionGuid;
      var file =
        Db.ImportFiles.SingleOrDefault(
          fi => fi.ElectionGuid == currentElectionGuid && fi.C_RowId == rowId);

      if (file == null)
      {
        throw new ApplicationException("File not found");
      }

      var xml = GetXmlDoc(file);

      if (xml == null || xml.DocumentElement == null)
      {
        throw new ApplicationException("Invalid Xml file");
      }

      var logHelper = new LogHelper();

      ImportV1Base importer;
      var currentPeople = Db.People.Where(p => p.ElectionGuid == currentElectionGuid).ToList();
      var personModel = new PeopleModel();


      switch (xml.DocumentElement.Name)
      {
        case "Community":
          importer = new ImportV1Community(Db, file, xml
                                           , currentPeople
                                           , delegate(Person person)
                                               {
                                                 personModel.SetCombinedInfoAtStart(person);
                                                 person.ElectionGuid = currentElectionGuid;
                                                 Db.People.Add(person);
                                               }
                                           , logHelper);
          break;
        case "Election":

          var currentElection = UserSession.CurrentElection;
          var currentLocation = UserSession.CurrentLocation;
          if (currentLocation == null)
          {
            currentLocation = Db.Locations.OrderBy(l => l.SortOrder).FirstOrDefault(l => l.ElectionGuid == currentElection.ElectionGuid);
            if (currentLocation == null)
            {
              throw new ApplicationException("An election must have a Location before importing.");
            }
          }

          EraseElectionContents(currentElection);

          importer = new ImportV1Election(Db, file, xml
                                          , currentElection
                                          , currentLocation
                                          , ballot => Db.Ballots.Add(ballot)
                                          , vote => Db.Votes.Add(vote)
                                          , currentPeople
                                          , person =>
                                              {
                                                personModel.SetCombinedInfoAtStart(person);
                                                Db.People.Add(person);
                                              }
                                          , summary => Db.ResultSummaries.Add(summary)
                                          , logHelper
            );
          break;
        default:
          throw new ApplicationException("Unexpected Xml file");
      }

      importer.Process();

      var resultsModel = new ResultsModel();
      resultsModel.GenerateResults();

      return importer.SendSummary();
    }