Example #1
0
async def test_set_spreadsheet_values(mocker):
    """Sets spreadsheet values."""
    mock_bot = tosurnament_mock.BotMock()
    mock_bot.session.add_stub(
        Tournament(id=1,
                   current_bracket_id=1,
                   guild_id=tosurnament_mock.GUILD_ID))
    mock_bot.session.add_stub(
        Bracket(id=1,
                tournament_id=1,
                schedules_spreadsheet_id=1,
                players_spreadsheet_id=1))
    mock_bot.session.add_stub(SchedulesSpreadsheet(id=1))
    cog = tosurnament_mock.mock_cog(bracket.get_class(mock_bot))
    mock_ctx = tosurnament_mock.CtxMock(mock_bot)

    await cog.set_schedules_spreadsheet_values(mock_ctx,
                                               {"sheet_name": SHEET_NAME})
    mock_bot.session.update.assert_called_once_with(
        tosurnament_mock.Matcher(SchedulesSpreadsheet(sheet_name=SHEET_NAME)))

    with pytest.raises(base.NoSpreadsheet):
        await cog.set_players_spreadsheet_values(mock_ctx,
                                                 {"sheet_name": SHEET_NAME})

    mock_bot.session.add_stub(PlayersSpreadsheet(id=1))
    await cog.set_players_spreadsheet_values(mock_ctx,
                                             {"sheet_name": SHEET_NAME})
Example #2
0
async def test_reaction_on_end_tournament_message(mocker):
    """Sends a message to react on in order to end the tournament."""
    mock_bot = tosurnament_mock.BotMock()
    cog = tosurnament_mock.mock_cog(guild_owner.get_class(mock_bot))

    mock_bot.session.add_stub(
        Tournament(id=1, guild_id=tosurnament_mock.GUILD_ID))
    mock_bot.session.add_stub(
        Bracket(tournament_id=1,
                schedules_spreadsheet_id=1,
                players_spreadsheet_id=1))
    mock_bot.session.add_stub(SchedulesSpreadsheet(id=1))
    mock_bot.session.add_stub(PlayersSpreadsheet(id=1))
    mock_bot.session.add_stub(Bracket(tournament_id=1))
    mock_bot.session.add_stub(
        Bracket(tournament_id=1,
                schedules_spreadsheet_id=42,
                players_spreadsheet_id=42))
    mock_bot.session.add_stub(SchedulesSpreadsheet(id=42))
    mock_bot.session.add_stub(PlayersSpreadsheet(id=42))
    mock_bot.session.add_stub(
        Bracket(tournament_id=1, schedules_spreadsheet_id=43))
    mock_bot.session.add_stub(RescheduleMessage(tournament_id=1))
    mock_bot.session.add_stub(RescheduleMessage(tournament_id=1))
    mock_bot.session.add_stub(StaffRescheduleMessage(tournament_id=1))
    mock_bot.session.add_stub(StaffRescheduleMessage(tournament_id=1))
    mock_bot.session.add_stub(EndTournamentMessage(message_id=MESSAGE_ID))

    mock_bot.session.add_stub(Tournament(id=2))
    mock_bot.session.add_stub(
        Bracket(tournament_id=2,
                schedules_spreadsheet_id=2,
                players_spreadsheet_id=2))
    mock_bot.session.add_stub(SchedulesSpreadsheet(id=2))
    mock_bot.session.add_stub(PlayersSpreadsheet(id=2))
    mock_bot.session.add_stub(RescheduleMessage(tournament_id=2))

    await cog.reaction_on_end_tournament_message(
        MESSAGE_ID,
        tosurnament_mock.EmojiMock("✅"),
        tosurnament_mock.GuildMock(tosurnament_mock.GUILD_ID),
        tosurnament_mock.ChannelMock(),
        tosurnament_mock.UserMock(user_id=tosurnament_mock.GuildMock.OWNER_ID),
    )
    cog.send_reply.assert_called_with(mocker.ANY, mocker.ANY, "success")

    assert len(mock_bot.session.tables[Tournament.__tablename__]) == 1
    assert len(mock_bot.session.tables[Bracket.__tablename__]) == 1
    assert len(
        mock_bot.session.tables[SchedulesSpreadsheet.__tablename__]) == 1
    assert len(mock_bot.session.tables[PlayersSpreadsheet.__tablename__]) == 1
    assert len(mock_bot.session.tables[RescheduleMessage.__tablename__]) == 1
    assert len(
        mock_bot.session.tables[StaffRescheduleMessage.__tablename__]) == 0
    assert len(
        mock_bot.session.tables[EndTournamentMessage.__tablename__]) == 0
