def test_count_vote(self): """test counting votes for a post also test""" u1, u2 = self.test_create_users() p = self.test_create_post() choices = Choice.get_choices_by_post_id(p.post_id) c1, c2 = [choice.choice_id for choice in choices][0], [choice.choice_id for choice in choices][1] v1 = Vote.create(user_id=u1.user_id, choice_id=c1) v2 = Vote.create(user_id=u2.user_id, choice_id=c2) vote_dict, total_votes, doughnut_chart_dict = p.count_votes() self.assertEqual(total_votes, 2) self.assertEqual({c1:1, c2:1}, vote_dict)
def test_create_vote(self): """test the creation of votes and check if it's correctly linked to post""" u1, u2 = self.test_create_users() p = self.test_create_post() choices = Choice.get_choices_by_post_id(p.post_id) c1, c2 = [choice.choice_id for choice in choices][0], [choice.choice_id for choice in choices][1] v1 = Vote.create(user_id=u1.user_id, choice_id=c1) v2 = Vote.create(user_id=u2.user_id, choice_id=c2) self.assertEqual(v1.user_id, u1.user_id) self.assertEqual(v2.choice_id, c2) self.assertIn(u1, p.get_voters()) self.assertIn(u2, p.get_voters()) self.assertEqual([u1, u2], p.get_voters()) self.assertEqual(Vote.get_vote_by_post_and_user_id(p.post_id, u1.user_id), c1) self.assertNotEqual(Vote.get_vote_by_post_and_user_id(p.post_id, u1.user_id), c2)
def test_create_post(self): """test to create a new post with file objects and make sure choices data are added to Choice table""" file_object1 = werkzeug.datastructures.FileStorage(filename="fileupload1.JPG") file_object2 = werkzeug.datastructures.FileStorage(filename="fileupload2.JPG") p = Post.create(author_id=1, description="test", file_name=None, tag_list=None, choice_data=[("text_choice1", file_object1), ("text_choice2", file_object2)]) choices = Choice.get_choices_by_post_id(p.post_id) choices_text = [choice.choice_text for choice in choices] choices_file = [choice.file_name for choice in choices] self.assertEqual(p.author_id, 1) self.assertEqual(p.description, "test") self.assertEqual(p.file_name, None) self.assertIn("text_choice1", choices_text) self.assertIn("text_choice2", choices_text) self.assertEqual([p], Post.get_all_posts()) for choice in choices: self.assertIn(hashlib.sha512(str(choice.choice_id)).hexdigest(), choices_file) return p