public void TestElectionEntries()
        {
            Election election = new Election();

            TimetableUserEntry[] users =
            {
                new TimetableUserEntry()
                {
                    Username                 = "******",
                    AccountTypeName          = "Student",
                    StudentStatusDescription = "Active"
                },
                new TimetableUserEntry()
                {
                    Username                 = "******",
                    AccountTypeName          = "Student",
                    StudentStatusDescription = "Inactive Program Completed"
                },
                new TimetableUserEntry()
                {
                    AccountTypeName = "Student Support"
                },
            };

            ElectionEligibilityEntry[] result = EligibilityGenerator
                                                .GenerateElectionEntries(election, users.AsQueryable())
                                                .ToArray();

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual("abc", result[0].Username);
            Assert.AreSame(election, result[0].Election);
        }
        public static void Execute(int electionId, IJobCancellationToken cancellationToken)
        {
            using (var db = new VotingDbContext())
            {
                Election election = db.Elections.Find(electionId);
                if (election == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(electionId), "No election with such id");
                }

                if (election.DisableAutomaticEligibility)
                {
                    // Nothing to do
                    return;
                }

                using (var timetable = new TimetableDbContext())
                {
                    IEnumerable <ElectionEligibilityEntry> electionEntries =
                        EligibilityGenerator.GenerateElectionEntries(election, timetable.Users);
                    cancellationToken.ThrowIfCancellationRequested();

                    IEnumerable <PositionEligibilityEntry> positionEntries =
                        EligibilityGenerator.GeneratePositionEntries(election, timetable.Users, db);
                    cancellationToken.ThrowIfCancellationRequested();

                    // Note that we cannot dispose the timetable db context here
                    // since the queries are only "planned" yet and not actually executed

                    // Save election entries, only ones that do not exist in database
                    AddNewEntries(
                        electionEntries, db.ElectionEligibilityEntries,
                        (entry, set) => set.Find(entry.Username, entry.Election.Id)
                        );

                    cancellationToken.ThrowIfCancellationRequested();

                    // Save position entries, only ones that do not exist in database
                    AddNewEntries(
                        positionEntries, db.PositionEligibilityEntries,
                        (entry, set) => set.Find(entry.Username, entry.Position.Id)
                        );

                    cancellationToken.ThrowIfCancellationRequested();
                    db.SaveChanges();
                }
            }
        }