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 --- 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 ++ 8 files changed, 475 insertions(+) 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 (limited to 'tests') 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' + ) -- cgit v1.2.3