From e9a42218065324aa3b36b1c20446cb6cec98fc3a Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Sun, 19 May 2019 20:37:35 -0500 Subject: [PATCH] Added tests for some API methods --- run_tests.py | 18 +++++++++++++++++ tests/__init__.py | 1 + tests/test_api.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 run_tests.py create mode 100644 tests/__init__.py create mode 100644 tests/test_api.py diff --git a/run_tests.py b/run_tests.py new file mode 100644 index 0000000..d643e6d --- /dev/null +++ b/run_tests.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +import unittest + +testmodules = ["tests.test_api"] + +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(verbosity=2).run(suite) \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..f2e4eb0 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..c4c1af6 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +import unittest +from funkwhale import api + +class testApi(unittest.TestCase): + + def setUp(self): + # For the moment we use the demo Funkwhale server. + # Later the API wrapper should have the possibility of using environment variables to fill the class constructor. + self.session = api.Session() + + def test_auth(self): + """ Testing user authentication against the Funkwhale server. """ + self.session.login() + self.assertFalse(self.session.token == None) + + def test_invalid_auth(self): + """ Testing the exception mechanism present in all API methods. """ + self.setUp() + self.session.username = "demo1" + self.assertRaises(api.APIError, self.session.login) + self.setUp() + self.session.login() + + def test_call_to_normal_methods(self): + """ Test that we call some endpoints correctly by using the API facilities. """ + api = self.session.get_api() + artists_without_params = api.artists.get() + present_keys = ["next", "previous", "results", "count"] + for i in present_keys: + self.assertTrue(i in artists_without_params) + artists_with_params = api.artists.get(page_size=5, playable=True, ordering="-id") + for i in present_keys: + self.assertTrue(i in artists_with_params) + self.assertTrue(len(artists_with_params["results"]) == 5) + self.assertTrue(artists_with_params["results"][0]["id"] > artists_with_params["results"][-1]["id"]) + + def test_call_to_special_methods(self): + """ Testing some special methods that are processed differently by the API, such as /artists/id, /artists/id/libraries. """ + api = self.session.get_api() + # Getting the first artist. + artist = api.artists.get(1) + #the only thing we can test about artist is that the ID matches and it's a dict object. + self.assertTrue(type(artist) == dict) + self.assertTrue(artist["id"] == 1) + libraries = api.albums.libraries.get(1) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file