23
23
24
24
class Base (object ):
25
25
26
+ def setup_method (self , method ):
27
+ self .dtype = self .create ()
28
+
26
29
def test_hash (self ):
27
30
hash (self .dtype )
28
31
@@ -37,14 +40,38 @@ def test_numpy_informed(self):
37
40
assert not np .str_ == self .dtype
38
41
39
42
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
40
50
result = tm .round_trip_pickle (self .dtype )
51
+ assert not len (self .dtype ._cache )
41
52
assert result == self .dtype
42
53
43
54
44
- class TestCategoricalDtype (Base , tm .TestCase ):
55
+ class TestCategoricalDtype (Base ):
56
+
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 )
45
66
46
- def setUp (self ):
47
- self .dtype = CategoricalDtype ()
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
48
75
49
76
def test_hash_vs_equality (self ):
50
77
# make sure that we satisfy is semantics
@@ -93,10 +120,10 @@ def test_basic(self):
93
120
assert not is_categorical (1.0 )
94
121
95
122
96
- class TestDatetimeTZDtype (Base , tm . TestCase ):
123
+ class TestDatetimeTZDtype (Base ):
97
124
98
- def setUp (self ):
99
- self . dtype = DatetimeTZDtype ('ns' , 'US/Eastern' )
125
+ def create (self ):
126
+ return DatetimeTZDtype ('ns' , 'US/Eastern' )
100
127
101
128
def test_hash_vs_equality (self ):
102
129
# make sure that we satisfy is semantics
@@ -209,10 +236,24 @@ def test_empty(self):
209
236
str (dt )
210
237
211
238
212
- class TestPeriodDtype (Base , tm . TestCase ):
239
+ class TestPeriodDtype (Base ):
213
240
214
- def setUp (self ):
215
- self .dtype = PeriodDtype ('D' )
241
+ def create (self ):
242
+ return PeriodDtype ('D' )
243
+
244
+ def test_hash_vs_equality (self ):
245
+ # make sure that we satisfy is semantics
246
+ dtype = self .dtype
247
+ dtype2 = PeriodDtype ('D' )
248
+ dtype3 = PeriodDtype (dtype2 )
249
+ assert dtype == dtype2
250
+ assert dtype2 == dtype
251
+ assert dtype3 == dtype
252
+ assert dtype is dtype2
253
+ assert dtype2 is dtype
254
+ assert dtype3 is dtype
255
+ assert hash (dtype ) == hash (dtype2 )
256
+ assert hash (dtype ) == hash (dtype3 )
216
257
217
258
def test_construction (self ):
218
259
with pytest .raises (ValueError ):
@@ -338,11 +379,37 @@ def test_not_string(self):
338
379
assert not is_string_dtype (PeriodDtype ('D' ))
339
380
340
381
341
- class TestIntervalDtype (Base , tm .TestCase ):
382
+ class TestIntervalDtype (Base ):
383
+
384
+ def create (self ):
385
+ return IntervalDtype ('int64' )
386
+
387
+ def test_hash_vs_equality (self ):
388
+ # make sure that we satisfy is semantics
389
+ dtype = self .dtype
390
+ dtype2 = IntervalDtype ('int64' )
391
+ dtype3 = IntervalDtype (dtype2 )
392
+ assert dtype == dtype2
393
+ assert dtype2 == dtype
394
+ assert dtype3 == dtype
395
+ assert dtype is dtype2
396
+ assert dtype2 is dtype
397
+ assert dtype3 is dtype
398
+ assert hash (dtype ) == hash (dtype2 )
399
+ assert hash (dtype ) == hash (dtype3 )
342
400
343
- # TODO: placeholder
344
- def setUp (self ):
345
- self .dtype = IntervalDtype ('int64' )
401
+ dtype1 = IntervalDtype ('interval' )
402
+ dtype2 = IntervalDtype (dtype1 )
403
+ dtype3 = IntervalDtype ('interval' )
404
+ assert dtype2 == dtype1
405
+ assert dtype2 == dtype2
406
+ assert dtype2 == dtype3
407
+ assert dtype2 is dtype1
408
+ assert dtype2 is dtype2
409
+ assert dtype2 is dtype3
410
+ assert hash (dtype2 ) == hash (dtype1 )
411
+ assert hash (dtype2 ) == hash (dtype2 )
412
+ assert hash (dtype2 ) == hash (dtype3 )
346
413
347
414
def test_construction (self ):
348
415
with pytest .raises (ValueError ):
@@ -356,9 +423,9 @@ def test_construction(self):
356
423
def test_construction_generic (self ):
357
424
# generic
358
425
i = IntervalDtype ('interval' )
359
- assert i .subtype is None
426
+ assert i .subtype == ''
360
427
assert is_interval_dtype (i )
361
- assert str (i ) == 'interval'
428
+ assert str (i ) == 'interval[] '
362
429
363
430
i = IntervalDtype ()
364
431
assert i .subtype is None
@@ -445,3 +512,15 @@ def test_basic_dtype(self):
445
512
assert not is_interval_dtype (np .object_ )
446
513
assert not is_interval_dtype (np .int64 )
447
514
assert not is_interval_dtype (np .float64 )
515
+
516
+ def test_caching (self ):
517
+ IntervalDtype .reset_cache ()
518
+ dtype = IntervalDtype ("int64" )
519
+ assert len (IntervalDtype ._cache ) == 1
520
+
521
+ IntervalDtype ("interval" )
522
+ assert len (IntervalDtype ._cache ) == 2
523
+
524
+ IntervalDtype .reset_cache ()
525
+ tm .round_trip_pickle (dtype )
526
+ assert len (IntervalDtype ._cache ) == 0
0 commit comments