1
0

test_aggregation.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.unit import TestCase
  6. # Import salt libs
  7. from salt.utils.aggregation import aggregate, Map, Scalar
  8. class TestAggregation(TestCase):
  9. def test_merging(self):
  10. a = {
  11. 'foo': 42,
  12. 'bar': 'first'
  13. }
  14. b = {
  15. 'bar': 'second'
  16. }
  17. c, d = 'first', 'second'
  18. # introspection
  19. for level in (None, False, 0, []):
  20. assert aggregate(a, b, level=level) == {
  21. 'bar': 'second'
  22. }
  23. assert aggregate(c, d, level=level) == 'second'
  24. # first level aggregation
  25. for level in (1, [1, 0], [True], '10000'):
  26. assert aggregate(a, b, level=level) == {
  27. 'foo': 42,
  28. 'bar': 'second'
  29. }
  30. assert aggregate(c, d, level=level) == ['first', 'second']
  31. # 1-2nd level aggregation
  32. for level in (2, [1, 1], [True, True], '11'):
  33. assert aggregate(a, b, level=level) == {
  34. 'foo': 42,
  35. 'bar': ['first', 'second']
  36. }, aggregate(a, b, level=2)
  37. assert aggregate(c, d, level=level) == ['first', 'second']
  38. # full aggregation
  39. for level in (True,):
  40. assert aggregate(a, b, level=level) == {
  41. 'foo': 42,
  42. 'bar': ['first', 'second']
  43. }
  44. assert aggregate(c, d, level=level) == ['first', 'second']
  45. def test_nested(self):
  46. a = {
  47. 'foo': {
  48. 'bar': 'first'
  49. }
  50. }
  51. b = {
  52. 'foo': {
  53. 'bar': 'second'
  54. }
  55. }
  56. assert aggregate(a, b) == {
  57. 'foo': {
  58. 'bar': 'second'
  59. }
  60. }, aggregate(a, b)
  61. a = {
  62. 'foo': {
  63. 'bar': Scalar('first')
  64. }
  65. }
  66. b = {
  67. 'foo': {
  68. 'bar': Scalar('second'),
  69. }
  70. }
  71. assert aggregate(a, b) == {
  72. 'foo': {
  73. 'bar': ['first', 'second']
  74. }
  75. }, aggregate(a, b)
  76. def test_introspection(self):
  77. a = {
  78. 'foo': {
  79. 'lvl1': {
  80. 'lvl2-a': 'first',
  81. 'lvl2-b': 'first'
  82. }
  83. }
  84. }
  85. b = {
  86. 'foo': {
  87. 'lvl1': {
  88. 'lvl2-a': 'second'
  89. }
  90. }
  91. }
  92. assert aggregate(a, b) == {
  93. 'foo': {
  94. 'lvl1': {
  95. 'lvl2-a': 'second'
  96. }
  97. }
  98. }, aggregate(a, b)
  99. def test_instruction(self):
  100. a = {
  101. 'foo': Map({
  102. 'bar': Scalar('first')
  103. })
  104. }
  105. b = {
  106. 'foo': Map({
  107. 'bar': Scalar('second')
  108. })
  109. }
  110. c = {
  111. 'foo': Map({
  112. 'another': 'value'
  113. })
  114. }
  115. result = aggregate(c, aggregate(a, b), level=2)
  116. assert result == {
  117. 'foo': {
  118. 'bar': ['first', 'second'],
  119. 'another': 'value'
  120. }
  121. }, result