Skip to content

Commit

Permalink
Codestyle checking was added for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
quasiyoke committed Jan 1, 2018
1 parent 7c2edb5 commit cbeffe8
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 308 deletions.
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ load-plugins=pylint.extensions.bad_builtin,
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
disable=abstract-method,
arguments-differ,
bad-builtin,
cyclic-import,
duplicate-code,
Expand All @@ -45,6 +46,7 @@ disable=abstract-method,
too-many-ancestors,
too-many-branches,
too-many-instance-attributes,
too-many-lines,
too-many-locals,
too-many-public-methods,
too-many-statements,
Expand Down
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ Launch some specific test.

$ python -m unittest tests.test_stranger.TestStranger

If you went into trouble with codestyle (``test_codestyle`` test suite), look at the specific codestyle issues you've got:
Codestyle
^^^^^^^^^

::

$ pylint randtalkbot setup tests
$ python setup.py lint
26 changes: 23 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
from setuptools import setup, Command
from setuptools.command.test import test as SetuptoolsTestCommand
from randtalkbot.utils import __version__
Expand All @@ -27,6 +29,13 @@ def run(self):
from coveralls import cli
cli.main(self.coveralls_args)

def lint(*options):
from pylint.lint import Run
run = Run(options, exit=False)

if run.linter.msg_status != os.EX_OK:
sys.exit(run.linter.msg_status)

class LintCommand(Command):
user_options = []

Expand All @@ -39,8 +48,20 @@ def finalize_options(self):

def run(self):
self.distribution.fetch_build_eggs(self.distribution.tests_require)
from pylint import epylint as lint
lint.py_run('randtalkbot setup')
lint(
'randtalkbot',
'setup',
)
lint(
'''--disable=
invalid-name,
no-member,
protected-access,
redefined-variable-type,
reimported,
''',
'tests',
)

class TestCommand(SetuptoolsTestCommand):
def finalize_options(self):
Expand All @@ -51,7 +72,6 @@ def finalize_options(self):

def run_tests(self):
import coverage.cmdline
import sys
sys.exit(coverage.cmdline.main(argv=['run', '--source=randtalkbot', '-m', 'unittest']))

