def test_load_but_do_not_restore_class_hierarchy(self): a = XMLArchive('testhierarchy') o = a.loads(test_xml['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)
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 = XMLArchive('testhierarchy', config=c) s = a.dumps(o) self.assertEqual(test_xml['hierarchy'], s)
def test_special_numerical_values(self): d = [float('nan'), float('inf'), -float('inf')] xml = '<test><count>3</count><item_version>0</item_version><item>NaN</item><item>Infinity</item><item>-Infinity</item></test>' a = XMLArchive('test', pretty_print=False) s = a.dumps(d) self.assertEqual(xml, s) d2 = a.loads(xml) self.assertEqual(d[1:], d2[1:]) self.assertTrue(math.isnan(d[0])) self.assertTrue(math.isnan(d2[0]))
def test_load_simple(self): with self.assertRaises(XMLArchiveError): a = XMLArchive('blah') a.loads(test_xml['simple']) a1 = XMLArchive('measurement') # 'None' works for any root_tag, generally not recommended a2 = XMLArchive(None) d1 = a1.loads(test_xml['simple']) d2 = a2.loads(test_xml['simple']) self.assertEqual(d1, d2) self.assertEqual(d1, test_data['simple'])
def test_load_numpy(self): a = XMLArchive('measurement') d = a.loads(test_xml['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_)
def test_no_pretty_printing_with_config_object(self): f1 = self.filename('testarchive1.json') f1_ref = self.filename('testarchive1_reference.json') f = open(f1_ref, 'w') f.write(test_xml['no_pretty_printing']) f.close() c = {'archive_default_format': 'json', 'archive_pretty_print': False} a = XMLArchive('measurement', config=c) f = open(f1, 'w') a.dump(test_data['simple'], f) f.close() self.assertTrue(os.path.exists(f1)) self.assertTrue(filecmp.cmp(f1, f1_ref, shallow=False))
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 = XMLArchive('testhierarchy') a.serializer = s o = a.loads(test_xml['hierarchy']) self.assertEquals(o.c, 3) self.assertEquals(o.d, 4) self.assertEquals(o.object1.a, 30) self.assertEquals(o.object1.b, 40)
def test_no_pretty_printing_with_invalid_config_object(self): f1 = self.filename('testarchive1.json') f1_ref = self.filename('testarchive1_reference.json') f = open(f1_ref, 'w') f.write(test_xml['simple']) f.close() # Invalid value for option. Does not throw an error right now, just # does not do anything. c = {'archive_default_format': 'json', 'archive_pretty_print': 'muh'} a = XMLArchive('measurement', config=c) f = open(f1, 'w') a.dump(test_data['simple'], f) f.close() self.assertTrue(os.path.exists(f1)) self.assertTrue(filecmp.cmp(f1, f1_ref, shallow=False))
def test_roundtrip(self): a = XMLArchive('measurement') s = a.dumps(test_data['simple']) d = a.loads(s) self.assertEqual(test_data['simple'], d) a = XMLArchive('measurement') d = a.loads(test_xml['simple']) s = a.dumps(d) self.assertEqual(test_xml['simple'], s)
def test_dump_simple_to_file(self): name = self.filename('test_serialization.xml') a = XMLArchive('measurement') f = open(name, 'w') a.dump(test_data['simple'], f) f.close() f = open(name) s = f.read() f.close() self.assertEqual(test_xml['simple'], s) a = XMLArchive('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_xml['simple'].replace('\n','').replace(' ',''), s.replace(' ',''))
def test_load_invalid_list(self): a = XMLArchive('measurement') self.assertRaises(XMLArchiveError, a.loads, test_xml['invalid_list_1']) self.assertRaises(XMLArchiveError, a.loads, test_xml['invalid_list_2'])
def test_load_list(self): a = XMLArchive('measurement') d = a.loads(test_xml['list']) self.assertEqual(test_data['list'], d)
def test_dump_numpy(self): a = XMLArchive('measurement') s = a.dumps(test_data['numpy']) self.assertEqual(test_xml['numpy'], s)
def test_dump_list(self): a = XMLArchive('measurement', indent=8) s = a.dumps(test_data['list']) self.assertEqual(test_xml['list'], s)
def test_dump_simple_no_pretty_print(self): a = XMLArchive('measurement', pretty_print=False) s = a.dumps(test_data['simple']) self.assertEqual(test_xml['simple'].replace('\n','').replace(' ',''), s.replace(' ',''))
def test_dump_simple(self): a = XMLArchive('measurement') s = a.dumps(test_data['simple']) self.assertEqual(test_xml['simple'], s)