Skip to content

Commit 7546ec7

Browse files
pp-momarqh
authored andcommitted
Make assertMaskedArrayXxx work with a nonmasked result. (SciTools#2361)
1 parent c4146dc commit 7546ec7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/iris/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ def _assertMaskedArray(self, assertion, a, b, strict, **kwargs):
515515
# Define helper function to extract unmasked values as a 1d
516516
# array.
517517
def unmasked_data_as_1d_array(array):
518+
array = ma.asarray(array)
518519
if array.ndim == 0:
519520
if array.mask:
520521
data = np.array([])

lib/iris/tests/unit/tests/test_IrisTest.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# (C) British Crown Copyright 2014 - 2015, Met Office
1+
# (C) British Crown Copyright 2014 - 2017, Met Office
22
#
33
# This file is part of Iris.
44
#
@@ -86,6 +86,47 @@ def _func(self):
8686
return self.assertMaskedArrayEqual
8787

8888

89+
class Test_assertMaskedArrayEqual__Nonmaasked(tests.IrisTest):
90+
def test_nonmasked_same(self):
91+
# Masked test can be used on non-masked arrays.
92+
arr1 = np.array([1, 2])
93+
self.assertMaskedArrayEqual(arr1, arr1)
94+
95+
def test_masked_nonmasked_same(self):
96+
# Masked test can be used between masked + non-masked arrays, and will
97+
# consider them equal, when mask=None.
98+
arr1 = np.ma.masked_array([1, 2])
99+
arr2 = np.array([1, 2])
100+
self.assertMaskedArrayEqual(arr1, arr2)
101+
102+
def test_masked_nonmasked_different(self):
103+
arr1 = np.ma.masked_array([1, 2])
104+
arr2 = np.array([1, 3])
105+
with self.assertRaisesRegexp(AssertionError, 'Arrays are not equal'):
106+
self.assertMaskedArrayEqual(arr1, arr2)
107+
108+
def test_nonmasked_masked_same(self):
109+
# Masked test can be used between masked + non-masked arrays, and will
110+
# consider them equal, when mask=None.
111+
arr1 = np.array([1, 2])
112+
arr2 = np.ma.masked_array([1, 2])
113+
self.assertMaskedArrayEqual(arr1, arr2)
114+
115+
def test_masked_nonmasked_same_falsemask(self):
116+
# Masked test can be used between masked + non-masked arrays, and will
117+
# consider them equal, when mask=False.
118+
arr1 = np.ma.masked_array([1, 2], mask=False)
119+
arr2 = np.array([1, 2])
120+
self.assertMaskedArrayEqual(arr1, arr2)
121+
122+
def test_masked_nonmasked_same_emptymask(self):
123+
# Masked test can be used between masked + non-masked arrays, and will
124+
# consider them equal, when mask=zeros.
125+
arr1 = np.ma.masked_array([1, 2], mask=[False, False])
126+
arr2 = np.array([1, 2])
127+
self.assertMaskedArrayEqual(arr1, arr2)
128+
129+
89130
class Test_assertMaskedArrayAlmostEqual(_MaskedArrayEquality, tests.IrisTest):
90131
@property
91132
def _func(self):

0 commit comments

Comments
 (0)