Skip to content

Commit

Permalink
assertItemsEqual (but may need coercion to sets)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jan 14, 2018
1 parent 519bafd commit fa8f89a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
10 changes: 5 additions & 5 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2836,7 +2836,7 @@ def test_to_and_from_cdms2(self):
actual = original.to_cdms2()
self.assertArrayEqual(actual, original)
assert actual.id == original.name
self.assertItemsEqual(actual.getAxisIds(), original.dims)
assert actual.getAxisIds() == original.dims
for axis, coord in zip(actual.getAxisList(), expected_coords):
assert axis.id == coord.name
self.assertArrayEqual(axis, coord.values)
Expand Down Expand Up @@ -2878,8 +2878,8 @@ def test_to_and_from_iris(self):
actual = original.to_iris()
self.assertArrayEqual(actual.data, original.data)
assert actual.var_name == original.name
self.assertItemsEqual([d.var_name for d in actual.dim_coords],
original.dims)
assert [d.var_name for d in actual.dim_coords] == \
original.dims
assert actual.cell_methods == \
(iris.coords.CellMethod(method='mean',
coords=('height',),
Expand Down Expand Up @@ -2949,8 +2949,8 @@ def test_to_and_from_iris_dask(self):
hasattr(actual, 'core_data') else actual.data
self.assertArrayEqual(actual_data, original.data)
assert actual.var_name == original.name
self.assertItemsEqual([d.var_name for d in actual.dim_coords],
original.dims)
assert [d.var_name for d in actual.dim_coords] == \
original.dims
assert actual.cell_methods == \
(iris.coords.CellMethod(method='mean',
coords=('height',),
Expand Down
56 changes: 28 additions & 28 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def test_constructor_with_coords(self):

ds = Dataset({}, {'a': ('x', [1])})
assert not ds.data_vars
self.assertItemsEqual(ds.coords.keys(), ['a'])
assert ds.coords.keys() == ['a']

mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]],
names=('level_1', 'level_2'))
Expand All @@ -412,28 +412,28 @@ def test_properties(self):
assert type(ds.dims.mapping.mapping) is dict

with pytest.warns(FutureWarning):
self.assertItemsEqual(ds, list(ds.variables))
assert ds == list(ds.variables)
with pytest.warns(FutureWarning):
self.assertItemsEqual(ds.keys(), list(ds.variables))
assert ds.keys() == list(ds.variables)
assert 'aasldfjalskdfj' not in ds.variables
assert 'dim1' in repr(ds.variables)
with pytest.warns(FutureWarning):
assert len(ds) == 7
with pytest.warns(FutureWarning):
assert bool(ds) == True

self.assertItemsEqual(ds.data_vars, ['var1', 'var2', 'var3'])
self.assertItemsEqual(ds.data_vars.keys(), ['var1', 'var2', 'var3'])
assert ds.data_vars == ['var1', 'var2', 'var3']
assert ds.data_vars.keys() == ['var1', 'var2', 'var3']
assert 'var1' in ds.data_vars
assert 'dim1' not in ds.data_vars
assert 'numbers' not in ds.data_vars
assert len(ds.data_vars) == 3

self.assertItemsEqual(ds.indexes, ['dim2', 'dim3', 'time'])
assert ds.indexes == ['dim2', 'dim3', 'time']
assert len(ds.indexes) == 3
assert 'dim2' in repr(ds.indexes)

self.assertItemsEqual(ds.coords, ['time', 'dim2', 'dim3', 'numbers'])
assert ds.coords == ['time', 'dim2', 'dim3', 'numbers']
assert 'dim2' in ds.coords
assert 'numbers' in ds.coords
assert 'var1' not in ds.coords
Expand Down Expand Up @@ -521,7 +521,7 @@ def test_coords_properties(self):

assert 4 == len(data.coords)

self.assertItemsEqual(['x', 'y', 'a', 'b'], list(data.coords))
assert ['x', 'y', 'a', 'b'] == list(data.coords)

assert_identical(data.coords['x'].variable, data['x'].variable)
assert_identical(data.coords['y'].variable, data['y'].variable)
Expand Down Expand Up @@ -817,7 +817,7 @@ def test_isel(self):
ret = data.isel(**slicers)

# Verify that only the specified dimension was altered
self.assertItemsEqual(data.dims, ret.dims)
assert data.dims == ret.dims
for d in data.dims:
if d in slicers:
assert ret.dims[d] == \
Expand All @@ -843,21 +843,21 @@ def test_isel(self):

ret = data.isel(dim1=0)
assert {'time': 20, 'dim2': 9, 'dim3': 10} == ret.dims
self.assertItemsEqual(data.data_vars, ret.data_vars)
self.assertItemsEqual(data.coords, ret.coords)
self.assertItemsEqual(data.indexes, ret.indexes)
assert data.data_vars == ret.data_vars
assert data.coords == ret.coords
assert data.indexes == ret.indexes

ret = data.isel(time=slice(2), dim1=0, dim2=slice(5))
assert {'time': 2, 'dim2': 5, 'dim3': 10} == ret.dims
self.assertItemsEqual(data.data_vars, ret.data_vars)
self.assertItemsEqual(data.coords, ret.coords)
self.assertItemsEqual(data.indexes, ret.indexes)
assert data.data_vars == ret.data_vars
assert data.coords == ret.coords
assert data.indexes == ret.indexes

ret = data.isel(time=0, dim1=0, dim2=slice(5))
self.assertItemsEqual({'dim2': 5, 'dim3': 10}, ret.dims)
self.assertItemsEqual(data.data_vars, ret.data_vars)
self.assertItemsEqual(data.coords, ret.coords)
self.assertItemsEqual(data.indexes, list(ret.indexes) + ['time'])
assert {'dim2': 5, 'dim3': 10} == ret.dims
assert data.data_vars == ret.data_vars
assert data.coords == ret.coords
assert data.indexes == list(ret.indexes) + ['time']

def test_isel_fancy(self):
# isel with fancy indexing.
Expand Down Expand Up @@ -2396,12 +2396,12 @@ def test_setitem_multiindex_level(self):
def test_delitem(self):
data = create_test_data()
all_items = set(data.variables)
self.assertItemsEqual(data.variables, all_items)
assert data.variables == all_items
del data['var1']
self.assertItemsEqual(data.variables, all_items - set(['var1']))
assert data.variables == all_items - set(['var1'])
del data['numbers']
self.assertItemsEqual(data.variables,
all_items - set(['var1', 'numbers']))
assert data.variables == \
all_items - set(['var1', 'numbers'])
assert 'numbers' not in data.coords

def test_squeeze(self):
Expand Down Expand Up @@ -3247,7 +3247,7 @@ def test_reduce(self):
((), ['dim1', 'dim2', 'dim3', 'time'])]:
actual = data.min(dim=reduct).dims
print(reduct, actual, expected)
self.assertItemsEqual(actual, expected)
assert actual == expected