setup(
Expand Down
18 changes: 8 additions & 10 deletions tests/test_admin_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import asyncio
from unittest.mock import create_autospec
import asynctest
from asynctest.mock import call, patch, Mock, CoroutineMock
from randtalkbot.admin_handler import AdminHandler
from randtalkbot.stranger_handler import StrangerHandler
from randtalkbot.admin_handler import StrangerServiceError
from randtalkbot.stranger_setup_wizard import StrangerSetupWizard
from unittest.mock import create_autospec


class TestAdminHandler(asynctest.TestCase):
@patch('randtalkbot.stranger_handler.StrangerService', Mock())
@patch('randtalkbot.stranger_handler.StrangerSetupWizard', create_autospec(StrangerSetupWizard))
@patch('randtalkbot.stranger_sender_service.StrangerSenderService._instance')
def setUp(self, stranger_sender_service):
from randtalkbot.stranger_handler import StrangerSetupWizard
from randtalkbot.stranger_handler import StrangerService
from randtalkbot.stranger_handler import StrangerSetupWizard as \
stranger_setup_wizard_cls_mock
from randtalkbot.stranger_handler import StrangerService as stranger_service_cls_mock
self.stranger = CoroutineMock()
stranger_service = StrangerService.get_instance.return_value
stranger_service = stranger_service_cls_mock.get_instance.return_value
stranger_service.get_or_create_stranger.return_value = self.stranger
StrangerSetupWizard.reset_mock()
self.StrangerSetupWizard = StrangerSetupWizard
self.stranger_setup_wizard = StrangerSetupWizard.return_value
stranger_setup_wizard_cls_mock.reset_mock()
self.stranger_setup_wizard_cls_mock = stranger_setup_wizard_cls_mock
self.stranger_setup_wizard = stranger_setup_wizard_cls_mock.return_value
self.stranger_setup_wizard.handle = CoroutineMock()
self.initial_msg = {
'from': {
Expand Down Expand Up @@ -143,7 +142,6 @@ async def test_handle_command_pay__unknown_stranger(self):

@patch('randtalkbot.admin_handler.StrangerHandler.handle_command')
async def test_handle_command__other_command(self, handle_command):
from randtalkbot.admin_handler import StrangerHandler
message = Mock()
message.command = 'foo_command'
message.command_args = 'foo_args'
Expand Down
16 changes: 8 additions & 8 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from peewee import *
from unittest.mock import create_autospec, patch, Mock
from peewee import DatabaseError
from randtalkbot.db import DB, RetryingDB
from randtalkbot.errors import DBError
from randtalkbot.stats import Stats
from randtalkbot.stranger import Stranger
from randtalkbot.talk import Talk
from unittest.mock import create_autospec, patch, Mock

class TestDB(unittest.TestCase):
@patch('randtalkbot.db.RetryingDB', create_autospec(RetryingDB))
@patch('randtalkbot.db.stats')
@patch('randtalkbot.db.stranger')
@patch('randtalkbot.db.talk')
def setUp(self, stats_module_mock, stranger_module_mock, talk_module_mock):
from randtalkbot.db import RetryingDB
from randtalkbot.db import RetryingDB as retrying_db_cls_mock
self.stats_module_mock = stats_module_mock
self.stranger_module_mock = stranger_module_mock
self.talk_module_mock = talk_module_mock
self.database = Mock()
self.RetryingDB = RetryingDB
self.RetryingDB.return_value = self.database
self.retrying_db_cls_mock = retrying_db_cls_mock
self.retrying_db_cls_mock.return_value = self.database
self.configuration = Mock()
self.configuration.database_host = 'foo_host'
self.configuration.database_name = 'foo_name'
self.configuration.database_user = 'foo_user'
self.configuration.database_password = 'foo_password'
self.RetryingDB.reset_mock()
self.retrying_db_cls_mock.reset_mock()
self.db = DB(self.configuration)

def test_init__ok(self):
self.RetryingDB.assert_called_once_with(
self.retrying_db_cls_mock.assert_called_once_with(
'foo_name',
host='foo_host',
user='foo_user',
Expand All @@ -46,7 +46,7 @@ def test_init__ok(self):
self.talk_module_mock.DATABASE_PROXY.initialize.assert_called_once_with(self.database)

def test_init__database_troubles(self):
self.RetryingDB.return_value.connect.side_effect = DatabaseError()
self.retrying_db_cls_mock.return_value.connect.side_effect = DatabaseError()
with self.assertRaises(DBError):
DB(self.configuration)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from unittest.mock import call, patch
from randtalkbot.i18n import get_languages_names, get_languages_codes, get_translation, \
LanguageNotFoundError
from unittest.mock import call, patch, Mock

class TestI18n(unittest.TestCase):
def test_get_languages_names__supported(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from unittest.mock import patch, Mock
from randtalkbot.message import Message, UnsupportedContentError
from unittest.mock import create_autospec, patch, Mock

class TestMessage(unittest.TestCase):
def setUp(self):
Expand Down
2 changes: 0 additions & 2 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from peewee import *
from randtalkbot.stats import Stats
from unittest.mock import create_autospec, patch, Mock

class TestStats(unittest.TestCase):
def setUp(self):
Expand Down
23 changes: 13 additions & 10 deletions tests/test_stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import asyncio
import asynctest
import datetime
import json
import types
import unittest
import asynctest
from asynctest.mock import patch, Mock, CoroutineMock
from peewee import *
from peewee import SqliteDatabase
from randtalkbot import stats
from randtalkbot.stats_service import StatsService
from randtalkbot.stats import Stats

# pylint: disable=line-too-long
ENDED_TALKS = (
{'begin': 315535576, 'end': 315539910, 'partner2_sent': 195, 'searched_since': 315525609, 'partner1_sent': 110},
{'begin': 315540982, 'end': 315549266, 'partner2_sent': 89, 'searched_since': 315532407, 'partner1_sent': 72},
Expand Down Expand Up @@ -376,15 +375,15 @@ def test_init__no_stats_in_db(self):

@asynctest.ignore_loop
def test_init__some_stats_in_db_1(self):
stats1 = Stats.create(data_json='', created=datetime.datetime(1980, 1, 1))
Stats.create(data_json='', created=datetime.datetime(1980, 1, 1))
stats2 = Stats.create(data_json='', created=datetime.datetime(1990, 1, 1))
stats_service = StatsService()
self.assertEqual(stats_service._stats, stats2)

@asynctest.ignore_loop
def test_init__some_stats_in_db_2(self):
stats1 = Stats.create(data_json='', created=datetime.datetime(1990, 1, 1))
stats2 = Stats.create(data_json='', created=datetime.datetime(1980, 1, 1))
Stats.create(data_json='', created=datetime.datetime(1980, 1, 1))
stats_service = StatsService()
self.assertEqual(stats_service._stats, stats1)

Expand All @@ -405,18 +404,18 @@ def test_get_stats(self):
@patch('randtalkbot.stats_service.asyncio', CoroutineMock())
@patch('randtalkbot.stats_service.datetime', Mock())
async def test_run__ok(self):
from randtalkbot.stats_service import asyncio
from randtalkbot.stats_service import asyncio as asyncio_mock
from randtalkbot.stats_service import datetime as datetime_mock
self.stats_service._update_stats.reset_mock()
datetime_mock.datetime.utcnow.side_effect = [datetime.datetime(1990, 1, 1, 3), RuntimeError]
with self.assertRaises(RuntimeError):
await self.stats_service.run()
asyncio.sleep.assert_called_once_with(3600)
asyncio_mock.sleep.assert_called_once_with(3600)
self.stats_service._update_stats.assert_called_once_with()

@patch('randtalkbot.stats_service.asyncio')
@patch('randtalkbot.stats_service.datetime', Mock())
async def test_run__too_late(self, asyncio):
async def test_run__too_late(self, asyncio_mock):
from randtalkbot.stats_service import datetime as datetime_mock
self.stats_service._update_stats.reset_mock()
datetime_mock.datetime.utcnow.side_effect = [
Expand All @@ -425,7 +424,7 @@ async def test_run__too_late(self, asyncio):
]
with self.assertRaises(RuntimeError):
await self.stats_service.run()
asyncio.sleep.assert_not_called()
asyncio_mock.sleep.assert_not_called()
self.stats_service._update_stats.assert_called_once_with()

@asynctest.ignore_loop
Expand All @@ -441,11 +440,13 @@ def test_update_stats__no_stats_in_db(self):
Talk.get_ended_talks.return_value = get_talks(ENDED_TALKS)
self.stats_service._update_stats = types.MethodType(self.update_stats, self.stats_service)
self.stats_service._stats = None
# pylint: disable=not-callable
self.stats_service._update_stats()
Talk.get_not_ended_talks.assert_called_once_with(after=None)
Talk.get_ended_talks.assert_called_once_with(after=None)
Talk.delete_old.assert_not_called()
actual = json.loads(self.stats_service._stats.data_json)
# pylint: disable=bad-continuation
expected = {
'languages_count_distribution': [[1, 88], [2, 13]],
'languages_popularity': [['en', 67], ['it', 34], ['ru', 12]],
Expand Down Expand Up @@ -511,6 +512,7 @@ def test_update_stats__some_stats_in_db(self):
Talk.get_ended_talks.return_value = get_talks(ENDED_TALKS)
self.stats_service._update_stats = types.MethodType(self.update_stats, self.stats_service)
# self.stats_service._stats is not None now.
# pylint: disable=not-callable
self.stats_service._update_stats()
Talk.get_not_ended_talks.assert_called_once_with(after=datetime.datetime(1990, 1, 1))
Talk.get_ended_talks.assert_called_once_with(after=datetime.datetime(1990, 1, 1))
Expand All @@ -531,6 +533,7 @@ def test_update_stats__no_talks(self):
Talk.get_ended_talks.return_value = []
self.stats_service._update_stats = types.MethodType(self.update_stats, self.stats_service)
# self.stats_service._stats is not None now.
# pylint: disable=not-callable
self.stats_service._update_stats()
self.assertEqual(
json.loads(self.stats_service._stats.data_json),
Expand Down

0 comments on commit cbeffe8

Please sign in to comment.