Skip to content

Commit

Permalink
100% code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
XayOn committed Sep 24, 2017
1 parent b28d874 commit c733dbf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CHANGES
=======

* Wrong version in changelog

1.0.0
-----
Expand Down
4 changes: 3 additions & 1 deletion katcr/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from katcr import Katcr, get_short
from docopt import docopt
from pygogo import Gogo
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
Expand All @@ -14,8 +15,9 @@ class KATBot(telepot.Bot):
def __init__(self, opts):
"""Initialize of KATBot."""
super().__init__(opts['--token'])
logger = Gogo(__name__, verbose=True)
self.shortener = opts['--shortener']
self.katcr = Katcr()
self.katcr = Katcr(logger)
self.responses = {}

# pylint: disable=too-few-public-methods
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_katcr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ def test_katcr_search_magnets():
"""Test katcr search magnets method."""
from katcr import Katcr
from robobrowser import RoboBrowser
from unittest.mock import patch
from unittest.mock import patch, MagicMock
proxies = {'Kickass Torrents': [['foo', None]]}

with patch('katcr.torrentmirror.get_proxies',
side_effect=(proxies,)) as mock:
with patch('katcr.robobrowser.RoboBrowser', spec=RoboBrowser) as mock:
Katcr().search_magnets('foo', 1)
Katcr(MagicMock()).search_magnets('foo', 1)
assert mock.open.called_once_with('foo')


Expand Down
65 changes: 48 additions & 17 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ def test_main():
"""Test argument parsing and calling."""
from katcr import main, get_from_short
from unittest.mock import patch, call
opts = {'<SEARCH_TERM>': "foo", '--plugin': 'Katcr',
opts = {'<SEARCH_TERM>': "foo", '--search-engine': ['Katcr'],
'--interactive': False, '--open': False, '-d': False,
'--enable-shortener': False, '--pages': 1}
'--disable-shortener': True, '--pages': [1]}
with patch('katcr.Katcr') as mock:
with patch('katcr.docopt', side_effect=(opts,)):
main()
mock().search.assert_called_with(opts['<SEARCH_TERM>'], 1)

opts = {'<SEARCH_TERM>': "foo", '--plugin': 'Katcr',
opts = {'<SEARCH_TERM>': "foo", '--search-engine': ['Katcr'],
'--interactive': False, '--open': False, '-d': False,
'--shortener': 'bar',
'--enable-shortener': True, '--pages': 1}
'--shortener': ['bar'],
'--disable-shortener': False, '--pages': [1]}
with patch('katcr.Katcr') as mock:
with patch('katcr.get_from_short') as short_mock:
with patch('katcr.docopt', side_effect=(opts,)):
Expand All @@ -36,9 +36,10 @@ class Foo:
mock.assert_has_calls([call('foo.com', data={'magnet': '2'}),
call('foo.com', data={'magnet': '4'})])

opts = {'<SEARCH_TERM>': "foo", '--plugin': 'Katcr',
opts = {'<SEARCH_TERM>': "foo", '--search-engine': ['Katcr'],
'--interactive': True, '--open': True, '-d': False,
'--enable-shortener': False, '--pages': 1}
'--disable-shortener': True, "--shortener": "http://foo.com",
'--pages': [1]}
torr = {'Link': 'foo'}
args = opts['<SEARCH_TERM>'], 1

Expand All @@ -54,9 +55,10 @@ class Foo:
smock.check_call.assert_called_with(
['xdg-open', 'bar'])

opts = {'<SEARCH_TERM>': "foo", '--plugin': 'Katcr',
opts = {'<SEARCH_TERM>': "foo", '--search-engine': ['Katcr'],
'--interactive': True, '--open': False, '-d': False,
'--enable-shortener': False, '--pages': 1}
'--disable-shortener': True, "--shortener": "http://foo.com",
'--pages': [1]}

with patch('katcr.Katcr') as mock:
with patch('katcr.Terminal') as tmock:
Expand All @@ -69,9 +71,10 @@ class Foo:
mock().search.assert_called_with(*args)
smock.check_call.assert_not_called()

opts = {'<SEARCH_TERM>': "foo", '--plugin': 'Katcr',
'--interactive': True, '--open': False, '-d': True,
'--enable-shortener': False, '--pages': 1}
opts = {'<SEARCH_TERM>': "foo", '--search-engine': ['Katcr'],
'--interactive': True, '--open': False,
'--verbose': True,
'--disable-shortener': True, '--pages': [1]}

with patch('katcr.Katcr') as mock:
with patch('katcr.Terminal') as tmock:
Expand All @@ -80,10 +83,27 @@ class Foo:
with patch('katcr.subprocess') as smock:
with patch('katcr.docopt', side_effect=(opts,)):
with patch('katcr.prompt', side_effect=(torr,)):
import logging
main()
level = logging.getLogger().getEffectiveLevel()
assert level == logging.DEBUG
with patch('katcr.Gogo') as logging_mock:
main()
logging_mock.assert_called_with(
'katcr', verbose=True)

opts = {'<SEARCH_TERM>': "foo",
'--search-engine': ['Katcr'],
'--interactive': False,
'--open': False,
'--disable-shortener': True,
"--shortener": "http://foo.com",
'--pages': [1]}
torr = {'Link': 'foo'}
args = opts['<SEARCH_TERM>'], 1

with patch('katcr.Katcr') as mock:
tmock().width = 50
mock().search.side_effect = ((('foo', 'bar', "baz"),),)
with patch('katcr.docopt', side_effect=(opts,)):
main()
mock().search.assert_called_with(*args)


def test_basesearch():
Expand All @@ -103,9 +123,20 @@ def get_torrents(self):
url = "Foo"
url_format = None

FakeSearch().search('foo', 2)
FakeSearch(mock.MagicMock).search('foo', 2)
assert mock.call_count == 2

class FakeSearch(BaseSearch):
browser = mock.MagicMock()
proxy_name = "The Pirate Bay"
url = "Foo"
url_format = "http://foo.com/"

def get_torrents(self):
return "foo"
FakeSearch(mock.MagicMock).search('foo', 2)
assert mock.call_count == 2


def test_cli_help():
"""Test help call."""
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_tpb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ def test_tpb_search_magnets():
"""Test tpb search magnets method."""
from katcr import ThePirateBay
from robobrowser import RoboBrowser
from unittest.mock import patch
from unittest.mock import patch, MagicMock
proxies = {'The Pirate Bay': [['foo', None]]}

with patch('katcr.torrentmirror.get_proxies',
side_effect=(proxies,)) as mock:
with patch('katcr.robobrowser.RoboBrowser', spec=RoboBrowser) as mock:
ThePirateBay().search_magnets('foo', 1)
ThePirateBay(MagicMock()).search_magnets('foo', 1)
assert mock.open.called_once_with('foo')


Expand Down

0 comments on commit c733dbf

Please sign in to comment.