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 post(self, tournament_id): body = request.json if not body or not body["name"]: raise exceptions.MissingRequiredInformation() if "tournament_id" in body: del body["tournament_id"] bracket = Bracket(**body, tournament_id=tournament_id) db.add(bracket) current_app.logger.debug( "The bracket {0} has been created successfully.".format( bracket.id)) return bracket.get_api_dict(), 201
def fill_bracket_object_from_data(bracket_data, include_spreadsheets): spreadsheets = {} if include_spreadsheets: for spreadsheet_type, spreadsheet_class in Bracket.get_spreadsheet_types( ).items(): spreadsheet_data = bracket_data.pop( spreadsheet_type + "_spreadsheet", None) if spreadsheet_data: spreadsheets[spreadsheet_type] = spreadsheet_class( **spreadsheet_data) bracket = Bracket(**bracket_data) for spreadsheet_type, spreadsheet in spreadsheets.items(): setattr(bracket, "_" + spreadsheet_type + "_spreadsheet", spreadsheet) return bracket
async def test_register_not_supported_yet_multiple_brackets(mocker): """Registers to the tournament, but there are multiple brackets.""" cog, mock_bot, _, _ = init_mocks() bracket2 = Bracket(id=2, tournament_id=1) mock_bot.session.add_stub(bracket2) await cog.register(cog, tosurnament_mock.CtxMock(mock_bot)) cog.send_reply.assert_called_once_with(mocker.ANY, "not_supported_yet")
def create_bracket(tournament_id, bracket): bracket_data = requests_wrapper( "post", TOSURNAMENT_URL + "tournaments/" + str(tournament_id) + "/brackets", data=bracket.get_table_dict(), ) return Bracket(**bracket_data)
async def copy_bracket(self, ctx, index_from: int, index_to: int): """Copies the settings of a bracket to another one.""" tournament = self.get_tournament(ctx.guild.id) brackets = tournament.brackets if index_from > 0 and index_from <= len( brackets) and index_to > 0 and index_to <= len(brackets): bracket_from = brackets[index_from - 1] bracket_to = brackets[index_to - 1] bracket_to.post_result_channel_id = bracket_from.post_result_channel_id bracket_to.current_round = bracket_from.current_round for spreadsheet_type in Bracket.get_spreadsheet_types().keys(): spreadsheet_from = bracket_from.get_spreadsheet_from_type( spreadsheet_type) if spreadsheet_from: spreadsheet_to = bracket_to.get_spreadsheet_from_type( spreadsheet_type) if not spreadsheet_to: spreadsheet_to = getattr( tosurnament_api, "create_" + spreadsheet_type)(tournament.id, bracket_to.id, spreadsheet_from) else: spreadsheet_from.copy_to(spreadsheet_to) getattr(tosurnament_api, "update_" + spreadsheet_type)(tournament.id, bracket_to.id, spreadsheet_to) await self.send_reply(ctx, "success", bracket_from.name, bracket_to.name) return raise commands.UserInputError()
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
async def create_bracket(self, ctx, *, name: str): """Creates a bracket and sets it as current bracket (for bracket settings purpose).""" tournament = self.get_tournament(ctx.guild.id) bracket = Bracket(tournament_id=tournament.id, name=name) bracket = tosurnament_api.create_bracket(tournament.id, bracket) tournament.current_bracket_id = bracket.id tournament = tosurnament_api.update_tournament(tournament) await self.send_reply(ctx, "success", name)
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(): 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
async def test_set_bracket_name(mocker): """Sets the bracket name.""" cog, mock_bot, bracket = init_mocks() bracket_name = "Bracket name" assert bracket.name != bracket_name await cog.set_bracket_name(cog, tosurnament_mock.CtxMock(mock_bot), name=bracket_name) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Bracket(tournament_id=1, name=bracket_name)) )
async def test_set_bracket_role(mocker): """Sets the bracket role.""" cog, mock_bot, bracket = init_mocks() role = tosurnament_mock.RoleMock("role", 324987) assert bracket.role_id != role.id await cog.set_bracket_role(cog, tosurnament_mock.CtxMock(mock_bot), role=role) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Bracket(tournament_id=1, role_id=role.id, name=BRACKET1_NAME)) )
async def test_set_post_result_channel(mocker): """Sets the post result channel.""" cog, mock_bot, bracket = init_mocks() channel = tosurnament_mock.ChannelMock(324769) assert bracket.post_result_channel_id != channel.id await cog.set_post_result_channel(cog, tosurnament_mock.CtxMock(mock_bot), channel=channel) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Bracket(tournament_id=1, post_result_channel_id=channel.id, name=BRACKET1_NAME)) )
async def test_set_current_bracket_round(mocker): """Sets the round of the current bracket.""" cog, mock_bot, bracket = init_mocks() current_round = "RO64" assert bracket.current_round != current_round await cog.set_current_bracket_round(cog, tosurnament_mock.CtxMock(mock_bot), current_round=current_round) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Bracket(tournament_id=1, current_round=current_round, name=BRACKET1_NAME)) )
async def test_set_challonge(mocker): """Sets the challonge.""" cog, mock_bot, bracket = init_mocks() challonge_id = "challonge_id" challonge_id_url = "https://www.challonge.com/" + challonge_id + "/" assert bracket.challonge != challonge_id await cog.set_challonge(cog, tosurnament_mock.CtxMock(mock_bot), challonge_tournament=challonge_id_url) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Bracket(tournament_id=1, challonge=challonge_id, name=BRACKET1_NAME)) )
async def test_copy_bracket_wrong_index(mocker): """Copies a bracket settings to another one, but input indexes are wrong.""" cog, mock_bot, _ = init_mocks() mock_bot.session.add_stub(Bracket(id=2, tournament_id=1)) with pytest.raises(commands.UserInputError): await cog.copy_bracket(cog, tosurnament_mock.CtxMock(mock_bot), 1, 3) with pytest.raises(commands.UserInputError): await cog.copy_bracket(cog, tosurnament_mock.CtxMock(mock_bot), 0, 2)
def get_spreadsheets_data_of_bracket(bracket): response_spreadsheets = {} for spreadsheet_type, spreadsheet_class in Bracket.get_spreadsheet_types( ).items(): spreadsheet = (db.query(spreadsheet_class).where( id=getattr(bracket, spreadsheet_type + "_spreadsheet_id")).first()) if spreadsheet: response_spreadsheets[spreadsheet_type + "_spreadsheet"] = spreadsheet.get_api_dict() return response_spreadsheets
async def test_set_bracket_values(mocker): """Puts the input values into the corresponding bracket.""" cog, mock_bot, bracket = init_mocks() bracket_name = "Bracket name" assert bracket.name != bracket_name await cog.set_bracket_values(tosurnament_mock.CtxMock(mock_bot), {"name": bracket_name}) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher(Bracket(tournament_id=1, name=bracket_name)) ) cog.send_reply.assert_called_once_with(mocker.ANY, "success", bracket_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 sync_spreadsheet(self, ctx): tournament = self.get_tournament(ctx.guild.id) spreadsheet_ids = [] for bracket in tournament.brackets: for spreadsheet_type in Bracket.get_spreadsheet_types().keys(): spreadsheet = bracket.get_spreadsheet_from_type(spreadsheet_type) if not spreadsheet: continue spreadsheet_id = spreadsheet.spreadsheet_id if spreadsheet_id not in spreadsheet_ids: await spreadsheet.get_spreadsheet(retry=True, force_sync=True) spreadsheet_ids.append(spreadsheet_id) await self.send_reply(ctx, "success")
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
def delete_bracket_and_associated_spreadsheets(bracket): for spreadsheet_type, spreadsheet_class in Bracket.get_spreadsheet_types( ).items(): spreadsheet_id = getattr(bracket, spreadsheet_type + "_spreadsheet_id") if spreadsheet_id > 0: spreadsheet = db.query(spreadsheet_class).where( id=spreadsheet_id).first() if spreadsheet: db.delete(spreadsheet) current_app.logger.debug( "The {0} spreadsheet {1} has been deleted successfully.". format(spreadsheet_type.replace("_", " "), spreadsheet_id)) db.delete(bracket) current_app.logger.debug( "The bracket {0} has been deleted successfully.".format(bracket.id))
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_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_set_registration_end_date(mocker): """Sets the registration end date.""" cog, mock_bot, bracket = init_mocks() date = "1 week" assert bracket.registration_end_date != date new_date = datetime.datetime.now() + datetime.timedelta(days=7) assert bracket.registration_end_date != new_date.strftime(tosurnament.DATABASE_DATE_FORMAT) parse_date_mock = mocker.Mock(return_value=new_date) with mock.patch.object(Tournament, "parse_date", new=parse_date_mock): await cog.set_registration_end(cog, tosurnament_mock.CtxMock(mock_bot), date=date) mock_bot.session.update.assert_called_once_with( tosurnament_mock.Matcher( Bracket( tournament_id=1, registration_end_date=new_date.strftime(tosurnament.DATABASE_DATE_FORMAT), name=BRACKET1_NAME, ) ) )