|
1
|
|
|
""" |
|
2
|
|
|
This module is the 'top level' configuration for all the unit tests. |
|
3
|
|
|
|
|
4
|
|
|
'Real world' fixtures are put here. |
|
5
|
|
|
If a test suite needs specific mocked versions of dependencies, |
|
6
|
|
|
these should be put in the ``conftest.py'' relative to it. |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
import asyncio |
|
10
|
|
|
|
|
11
|
|
|
import logging |
|
12
|
|
|
import subprocess |
|
|
|
|
|
|
13
|
|
|
import sys |
|
|
|
|
|
|
14
|
|
|
from server.config import DB_SERVER, DB_LOGIN, DB_PORT, DB_PASSWORD |
|
15
|
|
|
|
|
16
|
|
|
import pytest |
|
17
|
|
|
from unittest import mock |
|
18
|
|
|
from trueskill import Rating |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
logging.getLogger().setLevel(logging.DEBUG) |
|
21
|
|
|
|
|
22
|
|
|
import os |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def async_test(f): |
|
26
|
|
|
def wrapper(*args, **kwargs): |
|
27
|
|
|
coro = asyncio.coroutine(f) |
|
28
|
|
|
future = coro(*args, **kwargs) |
|
29
|
|
|
loop = asyncio.get_event_loop() |
|
|
|
|
|
|
30
|
|
|
loop.run_until_complete(future) |
|
31
|
|
|
return wrapper |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def pytest_pycollect_makeitem(collector, name, obj): |
|
35
|
|
|
if name.startswith('test_') and asyncio.iscoroutinefunction(obj): |
|
36
|
|
|
return list(collector._genfunctions(name, obj)) |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def pytest_addoption(parser): |
|
40
|
|
|
parser.addoption('--noslow', action='store_true', default=False, |
|
41
|
|
|
help="Don't run slow tests") |
|
42
|
|
|
parser.addoption('--aiodebug', action='store_true', default=False, |
|
43
|
|
|
help='Enable asyncio debugging') |
|
44
|
|
|
parser.addoption('--mysql_host', action='store', default=DB_SERVER, help='mysql host to use for test database') |
|
45
|
|
|
parser.addoption('--mysql_username', action='store', default=DB_LOGIN, help='mysql username to use for test database') |
|
|
|
|
|
|
46
|
|
|
parser.addoption('--mysql_password', action='store', default=DB_PASSWORD, help='mysql password to use for test database') |
|
|
|
|
|
|
47
|
|
|
parser.addoption('--mysql_database', action='store', default='faf_test', help='mysql database to use for tests') |
|
48
|
|
|
parser.addoption('--mysql_port', action='store', default=int(DB_PORT), help='mysql port to use for tests') |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def pytest_configure(config): |
|
52
|
|
|
if config.getoption('--aiodebug'): |
|
53
|
|
|
logging.getLogger('quamash').setLevel(logging.DEBUG) |
|
54
|
|
|
logging.captureWarnings(True) |
|
55
|
|
|
else: |
|
56
|
|
|
logging.getLogger('quamash').setLevel(logging.INFO) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def pytest_runtest_setup(item): |
|
60
|
|
|
""" |
|
61
|
|
|
Skip tests if they are marked slow, and --noslow is given on the commandline |
|
62
|
|
|
:param item: |
|
63
|
|
|
:return: |
|
64
|
|
|
""" |
|
65
|
|
|
if getattr(item.obj, 'slow', None) and item.config.getvalue('noslow'): |
|
66
|
|
|
pytest.skip("slow test") |
|
67
|
|
|
|
|
68
|
|
|
def pytest_pyfunc_call(pyfuncitem): |
|
69
|
|
|
testfn = pyfuncitem.obj |
|
70
|
|
|
|
|
71
|
|
|
if not asyncio.iscoroutinefunction(testfn): |
|
72
|
|
|
return |
|
73
|
|
|
|
|
74
|
|
|
funcargs = pyfuncitem.funcargs |
|
75
|
|
|
testargs = {} |
|
76
|
|
|
for arg in pyfuncitem._fixtureinfo.argnames: |
|
|
|
|
|
|
77
|
|
|
testargs[arg] = funcargs[arg] |
|
78
|
|
|
loop = testargs.get('loop', asyncio.get_event_loop()) |
|
|
|
|
|
|
79
|
|
|
loop.set_debug(True) |
|
80
|
|
|
coro = asyncio.wait_for(testfn(**testargs), 5) |
|
81
|
|
|
|
|
82
|
|
|
try: |
|
83
|
|
|
loop.run_until_complete(coro) |
|
84
|
|
|
except RuntimeError as err: |
|
85
|
|
|
logging.error(err) |
|
86
|
|
|
raise err |
|
87
|
|
|
return True |
|
88
|
|
|
|
|
89
|
|
|
@pytest.fixture(scope='session', autouse=True) |
|
90
|
|
|
def loop(request): |
|
|
|
|
|
|
91
|
|
|
import server |
|
92
|
|
|
server.stats = mock.MagicMock() |
|
93
|
|
|
return asyncio.get_event_loop() |
|
94
|
|
|
|
|
95
|
|
|
@pytest.fixture |
|
96
|
|
|
def sqlquery(): |
|
97
|
|
|
query = mock.MagicMock() |
|
98
|
|
|
query.exec_ = lambda: 0 |
|
99
|
|
|
query.size = lambda: 0 |
|
100
|
|
|
query.lastInsertId = lambda: 1 |
|
101
|
|
|
query.prepare = mock.MagicMock() |
|
102
|
|
|
query.addBindValue = lambda v: None |
|
103
|
|
|
return query |
|
104
|
|
|
|
|
105
|
|
|
@pytest.fixture |
|
106
|
|
|
def mock_db_pool(loop, db_pool, autouse=True): |
|
|
|
|
|
|
107
|
|
|
return db_pool |
|
108
|
|
|
|
|
109
|
|
|
@pytest.fixture(scope='session') |
|
110
|
|
|
def db_pool(request, loop): |
|
|
|
|
|
|
111
|
|
|
import server |
|
112
|
|
|
|
|
113
|
|
|
def opt(val): |
|
114
|
|
|
return request.config.getoption(val) |
|
115
|
|
|
host, user, pw, db, port = opt('--mysql_host'), opt('--mysql_username'), opt('--mysql_password'), opt('--mysql_database'), opt('--mysql_port') |
|
|
|
|
|
|
116
|
|
|
pool_fut = asyncio.async(server.db.connect(loop=loop, |
|
117
|
|
|
host=host, |
|
118
|
|
|
user=user, |
|
119
|
|
|
password=pw or None, |
|
120
|
|
|
port=port, |
|
121
|
|
|
db=db)) |
|
122
|
|
|
pool = loop.run_until_complete(pool_fut) |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
def fin(): |
|
126
|
|
|
pool.close() |
|
127
|
|
|
loop.run_until_complete(pool.wait_closed()) |
|
128
|
|
|
request.addfinalizer(fin) |
|
129
|
|
|
|
|
130
|
|
|
return pool |
|
131
|
|
|
|
|
132
|
|
|
@pytest.fixture |
|
133
|
|
|
def transport(): |
|
134
|
|
|
return mock.Mock(spec=asyncio.Transport) |
|
135
|
|
|
|
|
136
|
|
|
@pytest.fixture |
|
137
|
|
|
def game(players): |
|
|
|
|
|
|
138
|
|
|
from server.games import Game |
|
139
|
|
|
from server.abc.base_game import InitMode |
|
|
|
|
|
|
140
|
|
|
mock_parent = mock.Mock() |
|
141
|
|
|
game = mock.create_autospec(spec=Game(1, mock_parent, mock.Mock())) |
|
|
|
|
|
|
142
|
|
|
players.hosting.getGame = mock.Mock(return_value=game) |
|
143
|
|
|
players.joining.getGame = mock.Mock(return_value=game) |
|
144
|
|
|
players.peer.getGame = mock.Mock(return_value=game) |
|
145
|
|
|
game.hostPlayer = players.hosting |
|
146
|
|
|
game.init_mode = InitMode.NORMAL_LOBBY |
|
147
|
|
|
game.name = "Some game name" |
|
148
|
|
|
game.id = 1 |
|
149
|
|
|
return game |
|
150
|
|
|
|
|
151
|
|
|
@pytest.fixture |
|
152
|
|
|
def create_player(): |
|
153
|
|
|
from server.players import Player, PlayerState |
|
154
|
|
|
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)): |
|
|
|
|
|
|
155
|
|
|
p = mock.create_autospec(spec=Player(login)) |
|
156
|
|
|
p.global_rating = global_rating |
|
157
|
|
|
p.ladder_rating = ladder_rating |
|
158
|
|
|
p.ip = ip |
|
159
|
|
|
p.game_port = port |
|
160
|
|
|
p.state = state |
|
161
|
|
|
p.id = id |
|
162
|
|
|
p.login = login |
|
163
|
|
|
p.address_and_port = "{}:{}".format(ip, port) |
|
164
|
|
|
return p |
|
165
|
|
|
return make |
|
166
|
|
|
|
|
167
|
|
|
@pytest.fixture |
|
168
|
|
|
def players(create_player): |
|
|
|
|
|
|
169
|
|
|
from server.players import PlayerState |
|
170
|
|
|
return mock.Mock( |
|
171
|
|
|
hosting=create_player(login='Paula_Bean', id=1, port=6112, state=PlayerState.HOSTING), |
|
172
|
|
|
peer=create_player(login='That_Guy', id=2, port=6112, state=PlayerState.JOINING), |
|
173
|
|
|
joining=create_player(login='James_Kirk', id=3, port=6112, state=PlayerState.JOINING) |
|
174
|
|
|
) |
|
175
|
|
|
|
|
176
|
|
|
@pytest.fixture |
|
177
|
|
|
def player_service(loop, players, db_pool): |
|
|
|
|
|
|
178
|
|
|
from server import PlayerService |
|
179
|
|
|
return PlayerService(db_pool) |
|
180
|
|
|
|
|
181
|
|
|
@pytest.fixture |
|
182
|
|
|
def game_service(loop, player_service, game_stats_service): |
|
|
|
|
|
|
183
|
|
|
from server import GameService |
|
184
|
|
|
return GameService(player_service, game_stats_service) |
|
185
|
|
|
|
|
186
|
|
|
@pytest.fixture |
|
187
|
|
|
def api_accessor(): |
|
188
|
|
|
from server.api.api_accessor import ApiAccessor |
|
|
|
|
|
|
189
|
|
|
return ApiAccessor() |
|
190
|
|
|
|
|
191
|
|
|
@pytest.fixture |
|
192
|
|
|
def event_service(api_accessor): |
|
|
|
|
|
|
193
|
|
|
from server.stats.event_service import EventService |
|
|
|
|
|
|
194
|
|
|
return EventService(api_accessor) |
|
195
|
|
|
|
|
196
|
|
|
@pytest.fixture |
|
197
|
|
|
def achievement_service(api_accessor): |
|
|
|
|
|
|
198
|
|
|
from server.stats.achievement_service import AchievementService |
|
|
|
|
|
|
199
|
|
|
return AchievementService(api_accessor) |
|
200
|
|
|
|
|
201
|
|
|
@pytest.fixture |
|
202
|
|
|
def game_stats_service(event_service, achievement_service): |
|
|
|
|
|
|
203
|
|
|
from server.stats.game_stats_service import GameStatsService |
|
|
|
|
|
|
204
|
|
|
return GameStatsService(event_service, achievement_service) |
|
205
|
|
|
|