test_generate.py 8.6 KB

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