2
0

test_generate.py 7.1 KB

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