Example #3
0
async def test_copy_bracket(mocker):
    """Copies a bracket settings to another one."""
    mock_bot = tosurnament_mock.BotMock()
    mock_bot.session.add_stub(
        Tournament(id=1,
                   current_bracket_id=1,
                   guild_id=tosurnament_mock.GUILD_ID))

    mock_bot.session.add_stub(
        Bracket(id=1,
                tournament_id=1,
                schedules_spreadsheet_id=1,
                players_spreadsheet_id=1,
                post_result_channel_id=1))
    schedules_spreadsheet = SchedulesSpreadsheet(id=1,
                                                 sheet_name=SHEET_NAME + " s",
                                                 range_match_id="B2:B",
                                                 range_team1="C2:C")
    mock_bot.session.add_stub(schedules_spreadsheet)

    mock_bot.session.add_stub(
        Bracket(id=2, tournament_id=1, schedules_spreadsheet_id=2))
    mock_bot.session.add_stub(
        SchedulesSpreadsheet(id=2,
                             sheet_name=SHEET_NAME,
                             range_match_id="A1:A",
                             range_score_team1="B1:B"))

    cog = tosurnament_mock.mock_cog(bracket.get_class(mock_bot))
    mock_ctx = tosurnament_mock.CtxMock(mock_bot)

    await cog.copy_bracket(cog, mock_ctx, 1, 2)
    assert len(mock_bot.session.tables[Bracket.__tablename__]) == 2
    assert mock_bot.session.tables[
        Bracket.__tablename__][1].post_result_channel_id == 1
    assert len(
        mock_bot.session.tables[SchedulesSpreadsheet.__tablename__]) == 2
    assert mock_bot.session.tables[SchedulesSpreadsheet.__tablename__][
        1] != tosurnament_mock.Matcher(schedules_spreadsheet)
    schedules_spreadsheet.sheet_name = SHEET_NAME
    assert mock_bot.session.tables[SchedulesSpreadsheet.__tablename__][
        1] == tosurnament_mock.Matcher(schedules_spreadsheet)
    assert PlayersSpreadsheet.__tablename__ not in mock_bot.session.tables

    players_spreadsheet = PlayersSpreadsheet(id=1, range_team="A1:A")
    mock_bot.session.add_stub(players_spreadsheet)

    await cog.copy_bracket(cog, mock_ctx, 1, 2)
    assert len(mock_bot.session.tables[PlayersSpreadsheet.__tablename__]) == 2
    assert mock_bot.session.tables[PlayersSpreadsheet.__tablename__][
        1] == tosurnament_mock.Matcher(players_spreadsheet)
Example #4
0
 async def set_bracket_spreadsheet(self, ctx, spreadsheet_type, spreadsheet_id, sheet_name):
     """Puts the input spreadsheet into the corresponding bracket."""
     tournament = self.get_tournament(ctx.guild.id)
     bracket = tournament.current_bracket
     spreadsheet_id = spreadsheet.extract_spreadsheet_id(spreadsheet_id)
     if getattr(bracket, spreadsheet_type + "_spreadsheet_id") > 0:
         if spreadsheet_type == "players":
             any_spreadsheet = bracket.players_spreadsheet
         elif spreadsheet_type == "schedules":
             any_spreadsheet = bracket.schedules_spreadsheet
         else:
             return
     else:
         if spreadsheet_type == "players":
             any_spreadsheet = PlayersSpreadsheet()
         elif spreadsheet_type == "schedules":
             any_spreadsheet = SchedulesSpreadsheet()
         else:
             return
         self.bot.session.add(any_spreadsheet)
         setattr(bracket, spreadsheet_type + "_spreadsheet_id", any_spreadsheet.id)
         self.bot.session.update(bracket)
     any_spreadsheet.spreadsheet_id = spreadsheet_id
     if sheet_name:
         any_spreadsheet.sheet_name = sheet_name
     self.bot.session.update(any_spreadsheet)
     await self.send_reply(ctx, ctx.command.name, "success", spreadsheet_id)
