示例#1
0
 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"]
示例#2
0
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")
示例#3
0
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!")
示例#4
0
 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
示例#5
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 == {}
示例#6
0
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")
示例#7
0
 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"
示例#8
0
 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
示例#9
0
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")
示例#10
0
 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
示例#11
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")
示例#12
0
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
示例#13
0
 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
示例#14
0
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")
示例#15
0
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)
示例#16
0
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")
示例#17
0
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)
示例#18
0
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)
示例#19
0
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")
示例#20
0
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)
示例#21
0
    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"
示例#22
0
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)
示例#23
0
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)
示例#24
0
 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 == {}
示例#25
0
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
示例#26
0
 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
示例#27
0
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
示例#28
0
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
示例#29
0
 def test_add_choice(self):
     p = poll.Poll("Vai ter dojo?")
     p.add_choice("Sim")
     assert p.choices == ["Sim"]