예제 #1
0
 def test_simple_type(self):
     name = Name(['Hans', 'August'], 'Bronner')
     format = self.format
     serializer = Serializer_(format)
     deserializer = Deserializer_(format)
     buf = create_buffer(deserializer.mode)
     serializer.dump(name, buf)
     buf.seek(0)
     name2 = deserializer.load(buf)
     self.assertEqual(name, name2)
예제 #2
0
 def test_nested_types(self):
     name = Name(['Hans', 'August'], 'Bronner')
     addr = Address('Klubweg 35', 29338, 'Posemuckel')
     id = uuid1()
     hans = Person(id, name, addr, Mass('78.2 kg'))
     format = self.format
     serializer = Serializer_(format)
     deserializer = Deserializer_(format)
     buf = create_buffer(deserializer.mode)
     serializer.dump(hans, buf)
     buf.seek(0)
     hans2 = deserializer.load(buf)
     self.assertEqual(hans, hans2)
예제 #3
0
 def test_std_types(self):
     id = uuid1()
     dt = datetime(2014, 1, 2, 22, 17, 47, tzinfo=cest)
     obj = {
         'id': id,
         'dt': dt,
         'bigint': 2**73,
         'dec': Decimal('-12.34567890'),
         'tuple': ('astring', 4.56, 22908 * 10**14, dt),
         'embedded_doc': {
             'i32': -5,
             'i64': 2**45 + 3,
             't': True,
             'f': False,
             'bin': '\x00\x03',
             'None': None,
             'list': [1, 2, dt]
         }
     }
     format = self.format
     serializer = Serializer_(format)
     deserializer = Deserializer_(format)
     buf = create_buffer(deserializer.mode)
     serializer.dump(obj, buf)
     buf.seek(0)
     robj = deserializer.load(buf)
     self.assertEqual(len(obj), len(robj))
     self.assertEqual(obj['id'], robj['id'])
     self.assertEqual(obj['dt'], robj['dt'])
     self.assertEqual(obj['bigint'], robj['bigint'])
     self.assertEqual(obj['dec'], robj['dec'])
     for o, r in zip_longest(obj['tuple'], robj['tuple']):
         # when using 'json', float is reconstructed as Decimal
         if isinstance(o, float) and isinstance(r, Decimal):
             o = Decimal(str(o))
         self.assertEqual(o, r)
     emb, remb = obj['embedded_doc'], robj['embedded_doc']
     for key in emb:
         self.assertEqual(emb[key], remb[key])
예제 #4
0
 def test_nested_types(self):
     name = Name(['Hans', 'August'], 'Bronner')
     addr = Address('Klubweg 35', 29338, 'Posemuckel')
     id = uuid1()
     hans = Person(id, name, addr, Mass('78.2 kg'))
     format = self.format
     serializer = Serializer_(format)
     buf = create_buffer(serializer.mode)
     serializer.dump(hans, buf)
     buf.seek(0)
     decoder = get_utility(Decoder, format)
     py_repr = decoder.decode(buf)
     self.assertTrue('__class__' in py_repr)
     fullClsName = py_repr['__class__']
     self.assertEqual(fullClsName, __name__ + '.Person')
     self.assertTrue('__state__' in py_repr)
     state = py_repr.get('__state__')
     # tuples get reconstructed as lists, so we can't compare directly
     self.assertEqual((state[0], state[1], tuple(state[2])),
                      hans.__getstate__()[:3])
     self.assertEqual(state[3], {
         u'__class__': u'quantity.r',
         u'__clsargs__': [u'78.2 kg']
     })