def test_read_write_saved_tables_array_work_ok_for_normal_case(self): table = dt.DiceTable() table.add_die(1, dt.Die(3)) obj1 = create_saved_dice_table(table) table.add_die(2, dt.Die(5)) obj2 = create_saved_dice_table(table) save_data = np.array([obj1, obj2]) fh.write_saved_tables_array(save_data) msg, new_save_data = fh.read_saved_tables_array() self.assertEqual(msg, 'ok') self.assertArrayEqual(save_data, new_save_data)
def test_read_saved_tables_array_returns_error_and_empty_if_corrupted_file( self): table = dt.DiceTable() table.add_die(1, dt.Die(3)) obj1 = create_saved_dice_table(table) table.add_die(2, dt.Die(5)) obj2 = create_saved_dice_table(table) save_data_array = np.array([obj1, obj2]) fh.write_saved_tables_array(save_data_array) # for differences between python2 and 3 try: with open('save_data.npy', 'r') as f: to_write = f.read()[:-1] except UnicodeDecodeError: with open('save_data.npy', 'r', errors='ignore') as f: to_write = f.read()[:-1] with open('save_data.npy', 'w') as f: f.write(to_write) msg, new_data = fh.read_saved_tables_array() self.assertEqual(msg, 'error: file corrupted') self.assertArrayEqual(new_data, np.array([], dtype=object))
def test_read_saved_tables_array_returns_ok_and_empty_if_hist_empty_and_correct_type( self): fh.write_saved_tables_array(np.array([], dtype=object)) msg, save_data = fh.read_saved_tables_array() self.assertEqual(msg, 'ok: no saved data') self.assertArrayEqual(save_data, np.array([], dtype=object))
def test_read_saved_tables_array_returns_error_and_empty_if_check_hist_has_error( self): fh.write_saved_tables_array(np.array([1, 2, 3])) msg, save_data = fh.read_saved_tables_array() self.assertEqual(msg, 'error: wrong array type') self.assertArrayEqual(save_data, np.array([], dtype=object))
def write_to_file(self): """overwrites old save to 'save_data.npy' """ fh.write_saved_tables_array(self._saved_tables)