From b9b34dcfffb1efc04d30b213b27261c0c3764ec4 2013-11-06 20:43:30 From: MinRK Date: 2013-11-06 20:43:30 Subject: [PATCH] test date time treatment in Session header and parent_header date time objects are automatically parsed. metadata and content are not, but at least they are automatically serialized. --- diff --git a/IPython/kernel/zmq/tests/test_session.py b/IPython/kernel/zmq/tests/test_session.py index 06b6d6f..36c8d40 100644 --- a/IPython/kernel/zmq/tests/test_session.py +++ b/IPython/kernel/zmq/tests/test_session.py @@ -13,6 +13,8 @@ import os import uuid +from datetime import datetime + import zmq from zmq.tests import BaseZMQTestCase @@ -20,6 +22,10 @@ from zmq.eventloop.zmqstream import ZMQStream from IPython.kernel.zmq import session as ss +from IPython.testing.decorators import skipif, module_not_available +from IPython.utils.py3compat import string_types +from IPython.utils import jsonutil + def _bad_packer(obj): raise TypeError("I don't work") @@ -155,24 +161,6 @@ class TestSession(SessionTestCase): t.wait(1) # this will raise - # def test_rekey(self): - # """rekeying dict around json str keys""" - # d = {'0': uuid.uuid4(), 0:uuid.uuid4()} - # self.assertRaises(KeyError, ss.rekey, d) - # - # d = {'0': uuid.uuid4(), 1:uuid.uuid4(), 'asdf':uuid.uuid4()} - # d2 = {0:d['0'],1:d[1],'asdf':d['asdf']} - # rd = ss.rekey(d) - # self.assertEqual(d2,rd) - # - # d = {'1.5':uuid.uuid4(),'1':uuid.uuid4()} - # d2 = {1.5:d['1.5'],1:d['1']} - # rd = ss.rekey(d) - # self.assertEqual(d2,rd) - # - # d = {'1.0':uuid.uuid4(),'1':uuid.uuid4()} - # self.assertRaises(KeyError, ss.rekey, d) - # def test_unique_msg_ids(self): """test that messages receive unique ids""" ids = set() @@ -267,5 +255,35 @@ class TestSession(SessionTestCase): def test_bad_roundtrip(self): with self.assertRaises(ValueError): - session= ss.Session(unpack=lambda b: 5) + session = ss.Session(unpack=lambda b: 5) + + def _datetime_test(self, session): + content = dict(t=datetime.now()) + metadata = dict(t=datetime.now()) + p = session.msg('msg') + msg = session.msg('msg', content=content, metadata=metadata, parent=p['header']) + smsg = session.serialize(msg) + msg2 = session.unserialize(session.feed_identities(smsg)[1]) + assert isinstance(msg2['header']['date'], datetime) + self.assertEqual(msg['header'], msg2['header']) + self.assertEqual(msg['parent_header'], msg2['parent_header']) + self.assertEqual(msg['parent_header'], msg2['parent_header']) + assert isinstance(msg['content']['t'], datetime) + assert isinstance(msg['metadata']['t'], datetime) + assert isinstance(msg2['content']['t'], string_types) + assert isinstance(msg2['metadata']['t'], string_types) + self.assertEqual(msg['content'], jsonutil.extract_dates(msg2['content'])) + self.assertEqual(msg['content'], jsonutil.extract_dates(msg2['content'])) + + def test_datetimes(self): + self._datetime_test(self.session) + + def test_datetimes_pickle(self): + session = ss.Session(packer='pickle') + self._datetime_test(session) + + @skipif(module_not_available('msgpack')) + def test_datetimes_msgpack(self): + session = ss.Session(packer='msgpack.packb', unpacker='msgpack.unpackb') + self._datetime_test(session)