Fix protocols and add unittest

review12371002
This commit is contained in:
Cédric Krier committed 2014-06-07 23:54:44 +02:00
1 parent 66d9aeb29f
commit 174b2ed218
3 files changed
+82 -1

No files matched your search

+2 -1
View File
@@ -55,7 +55,8 @@ JSONDecoder.register('date',
JSONDecoder.register('time',
lambda dct: datetime.time(dct['hour'], dct['minute'], dct['second'],
dct['microsecond']))
JSONDecoder.register('buffer', buffer(base64.decodestring(dct['base64'])))
JSONDecoder.register('buffer', lambda dct:
buffer(base64.decodestring(dct['base64'])))
JSONDecoder.register('Decimal', lambda dct: Decimal(dct['decimal']))
+8
View File
@@ -118,6 +118,14 @@ def _end_dateTime(self, data):
xmlrpclib.Unmarshaller.dispatch["dateTime.iso8601"] = _end_dateTime
def _end_base64(self, data):
value = xmlrpclib.Binary()
value.decode(data)
self.append(buffer(value.data))
self._value = 0
xmlrpclib.Unmarshaller.dispatch['base64'] = _end_base64
class GenericXMLRPCRequestHandler:
def _dispatch(self, method, params):
+72
View File
@@ -0,0 +1,72 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import unittest
import json
import datetime
from decimal import Decimal
from trytond.protocols.jsonrpc import JSONEncoder, JSONDecoder
from trytond.protocols.xmlrpc import xmlrpclib
class JSONTestCase(unittest.TestCase):
'Test JSON'
def dumps_loads(self, value):
self.assertEqual(json.loads(
json.dumps(value, cls=JSONEncoder),
object_hook=JSONDecoder()), value)
def test_datetime(self):
'Test datetime'
self.dumps_loads(datetime.datetime.now())
def test_date(self):
'Test date'
self.dumps_loads(datetime.date.today())
def test_time(self):
'Test time'
self.dumps_loads(datetime.datetime.now().time())
def test_buffer(self):
'Test buffer'
self.dumps_loads(buffer('foo'))
def test_decimal(self):
'Test Decimal'
self.dumps_loads(Decimal('3.141592653589793'))
class XMLTestCase(unittest.TestCase):
'Test XML'
def dumps_loads(self, value):
s = xmlrpclib.dumps((value,))
result, _ = xmlrpclib.loads(s)
result, = result
self.assertEqual(value, result)
def test_decimal(self):
'Test Decimal'
self.dumps_loads(Decimal('3.141592653589793'))
def test_buffer(self):
'Test buffer'
self.dumps_loads(buffer('foo'))
def test_date(self):
'Test date'
self.dumps_loads(datetime.date.today())
def test_time(self):
'Test time'
self.dumps_loads(datetime.datetime.now().time())
def suite():
suite_ = unittest.TestSuite()
suite_.addTests(unittest.TestLoader().loadTestsFromTestCase(JSONTestCase))
suite_.addTests(unittest.TestLoader().loadTestsFromTestCase(XMLTestCase))
return suite_