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"
