test_generate.py 4.9 KB

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