mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge remote-tracking branch 'saltstack/3006.x' into merge/3007.x/3006.x
This commit is contained in:
commit
c38448909c
2 changed files with 123 additions and 118 deletions
123
tests/pytests/unit/utils/test_yamldumper.py
Normal file
123
tests/pytests/unit/utils/test_yamldumper.py
Normal file
|
@ -0,0 +1,123 @@
|
|||
"""
|
||||
Unit tests for salt.utils.yamldumper
|
||||
"""
|
||||
|
||||
from collections import OrderedDict, defaultdict
|
||||
|
||||
import salt.utils.yamldumper
|
||||
from salt.utils.context import NamespacedDictWrapper
|
||||
from salt.utils.odict import HashableOrderedDict
|
||||
|
||||
|
||||
def test_yaml_dump():
|
||||
"""
|
||||
Test yaml.dump a dict
|
||||
"""
|
||||
data = {"foo": "bar"}
|
||||
exp_yaml = "{foo: bar}\n"
|
||||
|
||||
assert salt.utils.yamldumper.dump(data) == exp_yaml
|
||||
|
||||
assert salt.utils.yamldumper.dump(
|
||||
data, default_flow_style=False
|
||||
) == exp_yaml.replace("{", "").replace("}", "")
|
||||
|
||||
|
||||
def test_yaml_safe_dump():
|
||||
"""
|
||||
Test yaml.safe_dump a dict
|
||||
"""
|
||||
data = {"foo": "bar"}
|
||||
assert salt.utils.yamldumper.safe_dump(data) == "{foo: bar}\n"
|
||||
|
||||
assert (
|
||||
salt.utils.yamldumper.safe_dump(data, default_flow_style=False) == "foo: bar\n"
|
||||
)
|
||||
|
||||
|
||||
def test_yaml_ordered_dump():
|
||||
"""
|
||||
Test yaml.dump with OrderedDict
|
||||
"""
|
||||
data = OrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||
assert (
|
||||
salt.utils.yamldumper.dump(data, Dumper=salt.utils.yamldumper.OrderedDumper)
|
||||
== exp_yaml
|
||||
)
|
||||
|
||||
|
||||
def test_yaml_safe_ordered_dump():
|
||||
"""
|
||||
Test yaml.safe_dump with OrderedDict
|
||||
"""
|
||||
data = OrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||
|
||||
|
||||
def test_yaml_indent_safe_ordered_dump():
|
||||
"""
|
||||
Test yaml.dump with IndentedSafeOrderedDumper
|
||||
"""
|
||||
data = OrderedDict([("foo", ["bar", "baz"]), ("qux", "quux")])
|
||||
# Account for difference in SafeDumper vs CSafeDumper
|
||||
if salt.utils.yamldumper.SafeDumper.__name__ == "SafeDumper":
|
||||
exp_yaml = "foo:\n - bar\n - baz\nqux: quux\n"
|
||||
else:
|
||||
exp_yaml = "foo:\n- bar\n- baz\nqux: quux\n"
|
||||
assert (
|
||||
salt.utils.yamldumper.dump(
|
||||
data,
|
||||
Dumper=salt.utils.yamldumper.IndentedSafeOrderedDumper,
|
||||
default_flow_style=False,
|
||||
)
|
||||
== exp_yaml
|
||||
)
|
||||
|
||||
|
||||
def test_yaml_defaultdict_dump():
|
||||
"""
|
||||
Test yaml.dump with defaultdict
|
||||
"""
|
||||
data = defaultdict(list)
|
||||
data["foo"].append("bar")
|
||||
exp_yaml = "foo: [bar]\n"
|
||||
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||
|
||||
|
||||
def test_yaml_namespaced_dict_wrapper_dump():
|
||||
"""
|
||||
Test yaml.dump with NamespacedDictWrapper
|
||||
"""
|
||||
data = NamespacedDictWrapper({"test": {"foo": "bar"}}, "test")
|
||||
exp_yaml = (
|
||||
"!!python/object/new:salt.utils.context.NamespacedDictWrapper\n"
|
||||
"dictitems: {foo: bar}\n"
|
||||
"state:\n"
|
||||
" _NamespacedDictWrapper__dict:\n"
|
||||
" test: {foo: bar}\n"
|
||||
" pre_keys: !!python/tuple [test]\n"
|
||||
)
|
||||
assert salt.utils.yamldumper.dump(data) == exp_yaml
|
||||
|
||||
|
||||
def test_yaml_undefined_dump():
|
||||
"""
|
||||
Test yaml.safe_dump with None
|
||||
"""
|
||||
data = {"foo": None}
|
||||
exp_yaml = "{foo: null}\n"
|
||||
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||
|
||||
|
||||
def test_yaml_hashable_ordered_dict_dump():
|
||||
"""
|
||||
Test yaml.dump with HashableOrderedDict
|
||||
"""
|
||||
data = HashableOrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||
assert (
|
||||
salt.utils.yamldumper.dump(data, Dumper=salt.utils.yamldumper.OrderedDumper)
|
||||
== exp_yaml
|
||||
)
|
|
@ -1,118 +0,0 @@
|
|||
"""
|
||||
Unit tests for salt.utils.yamldumper
|
||||
"""
|
||||
|
||||
from collections import OrderedDict, defaultdict
|
||||
|
||||
import salt.utils.yamldumper
|
||||
from salt.utils.context import NamespacedDictWrapper
|
||||
from salt.utils.odict import HashableOrderedDict
|
||||
from tests.support.unit import TestCase
|
||||
|
||||
|
||||
class YamlDumperTestCase(TestCase):
|
||||
"""
|
||||
TestCase for salt.utils.yamldumper module
|
||||
"""
|
||||
|
||||
def test_yaml_dump(self):
|
||||
"""
|
||||
Test yaml.dump a dict
|
||||
"""
|
||||
data = {"foo": "bar"}
|
||||
exp_yaml = "{foo: bar}\n"
|
||||
|
||||
assert salt.utils.yamldumper.dump(data) == exp_yaml
|
||||
|
||||
assert salt.utils.yamldumper.dump(
|
||||
data, default_flow_style=False
|
||||
) == exp_yaml.replace("{", "").replace("}", "")
|
||||
|
||||
def test_yaml_safe_dump(self):
|
||||
"""
|
||||
Test yaml.safe_dump a dict
|
||||
"""
|
||||
data = {"foo": "bar"}
|
||||
assert salt.utils.yamldumper.safe_dump(data) == "{foo: bar}\n"
|
||||
|
||||
assert (
|
||||
salt.utils.yamldumper.safe_dump(data, default_flow_style=False)
|
||||
== "foo: bar\n"
|
||||
)
|
||||
|
||||
def test_yaml_ordered_dump(self):
|
||||
"""
|
||||
Test yaml.dump with OrderedDict
|
||||
"""
|
||||
data = OrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||
assert (
|
||||
salt.utils.yamldumper.dump(data, Dumper=salt.utils.yamldumper.OrderedDumper)
|
||||
== exp_yaml
|
||||
)
|
||||
|
||||
def test_yaml_safe_ordered_dump(self):
|
||||
"""
|
||||
Test yaml.safe_dump with OrderedDict
|
||||
"""
|
||||
data = OrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||
|
||||
def test_yaml_indent_safe_ordered_dump(self):
|
||||
"""
|
||||
Test yaml.dump with IndentedSafeOrderedDumper
|
||||
"""
|
||||
data = OrderedDict([("foo", ["bar", "baz"]), ("qux", "quux")])
|
||||
exp_yaml = "foo:\n- bar\n- baz\nqux: quux\n"
|
||||
assert (
|
||||
salt.utils.yamldumper.dump(
|
||||
data,
|
||||
Dumper=salt.utils.yamldumper.IndentedSafeOrderedDumper,
|
||||
default_flow_style=False,
|
||||
)
|
||||
== exp_yaml
|
||||
)
|
||||
|
||||
def test_yaml_defaultdict_dump(self):
|
||||
"""
|
||||
Test yaml.dump with defaultdict
|
||||
"""
|
||||
data = defaultdict(list)
|
||||
data["foo"].append("bar")
|
||||
exp_yaml = "foo: [bar]\n"
|
||||
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||
|
||||
def test_yaml_namespaced_dict_wrapper_dump(self):
|
||||
"""
|
||||
Test yaml.dump with NamespacedDictWrapper
|
||||
"""
|
||||
data = NamespacedDictWrapper({"test": {"foo": "bar"}}, "test")
|
||||
exp_yaml = (
|
||||
"!!python/object/new:salt.utils.context.NamespacedDictWrapper\n"
|
||||
"dictitems: {foo: bar}\n"
|
||||
"state:\n"
|
||||
" _NamespacedDictWrapper__dict:\n"
|
||||
" test: {foo: bar}\n"
|
||||
" pre_keys: !!python/tuple [test]\n"
|
||||
)
|
||||
assert salt.utils.yamldumper.dump(data) == exp_yaml
|
||||
|
||||
def test_yaml_undefined_dump(self):
|
||||
"""
|
||||
Test yaml.safe_dump with None
|
||||
"""
|
||||
data = {"foo": None}
|
||||
exp_yaml = "{foo: null}\n"
|
||||
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||
|
||||
def test_yaml_hashable_ordered_dict_dump(self):
|
||||
"""
|
||||
Test yaml.dump with HashableOrderedDict
|
||||
"""
|
||||
data = HashableOrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||
assert (
|
||||
salt.utils.yamldumper.dump(data, Dumper=salt.utils.yamldumper.OrderedDumper)
|
||||
== exp_yaml
|
||||
)
|
Loading…
Add table
Reference in a new issue