1 | from unittest import mock |
||
2 | import pytest |
||
3 | |||
4 | from server import GameStatsService, LobbyConnection |
||
5 | from server.games import Game |
||
6 | from server.gameconnection import GameConnection, GameConnectionState |
||
7 | from tests import CoroMock |
||
8 | |||
9 | @pytest.fixture() |
||
10 | def lobbythread(): |
||
11 | return mock.Mock( |
||
12 | sendJSON=lambda obj: None |
||
13 | ) |
||
14 | |||
15 | View Code Duplication | @pytest.fixture |
|
16 | def game_connection(request, game, loop, player_service, players, game_service, transport): |
||
17 | from server import GameConnection, LobbyConnection |
||
18 | conn = GameConnection(loop=loop, |
||
19 | lobby_connection=mock.create_autospec(LobbyConnection(loop)), |
||
20 | player_service=player_service, |
||
21 | games=game_service) |
||
22 | conn._transport = transport |
||
23 | conn.player = players.hosting |
||
24 | conn.game = game |
||
25 | conn.lobby = mock.Mock(spec=LobbyConnection) |
||
26 | |||
27 | def fin(): |
||
28 | conn.abort() |
||
29 | |||
30 | request.addfinalizer(fin) |
||
31 | return conn |
||
32 | |||
33 | |||
34 | View Code Duplication | @pytest.fixture |
|
35 | def mock_game_connection(state=GameConnectionState.INITIALIZING, player=None): |
||
36 | gc = mock.create_autospec(spec=GameConnection) |
||
37 | gc.state = state |
||
38 | gc.player = player |
||
39 | return gc |
||
40 | |||
41 | |||
42 | @pytest.fixture() |
||
43 | def game_stats_service(): |
||
44 | service = mock.Mock(spec=GameStatsService) |
||
45 | service.process_game_stats = CoroMock() |
||
46 | return service |
||
47 | |||
48 | |||
49 | @pytest.fixture |
||
50 | def connections(loop, player_service, game_service, transport, game): |
||
51 | from server import GameConnection |
||
52 | |||
53 | def make_connection(player, connectivity): |
||
54 | lc = LobbyConnection(loop) |
||
55 | lc.protocol = mock.Mock() |
||
56 | conn = GameConnection(loop=loop, |
||
57 | lobby_connection=lc, |
||
58 | player_service=player_service, |
||
59 | games=game_service) |
||
60 | conn.player = player |
||
61 | conn.game = game |
||
62 | conn._transport = transport |
||
0 ignored issues
–
show
|
|||
63 | conn._connectivity_state.set_result(connectivity) |
||
64 | return conn |
||
65 | |||
66 | return mock.Mock( |
||
67 | make_connection=make_connection |
||
68 | ) |
||
69 | |||
70 | def add_connected_player(game: Game, player): |
||
71 | game.game_service.player_service[player.id] = player |
||
72 | gc = mock_game_connection(state=GameConnectionState.CONNECTED_TO_HOST, player=player) |
||
73 | game.set_player_option(player.id, 'Army', 0) |
||
74 | game.set_player_option(player.id, 'StartSpot', 0) |
||
75 | game.set_player_option(player.id, 'Team', 0) |
||
76 | game.set_player_option(player.id, 'Faction', 0) |
||
77 | game.set_player_option(player.id, 'Color', 0) |
||
78 | game.add_game_connection(gc) |
||
79 | return gc |
||
80 | |||
81 | |||
82 | def add_connected_players(game: Game, players): |
||
83 | """ |
||
84 | Utility to add players with army and StartSpot indexed by a list |
||
85 | """ |
||
86 | for army, player in enumerate(players): |
||
87 | add_connected_player(game, player) |
||
88 | game.set_player_option(player.id, 'Army', army) |
||
89 | game.set_player_option(player.id, 'StartSpot', army) |
||
90 | game.set_player_option(player.id, 'Team', army) |
||
91 | game.set_player_option(player.id, 'Faction', 0) |
||
92 | game.set_player_option(player.id, 'Color', 0) |
||
93 | game.host = players[0] |
||
94 | |||
95 |
Prefixing a member variable
_
is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: