diff options
Diffstat (limited to 'tests/other')
-rw-r--r-- | tests/other/__init__.py | 0 | ||||
-rw-r--r-- | tests/other/test_configparser.py | 43 | ||||
-rw-r--r-- | tests/other/test_curlDownload.py | 55 | ||||
-rw-r--r-- | tests/other/test_curlRequest.py | 42 | ||||
-rw-r--r-- | tests/other/test_filedatabase.py | 215 | ||||
-rw-r--r-- | tests/other/test_requestFactory.py | 38 | ||||
-rw-r--r-- | tests/other/test_syntax.py | 40 |
7 files changed, 433 insertions, 0 deletions
diff --git a/tests/other/__init__.py b/tests/other/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/other/__init__.py diff --git a/tests/other/test_configparser.py b/tests/other/test_configparser.py new file mode 100644 index 000000000..0efd41aee --- /dev/null +++ b/tests/other/test_configparser.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- + +from nose.tools import raises + +from tests.helper.Stubs import Core + +from pyload.config.ConfigParser import ConfigParser + +class TestConfigParser(): + + @classmethod + def setUpClass(cls): + # Only needed to get imports right + cls.core = Core() + cls.config = ConfigParser() + + def test_dict(self): + + assert self.config["general"]["language"] + self.config["general"]["language"] = "de" + assert self.config["general"]["language"] == "de" + + def test_contains(self): + + assert "general" in self.config + assert "foobaar" not in self.config + + def test_iter(self): + for section, config, values in self.config.iterSections(): + assert isinstance(section, basestring) + assert isinstance(config.config, dict) + assert isinstance(values, dict) + + def test_get(self): + assert self.config.getSection("general")[0].config + + @raises(KeyError) + def test_invalid_config(self): + print self.config["invalid"]["config"] + + @raises(KeyError) + def test_invalid_section(self): + print self.config["general"]["invalid"]
\ No newline at end of file diff --git a/tests/other/test_curlDownload.py b/tests/other/test_curlDownload.py new file mode 100644 index 000000000..17af1cdd4 --- /dev/null +++ b/tests/other/test_curlDownload.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- + +from os import stat + +from unittest import TestCase + +from tests.helper.Stubs import Core +from pyload.network.Bucket import Bucket +from pyload.plugins.network.CurlRequest import CurlRequest +from pyload.plugins.network.CurlDownload import CurlDownload + +class TestCurlRequest(TestCase): + + cookieURL = "http://forum.pyload.org" + + def setUp(self): + self.dl = CurlDownload(Bucket()) + + def tearDown(self): + self.dl.close() + + def test_download(self): + + assert self.dl.context is not None + + self.dl.download("http://pyload.org/lib/tpl/pyload/images/pyload-logo-edited3.5-new-font-small.png", "/tmp/random.bin") + + print self.dl.size, self.dl.arrived + assert self.dl.size == self.dl.arrived > 0 + assert stat("/tmp/random.bin").st_size == self.dl.size + + def test_cookies(self): + + req = CurlRequest({}) + req.load(self.cookieURL) + + assert len(req.cj) > 0 + + dl = CurlDownload(Bucket(), req) + + assert req.context is dl.context is not None + + dl.download(self.cookieURL + "/cookies.php", "cookies.txt") + cookies = open("cookies.txt", "rb").read().splitlines() + + self.assertEqual(len(cookies), len(dl.context)) + for c in cookies: + k, v = c.strip().split(":") + self.assertIn(k, req.cj) + + + def test_attributes(self): + assert self.dl.size == 0 + assert self.dl.speed == 0 + assert self.dl.arrived == 0
\ No newline at end of file diff --git a/tests/other/test_curlRequest.py b/tests/other/test_curlRequest.py new file mode 100644 index 000000000..6bd4a2772 --- /dev/null +++ b/tests/other/test_curlRequest.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +from tests.helper.Stubs import Core +from pyload.plugins.network.CurlRequest import CurlRequest + +from unittest import TestCase + + +class TestCurlRequest(TestCase): + # This page provides a test which prints all set cookies + cookieURL = "http://forum.pyload.org" + + def setUp(self): + self.req = CurlRequest({}) + + def tearDown(self): + self.req.close() + + def test_load(self): + self.req.load("http://pyload.org") + + def test_cookies(self): + self.req.load(self.cookieURL, cookies=False) + assert len(self.req.cj) == 0 + + self.req.load(self.cookieURL) + assert len(self.req.cj) > 1 + + cookies = dict([c.strip().split(":") for c in self.req.load(self.cookieURL + "/cookies.php").splitlines()]) + for k, v in cookies.iteritems(): + self.assertIn(k, self.req.cj) + self.assertEqual(v, self.req.cj[k].value) + + for c in self.req.cj: + self.assertIn(c, cookies) + + cookies = self.req.load(self.cookieURL + "/cookies.php", cookies=False) + self.assertEqual(cookies, "") + + + def test_auth(self): + pass
\ No newline at end of file diff --git a/tests/other/test_filedatabase.py b/tests/other/test_filedatabase.py new file mode 100644 index 000000000..f2a60e997 --- /dev/null +++ b/tests/other/test_filedatabase.py @@ -0,0 +1,215 @@ +# -*- coding: utf-8 -*- + +from tests.helper.Stubs import Core +from tests.helper.BenchmarkTest import BenchmarkTest + +from pyload.Api import DownloadState, PackageInfo, FileInfo +from pyload.database import DatabaseBackend + +# disable asyncronous queries +DatabaseBackend.async = DatabaseBackend.queue + +from random import choice + +class TestDatabase(BenchmarkTest): + bench = ["insert", "insert_links", "insert_many", "get_packages", + "get_files", "get_files_queued", "get_package_childs", "get_package_files", + "get_package_data", "get_file_data", "find_files", "collector", "purge"] + pids = None + fids = None + owner = 123 + pstatus = 0 + + @classmethod + def setUpClass(cls): + cls.pids = [-1] + cls.fids = [] + + cls.db = DatabaseBackend(Core()) + cls.db.manager = cls.db.core + + cls.db.setup() + cls.db.purgeAll() + + @classmethod + def tearDownClass(cls): + cls.db.purgeAll() + cls.db.shutdown() + + # benchmarker ignore setup + def setUp(self): + self.db.purgeAll() + self.pids = [-1] + self.fids = [] + + self.test_insert(20) + self.test_insert_many() + self.fids = self.db.getAllFiles().keys() + + + def test_insert(self, n=200): + for i in range(n): + pid = self.db.addPackage("name", "folder", choice(self.pids), "password", "site", "comment", self.pstatus, + self.owner) + self.pids.append(pid) + + def test_insert_links(self): + for i in range(10000): + fid = self.db.addLink("url %s" % i, "name", "plugin", choice(self.pids), self.owner) + self.fids.append(fid) + + def test_insert_many(self): + for pid in self.pids: + self.db.addLinks([("url %s" % i, "plugin") for i in range(50)], pid, self.owner) + + def test_get_packages(self): + packs = self.db.getAllPackages() + n = len(packs) + assert n == len(self.pids) - 1 + + print "Fetched %d packages" % n + self.assert_pack(choice(packs.values())) + + def test_get_files(self): + files = self.db.getAllFiles() + n = len(files) + assert n >= len(self.pids) + + print "Fetched %d files" % n + self.assert_file(choice(files.values())) + + def test_get_files_queued(self): + files = self.db.getAllFiles(state=DownloadState.Unfinished) + print "Fetched %d files queued" % len(files) + + def test_delete(self): + pid = choice(self.pids) + self.db.deletePackage(pid) + self.pids.remove(pid) + + def test_get_package_childs(self): + pid = choice(self.pids) + packs = self.db.getAllPackages(root=pid) + + print "Package %d has %d packages" % (pid, len(packs)) + self.assert_pack(choice(packs.values())) + + def test_get_package_files(self): + pid = choice(self.pids) + files = self.db.getAllFiles(package=pid) + + print "Package %d has %d files" % (pid, len(files)) + self.assert_file(choice(files.values())) + + def test_get_package_data(self, stats=False): + pid = choice(self.pids) + p = self.db.getPackageInfo(pid, stats) + self.assert_pack(p) + # test again with stat + if not stats: + self.test_get_package_data(True) + + def test_get_file_data(self): + fid = choice(self.fids) + f = self.db.getFileInfo(fid) + self.assert_file(f) + + def test_find_files(self): + files = self.db.getAllFiles(search="1") + print "Found %s files" % len(files) + f = choice(files.values()) + + assert "1" in f.name + names = self.db.getMatchingFilenames("1") + for name in names: + assert "1" in name + + def test_collector(self): + self.db.saveCollector(0, "data") + assert self.db.retrieveCollector(0) == "data" + self.db.deleteCollector(0) + + def test_purge(self): + self.db.purgeLinks() + + + def test_user_context(self): + self.db.purgeAll() + + p1 = self.db.addPackage("name", "folder", 0, "password", "site", "comment", self.pstatus, 0) + self.db.addLink("url", "name", "plugin", p1, 0) + p2 = self.db.addPackage("name", "folder", 0, "password", "site", "comment", self.pstatus, 1) + self.db.addLink("url", "name", "plugin", p2, 1) + + assert len(self.db.getAllPackages(owner=0)) == 1 == len(self.db.getAllFiles(owner=0)) + assert len(self.db.getAllPackages(root=0, owner=0)) == 1 == len(self.db.getAllFiles(package=p1, owner=0)) + assert len(self.db.getAllPackages(owner=1)) == 1 == len(self.db.getAllFiles(owner=1)) + assert len(self.db.getAllPackages(root=0, owner=1)) == 1 == len(self.db.getAllFiles(package=p2, owner=1)) + assert len(self.db.getAllPackages()) == 2 == len(self.db.getAllFiles()) + + self.db.deletePackage(p1, 1) + assert len(self.db.getAllPackages(owner=0)) == 1 == len(self.db.getAllFiles(owner=0)) + self.db.deletePackage(p1, 0) + assert len(self.db.getAllPackages(owner=1)) == 1 == len(self.db.getAllFiles(owner=1)) + self.db.deletePackage(p2) + + assert len(self.db.getAllPackages()) == 0 + + def test_count(self): + self.db.purgeAll() + + assert self.db.downloadstats() == (0,0) + assert self.db.queuestats() == (0,0) + assert self.db.processcount() == 0 + + def test_update(self): + p1 = self.db.addPackage("name", "folder", 0, "password", "site", "comment", self.pstatus, 0) + pack = self.db.getPackageInfo(p1) + assert isinstance(pack, PackageInfo) + + pack.folder = "new folder" + pack.comment = "lol" + pack.tags.append("video") + + self.db.updatePackage(pack) + + pack = self.db.getPackageInfo(p1) + assert pack.folder == "new folder" + assert pack.comment == "lol" + assert "video" in pack.tags + + def assert_file(self, f): + try: + assert f is not None + assert isinstance(f, FileInfo) + self.assert_int(f, ("fid", "status", "size", "media", "fileorder", "added", "package", "owner")) + assert f.status in range(5) + assert f.owner == self.owner + assert f.media in range(1024) + assert f.package in self.pids + assert f.added > 10 ** 6 # date is usually big integer + except: + print f + raise + + def assert_pack(self, p): + try: + assert p is not None + assert isinstance(p, PackageInfo) + self.assert_int(p, ("pid", "root", "added", "status", "packageorder", "owner")) + assert p.pid in self.pids + assert p.owner == self.owner + assert p.status in range(5) + assert p.root in self.pids + assert p.added > 10 ** 6 + assert isinstance(p.tags, list) + assert p.shared in (0, 1) + except: + print p + raise + + def assert_int(self, obj, list): + for attr in list: assert type(getattr(obj, attr)) == int + +if __name__ == "__main__": + TestDatabase.benchmark()
\ No newline at end of file diff --git a/tests/other/test_requestFactory.py b/tests/other/test_requestFactory.py new file mode 100644 index 000000000..751e7f03b --- /dev/null +++ b/tests/other/test_requestFactory.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- + +from tests.helper.Stubs import Core + +from pyload.plugins.network.CurlRequest import CurlRequest +from pyload.network.RequestFactory import RequestFactory + +class TestRequestFactory: + + @classmethod + def setUpClass(cls): + cls.req = RequestFactory(Core()) + + def test_get_request(self): + req = self.req.getRequest() + + new_req = self.req.getRequest(req.getContext()) + assert new_req.getContext() == req.getContext() + + cj = CurlRequest.CONTEXT_CLASS() + assert self.req.getRequest(cj).context is cj + + def test_get_request_class(self): + + self.req.getRequest(None, CurlRequest) + + def test_get_download(self): + dl = self.req.getDownloadRequest() + dl.close() + + # with given request + req = self.req.getRequest() + dl = self.req.getDownloadRequest(req) + + assert req.context is dl.context + assert req.options is dl.options + + dl.close()
\ No newline at end of file diff --git a/tests/other/test_syntax.py b/tests/other/test_syntax.py new file mode 100644 index 000000000..396fc8f4b --- /dev/null +++ b/tests/other/test_syntax.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- + +from os import walk +from os.path import abspath, dirname, join + +from unittest import TestCase + +PATH = abspath(join(dirname(abspath(__file__)), "..", "..", "")) + +# needed to register globals +from tests.helper import Stubs + +class TestSyntax(TestCase): + pass + + +for path, dirs, files in walk(join(PATH, "pyload")): + + for f in files: + if not f.endswith(".py") or f.startswith("__"): continue + fpath = join(path, f) + pack = fpath.replace(PATH, "")[1:-3] #replace / and .py + imp = pack.replace("/", ".") + packages = imp.split(".") + #__import__(imp) + + # to much sideeffect when importing + if "web" in packages or "lib" in packages: continue + + # currying + def meta(imp, sig): + def _test(self=None): + __import__(imp) + + _test.func_name = sig + return _test + + # generate test methods + sig = "test_%s_%s" % (packages[-2], packages[-1]) + setattr(TestSyntax, sig, meta(imp, sig))
\ No newline at end of file |