##// END OF EJS Templates
s/assertEquals/assertEqual/
s/assertEquals/assertEqual/

File last commit:

r7874:4a6836ce
r7874:4a6836ce
Show More
test_newserialized.py
116 lines | 3.9 KiB | text/x-python | PythonLexer
/ IPython / parallel / tests / test_newserialized.py
MinRK
update recently changed modules with Authors in docstring
r4018 """test serialization with newserialized
Authors:
* Min RK
"""
MinRK
prep newparallel for rebase...
r3539
MinRK
update API after sagedays29...
r3664 #-------------------------------------------------------------------------------
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
MinRK
don't special case for py3k+numpy...
r4571 import sys
MinRK
prep newparallel for rebase...
r3539 from unittest import TestCase
MinRK
rebase IPython.parallel after removal of IPython.kernel...
r3672 from IPython.testing.decorators import parametric
MinRK
some initial tests for newparallel
r3637 from IPython.utils import newserialized as ns
from IPython.utils.pickleutil import can, uncan, CannedObject, CannedFunction
MinRK
move IPython.zmq.parallel to IPython.parallel
r3666 from IPython.parallel.tests.clienttest import skip_without
MinRK
some initial tests for newparallel
r3637
MinRK
don't special case for py3k+numpy...
r4571 if sys.version_info[0] >= 3:
buffer = memoryview
MinRK
some initial tests for newparallel
r3637
class CanningTestCase(TestCase):
def test_canning(self):
d = dict(a=5,b=6)
cd = can(d)
MinRK
pyzmq-2.1.3 related testing adjustments
r3661 self.assertTrue(isinstance(cd, dict))
MinRK
some initial tests for newparallel
r3637
def test_canned_function(self):
f = lambda : 7
cf = can(f)
MinRK
pyzmq-2.1.3 related testing adjustments
r3661 self.assertTrue(isinstance(cf, CannedFunction))
MinRK
some initial tests for newparallel
r3637
@parametric
def test_can_roundtrip(cls):
objs = [
dict(),
set(),
list(),
['a',1,['a',1],u'e'],
]
return map(cls.run_roundtrip, objs)
@classmethod
MinRK
pyzmq-2.1.3 related testing adjustments
r3661 def run_roundtrip(self, obj):
MinRK
some initial tests for newparallel
r3637 o = uncan(can(obj))
MinRK
pyzmq-2.1.3 related testing adjustments
r3661 assert o == obj, "failed assertion: %r == %r"%(o,obj)
MinRK
some initial tests for newparallel
r3637
def test_serialized_interfaces(self):
us = {'a':10, 'b':range(10)}
s = ns.serialize(us)
uus = ns.unserialize(s)
MinRK
pyzmq-2.1.3 related testing adjustments
r3661 self.assertTrue(isinstance(s, ns.SerializeIt))
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(uus, us)
MinRK
some initial tests for newparallel
r3637
def test_pickle_serialized(self):
obj = {'a':1.45345, 'b':'asdfsdf', 'c':10000L}
original = ns.UnSerialized(obj)
originalSer = ns.SerializeIt(original)
firstData = originalSer.getData()
firstTD = originalSer.getTypeDescriptor()
firstMD = originalSer.getMetadata()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(firstTD, 'pickle')
self.assertEqual(firstMD, {})
MinRK
some initial tests for newparallel
r3637 unSerialized = ns.UnSerializeIt(originalSer)
secondObj = unSerialized.getObject()
for k, v in secondObj.iteritems():
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(obj[k], v)
MinRK
some initial tests for newparallel
r3637 secondSer = ns.SerializeIt(ns.UnSerialized(secondObj))
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(firstData, secondSer.getData())
self.assertEqual(firstTD, secondSer.getTypeDescriptor() )
self.assertEqual(firstMD, secondSer.getMetadata())
MinRK
some initial tests for newparallel
r3637
@skip_without('numpy')
def test_ndarray_serialized(self):
import numpy
a = numpy.linspace(0.0, 1.0, 1000)
unSer1 = ns.UnSerialized(a)
ser1 = ns.SerializeIt(unSer1)
td = ser1.getTypeDescriptor()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(td, 'ndarray')
MinRK
some initial tests for newparallel
r3637 md = ser1.getMetadata()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(md['shape'], a.shape)
self.assertEqual(md['dtype'], a.dtype)
MinRK
some initial tests for newparallel
r3637 buff = ser1.getData()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(buff, buffer(a))
MinRK
some initial tests for newparallel
r3637 s = ns.Serialized(buff, td, md)
final = ns.unserialize(s)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(buffer(a), buffer(final))
MinRK
pyzmq-2.1.3 related testing adjustments
r3661 self.assertTrue((a==final).all())
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(a.dtype, final.dtype)
self.assertEqual(a.shape, final.shape)
MinRK
some initial tests for newparallel
r3637 # test non-copying:
a[2] = 1e9
MinRK
pyzmq-2.1.3 related testing adjustments
r3661 self.assertTrue((a==final).all())
MinRK
update API after sagedays29...
r3664
def test_uncan_function_globals(self):
"""test that uncanning a module function restores it into its module"""
from re import search
cf = can(search)
csearch = uncan(cf)
self.assertEqual(csearch.__module__, search.__module__)
self.assertNotEqual(csearch('asd', 'asdf'), None)
csearch = uncan(cf, dict(a=5))
self.assertEqual(csearch.__module__, search.__module__)
self.assertNotEqual(csearch('asd', 'asdf'), None)
MinRK
some initial tests for newparallel
r3637