Skip to content

Commit

Permalink
Remove Player class mocking in tests
Browse files Browse the repository at this point in the history
The Player class is essentially plain old data right now. Remove all
mocking of it to make it easier to modify. If we ever bless Player with
non-trivial behaviour, we can make a mock or an abstract class for it.

FIXME - centralized creation.

Signed-off-by: Igor Kotrasinski <i.kotrasinsk@gmail.com>
  • Loading branch information
Igor Kotrasinski committed Aug 21, 2019
1 parent f0a7035 commit 2ad84e6
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 331 deletions.
23 changes: 9 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,28 +179,25 @@ def make_game(uid, players):


@pytest.fixture
def create_player():
def player_factory():
from server.players import Player, PlayerState

def make(login='', id=0, port=6112, state=PlayerState.HOSTING, ip='127.0.0.1', global_rating=Rating(1500, 250), ladder_rating=Rating(1500, 250)):
p = mock.create_autospec(spec=Player(login))
p.global_rating = global_rating
p.ladder_rating = ladder_rating
p.ip = ip
# FIXME
def make(state=PlayerState.IDLE, ip='127.0.0.1', **kwargs):
p = Player(ip=ip, **kwargs)
p.state = state
p.id = id
p.login = login
return p

return make


@pytest.fixture
def players(create_player):
def players(player_factory):
from server.players import PlayerState
return mock.Mock(
hosting=create_player(login='Paula_Bean', id=1, port=6112, state=PlayerState.HOSTING),
peer=create_player(login='That_Guy', id=2, port=6112, state=PlayerState.JOINING),
joining=create_player(login='James_Kirk', id=3, port=6112, state=PlayerState.JOINING)
hosting=player_factory(login='Paula_Bean', player_id=1, state=PlayerState.HOSTING),
peer=player_factory(login='That_Guy', player_id=2, state=PlayerState.JOINING),
joining=player_factory(login='James_Kirk', player_id=3, state=PlayerState.JOINING)
)


Expand Down Expand Up @@ -285,5 +282,3 @@ def twilio_sid():
@pytest.fixture
def twilio_token():
return "token_a"


28 changes: 17 additions & 11 deletions tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,23 @@ def add_connected_players(game: BaseGame, players):
game.host = players[0]


def add_players(gameobj: BaseGame, n: int, team: int=None):
game = gameobj
current = len(game.players)
players = []
for i in range(current, current+n):
players.append(Player(player_id=i + 1, login=f'Player {i + 1}', global_rating=(1500, 500)))
@pytest.fixture
def game_add_players(player_factory):
def add(gameobj: BaseGame, n: int, team: int=None):
game = gameobj
current = len(game.players)
players = []
for i in range(current, current+n):
p = player_factory(player_id=i+1, login=f'Player {i + 1}',
global_rating=(1500, 500))
players.append(p)

add_connected_players(game, players)

add_connected_players(game, players)
if team is not None:
for p in players:
game.set_player_option(p.id, 'Team', team)

if team is not None:
for p in players:
game.set_player_option(p.id, 'Team', team)
return players

return players
return add
27 changes: 8 additions & 19 deletions tests/unit_tests/ladder_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
from unittest import mock


import pytest

from server import LadderService
from server.players import Player


@pytest.fixture()
def map_pool():
return [(1, '', 'scmp_001'), (5, '', 'scmp_05'), (10, '', 'scmp_010'), (12, '', 'scmp_012'), (11, '', 'scmp_0011')]


def playerMock(lobbythread, id):
player_mock = mock.create_autospec(spec=Player(''))
player_mock.login = "Player %s" % id
player_mock.id = id
player_mock.lobby_connection = lobbythread
return player_mock
return [(1, '', 'scmp_001'), (5, '', 'scmp_05'), (10, '', 'scmp_010'),
(12, '', 'scmp_012'), (11, '', 'scmp_0011')]


@pytest.fixture()
def player1(lobbythread):
return playerMock(lobbythread, 1)
def player1(lobbythread, player_factory):
return player_factory(login=f"Player 1", player_id=1,
lobby_connection=lobbythread)


@pytest.fixture()
def player2(lobbythread):
return playerMock(lobbythread, 2)
def player2(lobbythread, player_factory):
return player_factory(login=f"Player 2", player_id=2,
lobby_connection=lobbythread)


