test_data.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import sys
  2. import pytest
  3. import salt.utils.data
  4. def test_get_value_simple_path():
  5. data = {"a": {"b": {"c": "foo"}}}
  6. assert [{"value": "foo"}] == salt.utils.data.get_value(data, "a:b:c")
  7. @pytest.mark.skipif(
  8. sys.version_info < (3, 6),
  9. reason="Test will randomly fail since Python3.5 does not have ordered dictionaries",
  10. )
  11. def test_get_value_placeholder_dict():
  12. data = {"a": {"b": {"name": "foo"}, "c": {"name": "bar"}}}
  13. assert [
  14. {"value": "foo", "id": "b"},
  15. {"value": "bar", "id": "c"},
  16. ] == salt.utils.data.get_value(data, "a:{id}:name")
  17. @pytest.mark.skipif(
  18. sys.version_info < (3, 6),
  19. reason="Test will randomly fail since Python3.5 does not have ordered dictionaries",
  20. )
  21. def test_get_value_placeholder_list():
  22. data = {"a": [{"name": "foo"}, {"name": "bar"}]}
  23. assert [
  24. {"value": "foo", "id": 0},
  25. {"value": "bar", "id": 1},
  26. ] == salt.utils.data.get_value(data, "a:{id}:name")
  27. @pytest.mark.skipif(
  28. sys.version_info < (3, 6),
  29. reason="Test will randomly fail since Python3.5 does not have ordered dictionaries",
  30. )
  31. def test_get_value_nested_placeholder():
  32. data = {
  33. "a": {
  34. "b": {"b1": {"name": "foo1"}, "b2": {"name": "foo2"}},
  35. "c": {"c1": {"name": "bar"}},
  36. }
  37. }
  38. assert [
  39. {"value": "foo1", "id": "b", "sub": "b1"},
  40. {"value": "foo2", "id": "b", "sub": "b2"},
  41. {"value": "bar", "id": "c", "sub": "c1"},
  42. ] == salt.utils.data.get_value(data, "a:{id}:{sub}:name")
  43. def test_get_value_nested_notfound():
  44. data = {"a": {"b": {"c": "foo"}}}
  45. assert [{"value": []}] == salt.utils.data.get_value(data, "a:b:d", [])
  46. def test_get_value_not_found():
  47. assert [{"value": []}] == salt.utils.data.get_value({}, "a", [])
  48. def test_get_value_none():
  49. assert [{"value": None}] == salt.utils.data.get_value({"a": None}, "a")
  50. def test_get_value_simple_type_path():
  51. assert [{"value": []}] == salt.utils.data.get_value({"a": 1024}, "a:b", [])
  52. def test_get_value_None_path():
  53. assert [{"value": None}] == salt.utils.data.get_value({"a": None}, "a:b", [])