Skip to content

Commit 35a03eb

Browse files
committed
Merge pull request #6376 from bwignall/assert_6175_newAsserts
ENH: Add new specialized asserts for tests
2 parents 9eace30 + a2f071f commit 35a03eb

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

pandas/tests/test_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_shallow_copying(self):
101101
original = self.container.copy()
102102
assert_isinstance(self.container.view(), FrozenNDArray)
103103
self.assert_(not isinstance(self.container.view(np.ndarray), FrozenNDArray))
104-
self.assert_(self.container.view() is not self.container)
104+
self.assertIsNot(self.container.view(), self.container)
105105
self.assert_numpy_array_equal(self.container, original)
106106
# shallow copy should be the same too
107107
assert_isinstance(self.container._shallow_copy(), FrozenNDArray)
@@ -115,7 +115,7 @@ def test_values(self):
115115
n = original[0] + 15
116116
vals = self.container.values()
117117
self.assert_numpy_array_equal(original, vals)
118-
self.assert_(original is not vals)
118+
self.assertIsNot(original, vals)
119119
vals[0] = n
120120
self.assert_numpy_array_equal(self.container, original)
121121
self.assertEqual(vals[0], n)

pandas/tests/test_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ def test_inplace_mutation_resets_values(self):
13521352
mi2 = MultiIndex(levels=levels2, labels=labels)
13531353
vals = mi1.values.copy()
13541354
vals2 = mi2.values.copy()
1355-
self.assert_(mi1._tuples is not None)
1355+
self.assertIsNotNone(mi1._tuples)
13561356

13571357
# make sure level setting works
13581358
new_vals = mi1.set_levels(levels2).values

pandas/tests/test_internals.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ def test_merge(self):
142142

143143
def test_copy(self):
144144
cop = self.fblock.copy()
145-
self.assert_(cop is not self.fblock)
145+
self.assertIsNot(cop, self.fblock)
146146
assert_block_equal(self.fblock, cop)
147147

148148
def test_items(self):
149149
cols = self.fblock.items
150150
self.assert_numpy_array_equal(cols, ['a', 'c', 'e'])
151151

152152
cols2 = self.fblock.items
153-
self.assert_(cols is cols2)
153+
self.assertIs(cols, cols2)
154154

155155
def test_assign_ref_items(self):
156156
new_cols = Index(['foo', 'bar', 'baz', 'quux', 'hi'])
@@ -305,8 +305,8 @@ def test_duplicate_item_failure(self):
305305
self.assertRaises(AssertionError, mgr._set_ref_locs, do_refs=True)
306306

307307
def test_contains(self):
308-
self.assert_('a' in self.mgr)
309-
self.assert_('baz' not in self.mgr)
308+
self.assertIn('a', self.mgr)
309+
self.assertNotIn('baz', self.mgr)
310310

311311
def test_pickle(self):
312312
import pickle
@@ -318,7 +318,7 @@ def test_pickle(self):
318318
assert_frame_equal(DataFrame(self.mgr), DataFrame(mgr2))
319319

320320
# share ref_items
321-
self.assert_(mgr2.blocks[0].ref_items is mgr2.blocks[1].ref_items)
321+
self.assertIs(mgr2.blocks[0].ref_items, mgr2.blocks[1].ref_items)
322322

323323
# GH2431
324324
self.assertTrue(hasattr(mgr2, "_is_consolidated"))

pandas/tests/test_series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def test_copy_index_name_checking(self):
6969
# making a copy
7070

7171
self.ts.index.name = None
72-
self.assert_(self.ts.index.name is None)
73-
self.assert_(self.ts is self.ts)
72+
self.assertIsNone(self.ts.index.name)
73+
self.assertIs(self.ts, self.ts)
7474
cp = self.ts.copy()
7575
cp.index.name = 'foo'
7676
print(self.ts.index.name)

pandas/util/testing.py

+25
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ def assert_numpy_array_equal(self, np_array, assert_equal):
7171
return
7272
raise AssertionError('{0} is not equal to {1}.'.format(np_array, assert_equal))
7373

74+
def assertIs(self, a, b, msg=''):
75+
"""Checks that 'a' is 'b'"""
76+
assert a is b, "%s: %r is not %r" % (msg.format(a,b), a, b)
77+
78+
def assertIsNot(self, a, b, msg=''):
79+
"""Checks that 'a' is not 'b'"""
80+
assert a is not b, "%s: %r is %r" % (msg.format(a,b), a, b)
81+
82+
def assertIsNone(self, a, msg=''):
83+
"""Checks that 'a' is None"""
84+
self.assertIs(a, None, msg)
85+
86+
def assertIsNotNone(self, a, msg=''):
87+
"""Checks that 'a' is not None"""
88+
self.assertIsNot(a, None, msg)
89+
90+
def assertIn(self, a, b, msg=''):
91+
"""Checks that 'a' is in 'b'"""
92+
assert a in b, "%s: %r is not in %r" % (msg.format(a,b), a, b)
93+
94+
def assertNotIn(self, a, b, msg=''):
95+
"""Checks that 'a' is not in 'b'"""
96+
assert a not in b, "%s: %r is in %r" % (msg.format(a,b), a, b)
97+
98+
7499
# NOTE: don't pass an NDFrame or index to this function - may not handle it
75100
# well.
76101
assert_almost_equal = _testing.assert_almost_equal

0 commit comments

Comments
 (0)