@pytest.fixture()
Expand Down
38 changes: 20 additions & 18 deletions tests/unit_tests/test_custom_game.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from unittest import mock

import pytest
import time

from server.games.game import GameState, ValidityState
from server.games import CustomGame
from server.players import Player
from tests.unit_tests.conftest import add_connected_players, add_players
from tests.unit_tests.conftest import add_connected_players


@pytest.yield_fixture
Expand All @@ -16,28 +13,31 @@ def custom_game(loop, game_service, game_stats_service):
loop.run_until_complete(game.clear_data())


async def test_rate_game_early_abort_no_enforce(game_service, game_stats_service, custom_game):
async def test_rate_game_early_abort_no_enforce(
game_service, game_stats_service, custom_game, player_factory):
custom_game.state = GameState.LOBBY
players = [
Player(player_id=1, login='Dostya', global_rating=(1500, 500)),
Player(player_id=2, login='Rhiza', global_rating=(1500, 500)),
player_factory(player_id=1, login='Dostya', global_rating=(1500, 500)),
player_factory(player_id=2, login='Rhiza', global_rating=(1500, 500)),
]
add_connected_players(custom_game, players)
custom_game.set_player_option(1, 'Team', 2)
custom_game.set_player_option(2, 'Team', 3)
await custom_game.launch()
await custom_game.add_result(0, 1, 'VICTORY', 5)

custom_game.launched_at = time.time() - 60 # seconds
custom_game.launched_at = time.time() - 60 # seconds

await custom_game.on_game_end()
assert custom_game.validity == ValidityState.TOO_SHORT

async def test_rate_game_early_abort_with_enforce(game_service, game_stats_service, custom_game):

async def test_rate_game_early_abort_with_enforce(
game_service, game_stats_service, custom_game, player_factory):
custom_game.state = GameState.LOBBY
players = [
Player(player_id=1, login='Dostya', global_rating=(1500, 500)),
Player(player_id=2, login='Rhiza', global_rating=(1500, 500)),
player_factory(player_id=1, login='Dostya', global_rating=(1500, 500)),
player_factory(player_id=2, login='Rhiza', global_rating=(1500, 500)),
]
add_connected_players(custom_game, players)
custom_game.set_player_option(1, 'Team', 2)
Expand All @@ -52,34 +52,36 @@ async def test_rate_game_early_abort_with_enforce(game_service, game_stats_servi
assert custom_game.validity == ValidityState.VALID


async def test_rate_game_late_abort_no_enforce(game_service, game_stats_service, custom_game):
async def test_rate_game_late_abort_no_enforce(
game_service, game_stats_service, custom_game, player_factory):
custom_game.state = GameState.LOBBY
players = [
Player(player_id=1, login='Dostya', global_rating=(1500, 500)),
Player(player_id=2, login='Rhiza', global_rating=(1500, 500)),
player_factory(player_id=1, login='Dostya', global_rating=(1500, 500)),
player_factory(player_id=2, login='Rhiza', global_rating=(1500, 500)),
]
add_connected_players(custom_game, players)
custom_game.set_player_option(1, 'Team', 2)
custom_game.set_player_option(2, 'Team', 3)
await custom_game.launch()
await custom_game.add_result(0, 1, 'VICTORY', 5)

custom_game.launched_at = time.time() - 600 # seconds
custom_game.launched_at = time.time() - 600 # seconds

await custom_game.on_game_end()
assert custom_game.validity == ValidityState.VALID


async def test_global_rating_higher_after_custom_game_win(custom_game: CustomGame):
async def test_global_rating_higher_after_custom_game_win(
custom_game: CustomGame, game_add_players):
game = custom_game
game.state = GameState.LOBBY
players = add_players(game, 2)
players = game_add_players(game, 2)
game.set_player_option(players[0].id, 'Team', 1)
game.set_player_option(players[1].id, 'Team', 2)
old_mean = players[0].global_rating[0]

await game.launch()
game.launched_at = time.time() - 60*20 # seconds
game.launched_at = time.time() - 60*20 # seconds
await game.add_result(0, 0, 'victory', 5)
await game.add_result(0, 1, 'defeat', -5)
await game.on_game_end()
Expand Down
Loading

0 comments on commit 2ad84e6

Please sign in to comment.