test_generate.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. from collections import OrderedDict
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.config import generate as module
  5. def test_get_properties_with_simple_object():
  6. schema = {
  7. 'type': 'object',
  8. 'properties': OrderedDict(
  9. [
  10. ('field1', {'example': 'Example'}),
  11. ]
  12. ),
  13. }
  14. assert module.get_properties(schema) == schema['properties']
  15. def test_get_properties_merges_one_of_list_properties():
  16. schema = {
  17. 'type': 'object',
  18. 'oneOf': [
  19. {
  20. 'properties': OrderedDict(
  21. [
  22. ('field1', {'example': 'Example 1'}),
  23. ('field2', {'example': 'Example 2'}),
  24. ]
  25. ),
  26. },
  27. {
  28. 'properties': OrderedDict(
  29. [
  30. ('field2', {'example': 'Example 2'}),
  31. ('field3', {'example': 'Example 3'}),
  32. ]
  33. ),
  34. },
  35. ],
  36. }
  37. assert module.get_properties(schema) == dict(
  38. schema['oneOf'][0]['properties'], **schema['oneOf'][1]['properties']
  39. )
  40. def test_schema_to_sample_configuration_generates_config_map_with_examples():
  41. schema = {
  42. 'type': 'object',
  43. 'properties': OrderedDict(
  44. [
  45. ('field1', {'example': 'Example 1'}),
  46. ('field2', {'example': 'Example 2'}),
  47. ('field3', {'example': 'Example 3'}),
  48. ]
  49. ),
  50. }
  51. flexmock(module).should_receive('get_properties').and_return(schema['properties'])
  52. flexmock(module.ruamel.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
  53. flexmock(module).should_receive('add_comments_to_configuration_object')
  54. config = module.schema_to_sample_configuration(schema)
  55. assert config == OrderedDict(
  56. [
  57. ('field1', 'Example 1'),
  58. ('field2', 'Example 2'),
  59. ('field3', 'Example 3'),
  60. ]
  61. )
  62. def test_schema_to_sample_configuration_generates_config_sequence_of_strings_with_example():
  63. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  64. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  65. schema = {'type': 'array', 'items': {'type': 'string'}, 'example': ['hi']}
  66. config = module.schema_to_sample_configuration(schema)
  67. assert config == ['hi']
  68. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_examples():
  69. schema = {
  70. 'type': 'array',
  71. 'items': {
  72. 'type': 'object',
  73. 'properties': OrderedDict(
  74. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  75. ),
  76. },
  77. }
  78. flexmock(module).should_receive('get_properties').and_return(schema['items']['properties'])
  79. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  80. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  81. flexmock(module).should_receive('add_comments_to_configuration_object')
  82. config = module.schema_to_sample_configuration(schema)
  83. assert config == [OrderedDict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  84. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_multiple_types():
  85. schema = {
  86. 'type': 'array',
  87. 'items': {
  88. 'type': ['object', 'null'],
  89. 'properties': OrderedDict(
  90. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  91. ),
  92. },
  93. }
  94. flexmock(module).should_receive('get_properties').and_return(schema['items']['properties'])
  95. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  96. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  97. flexmock(module).should_receive('add_comments_to_configuration_object')
  98. config = module.schema_to_sample_configuration(schema)
  99. assert config == [OrderedDict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  100. def test_schema_to_sample_configuration_with_unsupported_schema_raises():
  101. schema = {'gobbledygook': [{'type': 'not-your'}]}
  102. with pytest.raises(ValueError):
  103. module.schema_to_sample_configuration(schema)
  104. def test_merge_source_configuration_into_destination_inserts_map_fields():
  105. destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  106. source_config = {'foo': 'source1', 'baz': 'source2'}
  107. flexmock(module).should_receive('remove_commented_out_sentinel')
  108. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  109. module.merge_source_configuration_into_destination(destination_config, source_config)
  110. assert destination_config == {'foo': 'source1', 'bar': 'dest2', 'baz': 'source2'}
  111. def test_merge_source_configuration_into_destination_inserts_nested_map_fields():
  112. destination_config = {'foo': {'first': 'dest1', 'second': 'dest2'}, 'bar': 'dest3'}
  113. source_config = {'foo': {'first': 'source1'}}
  114. flexmock(module).should_receive('remove_commented_out_sentinel')
  115. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  116. module.merge_source_configuration_into_destination(destination_config, source_config)
  117. assert destination_config == {'foo': {'first': 'source1', 'second': 'dest2'}, 'bar': 'dest3'}
  118. def test_merge_source_configuration_into_destination_inserts_sequence_fields():
  119. destination_config = {'foo': ['dest1', 'dest2'], 'bar': ['dest3'], 'baz': ['dest4']}
  120. source_config = {'foo': ['source1'], 'bar': ['source2', 'source3']}
  121. flexmock(module).should_receive('remove_commented_out_sentinel')
  122. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  123. module.merge_source_configuration_into_destination(destination_config, source_config)
  124. assert destination_config == {
  125. 'foo': ['source1'],
  126. 'bar': ['source2', 'source3'],
  127. 'baz': ['dest4'],
  128. }
  129. def test_merge_source_configuration_into_destination_inserts_sequence_of_maps():
  130. destination_config = {'foo': [{'first': 'dest1', 'second': 'dest2'}], 'bar': 'dest3'}
  131. source_config = {'foo': [{'first': 'source1'}, {'other': 'source2'}]}
  132. flexmock(module).should_receive('remove_commented_out_sentinel')
  133. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  134. module.merge_source_configuration_into_destination(destination_config, source_config)
  135. assert destination_config == {
  136. 'foo': [{'first': 'source1', 'second': 'dest2'}, {'other': 'source2'}],
  137. 'bar': 'dest3',
  138. }
  139. def test_merge_source_configuration_into_destination_without_source_does_nothing():
  140. original_destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  141. destination_config = dict(original_destination_config)
  142. module.merge_source_configuration_into_destination(destination_config, None)
  143. assert destination_config == original_destination_config