async def test_block_and_unblock(self, cog: BlockCog): target = MagicMock() target.id = 2 target.display_name = "target-author-display-name" await self.run(cog.block, target=target) self.interaction.response.send_message.assert_called_once_with( f"<@{target.id}> has been blocked.", ephemeral=True, ) users = sorted(list(DatabaseSession.query(User).all()), key=lambda u: u.name) assert len(users) == 2 assert users[0].name == target.display_name assert users[0].xid == target.id assert users[1].name == self.interaction.user.display_name assert users[1].xid == self.interaction.user.id blocks = list(DatabaseSession.query(Block).all()) assert len(blocks) == 1 assert blocks[0].user_xid == self.interaction.user.id assert blocks[0].blocked_user_xid == target.id DatabaseSession.expire_all() await self.run(cog.unblock, target=target) blocks = list(DatabaseSession.query(Block).all()) assert len(blocks) == 0
async def test_lfg_with_friend_when_game_wrong_format( self, guild: Guild, channel: Channel): user1 = UserFactory.create(xid=101) user2 = UserFactory.create(xid=102) user3 = UserFactory.create(xid=103) bad_game = GameFactory.create( seats=4, channel=channel, guild=guild, format=GameFormat.TWO_HEADED_GIANT.value, ) games = GamesService() new = await games.upsert( guild_xid=guild.xid, channel_xid=channel.xid, author_xid=user1.xid, friends=[user2.xid, user3.xid], seats=4, format=GameFormat.COMMANDER.value, ) assert new DatabaseSession.expire_all() game = DatabaseSession.query(Game).filter(Game.id != bad_game.id).one() assert game.guild_xid == guild.xid assert game.channel_xid == channel.xid rows = DatabaseSession.query( User.xid).filter(User.game_id == game.id).all() assert set(row[0] for row in rows) == {101, 102, 103}
async def test_users_select(self): users = UsersService() assert not await users.select(201) UserFactory.create(xid=201) DatabaseSession.expire_all() assert await users.select(201)
async def test_games_make_ready(self, game: Game): games = GamesService() await games.select(game.id) await games.make_ready("http://link") DatabaseSession.expire_all() found = DatabaseSession.query(Game).get(game.id) assert found and found.spelltable_link == "http://link" assert found.status == GameStatus.STARTED.value
async def test_games_set_voice(self, game: Game): games = GamesService() await games.select(game.id) await games.set_voice(12345, "http://link") DatabaseSession.expire_all() found = DatabaseSession.query(Game).get(game.id) assert found and found.voice_xid == 12345 assert found.voice_invite_link == "http://link"
async def test_games_set_message_xid(self, game: Game): games = GamesService() await games.select(game.id) await games.set_message_xid(12345) DatabaseSession.expire_all() found = DatabaseSession.query(Game).filter_by( message_xid=12345).one_or_none() assert found and found.id == game.id
async def test_games_add_player(self, game: Game): user = UserFactory.create() games = GamesService() await games.select(game.id) await games.add_player(user.xid) DatabaseSession.expire_all() found = DatabaseSession.query(User).get(user.xid) assert found and found.game.id == game.id
async def test_set_motd(self, cog: AdminCog) -> None: await self.run(cog.motd, message="this is a test") self.interaction.response.send_message.assert_called_once_with( "Message of the day updated.", ephemeral=True, ) guild = DatabaseSession.query(Guild).one() assert guild.motd == "this is a test" await self.run(cog.motd) DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).one() assert guild.motd == ""
async def test_users_block(self): user1 = UserFactory.create() user2 = UserFactory.create() users = UsersService() await users.block(user1.xid, user2.xid) DatabaseSession.expire_all() blocks = [b.to_dict() for b in DatabaseSession.query(Block).all()] assert blocks == [{ "user_xid": user1.xid, "blocked_user_xid": user2.xid }]
async def test_users_watch_without_note(self, guild: Guild): user = UserFactory.create() users = UsersService() await users.watch(guild_xid=guild.xid, user_xid=user.xid) DatabaseSession.expire_all() watches = [w.to_dict() for w in DatabaseSession.query(Watch).all()] assert watches == [{ "guild_xid": guild.xid, "user_xid": user.xid, "note": None }]
async def test_guilds_award_delete(self, guild: Guild): award1 = GuildAwardFactory.create(guild=guild) award2 = GuildAwardFactory.create(guild=guild) guilds = GuildsService() await guilds.select(guild.xid) award1_id = award1.id await guilds.award_delete(award1.id) await guilds.award_delete(404) DatabaseSession.expire_all() assert not DatabaseSession.query(GuildAward).get(award1_id) assert DatabaseSession.query(GuildAward).get(award2.id)
async def test_games_watch_notes(self, game: Game): user1 = UserFactory.create(game=game) user2 = UserFactory.create(game=game) user3 = UserFactory.create() watch = WatchFactory.create(guild_xid=game.guild.xid, user_xid=user1.xid) DatabaseSession.expire_all() games = GamesService() await games.select(game.id) assert await games.watch_notes([user1.xid, user2.xid, user3.xid]) == { user1.xid: watch.note, }
async def test_channel_motd(self, cog: AdminCog): motd = "this is a channel message of the day" await self.run(cog.channel_motd, message=motd) self.interaction.response.send_message.assert_called_once_with( f"Message of the day for this channel has been set to: {motd}", ephemeral=True, ) channel = DatabaseSession.query(Channel).one() assert channel.motd == motd await self.run(cog.channel_motd) DatabaseSession.expire_all() channel = DatabaseSession.query(Channel).one() assert channel.motd == ""
async def test_guilds_set_motd(self): guilds = GuildsService() assert not await guilds.select(101) guild = GuildFactory.create() guilds = GuildsService() await guilds.select(guild.xid) message_of_the_day = "message of the day" await guilds.set_motd(message_of_the_day) DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).get(guild.xid) assert guild and guild.motd == message_of_the_day
async def test_lfg_alone_when_existing_game(self, game: Game, user: User): games = GamesService() new = await games.upsert( guild_xid=game.guild.xid, channel_xid=game.channel.xid, author_xid=user.xid, friends=[], seats=4, format=GameFormat.COMMANDER.value, ) assert not new DatabaseSession.expire_all() found = DatabaseSession.query(User).one() assert found.game_id == game.id
async def test_games_add_points(self, game: Game): user1 = UserFactory.create(game=game) user2 = UserFactory.create(game=game) PlayFactory.create(user_xid=user1.xid, game_id=game.id, points=5) PlayFactory.create(user_xid=user2.xid, game_id=game.id, points=None) games = GamesService() await games.select(game.id) await games.add_points(user1.xid, 5) DatabaseSession.expire_all() found = DatabaseSession.query(Play).filter( Play.user_xid == user1.xid).one() assert found.points == 5 found = DatabaseSession.query(Play).filter( Play.user_xid == user2.xid).one() assert found.points is None
async def test_guilds_upsert(self): discord_guild = MagicMock() discord_guild.id = 101 discord_guild.name = "guild-name" guilds = GuildsService() await guilds.upsert(discord_guild) DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).get(discord_guild.id) assert guild and guild.xid == discord_guild.id assert guild.name == "guild-name" discord_guild.name = "new-name" await guilds.upsert(discord_guild) DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).get(discord_guild.id) assert guild and guild.xid == discord_guild.id assert guild.name == "new-name"
async def test_lfg_with_friend_when_existing_game(self, game: Game): user1 = UserFactory.create(xid=101) user2 = UserFactory.create(xid=102) games = GamesService() new = await games.upsert( guild_xid=game.guild.xid, channel_xid=game.channel.xid, author_xid=user1.xid, friends=[user2.xid], seats=4, format=GameFormat.COMMANDER.value, ) assert not new DatabaseSession.expire_all() rows = DatabaseSession.query( User.xid).filter(User.game_id == game.id).all() assert set(row[0] for row in rows) == {101, 102}
async def test_lfg_alone_when_no_game(self, guild: Guild, channel: Channel, user: User): games = GamesService() new = await games.upsert( guild_xid=guild.xid, channel_xid=channel.xid, author_xid=user.xid, friends=[], seats=4, format=GameFormat.COMMANDER.value, ) assert new DatabaseSession.expire_all() found_user = DatabaseSession.query(User).one() found_game = DatabaseSession.query(Game).one() assert found_game.guild_xid == guild.xid assert found_game.channel_xid == channel.xid assert found_user.game_id == found_game.id
async def test_guilds_toggle_voice_create(self): guilds = GuildsService() assert not await guilds.select(101) guild = GuildFactory.create(xid=101) guilds = GuildsService() await guilds.select(101) await guilds.toggle_voice_create() DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).get(101) assert guild and guild.voice_create await guilds.select(101) await guilds.toggle_voice_create() DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).get(101) assert guild and not guild.voice_create
async def test_guilds_toggle_show_points(self): guilds = GuildsService() assert not await guilds.select(101) guild = GuildFactory.create(xid=101, name="guild-name") guilds = GuildsService() await guilds.select(101) await guilds.toggle_show_points() DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).get(101) assert guild and guild.show_points await guilds.select(101) await guilds.toggle_show_points() DatabaseSession.expire_all() guild = DatabaseSession.query(Guild).get(101) assert guild and not guild.show_points
async def test_ban_and_unban(self) -> None: target_user = MagicMock() target_user.id = 1002 cog = BanCog(self.bot) await self.run(cog, cog.ban, self.context, str(target_user.id)) self.context.author.send.assert_called_once_with( f"User <@{target_user.id}> has been banned.", ) users = list(DatabaseSession.query(User).all()) assert len(users) == 1 assert users[0].xid == target_user.id assert users[0].banned DatabaseSession.expire_all() await self.run(cog, cog.unban, self.context, str(target_user.id)) users = list(DatabaseSession.query(User).all()) assert len(users) == 1 assert users[0].xid == target_user.id assert not users[0].banned
async def test_channels_upsert(self, guild: Guild): channels = ChannelsService() discord_channel = MagicMock() discord_channel.id = 201 discord_channel.name = "channel-name" discord_guild = MagicMock() discord_guild.id = guild.xid discord_channel.guild = discord_guild await channels.upsert(discord_channel) DatabaseSession.expire_all() channel = DatabaseSession.query(Channel).get(discord_channel.id) assert channel and channel.xid == discord_channel.id assert channel.name == "channel-name" discord_channel.name = "new-name" await channels.upsert(discord_channel) DatabaseSession.expire_all() channel = DatabaseSession.query(Channel).get(discord_channel.id) assert channel and channel.xid == discord_channel.id assert channel.name == "new-name"
async def test_users_upsert(self): user = UserFactory.create(xid=201) discord_user = MagicMock() discord_user.id = 201 discord_user.display_name = "user-name" users = UsersService() await users.upsert(discord_user) DatabaseSession.expire_all() user = DatabaseSession.query(User).get(discord_user.id) assert user and user.xid == discord_user.id assert user.name == "user-name" discord_user.display_name = "new-name" await users.upsert(discord_user) DatabaseSession.expire_all() user = DatabaseSession.query(User).get(discord_user.id) assert user and user.xid == discord_user.id assert user.name == "new-name"
async def test_games_record_plays(self, guild: Guild, channel: Channel): game = GameFactory.create( guild=guild, channel=channel, seats=2, status=GameStatus.STARTED.value, ) user1 = UserFactory.create(xid=101, game=game) user2 = UserFactory.create(xid=102, game=game) games = GamesService() await games.select(game.id) await games.record_plays() DatabaseSession.expire_all() found = DatabaseSession.query(Play).filter( Play.user_xid == user1.xid).one() assert found.user_xid == user1.xid and found.game_id == game.id found = DatabaseSession.query(Play).filter( Play.user_xid == user2.xid).one() assert found.user_xid == user2.xid and found.game_id == game.id found = DatabaseSession.query(Play).filter( Play.user_xid == 103).one_or_none() assert not found
async def test_fetch(self): guild1 = GuildFactory.create() guild2 = GuildFactory.create() user1 = UserFactory.create(xid=101) user2 = UserFactory.create(xid=102) user3 = UserFactory.create(xid=103) watch1 = WatchFactory.create(guild_xid=guild1.xid, user_xid=user1.xid) watch2 = WatchFactory.create(guild_xid=guild1.xid, user_xid=user2.xid) WatchFactory.create(guild_xid=guild2.xid, user_xid=user3.xid) DatabaseSession.expire_all() watches = WatchesService() assert await watches.fetch(guild1.xid) == [ { "guild_xid": guild1.xid, "note": watch1.note, "user_xid": user1.xid, }, { "guild_xid": guild1.xid, "note": watch2.note, "user_xid": user2.xid, }, ]
async def test_lfg_fully_seated( self, cog: LookingForGameCog, add_channel: Callable[..., Channel], ): channel = add_channel(default_seats=2, xid=self.interaction.channel_id) game = self.factories.game.create( guild=self.guild, channel=channel, seats=2, message_xid=123, ) other_user = self.factories.user.create(xid=self.interaction.user.id + 1, game=game) other_player = mock_discord_object(other_user) with mock_operations(lfg_action, users=[other_player]): message = MagicMock(spec=discord.Message) message.id = game.message_xid lfg_action.safe_get_partial_message.return_value = message await self.run(cog.lfg) DatabaseSession.expire_all() game = DatabaseSession.query(Game).one() mock_call = lfg_action.safe_update_embed assert mock_call.call_args_list[0].kwargs["embed"].to_dict() == { "color": self.settings.EMBED_COLOR, "description": ("Please check your Direct Messages for your SpellTable link.\n\n" f"{self.guild.motd}\n\n{channel.motd}"), "fields": [ { "inline": False, "name": "Players", "value": f"<@{self.interaction.user.id}>, <@{other_player.id}>", }, { "inline": True, "name": "Format", "value": "Commander" }, { "inline": True, "name": "Started at", "value": f"<t:{game.started_at_timestamp}>", }, ], "footer": { "text": f"SpellBot Game ID: #SB{game.id}" }, "thumbnail": { "url": self.settings.THUMB_URL }, "title": "**Your game is ready!**", "type": "rich", }