test_generate.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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', {'example': 'Example 1'}),
  10. ('field2', {'example': 'Example 2'}),
  11. ('field3', {'example': 'Example 3'}),
  12. ]
  13. ),
  14. }
  15. flexmock(module).should_receive('get_properties').and_return(schema['properties'])
  16. flexmock(module.ruamel.yaml.comments).should_receive('CommentedMap').replace_with(dict)
  17. flexmock(module).should_receive('add_comments_to_configuration_object')
  18. config = module.schema_to_sample_configuration(schema)
  19. assert config == dict(
  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.ruamel.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. schema = {
  34. 'type': 'array',
  35. 'items': {
  36. 'type': 'object',
  37. 'properties': dict(
  38. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  39. ),
  40. },
  41. }
  42. flexmock(module).should_receive('get_properties').and_return(schema['items']['properties'])
  43. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  44. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  45. flexmock(module).should_receive('add_comments_to_configuration_object')
  46. config = module.schema_to_sample_configuration(schema)
  47. assert config == [dict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  48. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_multiple_types():
  49. schema = {
  50. 'type': 'array',
  51. 'items': {
  52. 'type': ['object', 'null'],
  53. 'properties': dict(
  54. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  55. ),
  56. },
  57. }
  58. flexmock(module).should_receive('get_properties').and_return(schema['items']['properties'])
  59. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  60. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  61. flexmock(module).should_receive('add_comments_to_configuration_object')
  62. config = module.schema_to_sample_configuration(schema)
  63. assert config == [dict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  64. def test_schema_to_sample_configuration_with_unsupported_schema_raises():
  65. schema = {'gobbledygook': [{'type': 'not-your'}]}
  66. with pytest.raises(ValueError):
  67. module.schema_to_sample_configuration(schema)
  68. def test_merge_source_configuration_into_destination_inserts_map_fields():
  69. destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  70. source_config = {'foo': 'source1', 'baz': 'source2'}
  71. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  72. module.merge_source_configuration_into_destination(destination_config, source_config)
  73. assert destination_config == {'foo': 'source1', 'bar': 'dest2', 'baz': 'source2'}
  74. def test_merge_source_configuration_into_destination_inserts_nested_map_fields():
  75. destination_config = {'foo': {'first': 'dest1', 'second': 'dest2'}, 'bar': 'dest3'}
  76. source_config = {'foo': {'first': 'source1'}}
  77. flexmock(module).should_receive('ruamel.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('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  84. module.merge_source_configuration_into_destination(destination_config, source_config)
  85. assert destination_config == {
  86. 'foo': ['source1'],
  87. 'bar': ['source2', 'source3'],
  88. 'baz': ['dest4'],
  89. }
  90. def test_merge_source_configuration_into_destination_inserts_sequence_of_maps():
  91. destination_config = {'foo': [{'first': 'dest1', 'second': 'dest2'}], 'bar': 'dest3'}
  92. source_config = {'foo': [{'first': 'source1'}, {'other': 'source2'}]}
  93. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  94. module.merge_source_configuration_into_destination(destination_config, source_config)
  95. assert destination_config == {
  96. 'foo': [{'first': 'source1', 'second': 'dest2'}, {'other': 'source2'}],
  97. 'bar': 'dest3',
  98. }
  99. def test_merge_source_configuration_into_destination_without_source_does_nothing():
  100. original_destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  101. destination_config = dict(original_destination_config)
  102. module.merge_source_configuration_into_destination(destination_config, None)
  103. assert destination_config == original_destination_config