def test_serialize_sls(self): data = {"foo": "bar"} serialized = yamlex.serialize(data) assert serialized == '{foo: bar}', serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized
def test_serialize_sls(self): data = { "foo": "bar" } serialized = yamlex.serialize(data) assert serialized == '{foo: bar}', serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized
def test_serialize_complex_sls(self): data = OrderedDict([ ("foo", 1), ("bar", 2), ("baz", True), ]) serialized = yamlex.serialize(data) assert serialized == '{foo: 1, bar: 2, baz: true}', serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized
def test_serialize_sls(self): data = {"foo": "bar"} serialized = yamlex.serialize(data) assert serialized == "{foo: bar}", serialized serialized = yamlex.serialize(data, default_flow_style=False) assert serialized == "foo: bar", serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized serialized = yaml.serialize(data) assert serialized == "{foo: bar}", serialized deserialized = yaml.deserialize(serialized) assert deserialized == data, deserialized serialized = yaml.serialize(data, default_flow_style=False) assert serialized == "foo: bar", serialized deserialized = yaml.deserialize(serialized) assert deserialized == data, deserialized
def test_serialize_complex_sls(): data = OrderedDict([("foo", 1), ("bar", 2), ("baz", True)]) serialized = yamlex.serialize(data) assert serialized == "{foo: 1, bar: 2, baz: true}", serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized serialized = yaml.serialize(data) assert serialized == "{bar: 2, baz: true, foo: 1}", serialized deserialized = yaml.deserialize(serialized) assert deserialized == data, deserialized
def test_compare_sls_vs_yaml_with_jinja(self): tpl = '{{ data }}' env = jinja2.Environment() src = '{foo: 1, bar: 2, baz: {qux: true}}' sls_src = env.from_string(tpl).render(data=yamlex.deserialize(src)) yml_src = env.from_string(tpl).render(data=yaml.deserialize(src)) sls_data = yamlex.deserialize(sls_src) yml_data = yaml.deserialize(yml_src) # ensure that sls & yaml have the same base assert isinstance(sls_data, dict) assert isinstance(yml_data, dict) # The below has been commented out because something the loader test # is modifying the yaml renderer to render things to unicode. Without # running the loader test, the below passes. Even reloading the module # from disk does not reset its internal state (per the Python docs). ## #assert sls_data == yml_data # ensure that sls is ordered, while yaml not assert isinstance(sls_data, OrderedDict) assert not isinstance(yml_data, OrderedDict) # prove that yaml does not handle well with OrderedDict # while sls is jinja friendly. obj = OrderedDict([ ('foo', 1), ('bar', 2), ('baz', {'qux': True}) ]) sls_obj = yamlex.deserialize(yamlex.serialize(obj)) try: yml_obj = yaml.deserialize(yaml.serialize(obj)) except SerializationError: # BLAAM! yaml was unable to serialize OrderedDict, # but it's not the purpose of the current test. yml_obj = obj.copy() sls_src = env.from_string(tpl).render(data=sls_obj) yml_src = env.from_string(tpl).render(data=yml_obj) final_obj = yaml.deserialize(sls_src) assert obj == final_obj # BLAAM! yml_src is not valid ! final_obj = OrderedDict(yaml.deserialize(yml_src)) assert obj != final_obj
def test_compare_sls_vs_yaml_with_jinja(self): tpl = '{{ data }}' env = jinja2.Environment() src = '{foo: 1, bar: 2, baz: {qux: true}}' sls_src = env.from_string(tpl).render(data=yamlex.deserialize(src)) yml_src = env.from_string(tpl).render(data=yaml.deserialize(src)) sls_data = yamlex.deserialize(sls_src) yml_data = yaml.deserialize(yml_src) # ensure that sls & yaml have the same base assert isinstance(sls_data, dict) assert isinstance(yml_data, dict) assert sls_data == yml_data # ensure that sls is ordered, while yaml not assert isinstance(sls_data, OrderedDict) assert not isinstance(yml_data, OrderedDict) # prove that yaml does not handle well with OrderedDict # while sls is jinja friendly. obj = OrderedDict([ ('foo', 1), ('bar', 2), ('baz', {'qux': True}) ]) sls_obj = yamlex.deserialize(yamlex.serialize(obj)) try: yml_obj = yaml.deserialize(yaml.serialize(obj)) except SerializationError: # BLAAM! yaml was unable to serialize OrderedDict, # but it's not the purpose of the current test. yml_obj = obj.copy() sls_src = env.from_string(tpl).render(data=sls_obj) yml_src = env.from_string(tpl).render(data=yml_obj) final_obj = yaml.deserialize(sls_src) assert obj == final_obj # BLAAM! yml_src is not valid ! final_obj = OrderedDict(yaml.deserialize(yml_src)) assert obj != final_obj
def convert(obj): return yamlex.deserialize(yamlex.serialize(obj))
def test_sls_aggregate(self): src = dedent(""" a: lol foo: !aggregate hello bar: !aggregate [1, 2, 3] baz: !aggregate a: 42 b: 666 c: the beast """).strip() # test that !aggregate is correctly parsed sls_obj = yamlex.deserialize(src) assert sls_obj == { 'a': 'lol', 'foo': ['hello'], 'bar': [1, 2, 3], 'baz': { 'a': 42, 'b': 666, 'c': 'the beast' } }, sls_obj assert dedent(""" a: lol foo: [hello] bar: [1, 2, 3] baz: {a: 42, b: 666, c: the beast} """).strip() == yamlex.serialize(sls_obj), sls_obj # test that !aggregate aggregates scalars src = dedent(""" placeholder: !aggregate foo placeholder: !aggregate bar placeholder: !aggregate baz """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {'placeholder': ['foo', 'bar', 'baz']}, sls_obj # test that !aggregate aggregates lists src = dedent(""" placeholder: !aggregate foo placeholder: !aggregate [bar, baz] placeholder: !aggregate [] placeholder: !aggregate ~ """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {'placeholder': ['foo', 'bar', 'baz']}, sls_obj # test that !aggregate aggregates dicts src = dedent(""" placeholder: !aggregate {foo: 42} placeholder: !aggregate {bar: null} placeholder: !aggregate {baz: inga} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { 'placeholder': { 'foo': 42, 'bar': None, 'baz': 'inga' } }, sls_obj # test that !aggregate aggregates deep dicts src = dedent(""" placeholder: {foo: !aggregate {foo: 42}} placeholder: {foo: !aggregate {bar: null}} placeholder: {foo: !aggregate {baz: inga}} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { 'placeholder': { 'foo': { 'foo': 42, 'bar': None, 'baz': 'inga' } } }, sls_obj # test that {foo: !aggregate bar} and {!aggregate foo: bar} # are roughly equivalent. src = dedent(""" placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} placeholder: {!aggregate foo: {baz: inga}} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { 'placeholder': { 'foo': { 'foo': 42, 'bar': None, 'baz': 'inga' } } }, sls_obj
def test_sls_aggregate(self): src = dedent( """ a: lol foo: !aggregate hello bar: !aggregate [1, 2, 3] baz: !aggregate a: 42 b: 666 c: the beast """ ).strip() # test that !aggregate is correctly parsed sls_obj = yamlex.deserialize(src) assert sls_obj == { "a": "lol", "foo": ["hello"], "bar": [1, 2, 3], "baz": {"a": 42, "b": 666, "c": "the beast"}, }, sls_obj assert ( dedent( """ a: lol foo: [hello] bar: [1, 2, 3] baz: {a: 42, b: 666, c: the beast} """ ).strip() == yamlex.serialize(sls_obj) ), sls_obj # test that !aggregate aggregates scalars src = dedent( """ placeholder: !aggregate foo placeholder: !aggregate bar placeholder: !aggregate baz """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {"placeholder": ["foo", "bar", "baz"]}, sls_obj # test that !aggregate aggregates lists src = dedent( """ placeholder: !aggregate foo placeholder: !aggregate [bar, baz] placeholder: !aggregate [] placeholder: !aggregate ~ """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {"placeholder": ["foo", "bar", "baz"]}, sls_obj # test that !aggregate aggregates dicts src = dedent( """ placeholder: !aggregate {foo: 42} placeholder: !aggregate {bar: null} placeholder: !aggregate {baz: inga} """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { "placeholder": {"foo": 42, "bar": None, "baz": "inga"} }, sls_obj # test that !aggregate aggregates deep dicts src = dedent( """ placeholder: {foo: !aggregate {foo: 42}} placeholder: {foo: !aggregate {bar: null}} placeholder: {foo: !aggregate {baz: inga}} """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { "placeholder": {"foo": {"foo": 42, "bar": None, "baz": "inga"}} }, sls_obj # test that {foo: !aggregate bar} and {!aggregate foo: bar} # are roughly equivalent. src = dedent( """ placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} placeholder: {!aggregate foo: {baz: inga}} """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { "placeholder": {"foo": {"foo": 42, "bar": None, "baz": "inga"}} }, sls_obj