changeset 7:3a6f3193cc7d

Created new tests
author Ben Croston <ben@croston.org>
date Thu, 01 Sep 2011 19:19:03 +0100
parents bb6b8df4dae8
children 685479d1f0a7
files wibble/client/ServerProxy.py wibble/tests.py
diffstat 2 files changed, 41 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/wibble/client/ServerProxy.py	Wed Aug 31 22:49:23 2011 +0100
+++ b/wibble/client/ServerProxy.py	Thu Sep 01 19:19:03 2011 +0100
@@ -175,14 +175,14 @@
 
 if __name__ == '__main__':
     ##### btc fixme
-    jsonrpc_client = ServerProxy2('http://localhost:1337/', username='testuser', password='', user_agent='Py2NotInternetExploiter')
-    #jsonrpc_client = ServerProxy2('https://www.croston.org/test/index.py',
+    jsonrpc_client = ServerProxy('http://localhost:1337/', username='testuser', password='', user_agent='Py2NotInternetExploiter')
+    #jsonrpc_client = ServerProxy('https://www.croston.org/test/index.py',
     #                             username='testuser',
     #                             password='',
     #                             user_agent='Py2NotInternetExploiter')
     assert jsonrpc_client.api.mymethod() == jsonrpc_client.mymethod()
     try:
-        print(jsonrpc_client.wibble('this should fail'))
+        print jsonrpc_client.wibble('this should fail')
     except BadRequestException:
         pass # test passed
     else:
--- a/wibble/tests.py	Wed Aug 31 22:49:23 2011 +0100
+++ b/wibble/tests.py	Thu Sep 01 19:19:03 2011 +0100
@@ -11,11 +11,13 @@
   def mymethod(self):
       return 'wibbler woz ere'
 
-  def myexception(self):
-      raise Exception('This is a test error')
+  def echo(self, mystring):
+      return 'ECHO: ' + mystring
+
+  def raiseexception(self):
+      dividebyzeroerror = 1/0
 
 def myauth(username, password, useragent=None):
-  #raise Exception("This is a test error in auth")
   return username == 'testuser' and \
          hashlib.md5('s3cr3t').hexdigest() == password and \
          useragent == 'wibble_unittest'
@@ -30,6 +32,16 @@
 ##### server ^^^ #####
 
 ##### client vvv #####
+class NotAuthTest(unittest.TestCase):
+    def runTest(self):
+        from client.ServerProxy import ServerProxy, UnauthorisedException
+        self.client = ServerProxy('http://localhost:1337/',
+                                  username='testuser',
+                                  password='s3cr3t',
+                                  user_agent='InternerExploiter')
+        with self.assertRaises(UnauthorisedException):
+            self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
+
 class WibbleTests(unittest.TestCase):
     def setUp(self):
         from client.ServerProxy import ServerProxy
@@ -42,16 +54,22 @@
     def runTest(self):
         self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
 
-#def client_tests():
-#    try:
-#        print(jsonrpc_client.wibble('this should fail'))
-#    except BadRequestException:
-#        pass # test passed
-#    else:
-#       raise Exception('Test failed (calling unknown method)')
-#
-#    print 'All tests passed'
+class ExceptionTest(WibbleTests):
+    def runTest(self):
+        with self.assertRaises(Exception):
+            self.client.raiseexception()
 
+class BadRequestTest(WibbleTests):
+    def runTest(self):
+        from client.ServerProxy import BadRequestException
+        with self.assertRaises(BadRequestException):
+            self.client.FunctionDoesNotExist()
+
+class EchoTest(WibbleTests):
+    def runTest(self):
+        POUND = '\xc2\xa3'
+        self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND)
+        self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!')
 ##### client ^^^ #####
 
 finished = False
@@ -59,6 +77,8 @@
     if platform.python_version().startswith('3'):
         # no tests for python 3 because server not ported yet
         return unittest.TestSuite()
+
+    # create server
     def test_wrapper():
         server = make_server()
         while not finished:
@@ -66,12 +86,17 @@
     thread = Thread(target=test_wrapper)
     thread.start()
     time.sleep(0.1) # wait for server thread to start
+
+    # tests are as client
     suite = unittest.TestSuite()
+    suite.addTest(NotAuthTest())
     suite.addTest(IgnoreClassNameTest())
+    suite.addTest(ExceptionTest())
+    suite.addTest(BadRequestTest())
+    suite.addTest(EchoTest())
     return suite
 
 if __name__ == '__main__':
-    finished = False
     unittest.TextTestRunner(verbosity=2).run(suite())
     finished = True