diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2012-10-13 21:39:58 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2012-10-13 21:39:58 +0200 |
commit | 7e7adc64713f74976d5994af36b5f758620fb37b (patch) | |
tree | 868084317f8818dbb327c76eaff3c7c7edce0231 /tests | |
parent | fixed JsEngine init (diff) | |
download | pyload-7e7adc64713f74976d5994af36b5f758620fb37b.tar.xz |
added JSON and WS client, re organized tests, new classes for easier api tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/__init__.py | 0 | ||||
-rw-r--r-- | tests/api/ApiProxy.py | 68 | ||||
-rw-r--r-- | tests/api/ApiTester.py | 35 | ||||
-rw-r--r-- | tests/api/__init__.py | 0 | ||||
-rw-r--r-- | tests/api/test_JSONBackend.py | 27 | ||||
-rw-r--r-- | tests/api/test_api.py | 39 | ||||
-rw-r--r-- | tests/api/test_noargs.py | 29 | ||||
-rw-r--r-- | tests/config/db.version | 1 | ||||
-rw-r--r-- | tests/helper/Stubs.py | 3 | ||||
-rw-r--r-- | tests/manager/__init__.py | 0 | ||||
-rw-r--r-- | tests/manager/test_filemanager.py (renamed from tests/test_filemanager.py) | 4 | ||||
-rw-r--r-- | tests/manager/test_interactionManager.py (renamed from tests/test_interactionManager.py) | 2 | ||||
-rw-r--r-- | tests/other/__init__.py | 0 | ||||
-rw-r--r-- | tests/other/test_configparser.py (renamed from tests/test_configparser.py) | 7 | ||||
-rw-r--r-- | tests/other/test_database.py (renamed from tests/test_database.py) | 7 | ||||
-rw-r--r-- | tests/other/test_syntax.py (renamed from tests/test_syntax.py) | 4 | ||||
-rw-r--r-- | tests/test_api.py | 18 | ||||
-rw-r--r-- | tests/test_backends.py | 59 |
18 files changed, 214 insertions, 89 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/__init__.py diff --git a/tests/api/ApiProxy.py b/tests/api/ApiProxy.py new file mode 100644 index 000000000..74c938870 --- /dev/null +++ b/tests/api/ApiProxy.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- + + +from module.remote.ttypes_debug import classes, methods + +class ApiProxy: + """ Proxy that does type checking on the api """ + + def __init__(self, api, user="User", pw="test"): + self.api = api + self.user = user + self.pw = pw + + if user and pw is not None: + self.api.login(user, pw) + + def assert_type(self, result, type): + if not type: return # void + try: + # Complex attribute + if isinstance(type, tuple): + # Optional result + if type[0] is None: + # Only check if not None + if result is not None: self.assert_type(result, type[1]) + + # List + elif type[0] == list: + assert isinstance(result, list) + for item in result: + self.assert_type(item, type[1]) + # Dict + elif type[0] == dict: + assert isinstance(result, dict) + for k, v in result.iteritems(): + self.assert_type(k, type[1]) + self.assert_type(v, type[2]) + + # Struct - Api class + elif hasattr(result, "__name__") and result.__name__ in classes: + for attr, atype in zip(result.__slots__, classes[result.__name__]): + self.assert_type(getattr(result, attr), atype) + else: # simple object + assert isinstance(result, type) + except AssertionError: + print "Assertion for %s as %s failed" % (result, type) + raise + + + def call(self, func, *args, **kwargs): + result = getattr(self.api, func)(*args, **kwargs) + self.assert_type(result, methods[func]) + + return result + + + def __getattr__(self, item): + def call(*args, **kwargs): + return self.call(item, *args, **kwargs) + + return call + +if __name__ == "__main__": + + from module.remote.JSONClient import JSONClient + + api = ApiProxy(JSONClient(), "User", "test") + api.getServerVersion()
\ No newline at end of file diff --git a/tests/api/ApiTester.py b/tests/api/ApiTester.py new file mode 100644 index 000000000..b17a7655e --- /dev/null +++ b/tests/api/ApiTester.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- + +from module.remote.JSONClient import JSONClient +from module.remote.WSClient import WSClient + +from ApiProxy import ApiProxy + +class ApiTester: + + tester= [] + + @classmethod + def register(cls, tester): + cls.tester.append(tester) + + @classmethod + def get_methods(cls): + """ All available methods for testing """ + methods = [] + for t in cls.tester: + methods.extend(getattr(t, attr) for attr in dir(t) if attr.startswith("test_")) + return methods + + def __init__(self): + ApiTester.register(self) + self.api = None + + def setApi(self, api): + self.api = api + + def enableJSON(self): + self.api = ApiProxy(JSONClient()) + + def enableWS(self): + self.api = ApiProxy(WSClient()) diff --git a/tests/api/__init__.py b/tests/api/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/api/__init__.py diff --git a/tests/api/test_JSONBackend.py b/tests/api/test_JSONBackend.py new file mode 100644 index 000000000..a3805497b --- /dev/null +++ b/tests/api/test_JSONBackend.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +from nose.tools import raises + +from module.remote.JSONClient import JSONClient + +class TestJSONBackend: + + def setUp(self): + self.client = JSONClient() + + def test_login(self): + self.client.login("User", "test") + self.client.getServerVersion() + self.client.logout() + + def test_wronglogin(self): + ret = self.client.login("WrongUser", "wrongpw") + assert ret == False + + @raises(Exception) + def test_access(self): + self.client.getServerVersion() + + @raises(Exception) + def test_unknown_method(self): + self.client.sdfdsg() diff --git a/tests/api/test_api.py b/tests/api/test_api.py new file mode 100644 index 000000000..68448b891 --- /dev/null +++ b/tests/api/test_api.py @@ -0,0 +1,39 @@ + +from unittest import TestCase +from random import choice + +from pyLoadCore import Core + +from ApiTester import ApiTester + +class TestAPI(TestCase): + """ + Test all available testers randomly and on all backends + """ + core = None + + @classmethod + def setUpClass(cls): + from test_noargs import TestNoArgs + + cls.core = Core() + cls.core.start(False, False, True) + for Test in (TestNoArgs,): + t = Test() + t.enableJSON() + t = Test() + t.enableWS() + t = Test() + t.setApi(cls.core.api) + + cls.methods = ApiTester.get_methods() + + @classmethod + def tearDownClass(cls): + cls.core.shutdown() + + def test_random(self, n=10000): + + for i in range(n): + func = choice(self.methods) + func() diff --git a/tests/api/test_noargs.py b/tests/api/test_noargs.py new file mode 100644 index 000000000..02e49cf13 --- /dev/null +++ b/tests/api/test_noargs.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +import inspect + +from ApiTester import ApiTester + +from module.remote.ttypes import Iface + +IGNORE = ('kill', 'restart') + +class TestNoArgs(ApiTester): + def setUp(self): + self.enableJSON() + +# Setup test_methods dynamically, only these which require no arguments +for name in dir(Iface): + if name.startswith("_") or name in IGNORE: continue + + spec = inspect.getargspec(getattr(Iface, name)) + if len(spec.args) == 1 and (not spec.varargs or len(spec.varargs) == 0): + def meta_test(name): #retain local scope + def test(self): + getattr(self.api, name)() + test.func_name = "test_%s" % name + return test + + setattr(TestNoArgs, "test_%s" % name, meta_test(name)) + + del meta_test
\ No newline at end of file diff --git a/tests/config/db.version b/tests/config/db.version deleted file mode 100644 index bf0d87ab1..000000000 --- a/tests/config/db.version +++ /dev/null @@ -1 +0,0 @@ -4
\ No newline at end of file diff --git a/tests/helper/Stubs.py b/tests/helper/Stubs.py index 5c44cfb58..4ebd12592 100644 --- a/tests/helper/Stubs.py +++ b/tests/helper/Stubs.py @@ -4,6 +4,7 @@ import sys from os.path import abspath, dirname, join from time import strftime from traceback import format_exc +from collections import defaultdict sys.path.append(abspath(join(dirname(__file__), "..", "..", "module", "lib"))) sys.path.append(abspath(join(dirname(__file__), "..", ".."))) @@ -73,6 +74,8 @@ class Core: self.cache = {} self.packageCache = {} + self.statusMsg = defaultdict(lambda: "statusmsg") + self.log = LogStub() def getServerVersion(self): diff --git a/tests/manager/__init__.py b/tests/manager/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/manager/__init__.py diff --git a/tests/test_filemanager.py b/tests/manager/test_filemanager.py index 7b82840b1..81acea4d0 100644 --- a/tests/test_filemanager.py +++ b/tests/manager/test_filemanager.py @@ -2,8 +2,8 @@ from random import choice -from helper.Stubs import Core -from helper.BenchmarkTest import BenchmarkTest +from tests.helper.Stubs import Core +from tests.helper.BenchmarkTest import BenchmarkTest from module.database import DatabaseBackend # disable asyncronous queries diff --git a/tests/test_interactionManager.py b/tests/manager/test_interactionManager.py index 920d84b9d..db233bb25 100644 --- a/tests/test_interactionManager.py +++ b/tests/manager/test_interactionManager.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from unittest import TestCase -from helper.Stubs import Core +from tests.helper.Stubs import Core from module.Api import Input, Output from module.interaction.InteractionManager import InteractionManager 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/test_configparser.py b/tests/other/test_configparser.py index d797c7912..acb05c63e 100644 --- a/tests/test_configparser.py +++ b/tests/other/test_configparser.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from collections import defaultdict -from helper.Stubs import Core +from tests.helper.Stubs import Core from module.database import DatabaseBackend from module.config.ConfigParser import ConfigParser @@ -9,6 +9,8 @@ from module.config.ConfigParser import ConfigParser # TODO class TestConfigParser(): + db = None + @classmethod def setUpClass(cls): cls.db = DatabaseBackend(Core()) @@ -18,6 +20,9 @@ class TestConfigParser(): cls.db.setup() cls.db.clearAllConfigs() + @classmethod + def tearDownClass(cls): + cls.db.shutdown() def test_db(self): diff --git a/tests/test_database.py b/tests/other/test_database.py index fb134ff41..dd733b4ff 100644 --- a/tests/test_database.py +++ b/tests/other/test_database.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- -from collections import defaultdict - -from helper.Stubs import Core -from helper.BenchmarkTest import BenchmarkTest +from tests.helper.Stubs import Core +from tests.helper.BenchmarkTest import BenchmarkTest from module.Api import DownloadState from module.database import DatabaseBackend @@ -29,7 +27,6 @@ class TestDatabase(BenchmarkTest): cls.db = DatabaseBackend(Core()) cls.db.manager = cls.db.core - cls.db.manager.statusMsg = defaultdict(lambda: "statusmsg") cls.db.setup() cls.db.purgeAll() diff --git a/tests/test_syntax.py b/tests/other/test_syntax.py index a4cc53ee5..fbf7edf8f 100644 --- a/tests/test_syntax.py +++ b/tests/other/test_syntax.py @@ -5,10 +5,10 @@ from os.path import abspath, dirname, join from unittest import TestCase -PATH = abspath(join(dirname(abspath(__file__)), "..", "")) +PATH = abspath(join(dirname(abspath(__file__)), "..", "..", "")) # needed to register globals -from helper import Stubs +from tests.helper import Stubs class TestSyntax(TestCase): pass diff --git a/tests/test_api.py b/tests/test_api.py deleted file mode 100644 index 0171b46bb..000000000 --- a/tests/test_api.py +++ /dev/null @@ -1,18 +0,0 @@ - -from unittest import TestCase - -from pyLoadCore import Core -from module.common.APIExerciser import APIExerciser - -class TestApi(TestCase): - - @classmethod - def setUpClass(cls): - cls.core = Core() - cls.core.start(False, False, True) - - def test_random(self): - api = APIExerciser(self.core) - - for i in range(2000): - api.testAPI() diff --git a/tests/test_backends.py b/tests/test_backends.py deleted file mode 100644 index 71ccedd2f..000000000 --- a/tests/test_backends.py +++ /dev/null @@ -1,59 +0,0 @@ -# -*- coding: utf-8 -*- - - -from urllib import urlencode -from urllib2 import urlopen, HTTPError -from json import loads - -from logging import log - -from module.common import APIExerciser - -url = "http://localhost:8001/api/%s" - -class TestBackends(): - - def setUp(self): - u = urlopen(url % "login", data=urlencode({"username": "TestUser", "password": "sometestpw"})) - self.key = loads(u.read()) - assert self.key is not False - - def test_random(self): - api = APIExerciser.APIExerciser(None, True, "TestUser", "sometestpw") - - assert api.api.login("crapp", "wrong pw") is False - - for i in range(0, 1000): - api.testAPI() - - def call(self, name, post=None): - if not post: post = {} - post["session"] = self.key - u = urlopen(url % name, data=urlencode(post)) - return loads(u.read()) - - def test_wronglogin(self): - u = urlopen(url % "login", data=urlencode({"username": "crap", "password": "wrongpw"})) - assert loads(u.read()) is False - - def test_access(self): - try: - urlopen(url % "getServerVersion") - except HTTPError, e: - assert e.code == 403 - else: - assert False - - def test_status(self): - ret = self.call("statusServer") - log(1, str(ret)) - assert "pause" in ret - assert "queue" in ret - - def test_unknown_method(self): - try: - self.call("notExisting") - except HTTPError, e: - assert e.code == 404 - else: - assert False |