Skip to content

Commit 5e98eae

Browse files
author
y-p
committed
TST: Add tests/test_config.py for new core.config module
1 parent 5719a5a commit 5e98eae

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed

pandas/tests/test_config.py

+267
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
from __future__ import with_statement # support python 2.5
4+
import pandas as pd
5+
import unittest
6+
import warnings
7+
import nose
8+
9+
class TestConfig(unittest.TestCase):
10+
11+
def __init__(self,*args):
12+
super(TestConfig,self).__init__(*args)
13+
14+
from copy import deepcopy
15+
self.cf = pd.core.config
16+
self.gc=deepcopy(getattr(self.cf, '__global_config'))
17+
self.do=deepcopy(getattr(self.cf, '__deprecated_options'))
18+
self.ro=deepcopy(getattr(self.cf, '__registered_options'))
19+
20+
def setUp(self):
21+
setattr(self.cf, '__global_config', {})
22+
setattr(self.cf, '__deprecated_options', {})
23+
setattr(self.cf, '__registered_options', {})
24+
25+
def tearDown(self):
26+
setattr(self.cf, '__global_config',self.gc)
27+
setattr(self.cf, '__deprecated_options', self.do)
28+
setattr(self.cf, '__registered_options', self.ro)
29+
30+
def test_api(self):
31+
32+
#the pandas object exposes the user API
33+
self.assertTrue(hasattr(pd, 'get_option'))
34+
self.assertTrue(hasattr(pd, 'set_option'))
35+
self.assertTrue(hasattr(pd, 'reset_option'))
36+
self.assertTrue(hasattr(pd, 'reset_options'))
37+
self.assertTrue(hasattr(pd, 'describe_options'))
38+
39+
def test_register_option(self):
40+
self.cf.register_option('a', 1, 'doc')
41+
42+
# can't register an already registered option
43+
self.assertRaises(KeyError, self.cf.register_option, 'a', 1, 'doc')
44+
45+
# can't register an already registered option
46+
self.assertRaises(KeyError, self.cf.register_option, 'a.b.c.d1', 1,
47+
'doc')
48+
self.assertRaises(KeyError, self.cf.register_option, 'a.b.c.d2', 1,
49+
'doc')
50+
51+
# we can register options several levels deep
52+
# without predefining the intermediate steps
53+
# and we can define differently named options
54+
# in the same namespace
55+
self.cf.register_option('k.b.c.d1', 1, 'doc')
56+
self.cf.register_option('k.b.c.d2', 1, 'doc')
57+
58+
def test_describe_options(self):
59+
self.cf.register_option('a', 1, 'doc')
60+
self.cf.register_option('b', 1, 'doc2')
61+
self.cf.deprecate_option('b')
62+
63+
self.cf.register_option('c.d.e1', 1, 'doc3')
64+
self.cf.register_option('c.d.e2', 1, 'doc4')
65+
self.cf.register_option('f', 1)
66+
self.cf.register_option('g.h', 1)
67+
self.cf.deprecate_option('g.h',rkey="blah")
68+
69+
# non-existent keys raise KeyError
70+
self.assertRaises(KeyError, self.cf.describe_options, 'no.such.key')
71+
72+
# we can get the description for any key we registered
73+
self.assertTrue('doc' in self.cf.describe_options('a',_print_desc=False))
74+
self.assertTrue('doc2' in self.cf.describe_options('b',_print_desc=False))
75+
self.assertTrue('precated' in self.cf.describe_options('b',_print_desc=False))
76+
77+
self.assertTrue('doc3' in self.cf.describe_options('c.d.e1',_print_desc=False))
78+
self.assertTrue('doc4' in self.cf.describe_options('c.d.e2',_print_desc=False))
79+
80+
# if no doc is specified we get a default message
81+
# saying "description not available"
82+
self.assertTrue('vailable' in self.cf.describe_options('f',_print_desc=False))
83+
self.assertTrue('vailable' in self.cf.describe_options('g.h',_print_desc=False))
84+
self.assertTrue('precated' in self.cf.describe_options('g.h',_print_desc=False))
85+
self.assertTrue('blah' in self.cf.describe_options('g.h',_print_desc=False))
86+
87+
def test_get_option(self):
88+
self.cf.register_option('a', 1, 'doc')
89+
self.cf.register_option('b.a', 'hullo', 'doc2')
90+
self.cf.register_option('b.b', None, 'doc2')
91+
92+
# gets of existing keys succeed
93+
self.assertEqual(self.cf.get_option('a'), 1)
94+
self.assertEqual(self.cf.get_option('b.a'), 'hullo')
95+
self.assertTrue(self.cf.get_option('b.b') is None)
96+
97+
# gets of non-existent keys fail
98+
self.assertRaises(KeyError, self.cf.get_option, 'no_such_option')
99+
100+
def test_set_option(self):
101+
self.cf.register_option('a', 1, 'doc')
102+
self.cf.register_option('b.a', 'hullo', 'doc2')
103+
self.cf.register_option('b.b', None, 'doc2')
104+
105+
self.assertEqual(self.cf.get_option('a'), 1)
106+
self.assertEqual(self.cf.get_option('b.a'), 'hullo')
107+
self.assertTrue(self.cf.get_option('b.b') is None)
108+
109+
self.cf.set_option('a', 2)
110+
self.cf.set_option('b.a', 'wurld')
111+
self.cf.set_option('b.b', 1.1)
112+
113+
self.assertEqual(self.cf.get_option('a'), 2)
114+
self.assertEqual(self.cf.get_option('b.a'), 'wurld')
115+
self.assertEqual(self.cf.get_option('b.b'), 1.1)
116+
117+
self.assertRaises(KeyError, self.cf.set_option, 'no.such.key', None)
118+
119+
def test_validation(self):
120+
self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)
121+
self.cf.register_option('b.a', 'hullo', 'doc2',
122+
validator=self.cf.is_text)
123+
self.assertRaises(ValueError, self.cf.register_option, 'a.b.c.d2',
124+
'NO', 'doc', validator=self.cf.is_int)
125+
126+
self.cf.set_option('a', 2) # int is_int
127+
self.cf.set_option('b.a', 'wurld') # str is_str
128+
129+
self.assertRaises(ValueError, self.cf.set_option, 'a', None) # None not is_int
130+
self.assertRaises(ValueError, self.cf.set_option, 'a', 'ab')
131+
self.assertRaises(ValueError, self.cf.set_option, 'b.a', 1)
132+
133+
def test_reset_option(self):
134+
self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)
135+
self.cf.register_option('b.a', 'hullo', 'doc2',
136+
validator=self.cf.is_str)
137+
self.assertEqual(self.cf.get_option('a'), 1)
138+
self.assertEqual(self.cf.get_option('b.a'), 'hullo')
139+
140+
self.cf.set_option('a', 2)
141+
self.cf.set_option('b.a', 'wurld')
142+
self.assertEqual(self.cf.get_option('a'), 2)
143+
self.assertEqual(self.cf.get_option('b.a'), 'wurld')
144+
145+
self.cf.reset_option('a')
146+
self.assertEqual(self.cf.get_option('a'), 1)
147+
self.assertEqual(self.cf.get_option('b.a'), 'wurld')
148+
self.cf.reset_option('b.a')
149+
self.assertEqual(self.cf.get_option('a'), 1)
150+
self.assertEqual(self.cf.get_option('b.a'), 'hullo')
151+
152+
def test_reset_options(self):
153+
self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)
154+
self.cf.register_option('b.a', 'hullo', 'doc2',
155+
validator=self.cf.is_str)
156+
self.assertEqual(self.cf.get_option('a'), 1)
157+
self.assertEqual(self.cf.get_option('b.a'), 'hullo')
158+
159+
self.cf.set_option('a', 2)
160+
self.cf.set_option('b.a', 'wurld')
161+
self.assertEqual(self.cf.get_option('a'), 2)
162+
self.assertEqual(self.cf.get_option('b.a'), 'wurld')
163+
164+
self.cf.reset_options()
165+
self.assertEqual(self.cf.get_option('a'), 1)
166+
self.assertEqual(self.cf.get_option('b.a'), 'hullo')
167+
168+
169+
def test_deprecate_option(self):
170+
import sys
171+
self.cf.deprecate_option('c') # we can deprecate non-existent options
172+
173+
# testing warning with catch_warning was only added in 2.6
174+
if sys.version_info[:2]<(2,6):
175+
raise nose.SkipTest()
176+
177+
self.assertTrue(self.cf._is_deprecated('c'))
178+
with warnings.catch_warnings(record=True) as w:
179+
warnings.simplefilter('always')
180+
try:
181+
self.cf.get_option('c')
182+
except KeyError:
183+
pass
184+
else:
185+
self.fail("Nonexistent option didn't raise KeyError")
186+
187+
self.assertEqual(len(w), 1) # should have raised one warning
188+
self.assertTrue('deprecated' in str(w[-1])) # we get the default message
189+
190+
self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)
191+
self.cf.register_option('b.a', 'hullo', 'doc2')
192+
self.cf.register_option('c', 'hullo', 'doc2')
193+
194+
self.cf.deprecate_option('a', removal_ver='nifty_ver')
195+
with warnings.catch_warnings(record=True) as w:
196+
warnings.simplefilter('always')
197+
self.cf.get_option('a')
198+
199+
self.assertEqual(len(w), 1) # should have raised one warning
200+
self.assertTrue('eprecated' in str(w[-1])) # we get the default message
201+
self.assertTrue('nifty_ver' in str(w[-1])) # with the removal_ver quoted
202+
203+
self.assertRaises(KeyError, self.cf.deprecate_option, 'a') # can't depr. twice
204+
205+
self.cf.deprecate_option('b.a', 'zounds!')
206+
with warnings.catch_warnings(record=True) as w:
207+
warnings.simplefilter('always')
208+
self.cf.get_option('b.a')
209+
210+
self.assertEqual(len(w), 1) # should have raised one warning
211+
self.assertTrue('zounds!' in str(w[-1])) # we get the custom message
212+
213+
# test rerouting keys
214+
self.cf.register_option('d.a', 'foo', 'doc2')
215+
self.cf.register_option('d.dep', 'bar', 'doc2')
216+
self.assertEqual(self.cf.get_option('d.a'), 'foo')
217+
self.assertEqual(self.cf.get_option('d.dep'), 'bar')
218+
219+
self.cf.deprecate_option('d.dep', rkey='d.a') # reroute d.dep to d.a
220+
with warnings.catch_warnings(record=True) as w:
221+
warnings.simplefilter('always')
222+
self.assertEqual(self.cf.get_option('d.dep'), 'foo')
223+
224+
self.assertEqual(len(w), 1) # should have raised one warning
225+
self.assertTrue('eprecated' in str(w[-1])) # we get the custom message
226+
227+
with warnings.catch_warnings(record=True) as w:
228+
warnings.simplefilter('always')
229+
self.cf.set_option('d.dep', 'baz') # should overwrite "d.a"
230+
231+
self.assertEqual(len(w), 1) # should have raised one warning
232+
self.assertTrue('eprecated' in str(w[-1])) # we get the custom message
233+
234+
with warnings.catch_warnings(record=True) as w:
235+
warnings.simplefilter('always')
236+
self.assertEqual(self.cf.get_option('d.dep'), 'baz')
237+
238+
self.assertEqual(len(w), 1) # should have raised one warning
239+
self.assertTrue('eprecated' in str(w[-1])) # we get the custom message
240+
241+
def test_config_prefix(self):
242+
with self.cf.config_prefix("base"):
243+
self.cf.register_option('a',1,"doc1")
244+
self.cf.register_option('b',2,"doc2")
245+
self.assertEqual(self.cf.get_option('a'), 1)
246+
self.assertEqual(self.cf.get_option('b'), 2)
247+
248+
self.cf.set_option('a',3)
249+
self.cf.set_option('b',4)
250+
self.assertEqual(self.cf.get_option('a'), 3)
251+
self.assertEqual(self.cf.get_option('b'), 4)
252+
253+
self.assertEqual(self.cf.get_option('base.a'), 3)
254+
self.assertEqual(self.cf.get_option('base.b'), 4)
255+
self.assertTrue('doc1' in self.cf.describe_options('base.a',_print_desc=False))
256+
self.assertTrue('doc2' in self.cf.describe_options('base.b',_print_desc=False))
257+
258+
self.cf.reset_option('base.a')
259+
self.cf.reset_option('base.b')
260+
261+
with self.cf.config_prefix("base"):
262+
self.assertEqual(self.cf.get_option('a'), 1)
263+
self.assertEqual(self.cf.get_option('b'), 2)
264+
265+
266+
# fmt.reset_printoptions and fmt.set_printoptions were altered
267+
# to use core.config, test_format exercises those paths.

0 commit comments

Comments
 (0)