test_generate.py 7.4 KB

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