Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEST: Extends #2569 to unpickle #2608

Merged
merged 5 commits into from
Oct 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 59 additions & 33 deletions lib/iris/tests/integration/test_pickle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2014 - 2017, Met Office
# (C) British Crown Copyright 2014 - 2019, Met Office
#
# This file is part of Iris.
#
Expand All @@ -23,6 +23,7 @@
# importing anything else.
import iris.tests as tests

import unittest
import six.moves.cPickle as pickle

import iris
Expand All @@ -31,57 +32,82 @@
from iris_grib.message import GribMessage


@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 Common(object):
def pickle_cube(self, protocol):
# Ensure that data proxies are pickleable.
cube = iris.load(self.path)[0]
with self.temp_filename('.pkl') as filename:
with open(filename, 'wb') as f:
pickle.dump(message, f)
pickle.dump(cube, f, protocol)
with open(filename, 'rb') as f:
ncube = pickle.load(f)
self.assertEqual(ncube, cube)

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)
def test_protocol_0(self):
self.pickle_cube(0)

def test_protocol_1(self):
self.pickle_cube(1)

class Common(object):
# Ensure that data proxies are pickleable.
def pickle_cube(self, path):
cube = iris.load(path)[0]
def test_protocol_2(self):
self.pickle_cube(2)


@tests.skip_data
@tests.skip_grib
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(cube, f)
pickle.dump(obj, f)

# These probably "ought" to work, but currently fail.
# see https://github.com/SciTools/iris/pull/2608
@unittest.expectedFailure
def test_protocol_0(self):
super(TestGribMessage, self).test_protocol_0()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpelley Now we're exclusively Python3, isn't this now simply super().test_protocol_0()?

If true, also applies through out. Fix this and I'll merge 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpelley This'll all be resolved by #3457, so let's bank some progress 👍


@unittest.expectedFailure
def test_protocol_1(self):
super(TestGribMessage, self).test_protocol_1()

@unittest.expectedFailure
def test_protocol_2(self):
super(TestGribMessage, self).test_protocol_2()

def test(self):
# Check that a GribMessage pickles without errors.
messages = GribMessage.messages_from_filename(self.path)
obj = next(messages)
self.pickle_obj(obj)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpelley Is the test simply that there are no exceptions raised by pickle.dump() within pickle_obj() ?

There are no explicit asserts associated with this test nor test_data ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, these are the original two tests from this module which I refactored only to perform a common pickle dump (self.pickle_obj). I have added test_roundtrip which performs a dump and load and performs an assert.

I do not know enough about grib to understand whether test and test_data actually test something that my new test_roundtrip does not (i.e. redundant or not). Can you confirm whether these two tests still serve a purpose?


def test_data(self):
# Check that GribMessage.data pickles without errors.
messages = GribMessage.messages_from_filename(self.path)
obj = next(messages).data
self.pickle_obj(obj)


@tests.skip_data
class test_netcdf(Common, tests.IrisTest):
def test(self):
path = tests.get_data_path(('NetCDF', 'global', 'xyt',
'SMALL_hires_wind_u_for_ipcc4.nc'))
self.pickle_cube(path)
def setUp(self):
self.path = tests.get_data_path(('NetCDF', 'global', 'xyt',
'SMALL_hires_wind_u_for_ipcc4.nc'))


@tests.skip_data
class test_pp(Common, tests.IrisTest):
def test(self):
path = tests.get_data_path(('PP', 'aPPglob1', 'global.pp'))
self.pickle_cube(path)
def setUp(self):
self.path = tests.get_data_path(('PP', 'aPPglob1', 'global.pp'))


@tests.skip_data
class test_ff(Common, tests.IrisTest):
def test(self):
path = tests.get_data_path(('FF', 'n48_multi_field'))
self.pickle_cube(path)
def setUp(self):
self.path = tests.get_data_path(('FF', 'n48_multi_field'))


if __name__ == '__main__':
Expand Down