コード例 #1
0
    def test_election(self):
        num_votes = 500
        session = Session()
        a = Member(first_name='A', last_name='B')
        session.add(a)
        c = Member(first_name='C', last_name='D')
        session.add(c)
        e = Member(first_name='E', last_name='F')
        candidates = []
        for member in [a, c, e]:
            cand = Candidate()
            cand.member = member
            candidates.append(cand)
        session.add(e)
        election = Election(name='Test', number_winners=2)
        election.candidates.extend(candidates)
        session.add(election)

        for i in range(0, num_votes):
            shuffle(candidates)
            vote = Vote()
            election.votes.append(vote)
            for j, cand in enumerate(candidates):
                rank = Ranking(rank=i)
                rank.candidate = cand
                vote.ranking.append(rank)
        session.commit()
        session.close()

        session = Session()
        election = session.query(Election).filter_by(name='Test').one()
        results = hold_election(election)
        assert len(results.winners) == 2
        assert len(results.votes) == num_votes
コード例 #2
0
 def decorated(*args, **kwargs):
     if USE_AUTH:
         auth = request.headers.get('authorization')
         if not auth:
             return deny('Authorization not found.')
         token = auth.split()[1]
         try:
             token = jwt.decode(token, JWT_SECRET, audience=JWT_CLIENT_ID)
         except Exception as e:
             return deny(str(e))
         email = token.get('email')
     else:
         email = NO_AUTH_EMAIL
     session = Session()
     try:
         member = session.query(Member).filter_by(email_address=email).one()
         authenticated = False
         if admin:
             for role in member.roles:
                 if role.committee_id is None and role.role == 'admin':
                     authenticated = True
         else:
             authenticated = True
         if authenticated:
             kwargs['requester'] = member
             kwargs['session'] = session
             return f(*args, **kwargs)
         return deny('not enough access')
     finally:
         session.close()
コード例 #3
0
    def test_election_prob(self, data):
        # Set up the SQLAlchemy session
        metadata.drop_all(engine)
        metadata.create_all(engine)
        session = Session()

        # Randomly generate parameters
        num_votes = data.draw(st.integers(min_value=0, max_value=500))
        num_candidates = data.draw(st.integers(min_value=1, max_value=10))
        num_winners = data.draw(
            st.integers(min_value=1, max_value=num_candidates))

        # Create candidates as members and add them to the DB
        candidate_members = [
            Member(first_name=str(i), last_name=str(i))
            for i in range(num_candidates)
        ]
        session.add_all(candidate_members)
        # Create candidates as candidates and add them to the DB
        candidates = [Candidate(member=member) for member in candidate_members]
        session.add_all(candidates)
        # Create the election and add to DB
        election = Election(name='Test2', number_winners=num_winners)
        election.candidates.extend(candidates)
        session.add(election)

        # Generate votes for the candidates
        for i in range(num_votes):
            shuffle(candidates)
            vote = Vote()
            election.votes.append(vote)
            for j, cand in enumerate(candidates):
                rank = Ranking(rank=i)
                rank.candidate = cand
                vote.ranking.append(rank)

        # Commit everything to DB
        session.commit()
        session.close()

        # Get the election back from the DB, hold the election
        new_session = Session()
        election = new_session.query(Election).filter_by(name='Test2').one()
        results = hold_election(election)

        # Check the results
        assert len(results.winners) == num_winners
        assert len(results.votes) == num_votes