test_arguments.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import arguments as module
  4. def test_set_values_without_keys_bails():
  5. config = {'option': 'value'}
  6. module.set_values(config=config, keys=(), value=5)
  7. assert config == {'option': 'value'}
  8. def test_set_values_with_keys_adds_them_to_config():
  9. config = {'option': 'value'}
  10. module.set_values(config=config, keys=('foo', 'bar', 'baz'), value=5)
  11. assert config == {'option': 'value', 'foo': {'bar': {'baz': 5}}}
  12. def test_set_values_with_one_existing_key_adds_others_to_config():
  13. config = {'foo': {'other': 'value'}}
  14. module.set_values(config=config, keys=('foo', 'bar', 'baz'), value=5)
  15. assert config == {'foo': {'other': 'value', 'bar': {'baz': 5}}}
  16. def test_set_values_with_two_existing_keys_adds_others_to_config():
  17. config = {'foo': {'bar': {'other': 'value'}}}
  18. module.set_values(config=config, keys=('foo', 'bar', 'baz'), value=5)
  19. assert config == {'foo': {'bar': {'other': 'value', 'baz': 5}}}
  20. def test_set_values_with_list_index_key_adds_it_to_config():
  21. config = {'foo': {'bar': [{'option': 'value'}, {'other': 'thing'}]}}
  22. module.set_values(config=config, keys=('foo', 'bar[1]', 'baz'), value=5)
  23. assert config == {'foo': {'bar': [{'option': 'value'}, {'other': 'thing', 'baz': 5}]}}
  24. def test_set_values_with_list_index_key_out_of_range_raises():
  25. config = {'foo': {'bar': [{'option': 'value'}]}}
  26. with pytest.raises(ValueError):
  27. module.set_values(config=config, keys=('foo', 'bar[1]', 'baz'), value=5)
  28. def test_set_values_with_list_index_key_missing_list_and_out_of_range_raises():
  29. config = {'other': 'value'}
  30. with pytest.raises(ValueError):
  31. module.set_values(config=config, keys=('foo', 'bar[1]', 'baz'), value=5)
  32. def test_set_values_with_final_list_index_key_adds_it_to_config():
  33. config = {'foo': {'bar': [1, 2]}}
  34. module.set_values(config=config, keys=('foo', 'bar[1]'), value=5)
  35. assert config == {'foo': {'bar': [1, 5]}}
  36. def test_type_for_option_with_option_finds_type():
  37. assert (
  38. module.type_for_option(
  39. schema={'type': 'object', 'properties': {'foo': {'type': 'integer'}}},
  40. option_keys=('foo',),
  41. )
  42. == 'integer'
  43. )
  44. def test_type_for_option_with_nested_option_finds_type():
  45. assert (
  46. module.type_for_option(
  47. schema={
  48. 'type': 'object',
  49. 'properties': {
  50. 'foo': {'type': 'object', 'properties': {'bar': {'type': 'boolean'}}}
  51. },
  52. },
  53. option_keys=('foo', 'bar'),
  54. )
  55. == 'boolean'
  56. )
  57. def test_type_for_option_with_missing_nested_option_finds_nothing():
  58. assert (
  59. module.type_for_option(
  60. schema={
  61. 'type': 'object',
  62. 'properties': {
  63. 'foo': {'type': 'object', 'properties': {'other': {'type': 'integer'}}}
  64. },
  65. },
  66. option_keys=('foo', 'bar'),
  67. )
  68. is None
  69. )
  70. def test_type_for_option_with_typeless_nested_option_finds_nothing():
  71. assert (
  72. module.type_for_option(
  73. schema={
  74. 'type': 'object',
  75. 'properties': {'foo': {'type': 'object', 'properties': {'bar': {'example': 5}}}},
  76. },
  77. option_keys=('foo', 'bar'),
  78. )
  79. is None
  80. )
  81. def test_type_for_list_index_option_finds_type():
  82. assert (
  83. module.type_for_option(
  84. schema={
  85. 'type': 'object',
  86. 'properties': {'foo': {'type': 'array', 'items': {'type': 'integer'}}},
  87. },
  88. option_keys=('foo[0]',),
  89. )
  90. == 'integer'
  91. )
  92. def test_type_for_nested_list_index_option_finds_type():
  93. assert (
  94. module.type_for_option(
  95. schema={
  96. 'type': 'object',
  97. 'properties': {
  98. 'foo': {
  99. 'type': 'array',
  100. 'items': {'type': 'object', 'properties': {'bar': {'type': 'integer'}}},
  101. }
  102. },
  103. },
  104. option_keys=('foo[0]', 'bar'),
  105. )
  106. == 'integer'
  107. )
  108. def test_prepare_arguments_for_config_converts_arguments_to_keys():
  109. assert module.prepare_arguments_for_config(
  110. global_arguments=flexmock(**{'my_option.sub_option': 'value1', 'other_option': 'value2'}),
  111. schema={
  112. 'type': 'object',
  113. 'properties': {
  114. 'my_option': {'type': 'object', 'properties': {'sub_option': {'type': 'string'}}},
  115. 'other_option': {'type': 'string'},
  116. },
  117. },
  118. ) == (
  119. (('my_option', 'sub_option'), 'value1'),
  120. (('other_option',), 'value2'),
  121. )
  122. def test_prepare_arguments_for_config_skips_option_with_none_value():
  123. assert module.prepare_arguments_for_config(
  124. global_arguments=flexmock(**{'my_option.sub_option': None, 'other_option': 'value2'}),
  125. schema={
  126. 'type': 'object',
  127. 'properties': {
  128. 'my_option': {'type': 'object', 'properties': {'sub_option': {'type': 'string'}}},
  129. 'other_option': {'type': 'string'},
  130. },
  131. },
  132. ) == ((('other_option',), 'value2'),)
  133. def test_prepare_arguments_for_config_skips_option_missing_from_schema():
  134. assert module.prepare_arguments_for_config(
  135. global_arguments=flexmock(**{'my_option.sub_option': 'value1', 'other_option': 'value2'}),
  136. schema={
  137. 'type': 'object',
  138. 'properties': {
  139. 'my_option': {'type': 'object'},
  140. 'other_option': {'type': 'string'},
  141. },
  142. },
  143. ) == ((('other_option',), 'value2'),)
  144. def test_apply_arguments_to_config_does_not_raise():
  145. flexmock(module).should_receive('prepare_arguments_for_config').and_return(
  146. (
  147. (('foo', 'bar'), 'baz'),
  148. (('one', 'two'), 'three'),
  149. )
  150. )
  151. flexmock(module).should_receive('set_values')
  152. module.apply_arguments_to_config(config={}, schema={}, arguments={'global': flexmock()})