@@ -72,33 +72,23 @@ def reset_testing_mode():
7272 if 'deprecate' in testing_mode :
7373 warnings .simplefilter ('ignore' , DeprecationWarning )
7474
75+
7576set_testing_mode ()
7677
78+
7779class TestCase (unittest .TestCase ):
7880
7981 @classmethod
8082 def setUpClass (cls ):
81- pd .set_option ('chained_assignment' ,'raise' )
83+ pd .set_option ('chained_assignment' , 'raise' )
8284
8385 @classmethod
8486 def tearDownClass (cls ):
8587 pass
8688
8789 def reset_display_options (self ):
8890 # reset the display options
89- pd .reset_option ('^display.' ,silent = True )
90-
91- def assert_numpy_array_equal (self , np_array , assert_equal ):
92- """Checks that 'np_array' is equal to 'assert_equal'
93-
94- Note that the expected array should not contain `np.nan`! Two numpy arrays are equal if all
95- elements are equal, which is not possible if `np.nan` is such an element!
96-
97- If the expected array includes `np.nan` use `assert_numpy_array_equivalent(...)`.
98- """
99- if np .array_equal (np_array , assert_equal ):
100- return
101- raise AssertionError ('{0} is not equal to {1}.' .format (np_array , assert_equal ))
91+ pd .reset_option ('^display.' , silent = True )
10292
10393 def round_trip_pickle (self , obj , path = None ):
10494 if path is None :
@@ -107,82 +97,6 @@ def round_trip_pickle(self, obj, path=None):
10797 pd .to_pickle (obj , path )
10898 return pd .read_pickle (path )
10999
110- def assert_numpy_array_equivalent (self , np_array , assert_equal , strict_nan = False ):
111- """Checks that 'np_array' is equivalent to 'assert_equal'
112-
113- Two numpy arrays are equivalent if the arrays have equal non-NaN elements, and
114- `np.nan` in corresponding locations.
115-
116- If the the expected array does not contain `np.nan` `assert_numpy_array_equivalent` is the
117- similar to `assert_numpy_array_equal()`. If the expected array includes `np.nan` use this
118- function.
119- """
120- if array_equivalent (np_array , assert_equal , strict_nan = strict_nan ):
121- return
122- raise AssertionError ('{0} is not equivalent to {1}.' .format (np_array , assert_equal ))
123-
124- def assert_categorical_equal (self , res , exp ):
125- if not array_equivalent (res .categories , exp .categories ):
126- raise AssertionError ('categories not equivalent: {0} vs {1}.' .format (res .categories ,
127- exp .categories ))
128- if not array_equivalent (res .codes , exp .codes ):
129- raise AssertionError ('codes not equivalent: {0} vs {1}.' .format (res .codes ,
130- exp .codes ))
131- self .assertEqual (res .ordered , exp .ordered , "ordered not the same" )
132- self .assertEqual (res .name , exp .name , "name not the same" )
133-
134- def assertIs (self , first , second , msg = '' ):
135- """Checks that 'first' is 'second'"""
136- a , b = first , second
137- assert a is b , "%s: %r is not %r" % (msg .format (a ,b ), a , b )
138-
139- def assertIsNot (self , first , second , msg = '' ):
140- """Checks that 'first' is not 'second'"""
141- a , b = first , second
142- assert a is not b , "%s: %r is %r" % (msg .format (a ,b ), a , b )
143-
144- def assertIsNone (self , expr , msg = '' ):
145- """Checks that 'expr' is None"""
146- self .assertIs (expr , None , msg )
147-
148- def assertIsNotNone (self , expr , msg = '' ):
149- """Checks that 'expr' is not None"""
150- self .assertIsNot (expr , None , msg )
151-
152- def assertIn (self , first , second , msg = '' ):
153- """Checks that 'first' is in 'second'"""
154- a , b = first , second
155- assert a in b , "%s: %r is not in %r" % (msg .format (a ,b ), a , b )
156-
157- def assertNotIn (self , first , second , msg = '' ):
158- """Checks that 'first' is not in 'second'"""
159- a , b = first , second
160- assert a not in b , "%s: %r is in %r" % (msg .format (a ,b ), a , b )
161-
162- def assertIsInstance (self , obj , cls , msg = '' ):
163- """Test that obj is an instance of cls
164- (which can be a class or a tuple of classes,
165- as supported by isinstance())."""
166- assert isinstance (obj , cls ), (
167- "%sExpected object to be of type %r, found %r instead" % (
168- msg , cls , type (obj )))
169-
170- def assertNotIsInstance (self , obj , cls , msg = '' ):
171- """Test that obj is not an instance of cls
172- (which can be a class or a tuple of classes,
173- as supported by isinstance())."""
174- assert not isinstance (obj , cls ), (
175- "%sExpected object to be of type %r, found %r instead" % (
176- msg , cls , type (obj )))
177-
178- def assertRaises (self , _exception , _callable = None , * args , ** kwargs ):
179- """ compat with 2.6; assert that an exception is raised """
180- assertRaises (_exception , _callable , * args , ** kwargs )
181-
182- def assertRaisesRegexp (self , _exception , _regexp , _callable = None , * args , ** kwargs ):
183- """ Port of assertRaisesRegexp from unittest in Python 2.7 - used in with statement """
184- assertRaisesRegexp (_exception , _regexp , _callable , * args , ** kwargs )
185-
186100# NOTE: don't pass an NDFrame or index to this function - may not handle it
187101# well.
188102assert_almost_equal = _testing .assert_almost_equal
@@ -633,6 +547,109 @@ def isiterable(obj):
633547def is_sorted (seq ):
634548 return assert_almost_equal (seq , np .sort (np .array (seq )))
635549
550+
551+ def assertIs (first , second , msg = '' ):
552+ """Checks that 'first' is 'second'"""
553+ a , b = first , second
554+ assert a is b , "%s: %r is not %r" % (msg .format (a , b ), a , b )
555+
556+
557+ def assertIsNot (first , second , msg = '' ):
558+ """Checks that 'first' is not 'second'"""
559+ a , b = first , second
560+ assert a is not b , "%s: %r is %r" % (msg .format (a , b ), a , b )
561+
562+
563+ def assertIn (first , second , msg = '' ):
564+ """Checks that 'first' is in 'second'"""
565+ a , b = first , second
566+ assert a in b , "%s: %r is not in %r" % (msg .format (a , b ), a , b )
567+
568+
569+ def assertNotIn (first , second , msg = '' ):
570+ """Checks that 'first' is not in 'second'"""
571+ a , b = first , second
572+ assert a not in b , "%s: %r is in %r" % (msg .format (a , b ), a , b )
573+
574+
575+ def assertIsNone (expr , msg = '' ):
576+ """Checks that 'expr' is None"""
577+ return assertIs (expr , None , msg )
578+
579+
580+ def assertIsNotNone (expr , msg = '' ):
581+ """Checks that 'expr' is not None"""
582+ return assertIsNot (expr , None , msg )
583+
584+
585+ def assertIsInstance (obj , cls , msg = '' ):
586+ """Test that obj is an instance of cls
587+ (which can be a class or a tuple of classes,
588+ as supported by isinstance())."""
589+ assert isinstance (obj , cls ), (
590+ "%sExpected object to be of type %r, found %r instead" % (
591+ msg , cls , type (obj )))
592+
593+
594+ def assertNotIsInstance (obj , cls , msg = '' ):
595+ """Test that obj is not an instance of cls
596+ (which can be a class or a tuple of classes,
597+ as supported by isinstance())."""
598+ assert not isinstance (obj , cls ), (
599+ "%sExpected object to be of type %r, found %r instead" % (
600+ msg , cls , type (obj )))
601+
602+
603+ def assert_categorical_equal (res , exp ):
604+ if not array_equivalent (res .categories , exp .categories ):
605+ raise AssertionError (
606+ 'categories not equivalent: {0} vs {1}.' .format (res .categories ,
607+ exp .categories ))
608+ if not array_equivalent (res .codes , exp .codes ):
609+ raise AssertionError (
610+ 'codes not equivalent: {0} vs {1}.' .format (res .codes , exp .codes ))
611+
612+ if res .ordered != exp .ordered :
613+ raise AssertionError ("ordered not the same" )
614+
615+ if res .name != exp .name :
616+ raise AssertionError ("name not the same" )
617+
618+
619+ def assert_numpy_array_equal (np_array , assert_equal ):
620+ """Checks that 'np_array' is equal to 'assert_equal'
621+
622+ Note that the expected array should not contain `np.nan`!
623+ Two numpy arrays are equal if all
624+ elements are equal, which is not possible if `np.nan` is such an element!
625+
626+ If the expected array includes `np.nan` use
627+ `assert_numpy_array_equivalent(...)`.
628+ """
629+ if np .array_equal (np_array , assert_equal ):
630+ return
631+ raise AssertionError (
632+ '{0} is not equal to {1}.' .format (np_array , assert_equal ))
633+
634+
635+ def assert_numpy_array_equivalent (np_array , assert_equal , strict_nan = False ):
636+ """Checks that 'np_array' is equivalent to 'assert_equal'
637+
638+ Two numpy arrays are equivalent if the arrays have equal non-NaN elements,
639+ and `np.nan` in corresponding locations.
640+
641+ If the the expected array does not contain `np.nan`
642+ `assert_numpy_array_equivalent` is the similar to
643+ `assert_numpy_array_equal()`. If the expected array includes
644+ `np.nan` use this
645+ function.
646+ """
647+ if array_equivalent (np_array , assert_equal , strict_nan = strict_nan ):
648+ return
649+ raise AssertionError (
650+ '{0} is not equivalent to {1}.' .format (np_array , assert_equal ))
651+
652+
636653# This could be refactored to use the NDFrame.equals method
637654def assert_series_equal (left , right , check_dtype = True ,
638655 check_index_type = False ,
@@ -1738,3 +1755,10 @@ def use_numexpr(use, min_elements=expr._MIN_ELEMENTS):
17381755 yield
17391756 expr ._MIN_ELEMENTS = oldmin
17401757 expr .set_use_numexpr (olduse )
1758+
1759+
1760+ # Also provide all assert_* functions in the TestCase class
1761+ for name , obj in inspect .getmembers (sys .modules [__name__ ]):
1762+ if inspect .isfunction (obj ) and name .startswith ('assert' ):
1763+ setattr (TestCase , name , staticmethod (obj ))
1764+
0 commit comments