Example #1
0
 def test_insert(self):
     u = Vote(vote_key = "vote_key123", \
         votation_id = self.__votation__.votation_id, \
         option_id = self.__option1.option_id, \
         jud_value = 5)
     self.assertTrue(vote_dao.insert_dto(u))
     ar = vote_dao.load_vote_by_key("vote_key123")
     self.assertEqual(1, len(ar))
     u1 = ar[0]
     self.assertIsNotNone(u1)
     self.assertEqual(u.votation_id, u1.votation_id)
     self.assertEqual(u.vote_key, u1.vote_key)
     self.assertEqual(u.option_id, u1.option_id)
     self.assertEqual(u.jud_value, u1.jud_value)
     vote_dao.delete_votes_by_key("vote_key123")
     ar = vote_dao.load_vote_by_key("vote_key123")
     self.assertEqual(0, len(ar))
Example #2
0
 def test_save_ok(self):
     vote_key = 'vote_key' + str(random.randint(0, 50000))
     self.assertTrue( vote_bo.save_votes(user_id = 1, \
                                         vote_key=vote_key, \
                                         votation_id=self.__votation__.votation_id, \
                                         vote_array=[0,1,2]) )
     ar = vote_dao.load_vote_by_key(vote_key)
     self.assertEqual(3, len(ar))
     self.assertTrue(voter_dao.has_voted(1, self.__votation__.votation_id))
Example #3
0
 def test_save_simple_ok(self):
     votation_id = self.__votation__.votation_id
     option_id = self.__option1.option_id
     #def save_vote(user_id, vote_key,votation_id,option_id):
     self.assertTrue(
         vote_simple.save_vote(1, "akey123", votation_id, option_id))
     ar = vote_dao.load_vote_by_key("akey123")
     for v in ar:
         if v.option_id == option_id:
             self.assertEqual(1, v.jud_value)
         else:
             self.assertEqual(0, v.jud_value)
Example #4
0
 def test_save_wrong_key(self):
     vote_key = 'vote_key' + str(random.randint(0, 50000))
     # first vote
     self.assertTrue( vote_bo.save_votes(user_id = 1, \
                                         vote_key=vote_key, \
                                         votation_id=self.__votation__.votation_id, \
                                         vote_array=[1,1,1]) )
     ar = vote_dao.load_vote_by_key(vote_key)
     self.assertEqual(3, len(ar))
     self.assertTrue(voter_dao.has_voted(1, self.__votation__.votation_id))
     # second vote
     self.assertFalse( vote_bo.save_votes(user_id = 1, \
                         vote_key="WRONG KEY", \
                         votation_id=self.__votation__.votation_id, \
                         vote_array=[1,3,1]) )
Example #5
0
 def test_save_ok2(self):
     vote_key = 'vote_key' + str(random.randint(0, 50000))
     self.assertTrue(
         vote_dao.save_vote(self.__votation__.votation_id, vote_key,
                            [1, 2, 2]))
     ar = vote_dao.load_vote_by_key(vote_key)
     self.assertEqual(3, len(ar))
     check = True
     for v in ar:
         if (v.option_id == self.__option1.option_id and \
             v.jud_value == 1) or \
            (v.option_id == self.__option2.option_id and \
            v.jud_value == 2) or \
            (v.option_id == self.__option3.option_id and \
            v.jud_value == 2):
             check = True
         else:
             check = False
     self.assertTrue(check)
Example #6
0
def save_votes(user_id, vote_key, votation_id, vote_array):
    """
    Save a vote, one rank number per option id.
    The vote in vote_array[0] is for the first option.
    The vote in vote_array[1] is for the second option, and so on.
    Every option need a vote.
    Also, the voter is set as voted.
    """
    u = user.load_user_by_id(user_id)
    if not u:
        return False
    if voter_dao.has_voted(user_id, votation_id):
        ar = vote_dao.load_vote_by_key(vote_key)
        if len(ar) == 0:
            return False
        vote_dao.delete_votes_by_key(vote_key)
    if vote_dao.save_vote(votation_id=votation_id,
                          vote_key=vote_key,
                          array_judgements=vote_array):
        if voter_dao.set_voted(user_id, votation_id):
            db.session.commit()
            return True
    db.session.rollback()
    return False