Skip to content

Commit

Permalink
Add tests for missing validity states
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Dec 30, 2023
1 parent 4ae503b commit 7eb85ed
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
6 changes: 4 additions & 2 deletions server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ async def validate_game_mode_settings(self):
await self.mark_invalid(ValidityState.UNEVEN_TEAMS_NOT_RANKED)
return

# TODO: This validity state seems to be impossible to get because it is
# already covered by UNEVEN_TEAMS_NOT_RANKED above.
if len(self.players) < 2:
await self.mark_invalid(ValidityState.SINGLE_PLAYER)
return
Expand Down Expand Up @@ -706,8 +708,8 @@ async def update_game_stats(self):
assert self.host is not None

async with self._db.acquire() as conn:
# Determine if the map is blacklisted, and invalidate the game for ranking purposes if
# so, and grab the map id at the same time.
# Determine if the map is blacklisted, and invalidate the game for
# ranking purposes if so, and grab the map id at the same time.
result = await conn.execute(
"SELECT id, ranked FROM map_version "
"WHERE lower(filename) = lower(:filename)",
Expand Down
52 changes: 38 additions & 14 deletions tests/unit_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ async def test_ffa_not_rated(game, game_add_players):
game_add_players(game, 5, team=1)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.FFA_NOT_RANKED
assert game.validity is ValidityState.FFA_NOT_RANKED


async def test_generated_map_is_rated(game, game_add_players):
Expand All @@ -174,7 +174,7 @@ async def test_generated_map_is_rated(game, game_add_players):
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.VALID
assert game.validity is ValidityState.VALID


async def test_unranked_generated_map_not_rated(game, game_add_players):
Expand All @@ -185,17 +185,28 @@ async def test_unranked_generated_map_not_rated(game, game_add_players):
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.BAD_MAP
assert game.validity is ValidityState.BAD_MAP


async def test_unranked_mod_not_rated(game, game_add_players):
game.state = GameState.LOBBY
game_add_players(game, 2, team=1)
game.mods = {"mod-id": "Some mod"}
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity is ValidityState.BAD_MOD


async def test_two_player_ffa_is_rated(game, game_add_players):
game.state = GameState.LOBBY
game_add_players(game, 2, team=1)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.VALID
assert game.validity is ValidityState.VALID


async def test_multi_team_not_rated(game, game_add_players):
Expand All @@ -205,9 +216,9 @@ async def test_multi_team_not_rated(game, game_add_players):
game_add_players(game, 2, team=4)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.MULTI_TEAM
assert game.validity is ValidityState.MULTI_TEAM


async def test_has_ai_players_not_rated(game, game_add_players):
Expand All @@ -227,9 +238,9 @@ async def test_has_ai_players_not_rated(game, game_add_players):
}
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.HAS_AI_PLAYERS
assert game.validity is ValidityState.HAS_AI_PLAYERS


async def test_uneven_teams_not_rated(game, game_add_players):
Expand All @@ -238,19 +249,30 @@ async def test_uneven_teams_not_rated(game, game_add_players):
game_add_players(game, 3, team=3)
await game.launch()
await game.add_result(0, 1, "victory", 5)
game.launched_at = time.time() - 60 * 20 # seconds
game.launched_at = time.time() - 60 * 20 # seconds
await game.on_game_finish()
assert game.validity == ValidityState.UNEVEN_TEAMS_NOT_RANKED
assert game.validity is ValidityState.UNEVEN_TEAMS_NOT_RANKED


async def test_single_team_not_rated(game, game_add_players):
n_players = 4
game.state = GameState.LOBBY
game_add_players(game, n_players, team=2)
print(game._player_options)
await game.launch()
game.launched_at = time.time() - 60 * 20
for i in range(n_players):
await game.add_result(0, i + 1, "victory", 5)
await game.add_result(0, i, "victory", 5)
await game.on_game_finish()
assert game.validity is ValidityState.UNEVEN_TEAMS_NOT_RANKED


async def test_single_player_not_rated(game, game_add_players):
game.state = GameState.LOBBY
game_add_players(game, 1, team=1)
await game.launch()
game.launched_at = time.time() - 60 * 20
await game.add_result(0, 0, "victory", 5)
await game.on_game_finish()
assert game.validity is ValidityState.UNEVEN_TEAMS_NOT_RANKED

Expand Down Expand Up @@ -449,7 +471,9 @@ async def test_game_marked_dirty_when_timed_out(game: Game, game_service):


async def test_clear_slot(
game: Game, mock_game_connection: GameConnection, player_factory
game: Game,
mock_game_connection: GameConnection,
player_factory,
):
game.state = GameState.LOBBY
players = [
Expand Down

0 comments on commit 7eb85ed

Please sign in to comment.