def test_save_archive_to_different_format(self): f1 = self.filename('testarchive1.xml') f2 = self.filename('testarchive1.json') f2_ref = self.filename('testarchive1_reference.json') f = open(f1, 'w') f.write(test_xml['simple']) f.close() f = open(f2_ref, 'w') f.write(test_json['simple']) f.close() # Note: This should be mainly useful for format conversions. a = Archive(self.filename('testarchive1'), 'measurement') a.save(a.load(), format='json') self.assertTrue(os.path.exists(f1)) self.assertTrue(os.path.exists(f2)) with self.assertRaises(ArchiveError): a = Archive(self.filename('testarchive1'), 'measurement') os.remove(f1) a = Archive(self.filename('testarchive1'), 'measurement') o = a.load() self.assertEqual(o, test_data['simple']) self.assertTrue(filecmp.cmp(f2, f2_ref, shallow=False))
def test_save_archive_to_different_format_with_invalid_format_fails(self): f1 = self.filename('testarchive1.xml') f = open(f1, 'w') f.write(test_xml['simple']) f.close() a = Archive(self.filename('testarchive1'), 'measurement') with self.assertRaises(ArchiveError): a.save(a.load(), format='invalid')
def test_use_archive_with_config_object_1(self): f1 = self.filename('testarchive1.xml') f1_ref = self.filename('testarchive1_reference.xml') f = open(f1_ref, 'w') f.write(test_xml['list']) f.close() c = {'archive_default_format': 'xml'} a = Archive(self.filename('testarchive1'), 'measurement', indent=8, config=c) a.save(test_data['list']) self.assertTrue(os.path.exists(f1)) self.assertTrue(filecmp.cmp(f1, f1_ref, shallow=False))
def test_use_archive_with_config_object_3(self): f1 = self.filename('testarchive1.json') f1_ref = self.filename('testarchive1_reference.json') f = open(f1_ref, 'w') f.write(test_json['no_pretty_printing']) f.close() # arguments to constructor overwrite config options c = {'archive_default_format': 'xml', 'archive_pretty_print': True} a = Archive(self.filename('testarchive1'), 'measurement', default_format='json', pretty_print=False, indent=8, config=c) a.save(test_data['simple']) self.assertTrue(os.path.exists(f1)) self.assertTrue(filecmp.cmp(f1, f1_ref, shallow=False))
def test_archive_cannot_be_constructed_when_multiple_archive_files_are_present(self): f1 = self.filename('testarchive.xml') f = open(f1, 'w') f.write(test_xml['simple']) f.close() f2 = self.filename('testarchive.json') f = open(f2, 'w') f.write(test_json['simple']) f.close() with self.assertRaises(ArchiveError): Archive(self.filename('testarchive'), 'measurement')
def test_default_format_is_overridden_by_exisiting_archive_file_format(self): f = open(self.filename('testarchive.json'), 'w') f.write(test_json['simple']) f.close() a = Archive(self.filename('testarchive'), 'measurement', default_format='xml') a.save(test_data['list']) self.assertTrue(os.path.exists(self.filename('testarchive.json'))) self.assertFalse(os.path.exists(self.filename('testarchive.xml'))) a = Archive(self.filename('testarchive'), 'measurement', default_format='xml') o = a.load() self.assertEqual(o, test_data['list'])
def test_save_to_xml_and_json(self): f1 = self.filename('testarchive1.xml') f1_ref = self.filename('testarchive1_reference.xml') f2 = self.filename('testarchive2.json') f2_ref = self.filename('testarchive2_reference.json') f = open(f1_ref, 'w') f.write(test_xml['list']) f.close() f = open(f2_ref, 'w') f.write(test_json['list']) f.close() a = Archive(self.filename('testarchive1'), 'measurement', indent=8, default_format='xml') a.save(test_data['list']) a = Archive(self.filename('testarchive2'), 'measurement', indent=8, default_format='json') a.save(test_data['list']) self.assertTrue(os.path.exists(f1)) self.assertTrue(os.path.exists(f2)) self.assertTrue(filecmp.cmp(f1, f1_ref, shallow=False)) self.assertTrue(filecmp.cmp(f2, f2_ref, shallow=False))
def test_load_archive_with_full_filename(self): f1 = self.filename('testarchive1.xml') f2 = self.filename('testarchive1.json') f = open(f1, 'w') f.write(test_xml['simple']) f.close() f = open(f2, 'w') f.write(test_json['list']) f.close() a = Archive(self.filename('testarchive1.xml'), 'measurement') o1 = a.load() a = Archive(self.filename('testarchive1.json'), 'measurement') o2 = a.load() self.assertEqual(o1, test_data['simple']) self.assertEqual(o2, test_data['list'])
def test_load_from_xml_and_json(self): f1 = self.filename('testarchive1.xml') f2 = self.filename('testarchive2.json') f = open(f1, 'w') f.write(test_xml['simple']) f.close() f = open(f2, 'w') f.write(test_json['simple']) f.close() a = Archive(self.filename('testarchive1'), 'measurement') o1 = a.load() a = Archive(self.filename('testarchive2'), 'measurement') o2 = a.load() self.assertEqual(o1, test_data['simple']) self.assertEqual(o2, test_data['simple'])
def test_archive_cannot_be_constructed_with_invalid_format(self): with self.assertRaises(ArchiveError): Archive(self.filename('testarchive'), 'test', default_format='txt') with self.assertRaises(ArchiveError): Archive(self.filename('testarchive'), 'test', default_format='.xml')
def test_default_format_is_used_if_no_archive_file_exists(self): a = Archive(self.filename('testarchive'), 'measurement', default_format='xml') a.save(test_data['simple']) self.assertTrue(os.path.exists(self.filename('testarchive.xml')))