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
|
|
|
@asyncio.coroutine |
125
|
|
|
def setup(): |
126
|
|
|
cmd = 'SET default_storage_engine=MEMORY; drop database if exists {}; create database {}; use {}; source {};'.format(db, db, db, 'db/db-structure.sql') |
|
|
|
|
127
|
|
|
try: |
128
|
|
|
subprocess.check_output(['mysql', |
129
|
|
|
'-h{}'.format(host), |
130
|
|
|
'-u{}'.format(user), |
131
|
|
|
'-p{}'.format(pw) if pw else '', '-e {}'.format(cmd)], |
132
|
|
|
stderr=subprocess.STDOUT) |
133
|
|
|
subprocess.check_call(['mysql', |
134
|
|
|
'-h{}'.format(host), |
135
|
|
|
'-u{}'.format(user), |
136
|
|
|
'-p{}'.format(pw) if pw else '', |
137
|
|
|
'-e use {}; source {};'.format(db, 'tests/data/db-fixtures.sql')], |
138
|
|
|
stderr=subprocess.STDOUT) |
139
|
|
|
except subprocess.CalledProcessError as e: |
140
|
|
|
raise Exception('SQL error: {} (cmd: {})'.format(e.output, e.cmd), e) |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
def fin(): |
144
|
|
|
pool.close() |
145
|
|
|
loop.run_until_complete(pool.wait_closed()) |
146
|
|
|
request.addfinalizer(fin) |
147
|
|
|
|
148
|
|
|
loop.run_until_complete(setup()) |
149
|
|
|
|
150
|
|
|
return pool |
151
|
|
|
|
152
|
|
|
@pytest.fixture |
153
|
|
|
def transport(): |
154
|
|
|
return mock.Mock(spec=asyncio.Transport) |
155
|
|
|
|
156
|
|
|
@pytest.fixture |
157
|
|
|
def game(players): |
|
|
|
|
158
|
|
|
from server.games import Game |
159
|
|
|
from server.abc.base_game import InitMode |
|
|
|
|
160
|
|
|
mock_parent = mock.Mock() |
161
|
|
|
game = mock.create_autospec(spec=Game(1, mock_parent, mock.Mock())) |
|
|
|
|
162
|
|
|
players.hosting.getGame = mock.Mock(return_value=game) |
163
|
|
|
players.joining.getGame = mock.Mock(return_value=game) |
164
|
|
|
players.peer.getGame = mock.Mock(return_value=game) |
165
|
|
|
game.hostPlayer = players.hosting |
166
|
|
|
game.init_mode = InitMode.NORMAL_LOBBY |
167
|
|
|
game.name = "Some game name" |
168
|
|
|
game.id = 1 |
169
|
|
|
return game |
170
|
|
|
|
171
|
|
|
@pytest.fixture |
172
|
|
|
def create_player(): |
173
|
|
|
from server.players import Player, PlayerState |
174
|
|
|
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)): |
|
|
|
|
175
|
|
|
p = mock.create_autospec(spec=Player(login)) |
176
|
|
|
p.global_rating = global_rating |
177
|
|
|
p.ladder_rating = ladder_rating |
178
|
|
|
p.ip = ip |
179
|
|
|
p.game_port = port |
180
|
|
|
p.state = state |
181
|
|
|
p.id = id |
182
|
|
|
p.login = login |
183
|
|
|
p.address_and_port = "{}:{}".format(ip, port) |
184
|
|
|
return p |
185
|
|
|
return make |
186
|
|
|
|
187
|
|
|
@pytest.fixture |
188
|
|
|
def players(create_player): |
|
|
|
|
189
|
|
|
from server.players import PlayerState |
190
|
|
|
return mock.Mock( |
191
|
|
|
hosting=create_player(login='Paula_Bean', id=1, port=6112, state=PlayerState.HOSTING), |
192
|
|
|
peer=create_player(login='That_Guy', id=2, port=6112, state=PlayerState.JOINING), |
193
|
|
|
joining=create_player(login='James_Kirk', id=3, port=6112, state=PlayerState.JOINING) |
194
|
|
|
) |
195
|
|
|
|
196
|
|
|
@pytest.fixture |
197
|
|
|
def player_service(loop, players, db_pool): |
|
|
|
|
198
|
|
|
from server import PlayerService |
199
|
|
|
return PlayerService(db_pool) |
200
|
|
|
|
201
|
|
|
@pytest.fixture |
202
|
|
|
def game_service(loop, player_service, game_stats_service): |
|
|
|
|
203
|
|
|
from server import GameService |
204
|
|
|
return GameService(player_service, game_stats_service) |
205
|
|
|
|
206
|
|
|
@pytest.fixture |
207
|
|
|
def api_accessor(): |
208
|
|
|
from server.api.api_accessor import ApiAccessor |
|
|
|
|
209
|
|
|
return ApiAccessor() |
210
|
|
|
|
211
|
|
|
@pytest.fixture |
212
|
|
|
def event_service(api_accessor): |
|
|
|
|
213
|
|
|
from server.stats.event_service import EventService |
|
|
|
|
214
|
|
|
return EventService(api_accessor) |
215
|
|
|
|
216
|
|
|
@pytest.fixture |
217
|
|
|
def achievement_service(api_accessor): |
|
|
|
|
218
|
|
|
from server.stats.achievement_service import AchievementService |
|
|
|
|
219
|
|
|
return AchievementService(api_accessor) |
220
|
|
|
|
221
|
|
|
@pytest.fixture |
222
|
|
|
def game_stats_service(event_service, achievement_service): |
|
|
|
|
223
|
|
|
from server.stats.game_stats_service import GameStatsService |
|
|
|
|
224
|
|
|
return GameStatsService(event_service, achievement_service) |
225
|
|
|
|