test_generate.py 5.4 KB

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