self.assert_equal(data.mean(dim=[]), data)

Expand Down Expand Up @@ -3282,7 +3282,7 @@ def test_reduce_cumsum_test_dims(self):
('time', ['dim1', 'dim2', 'dim3'])]:
actual = getattr(data, cumfunc)(dim=reduct).dims
print(reduct, actual, expected)
self.assertItemsEqual(actual, expected)
assert actual == expected

def test_reduce_non_numeric(self):
data1 = create_test_data(seed=44)
Expand Down Expand Up @@ -3416,14 +3416,14 @@ def test_rank(self):
ds = create_test_data(seed=1234)
# only ds.var3 depends on dim3
z = ds.rank('dim3')
self.assertItemsEqual(['var3'], list(z.data_vars))
assert ['var3'] == list(z.data_vars)
# same as dataarray version
x = z.var3
y = ds.var3.rank('dim3')
self.assert_equal(x, y)
# coordinates stick
self.assertItemsEqual(list(z.coords), list(ds.coords))
self.assertItemsEqual(list(x.coords), list(y.coords))
assert list(z.coords) == list(ds.coords)
assert list(x.coords) == list(y.coords)
# invalid dim
with raises_regex(ValueError, 'does not contain'):
x.rank('invalid_dim')
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_first_n_items(self):
for n in [3, 10, 13, 100, 200]:
actual = formatting.first_n_items(array, n)
expected = array.flat[:n]
self.assertItemsEqual(expected, actual)
assert expected == actual

with raises_regex(ValueError, 'at least one item'):
formatting.first_n_items(array, 0)
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_frozen(self):
def test_sorted_keys_dict(self):
x = {'a': 1, 'b': 2, 'c': 3}
y = utils.SortedKeysDict(x)
self.assertItemsEqual(y, ['a', 'b', 'c'])
assert y == ['a', 'b', 'c']
assert repr(utils.SortedKeysDict()) == \
"SortedKeysDict({})"

Expand All @@ -141,7 +141,7 @@ def test_chain_map(self):
m['x'] = 100
assert m['x'] == 100
assert m.maps[0]['x'] == 100
self.assertItemsEqual(['x', 'y', 'z'], m)
assert ['x', 'y', 'z'] == m


def test_repr_object():
Expand Down

0 comments on commit fa8f89a

Please sign in to comment.