From 2cfa3739a8d87a98d95d70381516aca05e9e9ebb Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Sat, 29 Dec 2018 20:18:00 -0600 Subject: [PATCH] Added unittests to the extractor pakage --- src/__init__.py | 0 src/run_tests.py | 18 +++++++++ src/test/__init__.py | 0 src/test/test_extractors.py | 80 +++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 src/__init__.py create mode 100644 src/run_tests.py create mode 100644 src/test/__init__.py create mode 100644 src/test/test_extractors.py diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/run_tests.py b/src/run_tests.py new file mode 100644 index 0000000..034d6e4 --- /dev/null +++ b/src/run_tests.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +import unittest + +testmodules = ['test.test_extractors'] + +suite = unittest.TestSuite() + +for t in testmodules: + try: + # If the module defines a suite() function, call it to get the suite. + mod = __import__(t, globals(), locals(), ['suite']) + suitefn = getattr(mod, 'suite') + suite.addTest(suitefn()) + except (ImportError, AttributeError): + # else, just load all the test cases from the module. + suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t)) + +unittest.TextTestRunner().run(suite) \ No newline at end of file diff --git a/src/test/__init__.py b/src/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/test/test_extractors.py b/src/test/test_extractors.py new file mode 100644 index 0000000..7ed3acc --- /dev/null +++ b/src/test/test_extractors.py @@ -0,0 +1,80 @@ +""" Unittests for extractors present in MusicDL. """ +from __future__ import unicode_literals +import sys +import unittest +import re +import i18n +import extractors +from extractors import baseFile + +# Pytohn 2/3 compat +if sys.version[0] == "2": + strtype = unicode +else: + strtype = str + +class extractorsTestCase(unittest.TestCase): + + def setUp(self): + i18n.setup() + + def search(self, extractor_name, search_query="piano", skip_validation=False): + """ Blank here""" + # Test basic instance stuff. + extractor_instance = getattr(extractors, extractor_name).interface() + extractor_instance.search(search_query) + self.assertIsInstance(extractor_instance.results, list) + self.assertNotEqual(len(extractor_instance.results), 0) + self.assertIsInstance(len(extractor_instance.results), int) + # Take and test validity of the first item. + item = extractor_instance.results[0] + self.assertIsInstance(item, baseFile.song) + self.assertIsInstance(item.title, strtype) + self.assertNotEqual(item.title, "") + if extractor_name == "youtube": # Duration is only available for youtube. + self.assertIsInstance(item.duration, strtype) + self.assertNotEqual(item.duration, "") + self.assertIsInstance(item.url, strtype) + self.assertNotEqual(item.url, "") + if extractor_name == "youtube" and skip_validation == False: + match = re.search("((?<=(v|V)/)|(?<=be/)|(?<=(\?|\&)v=)|(?<=embed/))([\w-]+)", item.url) + self.assertNotEqual(match, None) + formatted_track = item.format_track() + self.assertIsInstance(formatted_track, strtype) + self.assertNotEquals(formatted_track, "") + item.get_download_url() + self.assertIsInstance(item.download_url, strtype) + self.assertNotEquals(item.download_url, "") + + def search_blank(self, extractor_name, search_query=""): + extractor_instance = getattr(extractors, extractor_name).interface() + self.assertRaises(ValueError, extractor_instance.search, search_query) + + def test_youtube_search(self): + """ Testing a Youtube search. """ + self.search("youtube") + + def test_youtube_search_blank(self): + """ Testing a youtube search when text is blank or not passed. """ + self.search_blank("youtube") + + def test_youtube_direct_link(self): + self.search("youtube", "https://www.youtube.com/watch?v=hwDiI9p9L-g") + + def test_youtube_playlist(self): + self.search("youtube", "https://www.youtube.com/playlist?list=PLqivnvaruBVH8fqI5JU9h5jZKV-32bbEn", skip_validation=True) + + def test_mailru_search(self): + self.search("mailru") + + def test_mailru_search_blank(self): + self.search_blank("mailru") + + def test_zaycev_search(self): + self.search("zaycev") + + def test_zaycev_search_blank(self): + self.search_blank("zaycev") + +if __name__ == "__main__": + unittest.main() \ No newline at end of file