|
2 | 2 | import nose
|
3 | 3 |
|
4 | 4 | from pandas.util.decorators import deprecate_kwarg
|
| 5 | +from pandas.util.validators import validate_args, validate_kwargs |
| 6 | + |
5 | 7 | import pandas.util.testing as tm
|
6 | 8 |
|
7 | 9 |
|
@@ -73,6 +75,81 @@ def test_rands_array():
|
73 | 75 | assert(arr.shape == (10, 10))
|
74 | 76 | assert(len(arr[1, 1]) == 7)
|
75 | 77 |
|
| 78 | + |
| 79 | +class TestValidateArgs(tm.TestCase): |
| 80 | + |
| 81 | + def test_bad_min_length(self): |
| 82 | + msg = "'min_length' must be non-negative" |
| 83 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 84 | + validate_args((None,), min_length=-1, max_length=5) |
| 85 | + |
| 86 | + def test_bad_arg_length_no_max(self): |
| 87 | + min_length = 5 |
| 88 | + msg = "expected at least {min_length} arguments".format( |
| 89 | + min_length=min_length) |
| 90 | + |
| 91 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 92 | + validate_args((None,), min_length=min_length, max_length=None) |
| 93 | + |
| 94 | + def test_bad_arg_length_with_max(self): |
| 95 | + min_length = 5 |
| 96 | + max_length = 10 |
| 97 | + msg = ("expected between {min_length} and {max_length}" |
| 98 | + " arguments inclusive".format(min_length=min_length, |
| 99 | + max_length=max_length)) |
| 100 | + |
| 101 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 102 | + validate_args((None,), min_length=min_length, |
| 103 | + max_length=max_length) |
| 104 | + |
| 105 | + def test_bad_min_max_length(self): |
| 106 | + msg = "'min_length' > 'max_length'" |
| 107 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 108 | + validate_args((None,), min_length=5, max_length=2) |
| 109 | + |
| 110 | + def test_not_all_none(self): |
| 111 | + msg = "All arguments must be None" |
| 112 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 113 | + validate_args(('foo',), min_length=0, |
| 114 | + max_length=1, msg=msg) |
| 115 | + |
| 116 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 117 | + validate_args(('foo', 'bar', 'baz'), min_length=2, |
| 118 | + max_length=5, msg=msg) |
| 119 | + |
| 120 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 121 | + validate_args((None, 'bar', None), min_length=2, |
| 122 | + max_length=5, msg=msg) |
| 123 | + |
| 124 | + def test_validation(self): |
| 125 | + # No exceptions should be thrown |
| 126 | + validate_args((None,), min_length=0, max_length=1) |
| 127 | + validate_args((None, None), min_length=1, max_length=5) |
| 128 | + |
| 129 | + |
| 130 | +class TestValidateKwargs(tm.TestCase): |
| 131 | + |
| 132 | + def test_bad_kwarg(self): |
| 133 | + goodarg = 'f' |
| 134 | + badarg = goodarg + 'o' |
| 135 | + |
| 136 | + kwargs = {goodarg: 'foo', badarg: 'bar'} |
| 137 | + compat_args = (goodarg, badarg + 'o') |
| 138 | + fname = 'func' |
| 139 | + |
| 140 | + msg = ("{fname}\(\) got an unexpected " |
| 141 | + "keyword argument '{arg}'".format( |
| 142 | + fname=fname, arg=badarg)) |
| 143 | + |
| 144 | + with tm.assertRaisesRegexp(TypeError, msg): |
| 145 | + validate_kwargs(fname, kwargs, *compat_args) |
| 146 | + |
| 147 | + def test_validation(self): |
| 148 | + # No exceptions should be thrown |
| 149 | + compat_args = ('f', 'b', 'ba') |
| 150 | + kwargs = {'f': 'foo', 'b': 'bar'} |
| 151 | + validate_kwargs('func', kwargs, *compat_args) |
| 152 | + |
76 | 153 | if __name__ == '__main__':
|
77 | 154 | nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
|
78 | 155 | exit=False)
|
0 commit comments