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 |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
13 | import sys |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
The import
trueskill could not be resolved.
This can be caused by one of the following: 1. Missing DependenciesThis error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands. # .scrutinizer.yml
before_commands:
- sudo pip install abc # Python2
- sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use
the command for the correct version.
2. Missing __init__.py filesThis error could also result from missing ![]() |
|||
19 | |||
20 | logging.getLogger().setLevel(logging.DEBUG) |
||
21 | |||
22 | import os |
||
0 ignored issues
–
show
|
|||
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() |
||
0 ignored issues
–
show
loop is re-defining a name which is already available in the outer-scope (previously defined on line 90 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
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)) |
||
0 ignored issues
–
show
It seems like
_genfunctions was declared protected and should not be accessed from this context.
Prefixing a member variable class MyParent:
def __init__(self):
self._x = 1;
self.y = 2;
class MyChild(MyParent):
def some_method(self):
return self._x # Ok, since accessed from a child class
class AnotherClass:
def some_method(self, instance_of_my_child):
return instance_of_my_child._x # Would be flagged as AnotherClass is not
# a child class of MyParent
![]() |
|||
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') |
||
0 ignored issues
–
show
|
|||
46 | parser.addoption('--mysql_password', action='store', default=DB_PASSWORD, help='mysql password to use for test database') |
||
0 ignored issues
–
show
|
|||
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: |
||
0 ignored issues
–
show
It seems like
_fixtureinfo was declared protected and should not be accessed from this context.
Prefixing a member variable class MyParent:
def __init__(self):
self._x = 1;
self.y = 2;
class MyChild(MyParent):
def some_method(self):
return self._x # Ok, since accessed from a child class
class AnotherClass:
def some_method(self, instance_of_my_child):
return instance_of_my_child._x # Would be flagged as AnotherClass is not
# a child class of MyParent
![]() |
|||
77 | testargs[arg] = funcargs[arg] |
||
78 | loop = testargs.get('loop', asyncio.get_event_loop()) |
||
0 ignored issues
–
show
loop is re-defining a name which is already available in the outer-scope (previously defined on line 90 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
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): |
||
0 ignored issues
–
show
|
|||
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): |
||
0 ignored issues
–
show
db_pool is re-defining a name which is already available in the outer-scope (previously defined on line 110 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() loop is re-defining a name which is already available in the outer-scope (previously defined on line 90 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
107 | return db_pool |
||
108 | |||
109 | @pytest.fixture(scope='session') |
||
110 | def db_pool(request, loop): |
||
0 ignored issues
–
show
loop is re-defining a name which is already available in the outer-scope (previously defined on line 90 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
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') |
||
0 ignored issues
–
show
|
|||
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): |
||
0 ignored issues
–
show
players is re-defining a name which is already available in the outer-scope (previously defined on line 168 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
138 | from server.games import Game |
||
139 | from server.abc.base_game import InitMode |
||
0 ignored issues
–
show
The import
server.abc.base_game could not be resolved.
This can be caused by one of the following: 1. Missing DependenciesThis error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands. # .scrutinizer.yml
before_commands:
- sudo pip install abc # Python2
- sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use
the command for the correct version.
2. Missing __init__.py filesThis error could also result from missing ![]() |
|||
140 | mock_parent = mock.Mock() |
||
141 | game = mock.create_autospec(spec=Game(1, mock_parent, mock.Mock())) |
||
0 ignored issues
–
show
game is re-defining a name which is already available in the outer-scope (previously defined on line 137 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
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)): |
||
0 ignored issues
–
show
|
|||
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): |
||
0 ignored issues
–
show
create_player is re-defining a name which is already available in the outer-scope (previously defined on line 152 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
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): |
||
0 ignored issues
–
show
db_pool is re-defining a name which is already available in the outer-scope (previously defined on line 110 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() players is re-defining a name which is already available in the outer-scope (previously defined on line 168 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() loop is re-defining a name which is already available in the outer-scope (previously defined on line 90 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
178 | from server import PlayerService |
||
179 | return PlayerService(db_pool) |
||
180 | |||
181 | @pytest.fixture |
||
182 | def game_service(loop, player_service, game_stats_service): |
||
0 ignored issues
–
show
game_stats_service is re-defining a name which is already available in the outer-scope (previously defined on line 202 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() player_service is re-defining a name which is already available in the outer-scope (previously defined on line 177 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() loop is re-defining a name which is already available in the outer-scope (previously defined on line 90 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
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 |
||
0 ignored issues
–
show
The import
server.api.api_accessor could not be resolved.
This can be caused by one of the following: 1. Missing DependenciesThis error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands. # .scrutinizer.yml
before_commands:
- sudo pip install abc # Python2
- sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use
the command for the correct version.
2. Missing __init__.py filesThis error could also result from missing ![]() |
|||
189 | return ApiAccessor() |
||
190 | |||
191 | @pytest.fixture |
||
192 | def event_service(api_accessor): |
||
0 ignored issues
–
show
api_accessor is re-defining a name which is already available in the outer-scope (previously defined on line 187 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
193 | from server.stats.event_service import EventService |
||
0 ignored issues
–
show
Unable to import 'server.stats.event_service' (invalid syntax (<string>, line 41))
This can be caused by one of the following: 1. Missing DependenciesThis error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands. # .scrutinizer.yml
before_commands:
- sudo pip install abc # Python2
- sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use
the command for the correct version.
2. Missing __init__.py filesThis error could also result from missing ![]() |
|||
194 | return EventService(api_accessor) |
||
195 | |||
196 | @pytest.fixture |
||
197 | def achievement_service(api_accessor): |
||
0 ignored issues
–
show
api_accessor is re-defining a name which is already available in the outer-scope (previously defined on line 187 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
198 | from server.stats.achievement_service import AchievementService |
||
0 ignored issues
–
show
Unable to import 'server.stats.achievement_service' (invalid syntax (<string>, line 69))
This can be caused by one of the following: 1. Missing DependenciesThis error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands. # .scrutinizer.yml
before_commands:
- sudo pip install abc # Python2
- sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use
the command for the correct version.
2. Missing __init__.py filesThis error could also result from missing ![]() |
|||
199 | return AchievementService(api_accessor) |
||
200 | |||
201 | @pytest.fixture |
||
202 | def game_stats_service(event_service, achievement_service): |
||
0 ignored issues
–
show
achievement_service is re-defining a name which is already available in the outer-scope (previously defined on line 197 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() event_service is re-defining a name which is already available in the outer-scope (previously defined on line 192 ).
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: param = 5
class Foo:
def __init__(self, param): # "param" would be flagged here
self.param = param
![]() |
|||
203 | from server.stats.game_stats_service import GameStatsService |
||
0 ignored issues
–
show
Unable to import 'server.stats.game_stats_service' (invalid syntax (<string>, line 15))
This can be caused by one of the following: 1. Missing DependenciesThis error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands. # .scrutinizer.yml
before_commands:
- sudo pip install abc # Python2
- sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use
the command for the correct version.
2. Missing __init__.py filesThis error could also result from missing ![]() |
|||
204 | return GameStatsService(event_service, achievement_service) |
||
205 |