-
Notifications
You must be signed in to change notification settings - Fork 300
Ensure cubelists only contain cubes #3238
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
Changes from 12 commits
4ab7cfa
0b293ec
8a74df9
2622254
39d5f4d
4187162
569f35e
5b13dcc
8f272fb
f7134c1
bc7eb7a
d961a68
a84823e
7fe5d0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| * The `append`, `extend` and `insert` methods of :class:`iris.cube.CubeList` | ||
| now perform a check to ensure that only :class:`iris.cube.Cube` instances are | ||
| added. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # (C) British Crown Copyright 2014 - 2018, Met Office | ||
| # (C) British Crown Copyright 2014 - 2019, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
|
|
@@ -24,6 +24,8 @@ | |
| import iris.tests as tests | ||
| import iris.tests.stock | ||
|
|
||
| import copy | ||
|
|
||
| from cf_units import Unit | ||
| import numpy as np | ||
|
|
||
|
|
@@ -34,6 +36,26 @@ | |
| from iris.fileformats.pp import STASH | ||
| from iris.tests import mock | ||
|
|
||
| NOT_CUBE_MSG = "bject of type '{}'" | ||
|
|
||
|
|
||
| class Test_append(tests.IrisTest): | ||
| def setUp(self): | ||
| self.cubelist = iris.cube.CubeList() | ||
| self.cube1 = iris.cube.Cube(1, long_name='foo') | ||
| self.cube2 = iris.cube.Cube(1, long_name='bar') | ||
|
|
||
| def test_pass(self): | ||
| self.cubelist.append(self.cube1) | ||
| self.assertEqual(self.cubelist[-1], self.cube1) | ||
| self.cubelist.append(self.cube2) | ||
| self.assertEqual(self.cubelist[-1], self.cube2) | ||
|
|
||
| def test_fail(self): | ||
| with self.assertRaisesRegexp(TypeError, | ||
| NOT_CUBE_MSG.format('NoneType')): | ||
| self.cubelist.append(None) | ||
|
|
||
|
|
||
| class Test_concatenate_cube(tests.IrisTest): | ||
| def setUp(self): | ||
|
|
@@ -64,6 +86,30 @@ def test_empty(self): | |
| CubeList([]).concatenate_cube() | ||
|
|
||
|
|
||
| class Test_extend(tests.IrisTest): | ||
| def setUp(self): | ||
| self.cube1 = iris.cube.Cube(1, long_name='foo') | ||
| self.cube2 = iris.cube.Cube(1, long_name='bar') | ||
| self.cubelist1 = iris.cube.CubeList([self.cube1]) | ||
| self.cubelist2 = iris.cube.CubeList([self.cube2]) | ||
|
|
||
| def test_pass(self): | ||
| cubelist = copy.copy(self.cubelist1) | ||
| cubelist.extend(self.cubelist2) | ||
| self.assertEqual(cubelist, self.cubelist1 + self.cubelist2) | ||
| cubelist.extend([self.cube2]) | ||
| self.assertEqual(cubelist[-1], self.cube2) | ||
|
|
||
| def test_fail(self): | ||
| with self.assertRaisesRegexp(TypeError, 'Cube is not iterable'): | ||
| self.cubelist1.extend(self.cube1) | ||
| msg = "'NoneType' object is not iterable" | ||
| with self.assertRaisesRegexp(TypeError, msg): | ||
| self.cubelist1.extend(None) | ||
| with self.assertRaisesRegexp(ValueError, NOT_CUBE_MSG.format('int')): | ||
| self.cubelist1.extend(range(3)) | ||
|
|
||
|
|
||
| class Test_extract_overlapping(tests.IrisTest): | ||
| def setUp(self): | ||
| shape = (6, 14, 19) | ||
|
|
@@ -118,6 +164,47 @@ def test_different_orders(self): | |
| self.assertEqual(b.coord('time'), self.cube.coord('time')[2:4]) | ||
|
|
||
|
|
||
| class Test_iadd(tests.IrisTest): | ||
| def setUp(self): | ||
| self.cube1 = iris.cube.Cube(1, long_name='foo') | ||
| self.cube2 = iris.cube.Cube(1, long_name='bar') | ||
| self.cubelist1 = iris.cube.CubeList([self.cube1]) | ||
| self.cubelist2 = iris.cube.CubeList([self.cube2]) | ||
|
|
||
| def test_pass(self): | ||
| cubelist = copy.copy(self.cubelist1) | ||
| cubelist += self.cubelist2 | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Travis output showing the above:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test passes if I point it at the
I'm officially confused. |
||
| self.assertEqual(cubelist, self.cubelist1 + self.cubelist2) | ||
| cubelist += [self.cube2] | ||
| self.assertEqual(cubelist[-1], self.cube2) | ||
|
|
||
| def test_fail(self): | ||
| msg = 'Cube is not iterable' | ||
| with self.assertRaisesRegexp(TypeError, msg): | ||
| self.cubelist1 += self.cube1 | ||
| msg = "'float' object is not iterable" | ||
| with self.assertRaisesRegexp(TypeError, msg): | ||
| self.cubelist1 += 1. | ||
| with self.assertRaisesRegexp(ValueError, NOT_CUBE_MSG.format('int')): | ||
| self.cubelist1 += range(3) | ||
|
|
||
|
|
||
| class Test_insert(tests.IrisTest): | ||
| def setUp(self): | ||
| self.cube1 = iris.cube.Cube(1, long_name='foo') | ||
| self.cube2 = iris.cube.Cube(1, long_name='bar') | ||
| self.cubelist = iris.cube.CubeList([self.cube1] * 3) | ||
|
|
||
| def test_pass(self): | ||
| self.cubelist.insert(1, self.cube2) | ||
| self.assertEqual(self.cubelist[1], self.cube2) | ||
|
|
||
| def test_fail(self): | ||
| with self.assertRaisesRegexp(TypeError, | ||
| NOT_CUBE_MSG.format('NoneType')): | ||
| self.cubelist.insert(0, None) | ||
|
|
||
|
|
||
| class Test_merge_cube(tests.IrisTest): | ||
| def setUp(self): | ||
| self.cube1 = Cube([1, 2, 3], "air_temperature", units="K") | ||
|
|
@@ -241,6 +328,36 @@ def test_combination_with_extra_triple(self): | |
| self.assertCML(cube, checksum=False) | ||
|
|
||
|
|
||
| class Test_setitem(tests.IrisTest): | ||
| def setUp(self): | ||
| self.cube1 = iris.cube.Cube(1, long_name='foo') | ||
| self.cube2 = iris.cube.Cube(1, long_name='bar') | ||
| self.cube3 = iris.cube.Cube(1, long_name='boo') | ||
| self.cubelist = iris.cube.CubeList([self.cube1] * 3) | ||
|
|
||
| def test_pass(self): | ||
| self.cubelist[1] = self.cube2 | ||
| self.assertEqual(self.cubelist[1], self.cube2) | ||
| self.cubelist[:2] = (self.cube2, self.cube3) | ||
| self.assertEqual( | ||
| self.cubelist, | ||
| iris.cube.CubeList([self.cube2, self.cube3, self.cube1])) | ||
|
|
||
| def test_fail(self): | ||
| with self.assertRaisesRegexp(TypeError, | ||
| NOT_CUBE_MSG.format('NoneType')): | ||
| self.cubelist[0] = None | ||
| with self.assertRaisesRegexp(ValueError, | ||
| NOT_CUBE_MSG.format('NoneType')): | ||
| self.cubelist[0:2] = [self.cube3, None] | ||
rcomer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| msg = "can only assign an iterable" | ||
| with self.assertRaisesRegexp(TypeError, msg): | ||
| self.cubelist[:1] = 2.5 | ||
| with self.assertRaisesRegexp(TypeError, msg): | ||
| self.cubelist[:1] = self.cube1 | ||
|
|
||
|
|
||
| class Test_xml(tests.IrisTest): | ||
| def setUp(self): | ||
| self.cubes = CubeList([Cube(np.arange(3)), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.