Example #5
0
async def test_set_bracket_spreadsheet(mocker):
    """Sets bracket spreadsheets."""
    mock_bot = tosurnament_mock.BotMock()
    mock_bot.session.add_stub(
        Tournament(id=1,
                   current_bracket_id=1,
                   guild_id=tosurnament_mock.GUILD_ID))
    mock_bot.session.add_stub(Bracket(id=1, tournament_id=1))
    cog = tosurnament_mock.mock_cog(bracket.get_class(mock_bot))
    mock_ctx = tosurnament_mock.CtxMock(mock_bot)

    await cog.set_bracket_spreadsheet(mock_ctx, "players", SPREADSHEET_ID, "")
    update_expected = [
        mocker.call(
            tosurnament_mock.Matcher(
                Bracket(tournament_id=1, players_spreadsheet_id=1))),
        mocker.call(
            tosurnament_mock.Matcher(
                PlayersSpreadsheet(spreadsheet_id=SPREADSHEET_ID))),
    ]
    assert mock_bot.session.update.call_args_list == update_expected
    await cog.set_bracket_spreadsheet(mock_ctx, "players", SPREADSHEET_ID,
                                      SHEET_NAME)
    update_expected = [
        mocker.call(
            tosurnament_mock.Matcher(
                PlayersSpreadsheet(spreadsheet_id=SPREADSHEET_ID,
                                   sheet_name=SHEET_NAME))),
    ]
    assert mock_bot.session.update.call_args_list[2:] == update_expected

    await cog.set_bracket_spreadsheet(mock_ctx, "schedules", SPREADSHEET_ID,
                                      "")
    update_expected = [
        mocker.call(
            tosurnament_mock.Matcher(
                Bracket(tournament_id=1,
                        players_spreadsheet_id=1,
                        schedules_spreadsheet_id=1))),
        mocker.call(
            tosurnament_mock.Matcher(
                SchedulesSpreadsheet(spreadsheet_id=SPREADSHEET_ID))),
    ]
    assert mock_bot.session.update.call_args_list[3:] == update_expected
Example #6
0
async def test_remove_match_to_ignore(mocker):
    """Removes a match to ignore."""
    MATCH_ID_1 = "T1-1"
    MATCH_ID_2 = "t1-2"
    MATCH_ID_3 = "T1-3"

    mock_bot = tosurnament_mock.BotMock()
    mock_bot.session.add_stub(
        Tournament(
            id=1,
            guild_id=tosurnament_mock.GUILD_ID,
            matches_to_ignore=MATCH_ID_1 + "\n" + MATCH_ID_3,
            current_bracket_id=1,
            staff_channel_id=tosurnament_mock.ChannelMock.STAFF_CHANNEL_ID,
        ))
    mock_bot.session.add_stub(
        Bracket(id=1, tournament_id=1, schedules_spreadsheet_id=1))
    mock_bot.session.add_stub(
        SchedulesSpreadsheet(id=1,
                             spreadsheet_id="single",
                             sheet_name="Tier 1"))

    cog = tosurnament_mock.mock_cog(tournament.get_class(mock_bot))

    await cog.remove_match_to_ignore(cog, tosurnament_mock.CtxMock(mock_bot),
                                     MATCH_ID_2, MATCH_ID_3)
    assert mock_bot.session.tables[
        Tournament.__tablename__][0].matches_to_ignore == MATCH_ID_1
    expected_replies = [
        mocker.call(mocker.ANY, mocker.ANY, "success", MATCH_ID_1),
        mocker.call(
            tosurnament_mock.ChannelMock(
                tosurnament_mock.ChannelMock.STAFF_CHANNEL_ID),
            mocker.ANY,
            "to_not_ignore",
            mocker.ANY,
            MATCH_ID_3,
        ),
    ]
    assert cog.send_reply.call_args_list == expected_replies
Example #7
0
async def test_add_match_to_ignore(mocker):
    """Adds a match to ignore."""
    MATCH_ID_1 = "T1-1"
    MATCH_ID_2 = "t1-2"
    MATCH_ID_3 = "T1-3"
    INVALID_MATCH_ID = "Invalid"

    mock_bot = tosurnament_mock.BotMock()
    mock_bot.session.add_stub(
        Tournament(id=1,
                   guild_id=tosurnament_mock.GUILD_ID,
                   matches_to_ignore=MATCH_ID_3,
                   current_bracket_id=1))
    mock_bot.session.add_stub(
        Bracket(id=1, tournament_id=1, schedules_spreadsheet_id=1))
    mock_bot.session.add_stub(
        SchedulesSpreadsheet(id=1,
                             spreadsheet_id="single",
                             sheet_name="Tier 1"))

    cog = tosurnament_mock.mock_cog(tournament.get_class(mock_bot))

    await cog.add_match_to_ignore(cog, tosurnament_mock.CtxMock(mock_bot),
                                  MATCH_ID_1, MATCH_ID_2, INVALID_MATCH_ID)
    assert mock_bot.session.tables[
        Tournament.__tablename__][0].matches_to_ignore == "\n".join(
            [MATCH_ID_1, MATCH_ID_2.upper(), MATCH_ID_3])
    expected_replies = [
        mocker.call(mocker.ANY, mocker.ANY, "success",
                    " ".join([MATCH_ID_1,
                              MATCH_ID_2.upper(), MATCH_ID_3])),
        mocker.call(mocker.ANY, mocker.ANY, "not_found",
                    INVALID_MATCH_ID.lower()),
        mocker.call(mocker.ANY, mocker.ANY, "to_ignore", mocker.ANY,
                    MATCH_ID_1),
        mocker.call(mocker.ANY, mocker.ANY, "to_ignore", mocker.ANY,
                    MATCH_ID_2.upper()),
    ]
    assert cog.send_reply.call_args_list == expected_replies