def test_overridable_methods(self): formatter = Formatter() data = DataSet1D() with self.assertRaises(NotImplementedError): formatter.write(data, data.io, data.location) with self.assertRaises(NotImplementedError): formatter.read_one_file(data, 'a file!', set()) with self.assertRaises(NotImplementedError): formatter.write_metadata(data, data.io, data.location) with self.assertRaises(NotImplementedError): formatter.read_metadata(data)
def test_overridable_methods(self): formatter = Formatter() loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'test_overridable_methods'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) data = DataSet1D(name="test_overridable", location=loc_provider) with self.assertRaises(NotImplementedError): formatter.write(data, data.io, data.location) with self.assertRaises(NotImplementedError): formatter.read_one_file(data, 'a file!', set()) with self.assertRaises(NotImplementedError): formatter.write_metadata(data, data.io, data.location) with self.assertRaises(NotImplementedError): formatter.read_metadata(data)
def test_group_arrays(self): formatter = Formatter() data = DataSetCombined() groups = formatter.group_arrays(data.arrays) self.assertEqual(len(groups), 2, groups) groups.sort(key=lambda grp: len(grp.set_arrays)) g1d, g2d = groups self.assertEqual(g1d.shape, (2, )) self.assertEqual(g1d.set_arrays, (data.x_set, )) self.assertEqual(g1d.data, (data.y1, data.y2)) self.assertEqual(g1d.name, 'x_set') self.assertEqual(g2d.shape, (2, 3)) self.assertEqual(g2d.set_arrays, (data.x_set, data.y_set)) self.assertEqual(g2d.data, (data.z1, data.z2)) self.assertEqual(g2d.name, 'x_set_y_set')
def test_no_files(self): formatter = Formatter() data = DataSet1D(name="test_no_file", location=self.locations[0]) with self.assertRaises(IOError): formatter.read(data)
def test_match_save_range(self): formatter = Formatter() data = DataSet1D(name="test_match_save_range") group = formatter.group_arrays(data.arrays)[0] # no matter what else, if nothing is listed as modified # then save_range is None data.x_set.modified_range = data.y.modified_range = None for lsi_x in [None, 0, 3]: data.x_set.last_saved_index = lsi_x for lsi_y in [None, 1, 4]: data.y.last_saved_index = lsi_y for fe in [True, False]: save_range = formatter.match_save_range(group, file_exists=fe) self.assertEqual(save_range, None) # consistent last_saved_index: if it's None or within the # modified range, or if file does not exist, we need to overwrite # otherwise start just after last_saved_index for lsi, start in [(None, 0), (0, 1), (1, 2), (2, 3), (3, 0), (4, 0)]: data.x_set.last_saved_index = data.y.last_saved_index = lsi # inconsistent modified_range: if only_complete is False, expands # to greatest extent so these situations are identical # if only_complete is True, only gets to the last common point for xmr, ymr, last_common in ([(4, 4), (3, 3), 3], [(3, 4), None, None], [None, (3, 4), None]): data.x_set.modified_range = xmr data.y.modified_range = ymr save_range = formatter.match_save_range(group, file_exists=False, only_complete=False) self.assertEqual(save_range, (0, 4)) save_range = formatter.match_save_range(group, file_exists=True, only_complete=False) self.assertEqual(save_range, (start, 4)) save_all = formatter.match_save_range(group, file_exists=False) save_inc = formatter.match_save_range(group, file_exists=True) if last_common: # if last_saved_index is greater than we would otherwise # save, we still go up to last_saved_index (wouldn't want # this write to delete data!) last_save = max(last_common, lsi) if lsi else last_common self.assertEqual(save_all, (0, last_save), (lsi, xmr, ymr)) self.assertEqual(save_inc, (start, last_save), (lsi, xmr, ymr)) else: if lsi is None: self.assertIsNone(save_all) else: self.assertEqual(save_all, (0, lsi)) self.assertIsNone(save_inc) # inconsistent last_saved_index: need to overwrite if there are any # modifications data.x_set.last_saved_index = 1 data.y.last_saved_index = 2 data.x_set.modified_range = data.y.modified_range = (3, 4) save_range = formatter.match_save_range(group, file_exists=True) self.assertEqual(save_range, (0, 4))