File: test_dump.py

package info (click to toggle)
python-confuse 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 652 kB
  • sloc: python: 3,065; makefile: 112; sh: 8
file content (108 lines) | stat: -rw-r--r-- 3,275 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
import textwrap
import unittest
from collections import OrderedDict

import confuse

from . import _root


class PrettyDumpTest(unittest.TestCase):
    def test_dump_null(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": None})
        yaml = config.dump().strip()
        assert yaml == "foo:"

    def test_dump_true(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": True})
        yaml = config.dump().strip()
        assert yaml == "foo: yes"

    def test_dump_false(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": False})
        yaml = config.dump().strip()
        assert yaml == "foo: no"

    def test_dump_short_list(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": ["bar", "baz"]})
        yaml = config.dump().strip()
        assert yaml == "foo: [bar, baz]"

    def test_dump_ordered_dict(self):
        odict = OrderedDict()
        odict["foo"] = "bar"
        odict["bar"] = "baz"
        odict["baz"] = "qux"

        config = confuse.Configuration("myapp", read=False)
        config.add({"key": odict})
        yaml = config.dump().strip()
        assert (
            yaml
            == textwrap.dedent("""
            key:
                foo: bar
                bar: baz
                baz: qux
        """).strip()
        )

    def test_dump_sans_defaults(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": "bar"})
        config.sources[0].default = True
        config.add({"baz": "qux"})

        yaml = config.dump().strip()
        assert yaml == "foo: bar\nbaz: qux"

        yaml = config.dump(full=False).strip()
        assert yaml == "baz: qux"


class RedactTest(unittest.TestCase):
    def test_no_redaction(self):
        config = _root({"foo": "bar"})
        data = config.flatten(redact=True)
        assert data == {"foo": "bar"}

    def test_redact_key(self):
        config = _root({"foo": "bar"})
        config["foo"].redact = True
        data = config.flatten(redact=True)
        assert data == {"foo": "REDACTED"}

    def test_unredact(self):
        config = _root({"foo": "bar"})
        config["foo"].redact = True
        config["foo"].redact = False
        data = config.flatten(redact=True)
        assert data == {"foo": "bar"}

    def test_dump_redacted(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": "bar"})
        config["foo"].redact = True
        yaml = config.dump(redact=True).strip()
        assert yaml == "foo: REDACTED"

    def test_dump_unredacted(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": "bar"})
        config["foo"].redact = True
        yaml = config.dump(redact=False).strip()
        assert yaml == "foo: bar"

    def test_dump_redacted_sans_defaults(self):
        config = confuse.Configuration("myapp", read=False)
        config.add({"foo": "bar"})
        config.sources[0].default = True
        config.add({"baz": "qux"})
        config["baz"].redact = True

        yaml = config.dump(redact=True, full=False).strip()
        assert yaml == "baz: REDACTED"