def test_add_an_existing_choice(self): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") with pytest.raises(ValueError) as excinfo: p.add_choice("Sim") assert str(excinfo.value) == "Sim was already added" assert p.choices == ["Sim", "Não"]
def test_start_voting_poll_without_enough_choices(chat_update, chat_context): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") chat_context.chat_data["poll"] = p.to_dict() with patch.object(chat_update.message, "reply_text") as m: next_state = poll.poll_start_voting(chat_update, chat_context) assert next_state == poll.CHOICES m.assert_called_with("Please, add at least 2 choices")
def test_poll_cancel(chat_update, chat_context): p = poll.Poll("Vai ter dojo?") chat_context.chat_data["poll"] = p.to_dict() with patch.object(chat_update.message, "reply_text") as m: next_state = poll.poll_cancel(chat_update, chat_context) assert chat_context.chat_data.get("poll") is None assert next_state == ConversationHandler.END m.assert_called_with("Poll cancelled!")
def test_result_without_votes(self): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") winners, max_vote, percentage = p.result() assert winners == [] assert max_vote == 0 assert percentage == 0
def test_vote_invalid_option(self, user): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") with pytest.raises(ValueError) as excinfo: p.vote(user, 2) assert str(excinfo.value) == "Invalid choice" assert len(p.votes) == p.total == 0 assert p.votes == {}
def test_voting_wrong_choice(bot, update): bot.poll = poll.Poll("Vai ter dojo?") bot.poll.add_choice("Sim") bot.poll.add_choice("Não") with patch.object(update.message, "reply_text") as m: next_state = poll.poll_vote(bot, update, args=["3"]) assert next_state == poll.VOTING m.assert_called_with( "Invalid option, please choose:\n0. Sim (0)\n1. Não (0)\n")
def test_vote(self, user): user_id = str(user.id) p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") p.vote(user, 0) assert len(p.votes) == p.total == 1 assert p.votes[user_id] == 0 assert p.users[user_id] == "alanturing"
def test_vote_changing_vote(self, user): user_id = str(user.id) p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") p.vote(user, 0) p.vote(user, 1) assert len(p.votes) == p.total == 1 assert p.votes[user_id] == 1
def test_voting_same_choice(bot, update): bot.poll = poll.Poll("Vai ter dojo?") bot.poll.add_choice("Sim") bot.poll.add_choice("Não") next_state = poll.poll_vote(bot, update, args=["1"]) assert next_state == poll.VOTING with patch.object(update.message, "reply_text") as m: next_state = poll.poll_vote(bot, update, args=["1"]) assert next_state == poll.VOTING m.assert_called_with("You already voted on this choice")
def test_vote_in_the_same_choice(self, user): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") p.vote(user, 0) with pytest.raises(poll.AlreadyVotedError) as excinfo: p.vote(user, 0) assert str(excinfo.value) == "You already voted on this choice" assert len(p.votes) == p.total == 1 assert p.votes[user] == 0
def test_poll_choice_with_existing_choice(bot, update): bot.poll = poll.Poll("Vai ter dojo?") next_state = poll.poll_choice(bot, update, args=["Sim"]) assert next_state == poll.CHOICES assert bot.poll.choices == ["Sim"] with patch.object(update.message, "reply_text") as m: next_state = poll.poll_choice(bot, update, args=["Sim"]) assert next_state == poll.CHOICES assert bot.poll.choices == ["Sim"] m.assert_called_with("Sim was already added")
def test_voting_correct_choice(chat_update, chat_context): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") chat_context.args = ["1"] chat_context.chat_data["poll"] = p.to_dict() next_state = poll.poll_vote(chat_update, chat_context) assert next_state == poll.VOTING chat_poll = chat_context.chat_data["poll"] assert chat_poll["votes"][str(chat_update.message.from_user.id)] == 1
def test_poll_formating_with_vote(self, user): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") p.vote(user, 0) expected = ("Question: Vai ter dojo?\n" "0. Sim (1)\n" "\t[alanturing]\n" "1. Não (0)\n\n" "Winner: Sim - 1(100.00%)") assert str(p) == expected
def test_voting_wrong_choice(chat_update, chat_context): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") chat_context.chat_data["poll"] = p.to_dict() with patch.object(chat_update.message, "reply_text") as m: chat_context.args = ["3"] next_state = poll.poll_vote(chat_update, chat_context) assert next_state == poll.VOTING m.assert_called_with( "Invalid option, please choose:\n0. Sim (0)\n1. Não (0)\n")
def test_start_voting_poll(bot, update): bot.poll = poll.Poll("Vai ter dojo?") bot.poll.add_choice("Sim") bot.poll.add_choice("Não") with patch.object(update.message, "reply_text") as m: next_state = poll.poll_start_voting(bot, update) assert next_state == poll.VOTING expected = ("Question: Vai ter dojo?\n" "0. Sim (0)\n" "1. Não (0)\n\n" "Choose an option: /v <choice_id>") m.assert_called_with(expected)
def test_poll_choice_with_existing_choice(chat_update, chat_context): chat_context.chat_data["poll"] = poll.Poll("Vai ter dojo?").to_dict() chat_context.args = ["Sim"] next_state = poll.poll_choice(chat_update, chat_context) assert next_state == poll.CHOICES assert chat_context.chat_data["poll"]["choices"] == ["Sim"] with patch.object(chat_update.message, "reply_text") as m: chat_context.args = ["Sim"] next_state = poll.poll_choice(chat_update, chat_context) assert next_state == poll.CHOICES assert chat_context.chat_data["poll"]["choices"] == ["Sim"] m.assert_called_with("Sim was already added")
def test_start_voting_poll(chat_update, chat_context): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") chat_context.chat_data["poll"] = p.to_dict() with patch.object(chat_update.message, "reply_text") as m: next_state = poll.poll_start_voting(chat_update, chat_context) assert next_state == poll.VOTING expected = ("Question: Vai ter dojo?\n" "0. Sim (0)\n" "1. Não (0)\n\n" "Choose an option: /v <choice_id>") m.assert_called_with(expected)
def test_result(bot, update, user): bot.poll = poll.Poll("Vai ter dojo?") bot.poll.add_choice("Sim") bot.poll.add_choice("Não") bot.poll.vote(user, 0) with patch.object(update.message, "reply_text") as m: next_state = poll.poll_result(bot, update) assert next_state == poll.VOTING expected = ("Question: Vai ter dojo?\n" "0. Sim (1)\n" "\t[alanturing]\n" "1. Não (0)\n\n" "Winner: Sim - 1(100.00%)") m.assert_called_with(expected)
def test_voting_same_choice(chat_update, chat_context): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") chat_context.args = ["1"] chat_context.chat_data["poll"] = p.to_dict() next_state = poll.poll_vote(chat_update, chat_context) assert next_state == poll.VOTING with patch.object(chat_update.message, "reply_text") as m: chat_context.args = ["1"] next_state = poll.poll_vote(chat_update, chat_context) assert next_state == poll.VOTING m.assert_called_with("You already voted on this choice")
def test_result(chat_update, chat_context, user): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") p.vote(user, 0) chat_context.chat_data["poll"] = p.to_dict() with patch.object(chat_update.message, "reply_text") as m: next_state = poll.poll_result(chat_update, chat_context) assert next_state == poll.VOTING expected = ("Question: Vai ter dojo?\n" "0. Sim (1)\n" "\t[alanturing]\n" "1. Não (0)\n\n" "Winner: Sim - 1(100.00%)") m.assert_called_with(expected)
def test_result_with_draw(self, user): other_user1 = User(101, "user1", False) other_user2 = User(102, "user2", False) other_user3 = User(103, "user3", False) p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") p.vote(user, 0) p.vote(other_user1, 0) p.vote(other_user2, 1) p.vote(other_user3, 1) winners, max_vote, percentage = p.result() assert winners == ["Sim", "Não"] assert max_vote == 2 assert f"{percentage:0.2f}" == "50.00"
def test_finish(bot, update, user): bot.poll = poll.Poll("Vai ter dojo?") bot.poll.add_choice("Sim") bot.poll.add_choice("Não") bot.poll.vote(user, 0) with patch.object(update.message, "reply_text") as m: next_state = poll.poll_finish(bot, update) assert bot.poll is None assert next_state == ConversationHandler.END expected = ("Poll finished!\n" "Question: Vai ter dojo?\n" "0. Sim (1)\n" "\t[alanturing]\n" "1. Não (0)\n\n" "Winner: Sim - 1(100.00%)") m.assert_called_with(expected)
def test_finish(chat_update, chat_context, user): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") p.vote(user, 0) chat_context.chat_data["poll"] = p.to_dict() with patch.object(chat_update.message, "reply_text") as m: next_state = poll.poll_finish(chat_update, chat_context) assert chat_context.chat_data.get("poll") is None assert next_state == ConversationHandler.END expected = ("Poll finished!\n" "Question: Vai ter dojo?\n" "0. Sim (1)\n" "\t[alanturing]\n" "1. Não (0)\n\n" "Winner: Sim - 1(100.00%)") m.assert_called_with(expected)
def test_init(self): p = poll.Poll("Vai ter dojo?") assert p.question == "Vai ter dojo?" assert p.total == 0 assert p.choices == [] assert p.votes == {}
def test_poll_choice_with_text(chat_update, chat_context): chat_context.chat_data["poll"] = poll.Poll("Vai ter dojo?").to_dict() chat_context.args = ["Sim"] next_state = poll.poll_choice(chat_update, chat_context) assert chat_context.chat_data["poll"]["choices"] == ["Sim"] assert next_state == poll.CHOICES
def test_poll_formating_without_votes(self): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") p.add_choice("Não") expected = "Question: Vai ter dojo?\n0. Sim (0)\n1. Não (0)\n" assert str(p) == expected
def test_poll_choice_without_text(bot, update): bot.poll = poll.Poll("Vai ter dojo?") with patch.object(update.message, "reply_text") as m: next_state = poll.poll_choice(bot, update, args=[]) m.assert_called_with("Use: /choice <text>") assert next_state == poll.CHOICES
def test_poll_choice_with_text(bot, update): bot.poll = poll.Poll("Vai ter dojo?") next_state = poll.poll_choice(bot, update, args=["Sim"]) assert bot.poll.choices == ["Sim"] assert next_state == poll.CHOICES
def test_add_choice(self): p = poll.Poll("Vai ter dojo?") p.add_choice("Sim") assert p.choices == ["Sim"]