diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/test_cache.py | 58 | ||||
| -rw-r--r-- | test/test_config.py | 13 | ||||
| -rw-r--r-- | test/test_downloader.py | 54 | ||||
| -rw-r--r-- | test/test_oauth.py | 7 | ||||
| -rw-r--r-- | test/test_postprocessor.py | 19 | ||||
| -rw-r--r-- | test/test_results.py | 4 | ||||
| -rw-r--r-- | test/test_text.py | 27 |
7 files changed, 117 insertions, 65 deletions
diff --git a/test/test_cache.py b/test/test_cache.py index e19896e..ecf482c 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -10,8 +10,8 @@ import os import sys import unittest +from unittest.mock import patch -import time import tempfile sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -22,8 +22,8 @@ config.set(("cache",), "file", dbpath) from gallery_dl import cache # noqa E402 -def tearDownModule(): - util.remove_file(dbpath) +# def tearDownModule(): +# util.remove_file(dbpath) class TestCache(unittest.TestCase): @@ -86,32 +86,50 @@ class TestCache(unittest.TestCase): self.assertEqual(ka(9, 9, 2), 6) def test_expires_mem(self): - @cache.memcache(maxage=1) + @cache.memcache(maxage=2) def ex(a, b, c): return a+b+c - self.assertEqual(ex(1, 1, 1), 3) - self.assertEqual(ex(2, 2, 2), 3) - self.assertEqual(ex(3, 3, 3), 3) + with patch("time.time") as tmock: + tmock.return_value = 0.001 + self.assertEqual(ex(1, 1, 1), 3) + self.assertEqual(ex(2, 2, 2), 3) + self.assertEqual(ex(3, 3, 3), 3) - time.sleep(2) - self.assertEqual(ex(3, 3, 3), 9) - self.assertEqual(ex(2, 2, 2), 9) - self.assertEqual(ex(1, 1, 1), 9) + # value is still cached after 1 second + tmock.return_value += 1.0 + self.assertEqual(ex(3, 3, 3), 3) + self.assertEqual(ex(2, 2, 2), 3) + self.assertEqual(ex(1, 1, 1), 3) + + # new value after 'maxage' seconds + tmock.return_value += 1.0 + self.assertEqual(ex(3, 3, 3), 9) + self.assertEqual(ex(2, 2, 2), 9) + self.assertEqual(ex(1, 1, 1), 9) def test_expires_db(self): - @cache.cache(maxage=1) + @cache.cache(maxage=2) def ex(a, b, c): return a+b+c - self.assertEqual(ex(1, 1, 1), 3) - self.assertEqual(ex(2, 2, 2), 3) - self.assertEqual(ex(3, 3, 3), 3) - - time.sleep(2) - self.assertEqual(ex(3, 3, 3), 9) - self.assertEqual(ex(2, 2, 2), 9) - self.assertEqual(ex(1, 1, 1), 9) + with patch("time.time") as tmock: + tmock.return_value = 0.999 + self.assertEqual(ex(1, 1, 1), 3) + self.assertEqual(ex(2, 2, 2), 3) + self.assertEqual(ex(3, 3, 3), 3) + + # value is still cached after 1 second + tmock.return_value += 1.0 + self.assertEqual(ex(3, 3, 3), 3) + self.assertEqual(ex(2, 2, 2), 3) + self.assertEqual(ex(1, 1, 1), 3) + + # new value after 'maxage' seconds + tmock.return_value += 1.0 + self.assertEqual(ex(3, 3, 3), 9) + self.assertEqual(ex(2, 2, 2), 9) + self.assertEqual(ex(1, 1, 1), 9) def test_update_mem_simple(self): @cache.memcache(keyarg=0) diff --git a/test/test_config.py b/test/test_config.py index cb202be..4171435 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -14,7 +14,8 @@ import unittest import json import tempfile -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +ROOTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, ROOTDIR) from gallery_dl import config # noqa E402 @@ -156,10 +157,12 @@ class TestConfigFiles(unittest.TestCase): @staticmethod def _load(name): - rootdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - path = os.path.join(rootdir, "docs", name) - with open(path) as fp: - return json.load(fp) + path = os.path.join(ROOTDIR, "docs", name) + try: + with open(path) as fp: + return json.load(fp) + except FileNotFoundError: + raise unittest.SkipTest(path + " not available") if __name__ == '__main__': diff --git a/test/test_downloader.py b/test/test_downloader.py index 9393040..5d73a4c 100644 --- a/test/test_downloader.py +++ b/test/test_downloader.py @@ -14,21 +14,30 @@ from unittest.mock import Mock, MagicMock, patch import re import base64 +import logging import os.path import tempfile import threading import http.server + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from gallery_dl import downloader, extractor, config, util # noqa E402 -from gallery_dl.downloader.common import DownloaderBase # noqa E402 -from gallery_dl.output import NullOutput # noqa E402 +from gallery_dl import downloader, extractor, output, config, util # noqa E402 class MockDownloaderModule(Mock): __downloader__ = "mock" +class FakeJob(): + + def __init__(self): + self.extractor = extractor.find("test:") + self.pathfmt = util.PathFormat(self.extractor) + self.out = output.NullOutput() + self.get_logger = logging.getLogger + + class TestDownloaderModule(unittest.TestCase): @classmethod @@ -96,11 +105,10 @@ class TestDownloaderBase(unittest.TestCase): @classmethod def setUpClass(cls): - cls.extractor = extractor.find("test:") - cls.extractor.log.job = None cls.dir = tempfile.TemporaryDirectory() cls.fnum = 0 config.set((), "base-directory", cls.dir.name) + cls.job = FakeJob() @classmethod def tearDownClass(cls): @@ -113,12 +121,13 @@ class TestDownloaderBase(unittest.TestCase): cls.fnum += 1 kwdict = { - "category": "test", + "category" : "test", "subcategory": "test", - "filename": name, - "extension": extension, + "filename" : name, + "extension" : extension, } - pathfmt = util.PathFormat(cls.extractor) + + pathfmt = cls.job.pathfmt pathfmt.set_directory(kwdict) pathfmt.set_filename(kwdict) @@ -159,7 +168,7 @@ class TestHTTPDownloader(TestDownloaderBase): @classmethod def setUpClass(cls): TestDownloaderBase.setUpClass() - cls.downloader = downloader.find("http")(cls.extractor, NullOutput()) + cls.downloader = downloader.find("http")(cls.job) port = 8088 cls.address = "http://127.0.0.1:{}".format(port) @@ -196,7 +205,7 @@ class TestTextDownloader(TestDownloaderBase): @classmethod def setUpClass(cls): TestDownloaderBase.setUpClass() - cls.downloader = downloader.find("text")(cls.extractor, NullOutput()) + cls.downloader = downloader.find("text")(cls.job) def test_text_download(self): self._run_test("text:foobar", None, "foobar", "txt", "txt") @@ -208,29 +217,6 @@ class TestTextDownloader(TestDownloaderBase): self._run_test("text:", None, "", "txt", "txt") -class FakeDownloader(DownloaderBase): - scheme = "fake" - - def __init__(self, extractor, output): - DownloaderBase.__init__(self, extractor, output) - - def connect(self, url, offset): - pass - - def receive(self, file): - pass - - def reset(self): - pass - - def get_extension(self): - pass - - @staticmethod - def _check_extension(file, pathfmt): - pass - - class HttpRequestHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): diff --git a/test/test_oauth.py b/test/test_oauth.py index 58d4088..e4664e4 100644 --- a/test/test_oauth.py +++ b/test/test_oauth.py @@ -15,6 +15,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from gallery_dl import oauth, text # noqa E402 TESTSERVER = "http://term.ie/oauth/example" +TESTSERVER = "http://term.ie/oauth/example" CONSUMER_KEY = "key" CONSUMER_SECRET = "secret" REQUEST_TOKEN = "requestkey" @@ -99,8 +100,10 @@ class TestOAuthSession(unittest.TestCase): CONSUMER_KEY, CONSUMER_SECRET, oauth_token, oauth_token_secret, ) - url = TESTSERVER + endpoint - return session.get(url, params=params).text + try: + return session.get(TESTSERVER + endpoint, params=params).text + except OSError: + raise unittest.SkipTest() if __name__ == "__main__": diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py index 354f9ff..5da3131 100644 --- a/test/test_postprocessor.py +++ b/test/test_postprocessor.py @@ -12,12 +12,14 @@ import sys import unittest from unittest.mock import Mock, mock_open, patch +import logging import zipfile import tempfile from datetime import datetime, timezone as tz sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from gallery_dl import postprocessor, extractor, util, config # noqa E402 +from gallery_dl import extractor, output, util # noqa E402 +from gallery_dl import postprocessor, util, config # noqa E402 from gallery_dl.postprocessor.common import PostProcessor # noqa E402 @@ -25,6 +27,15 @@ class MockPostprocessorModule(Mock): __postprocessor__ = "mock" +class FakeJob(): + + def __init__(self): + self.extractor = extractor.find("test:") + self.pathfmt = util.PathFormat(self.extractor) + self.out = output.NullOutput() + self.get_logger = logging.getLogger + + class TestPostprocessorModule(unittest.TestCase): def setUp(self): @@ -58,9 +69,9 @@ class BasePostprocessorTest(unittest.TestCase): @classmethod def setUpClass(cls): - cls.extractor = extractor.find("test:") cls.dir = tempfile.TemporaryDirectory() config.set((), "base-directory", cls.dir.name) + cls.job = FakeJob() @classmethod def tearDownClass(cls): @@ -74,12 +85,12 @@ class BasePostprocessorTest(unittest.TestCase): if data is not None: kwdict.update(data) - self.pathfmt = util.PathFormat(self.extractor) + self.pathfmt = self.job.pathfmt self.pathfmt.set_directory(kwdict) self.pathfmt.set_filename(kwdict) pp = postprocessor.find(self.__class__.__name__[:-4].lower()) - return pp(self.pathfmt, options) + return pp(self.job, options) class ClassifyTest(BasePostprocessorTest): diff --git a/test/test_results.py b/test/test_results.py index 046efc5..5bef1a4 100644 --- a/test/test_results.py +++ b/test/test_results.py @@ -26,10 +26,14 @@ TRAVIS_SKIP = { "archivedmoe", "archiveofsins", "thebarchive", "fireden", "4plebs", "sankaku", "idolcomplex", "mangahere", "readcomiconline", "mangadex", "sankakucomplex", "warosu", "fuskator", "patreon", "komikcast", + "instagram", } # temporary issues, etc. BROKEN = { + "e621", + "imagevenue", + "jaiminisbox", "photobucket", "worldthree", } diff --git a/test/test_text.py b/test/test_text.py index 4f31d81..aeb8096 100644 --- a/test/test_text.py +++ b/test/test_text.py @@ -94,6 +94,33 @@ class TestText(unittest.TestCase): for value in INVALID: self.assertEqual(f(value), empty) + def test_ensure_http_scheme(self, f=text.ensure_http_scheme): + result = "https://example.org/filename.ext" + + # standard usage + self.assertEqual(f(""), "") + self.assertEqual(f("example.org/filename.ext"), result) + self.assertEqual(f("/example.org/filename.ext"), result) + self.assertEqual(f("//example.org/filename.ext"), result) + self.assertEqual(f("://example.org/filename.ext"), result) + + # no change + self.assertEqual(f(result), result) + self.assertEqual( + f("http://example.org/filename.ext"), + "http://example.org/filename.ext", + ) + + # ... + self.assertEqual( + f("htp://example.org/filename.ext"), + "https://htp://example.org/filename.ext", + ) + + # invalid arguments + for value in INVALID_ALT: + self.assertEqual(f(value), value) + def test_filename_from_url(self, f=text.filename_from_url): result = "filename.ext" |
