test_generate.py 8.4 KB

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