def test_insert2(self): o = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) voter_dao.delete_dto(o) self.assertFalse(voter_dao.has_voted(o.user_id, o.votation_id)) voter_dao.insert_dto(o) self.assertFalse(voter_dao.has_voted(o.user_id, o.votation_id)) voter_dao.delete_dto(o) self.assertFalse(voter_dao.has_voted(o.user_id, o.votation_id))
def test_set_voted_with_list(self): # insert a voter o = Voter(user_id=2, votation_id=self.__votation_list__.votation_id, voted=0) self.assertFalse(voter_dao.is_voter(o.votation_id, o.user_id)) voter_dao.insert_dto(o) self.assertTrue(voter_dao.is_voter(o.votation_id, o.user_id)) # run set_voted() self.assertFalse(voter_dao.has_voted(o.user_id, o.votation_id)) self.assertTrue(voter_dao.set_voted(o.user_id, o.votation_id)) self.assertTrue(voter_dao.has_voted(o.user_id, o.votation_id))
def test_set_voted_no_list(self): # run set_voted user_id = 2 votation_id = self.__votation__.votation_id # should perform insert self.assertTrue(voter_dao.is_voter(votation_id, user_id)) self.assertTrue(voter_dao.set_voted(user_id, votation_id)) self.assertTrue(voter_dao.has_voted(user_id, votation_id)) # should perform update self.assertTrue(voter_dao.set_voted(user_id, votation_id)) self.assertTrue(voter_dao.has_voted(user_id, votation_id)) self.assertTrue(voter_dao.is_voter(votation_id, user_id))
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))
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]) )
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