test_generate.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from collections import OrderedDict
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.config import generate as module
  5. def test_schema_to_sample_configuration_generates_config_map_with_examples():
  6. flexmock(module.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
  7. flexmock(module).should_receive('add_comments_to_configuration_map')
  8. schema = {
  9. 'map': OrderedDict(
  10. [
  11. ('section1', {'map': {'field1': OrderedDict([('example', 'Example 1')])}}),
  12. (
  13. 'section2',
  14. {
  15. 'map': OrderedDict(
  16. [
  17. ('field2', {'example': 'Example 2'}),
  18. ('field3', {'example': 'Example 3'}),
  19. ]
  20. )
  21. },
  22. ),
  23. ]
  24. )
  25. }
  26. config = module._schema_to_sample_configuration(schema)
  27. assert config == OrderedDict(
  28. [
  29. ('section1', OrderedDict([('field1', 'Example 1')])),
  30. ('section2', OrderedDict([('field2', 'Example 2'), ('field3', 'Example 3')])),
  31. ]
  32. )
  33. def test_schema_to_sample_configuration_generates_config_sequence_of_strings_with_example():
  34. flexmock(module.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  35. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  36. schema = {'seq': [{'type': 'str'}], 'example': ['hi']}
  37. config = module._schema_to_sample_configuration(schema)
  38. assert config == ['hi']
  39. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_examples():
  40. flexmock(module.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  41. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  42. flexmock(module).should_receive('add_comments_to_configuration_map')
  43. schema = {
  44. 'seq': [
  45. {
  46. 'map': OrderedDict(
  47. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  48. )
  49. }
  50. ]
  51. }
  52. config = module._schema_to_sample_configuration(schema)
  53. assert config == [OrderedDict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  54. def test_schema_to_sample_configuration_with_unsupported_schema_raises():
  55. schema = {'gobbledygook': [{'type': 'not-your'}]}
  56. with pytest.raises(ValueError):
  57. module._schema_to_sample_configuration(schema)
  58. def test_merge_source_configuration_into_destination_inserts_map_fields():
  59. destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  60. source_config = {'foo': 'source1', 'baz': 'source2'}
  61. flexmock(module).should_receive('remove_commented_out_sentinel')
  62. flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
  63. module.merge_source_configuration_into_destination(destination_config, source_config)
  64. assert destination_config == {'foo': 'source1', 'bar': 'dest2', 'baz': 'source2'}
  65. def test_merge_source_configuration_into_destination_inserts_nested_map_fields():
  66. destination_config = {'foo': {'first': 'dest1', 'second': 'dest2'}, 'bar': 'dest3'}
  67. source_config = {'foo': {'first': 'source1'}}
  68. flexmock(module).should_receive('remove_commented_out_sentinel')
  69. flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
  70. module.merge_source_configuration_into_destination(destination_config, source_config)
  71. assert destination_config == {'foo': {'first': 'source1', 'second': 'dest2'}, 'bar': 'dest3'}
  72. def test_merge_source_configuration_into_destination_inserts_sequence_fields():
  73. destination_config = {'foo': ['dest1', 'dest2'], 'bar': ['dest3'], 'baz': ['dest4']}
  74. source_config = {'foo': ['source1'], 'bar': ['source2', 'source3']}
  75. flexmock(module).should_receive('remove_commented_out_sentinel')
  76. flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
  77. module.merge_source_configuration_into_destination(destination_config, source_config)
  78. assert destination_config == {
  79. 'foo': ['source1'],
  80. 'bar': ['source2', 'source3'],
  81. 'baz': ['dest4'],
  82. }
  83. def test_merge_source_configuration_into_destination_inserts_sequence_of_maps():
  84. destination_config = {'foo': [{'first': 'dest1', 'second': 'dest2'}], 'bar': 'dest3'}
  85. source_config = {'foo': [{'first': 'source1'}, {'other': 'source2'}]}
  86. flexmock(module).should_receive('remove_commented_out_sentinel')
  87. flexmock(module).should_receive('yaml.comments.CommentedSeq').replace_with(list)
  88. module.merge_source_configuration_into_destination(destination_config, source_config)
  89. assert destination_config == {
  90. 'foo': [{'first': 'source1', 'second': 'dest2'}, {'other': 'source2'}],
  91. 'bar': 'dest3',
  92. }
  93. def test_merge_source_configuration_into_destination_without_source_does_nothing():
  94. original_destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  95. destination_config = dict(original_destination_config)
  96. module.merge_source_configuration_into_destination(destination_config, None)
  97. assert destination_config == original_destination_config