Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
I still need to test the new tabulation and the shortener
functionality...
  • Loading branch information
XayOn committed Sep 6, 2017
1 parent eb9acb7 commit 0607a36
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
4 changes: 4 additions & 0 deletions katcr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def main():

search_res = list(globals()[opt['--plugin']]().search(
opt["<SEARCH_TERM>"], int(opt.get("--pages") or 1)))

if opt['--enable-shortener']:
def get_from_short(search_res):
"""Get new search res from shortened urls."""
Expand All @@ -157,6 +158,9 @@ def get_from_short(search_res):

search_res = list(get_from_short(search_res))

if not search_res:
return

lengths = [max(len(a[pos]) for a in search_res)
for pos in range(0, len(search_res[0]))]

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_katcr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_katcr_search_magnets():
from katcr import Katcr
from robobrowser import RoboBrowser
from unittest.mock import patch
proxies = {'Kickass Torrents': ['foo']}
proxies = {'Kickass Torrents': [['foo', None]]}

with patch('katcr.torrentmirror.get_proxies',
side_effect=(proxies,)) as mock:
Expand All @@ -15,8 +15,8 @@ def test_katcr_search_magnets():
assert mock.open.called_once_with('foo')


def test_katcr_make_printable():
"""Test make_printable method given a known structure.
def test_katcr_tabulate():
"""Test tabulate method given a known structure.
Note that this structure may change in the future or even between
different proxied sites... This needs to be handled somehow.
Expand All @@ -26,5 +26,5 @@ def test_katcr_make_printable():
to_parse = """<div class=cellMainLink>Foo</div><td>Nope</td><td>Bar</td>
<a title='Torrent magnet link' href='foo'></a>"""
fakelink = bs4.BeautifulSoup(to_parse, "html.parser")
result = Katcr.make_printable(fakelink)
assert result == ('Foo - Bar', 'foo')
result = Katcr.tabulate(fakelink)
assert result == ('Foo', 'Bar', 'foo')
41 changes: 34 additions & 7 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ def test_main():
from katcr import main
from unittest.mock import patch
opts = {'<SEARCH_TERM>': "foo", '--plugin': 'Katcr',
'--interactive': False, '--open': False}
'--interactive': False, '--open': False, '-d': False,
'--enable-shortener': False}
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',
'--interactive': True, '--open': True}
'--interactive': True, '--open': True, '-d': False,
'--enable-shortener': False}
torr = {'Torrent': 'foo'}
args = opts['<SEARCH_TERM>'], 1

Expand All @@ -30,7 +32,8 @@ def test_main():
['xdg-open', 'bar'])

opts = {'<SEARCH_TERM>': "foo", '--plugin': 'Katcr',
'--interactive': True, '--open': False}
'--interactive': True, '--open': False, '-d': False,
'--enable-shortener': False}

with patch('katcr.Katcr') as mock:
with patch('katcr.Terminal') as tmock:
Expand All @@ -43,24 +46,48 @@ def test_main():
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}

with patch('katcr.Katcr') as mock:
with patch('katcr.Terminal') as tmock:
tmock().width = 10
mock().search.side_effect = ((('foo', 'bar'),),)
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


def test_basesearch():
"""Test basesearch has required methods."""
import pytest
import unittest.mock
from katcr import BaseSearch

assert hasattr(BaseSearch, "search")
assert hasattr(BaseSearch, "search_magnets")

with pytest.raises(NotImplementedError):
BaseSearch().search_magnets("foo", 1)

with unittest.mock.patch('katcr.BaseSearch.search_magnets',
side_effect=(['foo'],)) as mock:
BaseSearch().search('foo', 2)
assert mock.call_count == 2

proxies = {'The Pirate Bay': [['foo', None]]}
BaseSearch.proxy_name = "The Pirate Bay"
BaseSearch.url = "Foo"
BaseSearch.url_format = "None"

with unittest.mock.patch('katcr.torrentmirror.get_proxies',
side_effect=(proxies,)) as mock:
with unittest.mock.patch('katcr.BaseSearch.get_torrents',
side_effect=(['foo'],)) as mock:
with unittest.mock.patch('katcr.robobrowser.RoboBrowser'):
BaseSearch().search('foo', 2)


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

with patch('katcr.torrentmirror.get_proxies',
side_effect=(proxies,)) as mock:
Expand All @@ -15,8 +15,8 @@ def test_tpb_search_magnets():
assert mock.open.called_once_with('foo')


def test_tpb_make_printable():
"""Test make_printable method given a known structure.
def test_tpb_tabulate():
"""Test tabulate method given a known structure.
Note that this structure may change in the future or even between
different proxied sites... This needs to be handled somehow.
Expand All @@ -27,5 +27,5 @@ def test_tpb_make_printable():
<div class=detDesc>Bar</div>
<a href='foo'></a></div>"""
fakelink = bs4.BeautifulSoup(to_parse, "html.parser")
result = ThePirateBay.make_printable(fakelink.find('a'))
assert result == ('Foo - Bar', 'foo')
result = ThePirateBay.tabulate(fakelink.find('a'))
assert result == ('Foo', 'Bar', 'foo')

0 comments on commit 0607a36

Please sign in to comment.