jsonrpc-base

Pure python standard library JSONRPC data interface
git clone git://git.defalsify.org/python-jsonrpc-base.git
Log | Files | Refs | LICENSE

test_error.py (2181B)


      1 # standard imports
      2 import unittest
      3 import json
      4 
      5 # local imports
      6 from jsonrpc_std.error import (
      7         JSONRPCException,
      8         JSONRPCCustomException,
      9         JSONRPCErrors,
     10         JSONRPCParseError,
     11         )
     12 from jsonrpc_std.interface import (
     13         jsonrpc_error,
     14         DefaultErrorParser,
     15         )
     16 
     17 class WrongError(Exception):
     18     pass
     19 
     20 
     21 class RightError(JSONRPCCustomException):
     22     message = 'Right'
     23     code = -32001
     24 
     25 
     26 class TestError(unittest.TestCase):
     27 
     28     def test_base(self):
     29         e = JSONRPCException('foo')
     30         self.assertEqual(str(e), 'Unknown error: foo')
     31 
     32 
     33     def test_error_by_code(self):
     34         e = JSONRPCErrors.get(-32700)
     35         self.assertTrue(isinstance(e, JSONRPCParseError))
     36 
     37 
     38     def test_custom_error(self):
     39         with self.assertRaises(KeyError):
     40             e = JSONRPCErrors.get(-1000)
     41 
     42         with self.assertRaises(ValueError):
     43             JSONRPCErrors.add(-32000, WrongError)
     44 
     45         with self.assertRaises(ValueError):
     46             JSONRPCErrors.add(JSONRPCErrors.local_min - 1, RightError)
     47 
     48         with self.assertRaises(ValueError):
     49             JSONRPCErrors.add(JSONRPCErrors.local_max + 1, RightError)
     50 
     51         JSONRPCErrors.add(-32000, RightError)
     52         e_retrieved = JSONRPCErrors.get(-32000, 'foo')
     53         self.assertEqual(type(RightError(None)), type(e_retrieved))
     54         self.assertEqual(str(e_retrieved), 'Right error: foo')
     55 
     56 
     57     def test_error_interface(self):
     58         uu = 'foo'
     59         e = jsonrpc_error(uu, -32700)
     60         self.assertEqual(e['error']['code'], -32700)
     61         self.assertEqual(e['error']['message'], 'Parse error')
     62 
     63       
     64     def test_default_error_translate(self):
     65         uu = 'foo'
     66         p = DefaultErrorParser()
     67         e = jsonrpc_error(uu, -32700)
     68         o = p.translate(e)
     69         print('e {}'.format(o))
     70 
     71 
     72     def test_error_serialize(self):
     73         e = JSONRPCParseError('foo', request_id='bar')
     74         o = dict(e)
     75         self.assertEqual(o['jsonrpc'], '2.0')
     76         self.assertEqual(o['id'], 'bar')
     77         self.assertEqual(o['error']['code'], -32700)
     78         self.assertEqual(o['error']['message'], 'Parse error: foo')
     79 
     80 
     81 if __name__ == '__main__':
     82     unittest.main()