Ejemplo n.º 1
0
 def test_load_but_do_not_restore_class_hierarchy(self):
     a = JsonArchive('testhierarchy')
     o = a.loads(test_json['hierarchy'])
     self.assertEquals(o['parameters']['c'], 3)
     self.assertEquals(o['parameters']['d'], 4)
     self.assertEquals(o['parameters']['object1']['parameters']['a'], 30)
     self.assertEquals(o['parameters']['object1']['parameters']['b'], 40)
Ejemplo n.º 2
0
    def test_serialize_and_dump_class_hierarchy(self):
        o = Class2(3,4)
        o.object1.a = 30
        o.object1.b = 40

        c = {'serializer_getstate': '__getstate__'}
        a = JsonArchive('testhierarchy', config=c)
        s = a.dumps(o)
        self.assertEqual(test_json['hierarchy'], s)
Ejemplo n.º 3
0
    def test_special_numerical_values(self):
        d = [float('nan'), float('inf'), -float('inf')]
        json = '{"test":[NaN,Infinity,-Infinity]}'
        
        a = JsonArchive('test', pretty_print=False)
        s = a.dumps(d)
        self.assertEqual(json, s)

        d2 = a.loads(json)
        self.assertEqual(d[1:], d2[1:])
        self.assertTrue(math.isnan(d[0]))
        self.assertTrue(math.isnan(d2[0]))
Ejemplo n.º 4
0
 def test_load_numpy(self):
     a = JsonArchive('measurement')
     d = a.loads(test_json['numpy'])
     d_ = copy.deepcopy(test_data['numpy'])
     self.assertTrue((d_['results']['numpy_array'] == 
                       d['results']['numpy_array']).all())
     self.assertTrue((d_['results']['numpy_array_simple'] == 
                       d['results']['numpy_array_simple']).all())
     del d['results']['numpy_array']
     del d_['results']['numpy_array']
     del d['results']['numpy_array_simple']
     del d_['results']['numpy_array_simple']
     self.assertEqual(d, d_)
Ejemplo n.º 5
0
    def test_load_simple(self):
        with self.assertRaises(JsonArchiveError):
            a = JsonArchive('blah')
            a.loads(test_json['simple'])

        a1 = JsonArchive('measurement')
        # Use 'None' to read any JSON file, regardless of top level "tag";
        # generally it's not recommended to use this.
        a2 = JsonArchive(None)
        d1 = a1.loads(test_json['simple'])
        d2 = a2.loads(test_json['simple'])

        self.assertEqual(d1, d2['measurement'])
        self.assertEqual(d1, test_data['simple'])
Ejemplo n.º 6
0
 def test_load_and_restore_class_hierarchy(self):
     # At least for the time being, we need to set the serializer by hand;
     # full object restore is not supported via the config dictionary
     c = {
         'serializer_getstate': '__getstate__',
         'serializer_setstate': '__setstate__'
     }
     s = Serializer(restore_objects=True, config=c)
     a = JsonArchive('testhierarchy')
     a.serializer = s
     o = a.loads(test_json['hierarchy'])
     self.assertEquals(o.c, 3)
     self.assertEquals(o.d, 4)
     self.assertEquals(o.object1.a, 30)
     self.assertEquals(o.object1.b, 40)
Ejemplo n.º 7
0
    def test_dump_simple_to_file(self):
        name = self.filename('test_serialization.json')
        
        a = JsonArchive('measurement')
        f = open(name, 'w')
        a.dump(test_data['simple'], f)
        f.close()
        f = open(name)
        s = f.read()
        f.close()
        self.assertEqual(test_json['simple'], s)

        a = JsonArchive('measurement', pretty_print=False)
        f = open(name, 'w')
        a.dump(test_data['simple'], f)
        f.close()
        f = open(name)
        s = f.read()
        f.close()
        self.assertEqual(test_json['simple'].replace('\n','').replace(' ',''), s.replace(' ',''))
Ejemplo n.º 8
0
    def test_roundtrip(self):
        a = JsonArchive('measurement')
        s = a.dumps(test_data['simple'])
        d = a.loads(s)
        self.assertEqual(test_data['simple'], d)

        a = JsonArchive('measurement')
        d = a.loads(test_json['simple'])
        s = a.dumps(d)
        self.assertEqual(test_json['simple'], s)

        # now without root_tag
        a = JsonArchive(None)
        s = a.dumps(test_data['simple'])
        d = a.loads(s)
        self.assertEqual(test_data['simple'], d)

        a = JsonArchive(None)
        d = a.loads(test_json['simple'])
        s = a.dumps(d)
        self.assertEqual(test_json['simple'], s)
Ejemplo n.º 9
0
 def test_load_list(self):
     a = JsonArchive('measurement')
     d = a.loads(test_json['list'])
     self.assertEqual(test_data['list'], d)
Ejemplo n.º 10
0
 def test_load_invalid(self):
     a = JsonArchive('measurement')
     with self.assertRaises(JsonArchiveError):
         a.loads(test_json['invalid_1']);
Ejemplo n.º 11
0
 def test_dump_numpy(self):
     a = JsonArchive('measurement')
     s = a.dumps(test_data['numpy'])
     self.assertEqual(test_json['numpy'], s)
Ejemplo n.º 12
0
 def test_dump_list(self):
     a = JsonArchive('measurement', indent=8)
     s = a.dumps(test_data['list'])
     self.assertEqual(test_json['list'], s)
Ejemplo n.º 13
0
 def test_dump_simple_no_pretty_print(self):
     a = JsonArchive('measurement', pretty_print=False)
     s = a.dumps(test_data['simple'])
     self.assertEqual(test_json['simple'].replace('\n','').replace(' ',''), s.replace(' ',''))
Ejemplo n.º 14
0
 def test_dump_simple(self):
     a = JsonArchive('measurement')
     s = a.dumps(test_data['simple'])
     self.assertEqual(test_json['simple'], s)