File: test_warning.py

package info (click to toggle)
python-skbio 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,924 kB
  • sloc: python: 67,527; ansic: 672; makefile: 225
file content (109 lines) | stat: -rw-r--r-- 3,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------

import unittest
import warnings

from skbio.util._warning import _warn_once, _warn_deprecated, _warn_param_deprecated


class TestWarning(unittest.TestCase):
    def test_warn_once(self):

        def foo(param):
            pass

        # function warning
        self.assertFalse(hasattr(foo, "_warned"))
        wtype = FutureWarning
        msg = "`foo` will become `bar` in 2.0."
        with self.assertWarns(wtype) as ctx:
            _warn_once(foo, wtype, msg)
        self.assertEqual(str(ctx.warning), msg)
        self.assertTrue(hasattr(foo, "_warned"))
        with self.assertRaises(AssertionError):
            self.assertWarns(wtype, _warn_once, foo, wtype, msg)

        # parameter warning
        self.assertFalse(hasattr(foo, "_warned_params"))
        wtype = DeprecationWarning
        msg = "`param` is deprecated as of 3.0."
        with self.assertWarns(wtype) as ctx:
            _warn_once(foo, wtype, msg, "param")
        self.assertEqual(str(ctx.warning), msg)
        self.assertIn("param", foo._warned_params)
        with self.assertRaises(AssertionError):
            self.assertWarns(wtype, _warn_once, foo, wtype, msg, "param")


    def test_warn_deprecated(self):

        def foo():
            pass

        self.assertFalse(hasattr(foo, "_warned"))
        with self.assertWarns(DeprecationWarning) as ctx:
            _warn_deprecated(foo)
        exp = "`foo` is deprecated."
        self.assertEqual(str(ctx.warning), exp)
        self.assertTrue(hasattr(foo, "_warned"))
        with self.assertRaises(AssertionError):
            self.assertWarns(DeprecationWarning, _warn_deprecated, foo)

        def foo():
            pass

        with self.assertWarns(DeprecationWarning) as ctx:
            _warn_deprecated(foo, ver="1.0")
        exp = "`foo` has been deprecated since 1.0."
        self.assertEqual(str(ctx.warning), exp)

        def foo():
            pass

        with self.assertWarns(DeprecationWarning) as ctx:
            _warn_deprecated(foo, ver="1.0", msg="Use `bar` instead.")
        exp = "`foo` has been deprecated since 1.0. Use `bar` instead."
        self.assertEqual(str(ctx.warning), exp)

        def foo():
            pass

        msg="Use `bar` instead of `foo`."
        with self.assertWarns(DeprecationWarning) as ctx:
            _warn_deprecated(foo, ver="1.0", msg=msg, append=False)
        self.assertEqual(str(ctx.warning), msg)

    def test_warn_param_deprecated(self):

        def foo(param1, param2):
            pass

        self.assertFalse(hasattr(foo, "_warned_params"))

        # parameter 1
        with self.assertWarns(DeprecationWarning) as ctx:
            _warn_param_deprecated(foo, "param1")
        exp = "`foo`'s parameter `param1` is deprecated."
        self.assertEqual(str(ctx.warning), exp)
        self.assertIn("param1", foo._warned_params)
        with self.assertRaises(AssertionError):
            self.assertWarns(DeprecationWarning, _warn_param_deprecated, foo, "param1")

        # parameter 2
        with self.assertWarns(DeprecationWarning) as ctx:
            _warn_param_deprecated(foo, "param2", ver="1.0")
        exp = "`foo`'s parameter `param2` has been deprecated since 1.0."
        self.assertEqual(str(ctx.warning), exp)
        self.assertIn("param2", foo._warned_params)
        with self.assertRaises(AssertionError):
            self.assertWarns(DeprecationWarning, _warn_param_deprecated, foo, "param2")


if __name__ == '__main__':
    unittest.main()