def test_is_staff(): """Checks if the user is part of the tournament staff.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id)) mock_ctx = tosurnament_mock.CtxMock(mock_bot) user_roles = tosurnament.UserDetails.get_from_ctx(mock_ctx) assert not user_roles.is_staff() mock_ctx.author.roles.append(tosurnament_mock.RoleMock("Random")) user_roles = tosurnament.UserDetails.get_from_ctx(mock_ctx) assert not user_roles.is_staff() mock_ctx.author.roles.append(tosurnament_mock.RoleMock("Referee")) user_roles = tosurnament.UserDetails.get_from_ctx(mock_ctx) assert user_roles.is_staff() assert user_roles.referee assert not user_roles.streamer assert not user_roles.commentator mock_ctx.author.roles.append(tosurnament_mock.RoleMock("Commentator")) user_roles = tosurnament.UserDetails.get_from_ctx(mock_ctx) assert user_roles.is_staff() assert user_roles.referee assert not user_roles.streamer assert user_roles.commentator mock_ctx.author.roles.append(tosurnament_mock.RoleMock("Streamer")) user_roles = tosurnament.UserDetails.get_from_ctx(mock_ctx) assert user_roles.is_staff() assert user_roles.referee assert user_roles.streamer assert user_roles.commentator
def post(self): body = request.json if (not body or not body["guild_id"] or not body["guild_id_snowflake"] or not body["name"] or not body["acronym"]): raise exceptions.MissingRequiredInformation() body["guild_id"] = str(body["guild_id"]) tournament = Tournament(**body) db.add(tournament) bracket = Bracket(tournament_id=tournament.id, name=tournament.name) db.add(bracket) tournament.current_bracket_id = bracket.id db.update(tournament) current_app.logger.debug( "The tournament {0} has been created successfully.".format( tournament.id)) return get_tournament_data(tournament, True), 201
async def test_set_qualifiers_results_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.DEFAULT_GUILD_MOCK.id)) mock_bot.session.add_stub(Bracket(id=1, tournament_id=1)) cog = tosurnament_mock.mock_cog(module_to_test.get_class(mock_bot)) mock_ctx = tosurnament_mock.CtxMock(mock_bot) spreadsheet_id = "abcd1234" sheet_name = "a sheet name" await cog.set_qualifiers_results_spreadsheet(cog, mock_ctx, spreadsheet_id, sheet_name=sheet_name) update_expected = [ mocker.call(tosurnament_mock.Matcher(Bracket(tournament_id=1, qualifiers_results_spreadsheet_id=1))), mocker.call( tosurnament_mock.Matcher(QualifiersResultsSpreadsheet(spreadsheet_id=spreadsheet_id, sheet_name=sheet_name)) ), ] assert mock_bot.session.update.call_args_list == update_expected await cog.set_qualifiers_results_spreadsheet(cog, mock_ctx, spreadsheet_id, sheet_name="") update_expected = [ mocker.call( tosurnament_mock.Matcher(QualifiersResultsSpreadsheet(spreadsheet_id=spreadsheet_id, sheet_name=sheet_name)) ), ] assert mock_bot.session.update.call_args_list[2:] == update_expected
def init_mocks(): mock_bot = tosurnament_mock.BotMock() tournament = Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id) mock_bot.session.add_stub(tournament) bracket = Bracket(id=1, tournament_id=1) mock_bot.session.add_stub(bracket) cog = tosurnament_mock.mock_cog(staff_module.get_class(mock_bot)) return cog, mock_bot, tournament, bracket
def init_mocks(n_of_brackets=1): mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id)) for i in range(n_of_brackets): bracket = Bracket(id=i + 1, tournament_id=1, name=("Bracket " + str(i + 1))) mock_bot.session.add_stub(bracket) cog = tosurnament_mock.mock_cog(bracket_module.get_class(mock_bot)) return cog, mock_bot, bracket
def init_mocks(): mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id)) mock_bot.session.add_stub(Bracket(id=1, tournament_id=1, qualifiers_results_spreadsheet_id=1)) qualifiers_results_spreadsheet = QualifiersResultsSpreadsheet(id=1) mock_bot.session.add_stub(qualifiers_results_spreadsheet) cog = tosurnament_mock.mock_cog(module_to_test.get_class(mock_bot)) return cog, mock_bot, qualifiers_results_spreadsheet
def fill_tournament_object_from_data(tournament_data, include_brackets, include_spreadsheets): brackets = [] if include_brackets: brackets_data = tournament_data.pop("brackets", []) for bracket_data in brackets_data: brackets.append( fill_bracket_object_from_data(bracket_data, include_spreadsheets)) tournament = Tournament(**tournament_data) setattr(tournament, "_brackets", brackets) return tournament
def init_mocks(): mock_bot = tosurnament_mock.BotMock() tournament = Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id) mock_bot.session.add_stub(tournament) bracket = Bracket(id=1, tournament_id=1) mock_bot.session.add_stub(bracket) cog = tosurnament_mock.mock_cog(player_module.get_class(mock_bot)) mock_bot.session.add_stub(tosurnament_mock.REFEREE_USER_STUB) mock_bot.session.add_stub(tosurnament_mock.STREAMER_USER_STUB) mock_bot.session.add_stub(tosurnament_mock.COMMENTATOR_USER_STUB) mock_bot.session.add_stub(tosurnament_mock.COMMENTATOR2_USER_STUB) mock_bot.session.add_stub(tosurnament_mock.DEFAULT_USER_STUB) return cog, mock_bot, tournament, bracket
async def test_set_qualifiers_results_spreadsheet_values(mocker): """Sets qualifiers results spreadsheet values.""" mock_bot = tosurnament_mock.BotMock() mock_bot.session.add_stub(Tournament(id=1, current_bracket_id=1, guild_id=tosurnament_mock.DEFAULT_GUILD_MOCK.id)) mock_bot.session.add_stub(Bracket(id=1, tournament_id=1, qualifiers_results_spreadsheet_id=1)) cog = tosurnament_mock.mock_cog(module_to_test.get_class(mock_bot)) mock_ctx = tosurnament_mock.CtxMock(mock_bot) sheet_name = "a sheet name" with pytest.raises(base.NoSpreadsheet): await cog.set_qualifiers_results_spreadsheet_values(mock_ctx, {"sheet_name": sheet_name}) qualifiers_results_spreadsheet = QualifiersResultsSpreadsheet(id=1) mock_bot.session.add_stub(qualifiers_results_spreadsheet) assert qualifiers_results_spreadsheet.sheet_name != sheet_name await cog.set_qualifiers_results_spreadsheet_values(mock_ctx, {"sheet_name": sheet_name}) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(QualifiersResultsSpreadsheet(sheet_name=sheet_name)) )
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.DEFAULT_GUILD_MOCK.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) ) sheet_name = "a sheet name" 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_module.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)
async def create_tournament(self, ctx, acronym: str, name: str, bracket_name: str = ""): """ Creates a tournament. If a bracket name is not specified, the bracket takes the tournament's name as its name too. """ guild_id = str(ctx.guild.id) tournament = tosurnament_api.get_tournament_by_discord_guild_id( guild_id) if tournament: raise tosurnament.TournamentAlreadyCreated() tournament = Tournament(guild_id=guild_id, guild_id_snowflake=guild_id, acronym=acronym, name=name) tournament = tosurnament_api.create_tournament(tournament) if bracket_name: tournament.current_bracket.name = bracket_name tosurnament_api.update_bracket(tournament.id, tournament.current_bracket) await self.send_reply(ctx, "success", acronym, name, bracket_name)