Skip to content

Commit

Permalink
100% coverage is back.
Browse files Browse the repository at this point in the history
  • Loading branch information
XayOn committed Oct 1, 2017
1 parent 327a864 commit 3e04e64
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 9 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGES
=======

* Segmented tests
* Fixed tests
* Fixed tests?
* Updated usage
* Fixed last commit
* Moved asciicast to gif
* Fixed tests, still no tests for new additions
* Added more engines
* Added todo
Expand Down
24 changes: 15 additions & 9 deletions katcr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def tabulate(row):
"""Extract all information from each href."""
with suppress(IndexError, TypeError):
tail = row.find(class_='tail')
return (row.find('div').find('a').text,
list(filter(lambda x: x, tail.text.split(' ')))[4],
tail.find('a')['href'])
return (row.find('div').find('a').text.strip(),
list(filter(lambda x: x, tail.text.split(' ')))[4].strip(),
tail.find('a')['href'].strip())

def get_torrents(self):
"""Return torrents."""
Expand Down Expand Up @@ -224,6 +224,17 @@ def limit_terminal_size(what, limit=-20):
return what[:Terminal().width + limit]


def get_shortener_from_opts(opt):
"""Return shortener with token if needed."""
shortener = opt['--shortener'][0]
token = opt.get('--token')
if not token and opt.get('--token_file'):
token = Path(opt.get('--token_file')).read_text()
if token:
shortener = shortener.format(token)
return shortener


def search_in_engines(logger, engines, search_term, pages):
"""Search in engines."""
search_res = None
Expand Down Expand Up @@ -284,12 +295,7 @@ def main():
int(opt.get("--pages")[0]))

if not opt['--disable-shortener']:
shortener = opt['--shortener'][0]
token = opt.get('--token')
if not token and opt.get('--token_file'):
token = Path(opt.get('--token_file')).read_text()
if token:
shortener = shortener.format(token)
shortener = get_shortener_from_opts(opt)
with suppress(TypeError):
search_res = list(get_from_short(shortener, search_res))

Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_digbit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tets dibgit plugin."""


def test_digbit_search_magnets():
"""Test digbit search magnets method."""
from katcr import DigBt
from robobrowser import RoboBrowser
from unittest.mock import patch, MagicMock

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


def test_digbit_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.
"""
from katcr import DigBt
import bs4
to_parse = """<tr><div class=head><a>Foo</a></div>
<div class=tail>1 2 3 4 Bar<a href='foo'>
</a></div></tr>"""
fakelink = bs4.BeautifulSoup(to_parse, "html.parser")
result = DigBt.tabulate(fakelink.find('tr'))
assert result == ('Foo', 'Bar', 'foo')
27 changes: 27 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@ def test_search_call():
mock().search.assert_called_with(*args)


def test_main_token_file():
"""Get shortener with token from file."""
from katcr import get_shortener_from_opts

opts = {
'--shortener': ['http://foo.com/{}'],
'--token': 'bar'}
assert get_shortener_from_opts(opts) == 'http://foo.com/bar'


def test_main_token():
"""Get shortener with token."""
import tempfile
import os
from katcr import get_shortener_from_opts
with tempfile.NamedTemporaryFile(delete=False, mode='wb') as fileo:
name = fileo.name
fileo.write(b'bar')

opts = {
'--shortener': ['http://foo.com/{}'],
'--token_file': name}

assert get_shortener_from_opts(opts) == 'http://foo.com/bar'
os.unlink(name)


def test_basesearch():
"""Test basesearch has required methods."""
from katcr import BaseSearch
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_nyaasi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Test nyaasi plugin."""


def test_nyaasi_search_magnets():
"""Test nyaasi search magnets method."""
from katcr import NyaaSi
from robobrowser import RoboBrowser
from unittest.mock import patch, MagicMock

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


def test_nyaasi_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.
"""
from katcr import NyaaSi
import bs4

to_parse = """<tr><td><a href=""><img src="" alt="Cat"></a></td>
<td><a href="">Foo</a></td>
<td><a href=""></a> <a href="foo"><i class=fa-magnet></i></a></td>
<td class="text-center">Bar</td></tr>"""
fakelink = bs4.BeautifulSoup(to_parse, "html.parser")
result = NyaaSi.tabulate(fakelink.find(class_='fa-magnet'))
assert result == ('Foo Cat', 'Bar', 'foo')
29 changes: 29 additions & 0 deletions tests/unit/test_skytorrents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Test skytorrents plugin."""


def test_skytorrents_search_magnets():
"""Test skytorrents search magnets method."""
from katcr import Skytorrents
from robobrowser import RoboBrowser
from unittest.mock import patch, MagicMock

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


def test_skytorrents_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.
"""
from katcr import Skytorrents
import bs4
to_parse = """<tr><td><a href="">Foo</a>
<a href="foo"></a></td><td>Bar</td></tr>"""
fakelink = bs4.BeautifulSoup(to_parse, "html.parser")
result = Skytorrents.tabulate(fakelink.find('tr'))
assert result == ('Foo', 'Bar', 'foo')

0 comments on commit 3e04e64

Please sign in to comment.