Skip to content

Commit b39b435

Browse files
Extra testing for dataset-compare fixes. (#157)
* Extra testing for dataset-compare fixes. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add change note. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ed3317e commit b39b435

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a bug in dataset comparison, where variables with missing or unbroadcastable data arrays could cause errors rather than generating difference messages.

tests/unit/utils/compare_nc_datasets/test_variable_differences.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ def test_signed_unsigned(self, equaldata):
165165
@pytest.mark.parametrize("given", ["nodata", "data", "dtype"])
166166
def test_nodata_nodtype(self, given):
167167
# Check that we can correctly compare a variable with NO specified data or dtype,
168-
# with one that may have either.
169-
# N.B. this omits comparing 2 variables with dtype only. See following.
168+
# with one that may have either.
169+
# N.B. this omits comparing 2 variables with dtype only. See following test,
170+
# 'test_nodata_withdtype` for that.
170171
v1 = NcVariable("x")
171172

172173
kwargs = {}
@@ -207,6 +208,55 @@ def test_nodata_withdtype(self, equality):
207208
errs = variable_differences(v1, v2)
208209
check(errs, expected)
209210

211+
@pytest.mark.parametrize(
212+
"differs",
213+
["shapes", "shapes_nodata", "dims", "dims_nodata", "dims_both_nodata"],
214+
)
215+
def test_differences_ignore_datacheck(self, differs):
216+
# Check that we can safely compare variables with different shapes or dims,
217+
# even with non-broadcastable content, as this should prevent the data
218+
# equivalence testing.
219+
# Effectively, this is also testing "safe_varshape"
220+
dims1 = ["x", "y"]
221+
dims2 = dims1
222+
shape1 = (2, 3)
223+
shape2 = shape1
224+
nodata = differs.endswith("nodata")
225+
if differs.startswith("dims"):
226+
dims2 = ["x"]
227+
expected = [
228+
"Variable \"x\" dimensions differ : ('x', 'y') != ('x',)"
229+
]
230+
if nodata:
231+
# In this case, either one or both have no data.
232+
shape1 = None
233+
both_nodata = "both" in differs
234+
if both_nodata:
235+
shape2 = None
236+
else:
237+
# Only one has data --> triggers a shape warning too.
238+
expected.append(
239+
'Variable "x" shapes differ : None != (2, 3)'
240+
)
241+
elif differs.startswith("shapes"):
242+
if nodata:
243+
# In this case one has data (and therefore a shape), and the other not.
244+
shape2 = None
245+
expected = ['Variable "x" shapes differ : (2, 3) != None']
246+
else:
247+
shape2 = (3, 4)
248+
expected = ['Variable "x" shapes differ : (2, 3) != (3, 4)']
249+
else:
250+
raise ValueError(f"unrecognised 'differs' param : {differs!s}")
251+
252+
data1 = np.ones(shape1) if shape1 is not None else None
253+
data2 = np.ones(shape2) if shape2 is not None else None
254+
var1 = NcVariable("x", dimensions=dims1, data=data1)
255+
var2 = NcVariable("x", dimensions=dims2, data=data2)
256+
257+
errs = variable_differences(var1, var2)
258+
check(errs, expected)
259+
210260

211261
class TestDataCheck__controls:
212262
# Note: testing variable comparison via the 'main' public API instead of

0 commit comments

Comments
 (0)