From 4e2d04eca0147396092d2163bd8706fe497a8bf1 Mon Sep 17 00:00:00 2001 From: Matt Lyon Date: Wed, 9 Sep 2020 12:26:33 +0100 Subject: added tests, switched to lxml --- .travis.yaml | 13 ++ README.md | 3 +- setup.py | 2 +- tests/data/no_body.html | 50 ++++++++ tests/data/torrent_test.html | 276 ++++++++++++++++++++++++++++++++++++++++++ tests/integration/__init__.py | 0 tests/unit/__init__.py | 0 tests/unit/test_constants.py | 27 +++++ tests/unit/test_torrents.py | 102 ++++++++++++++++ tests/unit/test_tpb.py | 9 ++ tests/unit/test_utils.py | 11 ++ tpblite/models/torrents.py | 6 +- 12 files changed, 492 insertions(+), 7 deletions(-) create mode 100644 .travis.yaml create mode 100644 tests/data/no_body.html create mode 100644 tests/data/torrent_test.html create mode 100644 tests/integration/__init__.py create mode 100644 tests/unit/__init__.py create mode 100644 tests/unit/test_constants.py create mode 100644 tests/unit/test_torrents.py create mode 100644 tests/unit/test_tpb.py create mode 100644 tests/unit/test_utils.py diff --git a/.travis.yaml b/.travis.yaml new file mode 100644 index 0000000..9dc2570 --- /dev/null +++ b/.travis.yaml @@ -0,0 +1,13 @@ +language: python +python: + - "3.5" + - "3.6" + - "3.7" + - "3.8" + +# command to install dependencies +install: + - pip install coverage coveralls . +# command to run tests +script: "coverage run -m unittest discover -s '.tests/' -p 'test_*.py'" +after_success: "coveralls" diff --git a/README.md b/README.md index 6c69069..f4f4772 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ Unofficial Lightweight Python API for ThePirateBay - -[![PyPI version](https://badge.fury.io/py/tpblite.png)](https://badge.fury.io/py/tpblite) +[![Build Status](https://travis-ci.com/mattlyon93/tpb-lite.svg?branch=master)](https://travis-ci.com/mattlyon93/tpb-lite) [![Coverage Status](https://coveralls.io/repos/github/mattlyon93/tpb-lite/badge.svg?branch=master)](https://coveralls.io/github/mattlyon93/tpb-lite?branch=master) [![PyPI version](https://badge.fury.io/py/tpblite.png)](https://badge.fury.io/py/tpblite) Installation ============= diff --git a/setup.py b/setup.py index 9bb031d..4f925ef 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ this_dir = path.abspath(path.dirname(__file__)) with open(path.join(this_dir, 'README.md'), encoding='utf-8') as f: long_description = f.read() -version = '0.5.0' +version = '0.6.0' setup(name = 'tpblite', version = version, diff --git a/tests/data/no_body.html b/tests/data/no_body.html new file mode 100644 index 0000000..f301987 --- /dev/null +++ b/tests/data/no_body.html @@ -0,0 +1,50 @@ + + + + The Pirate Bay - The galaxy's most resilient bittorrent site + + + + + + + + + + diff --git a/tests/data/torrent_test.html b/tests/data/torrent_test.html new file mode 100644 index 0000000..7ea9e6e --- /dev/null +++ b/tests/data/torrent_test.html @@ -0,0 +1,276 @@ + + + + The Pirate Bay - The galaxy's most resilient bittorrent site + + + + + + + + + + + + + +

Recent Torrents 

