async def test_post_saves_message_id_to_game(): # Given guild_id = 1001 game_id = 'game-1' channel_id = 50001 new_message_id = 1000 discord_messaging = FakeDiscordMessaging() discord_messaging.created_channel_message_id = 1000 game = Game(guild_id=guild_id, game_id=game_id) games_repository = FakeGamesRepository([game]) post_route = PostRoute( games_repository=games_repository, discord_messaging=discord_messaging, message_provider=FakeMessageProvider(), ) # When event = _make_event(guild_id=guild_id, options={ 'game-id': game_id, 'channel': channel_id, }) await post_route.call(event) # Then updated_game = games_repository.get(guild_id, game_id) assert updated_game.channel_messages is not None assert any(channel_message.channel_id == channel_id and channel_message.message_id == new_message_id for channel_message in updated_game.channel_messages)
async def test_close_already_closed_game(): # Given guild_id = 1007 game_id = 'game-id-1' # We have a closed game game = Game(game_id=game_id, guild_id=guild_id, closed=True) games_repository = FakeGamesRepository([game]) event = _make_event(guild_id=guild_id, options={ 'game-id': game.game_id, }) game_already_closed_message = "Game was already closed closed." message_provider = MagicMock(MessageProvider) message_provider.game_closed.return_value = game_already_closed_message route = CloseGameRoute( games_repository=games_repository, discord_messaging=FakeDiscordMessaging(), message_provider=message_provider, ) # When we close it again response = await route.call(event) # Then it should just still be closed saved_game = games_repository.get(guild_id, game.game_id) assert saved_game.closed assert response.is_ephemeral assert response.content == game_already_closed_message
async def test_guess_non_numerical_on_numeric_game(): # Given guild_id = 1005 user_id = 13000 game_id = 'game-id' response_message = "Guess must be a number" message_provider = MagicMock(MessageProvider, autospec=True) message_provider.invalid_guess.return_value = response_message # And we have a game with a min and a max game = Game(guild_id=guild_id, game_id=game_id, min_guess=1, max_guess=100) games_repository = FakeGamesRepository([game]) guess_route = _route( games_repository=games_repository, message_provider=message_provider, ) # When event = _create_guess_event(guild_id, game.game_id, user_id, 'nickname') response = await guess_route.call(event) # Then assert response.is_ephemeral assert response.content == response_message # And the guess was not added updated_game = games_repository.get(guild_id=guild_id, game_id=game_id) assert len(updated_game.guesses) == 0
async def test_edit_guess(): # Given guild_id = 10 game_id = "game-1" guessing_player_id = 100 old_guess = "<My Guess>" guess_edited_message = "Guess has been edited." message_provider = MagicMock(MessageProvider) message_provider.guess_edited.return_value = guess_edited_message game = Game( guild_id=guild_id, game_id=game_id, guesses={ guessing_player_id: GameGuess( user_id=guessing_player_id, guess=old_guess, ) } ) games_repository = FakeGamesRepository(games=[game]) game_post_manager = AsyncMock(GamePostManager) route = _route( games_repository=games_repository, message_provider=message_provider, game_post_manager=game_post_manager, ) # When new_guess = "500" event = _make_event( guild_id=guild_id, options={ 'game-id': game_id, 'member': str(guessing_player_id), 'guess': new_guess, } ) response = await route.call(event) # Then updated_game = games_repository.get(guild_id, game_id) assert updated_game.guesses[guessing_player_id].guess == new_guess game_post_manager.update.assert_called_with(game) assert response.is_ephemeral assert response.content == guess_edited_message
async def test_guess_duplicate_guess(): # Given guild_id = 1000 game_id = 'game-id' event_channel_id = 7002 guessing_user_id = 2000 member = DiscordMember(user_id=guessing_user_id, ) old_guess = '100' new_guess = '100' existing_guesses = { guessing_user_id: GameGuess(user_id=guessing_user_id, guess=old_guess), } # We have a game existing_game = Game(guild_id=guild_id, game_id=game_id, guesses=existing_guesses) games_repository = FakeGamesRepository(games=[existing_game]) duplicate_guess_message = "You already placed a guess for this game." message_provider = MagicMock(MessageProvider) message_provider.error_duplicate_guess.return_value = duplicate_guess_message guess_route = _route(games_repository=games_repository, message_provider=message_provider) # When we guess '42' as the same user event = _create_guess_event( guild_id=guild_id, user_id=guessing_user_id, game_id=game_id, guess=new_guess, event_channel_id=event_channel_id, member=member, ) response = await guess_route.call(event) # Then no new guess is added saved_game = games_repository.get(guild_id, existing_game.game_id) assert saved_game.guesses[guessing_user_id].guess == old_guess # And an ephemeral error is the response assert response.is_ephemeral assert response.content == duplicate_guess_message
async def test_guess_closed_game(): # Given guild_id = 1515 game_id = 'closed-game' guessing_user_id = 3000 other_user_id = 3050 event_channel_id = 7775 member = DiscordMember() # We have a closed game game = Game(game_id=game_id, guild_id=guild_id, closed=True, guesses={ other_user_id: GameGuess(), }) games_repository = FakeGamesRepository([game]) duplicate_guess_message = "This game has been closed for new guesses." message_provider = MagicMock(MessageProvider) message_provider.error_guess_on_closed_game.return_value = duplicate_guess_message route = _route( games_repository=games_repository, message_provider=message_provider, ) # When event = _create_guess_event( guild_id=guild_id, game_id=game_id, user_id=guessing_user_id, event_channel_id=event_channel_id, member=member, ) response = await route.call(event) # Then: no guess is added saved_game = games_repository.get(guild_id, game_id) assert list(saved_game.guesses.keys()) == [other_user_id] # And an ephemeral error is the response assert response.is_ephemeral assert response.content == duplicate_guess_message
async def test_lower_than_min(): # Given guild_id = 1005 user_id = 13000 game_id = 'game-id' min_guess = -5 response_message = "Guess must be a number higher than -5" message_provider = MagicMock(MessageProvider, autospec=True) message_provider.invalid_guess.return_value = response_message # And we have a game with a max game = Game( guild_id=guild_id, game_id=game_id, min_guess=min_guess, ) games_repository = FakeGamesRepository([game]) guess_route = _route( games_repository=games_repository, message_provider=message_provider, ) # When we guess lower than that guess = "-6" event = _create_guess_event( guild_id=guild_id, game_id=game.game_id, user_id=user_id, user_nickname='nickname', guess=guess, ) response = await guess_route.call(event) # Then assert response.is_ephemeral assert response.content == response_message # And the guess was not added updated_game = games_repository.get(guild_id=guild_id, game_id=game_id) assert len(updated_game.guesses) == 0
async def test_guess_updates_game_guesses(mock_datetime): # Given game_id = 'game-id' guild_id = 1000 other_user_id = 5000 user_id = 100 user_nickname = 'user-1' guess_answer = '42' guess_timestamp = datetime.now() mock_datetime.now.return_value = guess_timestamp existing_game = Game(guild_id=guild_id, game_id=game_id, guesses={ other_user_id: GameGuess(), }) fake_games_repository = FakeGamesRepository([existing_game]) guess_route = _route(games_repository=fake_games_repository, ) # When we make a guess event = _create_guess_event(guild_id=guild_id, game_id=game_id, user_id=user_id, user_nickname=user_nickname, guess=guess_answer) await guess_route.call(event) # Then saved_game = fake_games_repository.get(guild_id, game_id) assert saved_game.guild_id == guild_id assert saved_game.game_id == game_id assert other_user_id in saved_game.guesses.keys() saved_guess = saved_game.guesses[user_id] assert saved_guess.user_id == user_id assert saved_guess.user_nickname == user_nickname assert saved_guess.timestamp == guess_timestamp assert saved_guess.guess == guess_answer
async def test_post_creates_channel_message(): # Given guild_id = 1001 game_id = 'game-1' channel_id = 50001 # We have a game game = Game(guild_id=guild_id, game_id=game_id) games_repository = FakeGamesRepository([game]) # And we have a mock message provider post_embed = discord.Embed() message_provider = MagicMock(MessageProvider) message_provider.game_post_embed.return_value = post_embed discord_messaging = FakeDiscordMessaging() post_route = PostRoute( games_repository=games_repository, discord_messaging=discord_messaging, message_provider=message_provider, ) # When we post a channel message for our game with an explicit channel event = _make_event(guild_id=guild_id, options={ 'game-id': game_id, 'channel': channel_id, }) await post_route.call(event) # Then a message about that game is posted in the given channel assert { 'channel_id': channel_id, 'embed': post_embed } in discord_messaging.sent_channel_messages # And the channel id is saved in the game saved_game = games_repository.get(guild_id, game_id) assert len(saved_game.channel_messages) == 1