test_generate.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_generates_config_sequence_of_strings_with_example():
  36. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  37. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  38. schema = {'type': 'array', 'items': {'type': 'string'}, 'example': ['hi']}
  39. config = module.schema_to_sample_configuration(schema)
  40. assert config == ['hi']
  41. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_examples():
  42. schema = {
  43. 'type': 'array',
  44. 'items': {
  45. 'type': 'object',
  46. 'properties': dict(
  47. [
  48. ('field1', {'type': 'string', 'example': 'Example 1'}),
  49. ('field2', {'type': 'string', 'example': 'Example 2'}),
  50. ]
  51. ),
  52. },
  53. }
  54. flexmock(module.borgmatic.config.schema).should_receive('compare_types').and_return(False)
  55. flexmock(module.borgmatic.config.schema).should_receive('compare_types').with_args(
  56. 'array', {'array'}
  57. ).and_return(True)
  58. flexmock(module.borgmatic.config.schema).should_receive('compare_types').with_args(
  59. 'object', {'object'}
  60. ).and_return(True)
  61. flexmock(module.borgmatic.config.schema).should_receive('compare_types').with_args(
  62. 'string', module.SCALAR_SCHEMA_TYPES, match=all
  63. ).and_return(True)
  64. flexmock(module.borgmatic.config.schema).should_receive('get_properties').and_return(
  65. schema['items']['properties']
  66. )
  67. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  68. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  69. flexmock(module).should_receive('add_comments_to_configuration_object')
  70. config = module.schema_to_sample_configuration(schema)
  71. assert config == [dict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  72. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_multiple_types():
  73. schema = {
  74. 'type': 'array',
  75. 'items': {
  76. 'type': ['object', 'null'],
  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', {'array'}
  88. ).and_return(True)
  89. flexmock(module.borgmatic.config.schema).should_receive('compare_types').with_args(
  90. ['object', 'null'], {'object'}
  91. ).and_return(True)
  92. flexmock(module.borgmatic.config.schema).should_receive('compare_types').with_args(
  93. 'string', module.SCALAR_SCHEMA_TYPES, match=all
  94. ).and_return(True)
  95. flexmock(module.borgmatic.config.schema).should_receive('get_properties').and_return(
  96. schema['items']['properties']
  97. )
  98. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  99. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  100. flexmock(module).should_receive('add_comments_to_configuration_object')
  101. config = module.schema_to_sample_configuration(schema)
  102. assert config == [dict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  103. def test_schema_to_sample_configuration_with_unsupported_schema_raises():
  104. schema = {'gobbledygook': [{'type': 'not-your'}]}
  105. with pytest.raises(ValueError):
  106. module.schema_to_sample_configuration(schema)
  107. def test_merge_source_configuration_into_destination_inserts_map_fields():
  108. destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  109. source_config = {'foo': 'source1', 'baz': 'source2'}
  110. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  111. module.merge_source_configuration_into_destination(destination_config, source_config)
  112. assert destination_config == {'foo': 'source1', 'bar': 'dest2', 'baz': 'source2'}
  113. def test_merge_source_configuration_into_destination_inserts_nested_map_fields():
  114. destination_config = {'foo': {'first': 'dest1', 'second': 'dest2'}, 'bar': 'dest3'}
  115. source_config = {'foo': {'first': 'source1'}}
  116. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  117. module.merge_source_configuration_into_destination(destination_config, source_config)
  118. assert destination_config == {'foo': {'first': 'source1', 'second': 'dest2'}, 'bar': 'dest3'}
  119. def test_merge_source_configuration_into_destination_inserts_sequence_fields():
  120. destination_config = {'foo': ['dest1', 'dest2'], 'bar': ['dest3'], 'baz': ['dest4']}
  121. source_config = {'foo': ['source1'], 'bar': ['source2', 'source3']}
  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('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  133. module.merge_source_configuration_into_destination(destination_config, source_config)
  134. assert destination_config == {
  135. 'foo': [{'first': 'source1', 'second': 'dest2'}, {'other': 'source2'}],
  136. 'bar': 'dest3',
  137. }
  138. def test_merge_source_configuration_into_destination_without_source_does_nothing():
  139. original_destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  140. destination_config = dict(original_destination_config)
  141. module.merge_source_configuration_into_destination(destination_config, None)
  142. assert destination_config == original_destination_config