+
+
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Type
Name
View: Single / Double 
SELE
+
+ Games
+ (PC) +
+
+ +Magnet link DownloadVIP + Uploaded 4 mins ago, Size 3.32 GiB, ULed by Drarbg + 00
+
+ Audio
+ (Music) +
+
+ +Magnet link DownloadThis torrent has a cover imageVIP + Uploaded 5 mins ago, Size 119.24 MiB, ULed by WildcardSearch + 00
+
+ Video
+ (Movies) +
+
+ +Magnet link DownloadVIP + Uploaded 6 mins ago, Size 1.6 GiB, ULed by Drarbg + 00
+
+ Porn
+ (Movie clips) +
+
+ +Magnet link DownloadThis torrent has a cover imageVIP + Uploaded 6 mins ago, Size 352.69 MiB, ULed by Pornmaze + 00
+
+ Video
+ (TV shows) +
+
+ +Magnet link DownloadTrusted + Uploaded 9 mins ago, Size 142.17 MiB, ULed by Anonymous + 500400
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Next  +
+
+ +
+ +
+
+ + + + + diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_constants.py b/tests/unit/test_constants.py new file mode 100644 index 0000000..944abd8 --- /dev/null +++ b/tests/unit/test_constants.py @@ -0,0 +1,27 @@ +import io +import unittest +import contextlib + +from tpblite.models import constants + + +class ConstantsTestCase(unittest.TestCase): + + def test_categories(self): + self.assertEqual(constants.CATEGORIES.VIDEO.MOVIES, 201) + + def test_printOptions_one(self): + sobj = io.StringIO() + with contextlib.redirect_stdout(sobj): + constants.ORDERS.NAME.printOptions() + self.assertEqual(sobj.getvalue(), 'DES\nASC\n') + + def test_printOptions_two(self): + sobj = io.StringIO() + with contextlib.redirect_stdout(sobj): + constants.CATEGORIES.GAMES.printOptions() + self.assertEqual( + sobj.getvalue(), + 'ALL\nPC\nMAC\nPSX\nXBOX360\nWII\nHANDHELD\nIOS\nANDROID\nOTHER\n' + ) + \ No newline at end of file diff --git a/tests/unit/test_torrents.py b/tests/unit/test_torrents.py new file mode 100644 index 0000000..4326f68 --- /dev/null +++ b/tests/unit/test_torrents.py @@ -0,0 +1,102 @@ +import unittest + +from pathlib import Path + +from tpblite.models import torrents + +DATA_DIR = Path(__file__).parents[1].joinpath('data') + +class TorrentsTestCase(unittest.TestCase): + + def setUp(self): + fobj = open(DATA_DIR.joinpath('torrent_test.html'), 'r') + contents = fobj.read() + fobj.close() + self.torrents = torrents.Torrents(contents) + + def test_str(self): + self.assertEqual(str(self.torrents), 'Torrents object: 5 torrents') + + def test_repr(self): + self.assertEqual(repr(self.torrents), '') + + def test_length(self): + self.assertEqual(len(self.torrents), 5) + + def test_title(self): + self.assertEqual(self.torrents[2].title, 'CBGB 2013 HDRip x264 AC3 UNiQUE') + + def test_seeds(self): + self.assertEqual(self.torrents[-1].seeds, 500) + + def test_leeches(self): + self.assertEqual(self.torrents[-1].leeches, 400) + + def test_upload_date(self): + self.assertEqual(self.torrents[0].upload_date, '4 mins ago') + + def test_uploader(self): + self.assertEqual(self.torrents[0].uploader, 'Drarbg') + + def test_filesize(self): + self.assertEqual(self.torrents[1].filesize, '119.24 MiB') + + def test_byte_size(self): + self.assertEqual(self.torrents[0].byte_size, 3564822855) + + def test_magnetlink(self): + self.assertEqual(self.torrents[4].magnetlink, 'magnet:?xt=urn:btih:12dce429d8ca04f2b17b28036e11abb2c1239fa6&dn=Fair.Ci') + + def test_url(self): + self.assertEqual(self.torrents[0].url, '/torrent/8935177/Hot.Wheels.Worlds.Best.Driver-SKIDROW') + + def test_getBestTorrent(self): + tor = self.torrents.getBestTorrent(min_seeds=50, min_filesize='100 MiB', max_filesize='300 MiB') + self.assertEqual(tor.title, 'Fair.City.S ') + + def test_getBestTorrentNone(self): + tor = self.torrents.getBestTorrent() + self.assertEqual(tor, None) + +class TorrentsExceptionsTestCase(unittest.TestCase): + + def test_createTorrentListRaise(self): + fobj = open(DATA_DIR.joinpath('no_body.html'), 'r') + contents = fobj.read() + fobj.close() + self.assertRaises(ConnectionError, torrents.Torrents, contents) + + +class TorrentTestCase(unittest.TestCase): + + def setUp(self): + fobj = open(DATA_DIR.joinpath('torrent_test.html'), 'r') + contents = fobj.read() + fobj.close() + self.torrent = torrents.Torrents(contents)[4] + + def test_str(self): + self.assertEqual( + str(self.torrent), + 'Fair.City.S , S: 500, L: 400, 142.17 MiB' + ) + + def test_repr(self): + self.assertEqual(repr(self.torrent), '') + + +class fileSizeTestCase(unittest.TestCase): + + def test_raise_one(self): + self.assertRaises(AttributeError, torrents.fileSizeStrToInt, '400 MiBB') + + def test_raise_two(self): + self.assertRaises(AttributeError, torrents.fileSizeStrToInt, '400 LiB') + + def test_raise_three(self): + self.assertRaises(AttributeError, torrents.fileSizeStrToInt, 'm400 MiB') + + + + + \ No newline at end of file diff --git a/tests/unit/test_tpb.py b/tests/unit/test_tpb.py new file mode 100644 index 0000000..07eb5d1 --- /dev/null +++ b/tests/unit/test_tpb.py @@ -0,0 +1,9 @@ +import unittest + +from tpblite import TPB + +class TPBTestCase(unittest.TestCase): + + def test_str(self): + t = TPB('https://tpb.party') + self.assertEqual(str(t), 'TPB Object, base URL: https://tpb.party') diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py new file mode 100644 index 0000000..5fa0adf --- /dev/null +++ b/tests/unit/test_utils.py @@ -0,0 +1,11 @@ +import unittest + +from tpblite.models import utils + + +class URLTestCase(unittest.TestCase): + def test_URL(self): + self.assertEqual( + utils.URL('https://some.domain', ('1', '99', '200')), + 'https://some.domain/1/99/200' + ) diff --git a/tpblite/models/torrents.py b/tpblite/models/torrents.py index 1c2b32d..4803740 100644 --- a/tpblite/models/torrents.py +++ b/tpblite/models/torrents.py @@ -1,7 +1,5 @@ import unicodedata -from lxml.etree import HTML - -# TODO: write better comments +import lxml.etree as ET def fileSizeStrToInt(size_str): @@ -97,7 +95,7 @@ class Torrents: return self.list[index] def _createTorrentList(self): - root = HTML(self.html_source) + root = ET.HTML(self.html_source) if root.find("body") is None: raise ConnectionError("Could not determine torrents (empty html body)") rows = root.xpath('//tr[td[@class="vertTh"]]') -- cgit v1.2.3