2323
2424class Base (object ):
2525
26+ def setUp (self ):
27+ self .dtype = self .create ()
28+
2629 def test_hash (self ):
2730 hash (self .dtype )
2831
@@ -37,14 +40,38 @@ def test_numpy_informed(self):
3740 assert not np .str_ == self .dtype
3841
3942 def test_pickle (self ):
43+ # make sure our cache is NOT pickled
44+
45+ # clear the cache
46+ type (self .dtype ).reset_cache ()
47+ assert not len (self .dtype ._cache )
48+
49+ # force back to the cache
4050 result = tm .round_trip_pickle (self .dtype )
51+ assert not len (self .dtype ._cache )
4152 assert result == self .dtype
4253
4354
4455class TestCategoricalDtype (Base , tm .TestCase ):
4556
46- def setUp (self ):
47- self .dtype = CategoricalDtype ()
57+ def create (self ):
58+ return CategoricalDtype ()
59+
60+ def test_pickle (self ):
61+ # make sure our cache is NOT pickled
62+
63+ # clear the cache
64+ type (self .dtype ).reset_cache ()
65+ assert not len (self .dtype ._cache )
66+
67+ # force back to the cache
68+ result = tm .round_trip_pickle (self .dtype )
69+
70+ # we are a singular object so we are added
71+ # back to the cache upon unpickling
72+ # this is to ensure object identity
73+ assert len (self .dtype ._cache ) == 1
74+ assert result == self .dtype
4875
4976 def test_hash_vs_equality (self ):
5077 # make sure that we satisfy is semantics
@@ -95,8 +122,8 @@ def test_basic(self):
95122
96123class TestDatetimeTZDtype (Base , tm .TestCase ):
97124
98- def setUp (self ):
99- self . dtype = DatetimeTZDtype ('ns' , 'US/Eastern' )
125+ def create (self ):
126+ return DatetimeTZDtype ('ns' , 'US/Eastern' )
100127
101128 def test_hash_vs_equality (self ):
102129 # make sure that we satisfy is semantics
@@ -211,8 +238,8 @@ def test_empty(self):
211238
212239class TestPeriodDtype (Base , tm .TestCase ):
213240
214- def setUp (self ):
215- self . dtype = PeriodDtype ('D' )
241+ def create (self ):
242+ return PeriodDtype ('D' )
216243
217244 def test_construction (self ):
218245 with pytest .raises (ValueError ):
@@ -340,9 +367,22 @@ def test_not_string(self):
340367
341368class TestIntervalDtype (Base , tm .TestCase ):
342369
343- # TODO: placeholder
344- def setUp (self ):
345- self .dtype = IntervalDtype ('int64' )
370+ def create (self ):
371+ return IntervalDtype ('int64' )
372+
373+ def test_hash_vs_equality (self ):
374+ # make sure that we satisfy is semantics
375+ dtype = self .dtype
376+ dtype2 = IntervalDtype ('int64' )
377+ dtype3 = IntervalDtype (dtype2 )
378+ assert dtype == dtype2
379+ assert dtype2 == dtype
380+ assert dtype3 == dtype
381+ assert dtype is dtype2
382+ assert dtype2 is dtype
383+ assert dtype3 is dtype
384+ assert hash (dtype ) == hash (dtype2 )
385+ assert hash (dtype ) == hash (dtype3 )
346386
347387 def test_construction (self ):
348388 with pytest .raises (ValueError ):
@@ -445,3 +485,14 @@ def test_basic_dtype(self):
445485 assert not is_interval_dtype (np .object_ )
446486 assert not is_interval_dtype (np .int64 )
447487 assert not is_interval_dtype (np .float64 )
488+
489+ def test_caching (self ):
490+ IntervalDtype .reset_cache ()
491+ dtype = IntervalDtype ("int64" )
492+ assert len (IntervalDtype ._cache ) == 1
493+
494+ IntervalDtype ("interval" )
495+ assert len (IntervalDtype ._cache ) == 1
496+
497+ tm .round_trip_pickle (dtype )
498+ assert len (IntervalDtype ._cache ) == 1
0 commit comments