Closed Gregory-Nitch closed 6 months ago
Might be a little forward but I took some time to make some unit tests for the leaderboard functionality, should it be merged and should it need unit tests. I've set the pull request as a draft, if you see any required changes I would love to make them. Have a good weekend!
-Greg
Check proper rejection of a requested leaderboard length < 1:
def test_game_rejects_invalid_leaderboard_length(game_state: game.WordDebtGame):
with pytest.raises(ValueError):
game_state.create_leaderboard("debt", 0)
Check proper rejection of an invalid sorting key, ie != "debt" or "cranes":
def test_game_rejects_invalid_leaderboard_sorting(game_state: game.WordDebtGame):
with pytest.raises(ValueError):
game_state.create_leaderboard("INVALID", 1)
Check proper leaderboard layout given 1 player and test values:
def test_game_produces_valid_leaderboard(
game_state: game.WordDebtGame, player: game.WordDebtPlayer
):
player.display_name = "test_name"
player.word_debt = 10000
player.cranes = 0
game_state.register_player(player)
assert (
game_state.create_leaderboard("debt", 1)
== f"1. {player.display_name} - {player.word_debt:,} debt - {player.cranes:,} cranes\n"
)
Check command properly informs users if no one has registered and a leaderboard cannot be made:
@pytest.mark.asyncio
async def test_request_leaderboard_no_register(game_commands_cog: cogs.GameCommands):
ctx = AsyncMock()
await game_commands_cog.leaderboard(game_commands_cog, ctx)
ctx.send.assert_called_with(String() & Regex("No registered users.*"))
Check command properly informs users when the game has not been properly loaded.
@pytest.mark.asyncio
async def test_request_leaderboard_no_game(game_commands_cog: cogs.GameCommands):
ctx = AsyncMock()
game_commands_cog.game = None
await game_commands_cog.leaderboard(game_commands_cog, ctx)
ctx.send.assert_called_with(String() & Regex("Game not loaded.*"))
It looks like these commits were put on the main branch making a large diff / merge conflicts when creating a PR for main -- Can you rebase the new commits to dev
to get an accurate diff and resolve the merge conflicts?
Sorry for the headache, obviously I have more learning on git to do. My attempt to rebase to dev is still yielding 22 file changes. I'll see what I can do.
Created new branch & pull request to avoid the above mess:
Targets Issue Leaderboard command #5
Unfortunately, GitHub is showing 1k plus additions for this pull request once I switched to the dev branch from the main branch, this is my first pull request ever; forgive me if I've done something silly.
Below you can find the code that is added and where.
Implements
leaderboard()
function ingame_commands.py
create_leaderboard()
method incore.py
The default values for the leaderboard command are 'debt' and 5 therefore sending a bot message with the top 5 users based on debt, but users can input cranes and any other length.
Achieved functionality:
Though with this solution it requires the user to enter a sorting type if they wish to enter a leaderboard length requirement.
Code
leaderboard()
function ingame_commands.py
-> Lines 71-83create_leaderboard()
method incore.py
-> Lines 59-79Hope this helps!