From cf188f30e1c27bdb900fa2623a9ff91b944633b2 Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Tue, 14 Apr 2020 18:18:40 -0400 Subject: New upstream version 1.13.4 --- test/test_extractor.py | 122 +++++++++++++++++++++++++++++++++++++++++++++++-- test/test_results.py | 4 +- test/test_text.py | 6 ++- 3 files changed, 127 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/test_extractor.py b/test/test_extractor.py index 2555b58..e6f4963 100644 --- a/test/test_extractor.py +++ b/test/test_extractor.py @@ -1,17 +1,22 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2018-2019 Mike Fährmann +# Copyright 2018-2020 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. import sys -import unittest +import time import string +from datetime import datetime, timedelta + +import unittest +from unittest.mock import patch from gallery_dl import extractor +from gallery_dl.extractor import mastodon from gallery_dl.extractor.common import Extractor, Message from gallery_dl.extractor.directlink import DirectlinkExtractor as DLExtractor @@ -26,7 +31,7 @@ class FakeExtractor(Extractor): yield Message.Url, "text:foobar", {} -class TestExtractor(unittest.TestCase): +class TestExtractorModule(unittest.TestCase): VALID_URIS = ( "https://example.org/file.jpg", "tumblr:foobar", @@ -170,5 +175,116 @@ class TestExtractor(unittest.TestCase): self.assertEqual(expected, extr.__name__) +class TestExtractorWait(unittest.TestCase): + + def test_wait_seconds(self): + extr = extractor.find("test:") + seconds = 5 + until = time.time() + seconds + + with patch("time.sleep") as sleep, patch.object(extr, "log") as log: + extr.wait(seconds=seconds) + + sleep.assert_called_once_with(6.0) + + calls = log.info.mock_calls + self.assertEqual(len(calls), 1) + self._assert_isotime(calls[0][1][1], until) + + def test_wait_until(self): + extr = extractor.find("test:") + until = time.time() + 5 + + with patch("time.sleep") as sleep, patch.object(extr, "log") as log: + extr.wait(until=until) + + calls = sleep.mock_calls + self.assertEqual(len(calls), 1) + self.assertAlmostEqual(calls[0][1][0], 6.0, places=1) + + calls = log.info.mock_calls + self.assertEqual(len(calls), 1) + self._assert_isotime(calls[0][1][1], until) + + def test_wait_until_datetime(self): + extr = extractor.find("test:") + until = datetime.utcnow() + timedelta(seconds=5) + until_local = datetime.now() + timedelta(seconds=5) + + with patch("time.sleep") as sleep, patch.object(extr, "log") as log: + extr.wait(until=until) + + calls = sleep.mock_calls + self.assertEqual(len(calls), 1) + self.assertAlmostEqual(calls[0][1][0], 6.0, places=1) + + calls = log.info.mock_calls + self.assertEqual(len(calls), 1) + self._assert_isotime(calls[0][1][1], until_local) + + def _assert_isotime(self, output, until): + if not isinstance(until, datetime): + until = datetime.fromtimestamp(until) + o = self._isotime_to_seconds(output) + u = self._isotime_to_seconds(until.time().isoformat()[:8]) + self.assertLess(o-u, 1.0) + + @staticmethod + def _isotime_to_seconds(isotime): + parts = isotime.split(":") + return int(parts[0]) * 3600 + int(parts[1]) * 60 + int(parts[2]) + + +class TextExtractorOAuth(unittest.TestCase): + + @classmethod + def setUpClass(cls): + mastodon.generate_extractors() + + def test_oauth1(self): + for category in ("flickr", "smugmug", "tumblr"): + extr = extractor.find("oauth:" + category) + + with patch.object(extr, "_oauth1_authorization_flow") as m: + for msg in extr: + pass + self.assertEqual(len(m.mock_calls), 1) + + def test_oauth2(self): + for category in ("deviantart", "reddit"): + extr = extractor.find("oauth:" + category) + + with patch.object(extr, "_oauth2_authorization_code_grant") as m: + for msg in extr: + pass + self.assertEqual(len(m.mock_calls), 1) + + def test_oauth2_mastodon(self): + extr = extractor.find("oauth:mastodon:pawoo.net") + + with patch.object(extr, "_oauth2_authorization_code_grant") as m, \ + patch.object(extr, "_register") as r: + for msg in extr: + pass + self.assertEqual(len(r.mock_calls), 0) + self.assertEqual(len(m.mock_calls), 1) + + def test_oauth2_mastodon_unknown(self): + extr = extractor.find("oauth:mastodon:example.com") + + with patch.object(extr, "_oauth2_authorization_code_grant") as m, \ + patch.object(extr, "_register") as r: + r.return_value = { + "client-id" : "foo", + "client-secret": "bar", + } + + for msg in extr: + pass + + self.assertEqual(len(r.mock_calls), 1) + self.assertEqual(len(m.mock_calls), 1) + + if __name__ == "__main__": unittest.main() diff --git a/test/test_results.py b/test/test_results.py index b697d15..9064810 100644 --- a/test/test_results.py +++ b/test/test_results.py @@ -27,7 +27,9 @@ TRAVIS_SKIP = { # temporary issues, etc. BROKEN = { - "myportfolio", + "dokireader", + "mangafox", + "mangahere", "photobucket", "worldthree", } diff --git a/test/test_text.py b/test/test_text.py index 6a6d83a..0390823 100644 --- a/test/test_text.py +++ b/test/test_text.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2015-2018 Mike Fährmann +# Copyright 2015-2020 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -375,6 +375,10 @@ class TestText(unittest.TestCase): f("2019-05-07T21:25:02+0900"), datetime.datetime(2019, 5, 7, 12, 25, 2), ) + self.assertEqual( + f("2019-05-07T21:25:02", "%Y-%m-%dT%H:%M:%S", utcoffset=9), + datetime.datetime(2019, 5, 7, 12, 25, 2), + ) self.assertEqual( f("2019-05-07 21:25:02"), "2019-05-07 21:25:02", -- cgit v1.2.3