From 665875ad532de766814d8afa6465dc64ef2a3f64 Mon Sep 17 00:00:00 2001 From: Carwyn Pelley Date: Fri, 16 Jun 2017 15:11:29 +0100 Subject: [PATCH] TEST: Extends #2569 to unpickle --- lib/iris/tests/integration/test_pickle.py | 50 +++++++++++++---------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/iris/tests/integration/test_pickle.py b/lib/iris/tests/integration/test_pickle.py index c508cb49c2e..f04caf49e86 100644 --- a/lib/iris/tests/integration/test_pickle.py +++ b/lib/iris/tests/integration/test_pickle.py @@ -31,35 +31,43 @@ from iris.fileformats.grib.message import GribMessage +class Common(object): + def pickle_cube(self, path): + # Ensure that data proxies are pickleable. + cube = iris.load(path)[0] + with self.temp_filename('.pkl') as filename: + with open(filename, 'wb') as f: + pickle.dump(cube, f) + with open(filename, 'r') as f: + ncube = pickle.load(f) + self.assertEqual(ncube, cube) + + @tests.skip_data @tests.skip_grib -class TestGribMessage(tests.IrisTest): - def test(self): - # Check that a GribMessage pickles without errors. - path = tests.get_data_path(('GRIB', 'fp_units', 'hours.grib2')) - messages = GribMessage.messages_from_filename(path) - message = next(messages) +class TestGribMessage(Common, tests.IrisTest): + def setUp(self): + self.path = tests.get_data_path(('GRIB', 'fp_units', 'hours.grib2')) + + def pickle_obj(self, obj): with self.temp_filename('.pkl') as filename: with open(filename, 'wb') as f: - pickle.dump(message, f) + pickle.dump(obj, f) + + def test(self): + # Check that a GribMessage pickles without errors. + messages = GribMessage.messages_from_filename(self.path) + obj = next(messages) + self.pickle_obj(obj) def test_data(self): # Check that GribMessage.data pickles without errors. - path = tests.get_data_path(('GRIB', 'fp_units', 'hours.grib2')) - messages = GribMessage.messages_from_filename(path) - message = next(messages) - with self.temp_filename('.pkl') as filename: - with open(filename, 'wb') as f: - pickle.dump(message.data, f) - + messages = GribMessage.messages_from_filename(self.path) + obj = next(messages).data + self.pickle_obj(obj) -class Common(object): - # Ensure that data proxies are pickleable. - def pickle_cube(self, path): - cube = iris.load(path)[0] - with self.temp_filename('.pkl') as filename: - with open(filename, 'wb') as f: - pickle.dump(cube, f) + def test_roundtrip(self): + self.pickle_cube(self.path) @tests.skip_data