def test_BaseSerializer_dump_object(self): serializer = base.BaseSerializer(dumps=json.dumps, loads=json.loads, dump=json.dump, load=json.load) self.assertEqual( serializer.dump_object( datetime.datetime(2014, 9, 12, 8, 33, 12, 34)), { '__type__': 'datetime', '_': '2014-09-12T08:33:12' }) self.assertEqual(serializer.dump_object(datetime.date(2014, 9, 12)), { '__type__': 'date', '_': '2014-09-12' }) self.assertEqual(serializer.dump_object(decimal.Decimal('3.1415')), { '__type__': 'Decimal', '_': '3.1415' }) self.assertEqual( serializer.dump_object( uuid.UUID('00000000-0000-4000-8000-000000000000')), { '__type__': 'UUID', '_': '00000000-0000-4000-8000-000000000000' }, )
def test_BaseSerializer_dump(self): serializer = base.BaseSerializer(dumps=json.dumps, loads=json.loads, dump=json.dump, load=json.load) self.assertJsonEquals( serializer.dumps(datetime.datetime(2014, 9, 12, 8, 33, 12, 34)), {"__type__": "datetime", "_": "2014-09-12T08:33:12Z"} ) self.assertJsonEquals( serializer.dumps(decimal.Decimal('NaN')), {"__type__": "Decimal", "_": "NaN"} ) self.assertJsonEquals( serializer.dumps(set([datetime.datetime(2014, 9, 12, 8, 33, 12, 34)])), {"__type__": "set", "_": [{"__type__": "datetime", "_": "2014-09-12T08:33:12Z"}]} )
def test_BaseSerializer_loads(self): serializer = base.BaseSerializer(dumps=json.dumps, loads=json.loads, dump=json.dump, load=json.load) normal_datetime = '{"__type__": "datetime", "_": "2014-09-12T08:33:12Z"}' normal_date = '{"__type__": "date", "_": "2014-09-12"}' number = '{"__type__": "Decimal", "_": "3.1415"}' nan = '{"__type__": "Decimal", "_": "NaN"}' some_set = '{"__type__": "set", "_": ["this", "a", "as", "set", "is"]}' self.assertEqual(serializer.loads(normal_datetime), datetime.datetime(2014, 9, 12, 8, 33, 12)) self.assertEqual(serializer.loads(normal_date), datetime.date(2014, 9, 12)) self.assertEqual(serializer.loads(number), decimal.Decimal('3.1415')) self.assertEqual(serializer.loads(nan).number_class(), 'NaN') self.assertEqual(serializer.loads(some_set), set([u'this', u'a', u'as', u'set', u'is'])) self.assertEqual(serializer.loads('{"__type__": "set", "_": [' '{"__type__": "datetime", "_": "2014-09-12T08:33:12Z"}, ' '{"__type__": "datetime", "_": "2015-09-12T08:33:12Z"}' ']}'), set([datetime.datetime(2014, 9, 12, 8, 33, 12), datetime.datetime(2015, 9, 12, 8, 33, 12)]))
def test_BaseSerializer_load_object(self): serializer = base.BaseSerializer(dumps=json.dumps, loads=json.loads, dump=json.dump, load=json.load) self.assertEqual(serializer.load_object( {'__type__': 'datetime', '_': '2014-09-12T08:33:12Z'}), datetime.datetime(2014, 9, 12, 8, 33, 12)) self.assertEqual(serializer.load_object( {'__type__': 'date', '_': '2014-09-12'}), datetime.date(2014, 9, 12)) self.assertEqual(serializer.load_object( {'__type__': 'Decimal', '_': '3.1415'}), decimal.Decimal('3.1415')) self.assertEqual(serializer.load_object( {'__type__': 'Decimal', '_': 'NaN'}).number_class(), 'NaN') self.assertEqual( serializer.load_object({'__type__': 'set', '_': ['this', 'a', 'as', 'set', 'is']}), set(['this', 'a', 'as', 'set', 'is']), ) self.assertEqual( serializer.load_object({'__type__': 'UUID', '_': '00000000-0000-4000-8000-000000000000'}), uuid.UUID('00000000-0000-4000-8000-000000000000'), )
def setUp(self): self.json_serializer = base.BaseSerializer(dumps=json.dumps, loads=json.loads, dump=json.dump, load=json.load)