test_generate.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from collections import OrderedDict
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.config import generate as module
  5. def test_get_properties_with_simple_object():
  6. schema = {
  7. 'type': 'object',
  8. 'properties': OrderedDict(
  9. [
  10. ('field1', {'example': 'Example'}),
  11. ]
  12. ),
  13. }
  14. assert module.get_properties(schema) == schema['properties']
  15. def test_get_properties_merges_one_of_list_properties():
  16. schema = {
  17. 'type': 'object',
  18. 'oneOf': [
  19. {
  20. 'properties': OrderedDict(
  21. [
  22. ('field1', {'example': 'Example 1'}),
  23. ('field2', {'example': 'Example 2'}),
  24. ]
  25. ),
  26. },
  27. {
  28. 'properties': OrderedDict(
  29. [
  30. ('field2', {'example': 'Example 2'}),
  31. ('field3', {'example': 'Example 3'}),
  32. ]
  33. ),
  34. },
  35. ],
  36. }
  37. assert module.get_properties(schema) == dict(
  38. schema['oneOf'][0]['properties'], **schema['oneOf'][1]['properties']
  39. )
  40. def test_schema_to_sample_configuration_generates_config_map_with_examples():
  41. schema = {
  42. 'type': 'object',
  43. 'properties': OrderedDict(
  44. [
  45. ('field1', {'example': 'Example 1'}),
  46. ('field2', {'example': 'Example 2'}),
  47. ('field3', {'example': 'Example 3'}),
  48. ]
  49. ),
  50. }
  51. flexmock(module).should_receive('get_properties').and_return(schema['properties'])
  52. flexmock(module.ruamel.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
  53. flexmock(module).should_receive('add_comments_to_configuration_object')
  54. config = module.schema_to_sample_configuration(schema)
  55. assert config == OrderedDict(
  56. [
  57. ('field1', 'Example 1'),
  58. ('field2', 'Example 2'),
  59. ('field3', 'Example 3'),
  60. ]
  61. )
  62. def test_schema_to_sample_configuration_generates_config_sequence_of_strings_with_example():
  63. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  64. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  65. schema = {'type': 'array', 'items': {'type': 'string'}, 'example': ['hi']}
  66. config = module.schema_to_sample_configuration(schema)
  67. assert config == ['hi']
  68. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_examples():
  69. schema = {
  70. 'type': 'array',
  71. 'items': {
  72. 'type': 'object',
  73. 'properties': OrderedDict(
  74. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  75. ),
  76. },
  77. }
  78. flexmock(module).should_receive('get_properties').and_return(schema['items']['properties'])
  79. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  80. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  81. flexmock(module).should_receive('add_comments_to_configuration_object')
  82. config = module.schema_to_sample_configuration(schema)
  83. assert config == [OrderedDict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  84. def test_schema_to_sample_configuration_with_unsupported_schema_raises():
  85. schema = {'gobbledygook': [{'type': 'not-your'}]}
  86. with pytest.raises(ValueError):
  87. module.schema_to_sample_configuration(schema)
  88. def test_merge_source_configuration_into_destination_inserts_map_fields():
  89. destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  90. source_config = {'foo': 'source1', 'baz': 'source2'}
  91. flexmock(module).should_receive('remove_commented_out_sentinel')
  92. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  93. module.merge_source_configuration_into_destination(destination_config, source_config)
  94. assert destination_config == {'foo': 'source1', 'bar': 'dest2', 'baz': 'source2'}
  95. def test_merge_source_configuration_into_destination_inserts_nested_map_fields():
  96. destination_config = {'foo': {'first': 'dest1', 'second': 'dest2'}, 'bar': 'dest3'}
  97. source_config = {'foo': {'first': 'source1'}}
  98. flexmock(module).should_receive('remove_commented_out_sentinel')
  99. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  100. module.merge_source_configuration_into_destination(destination_config, source_config)
  101. assert destination_config == {'foo': {'first': 'source1', 'second': 'dest2'}, 'bar': 'dest3'}
  102. def test_merge_source_configuration_into_destination_inserts_sequence_fields():
  103. destination_config = {'foo': ['dest1', 'dest2'], 'bar': ['dest3'], 'baz': ['dest4']}
  104. source_config = {'foo': ['source1'], 'bar': ['source2', 'source3']}
  105. flexmock(module).should_receive('remove_commented_out_sentinel')
  106. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  107. module.merge_source_configuration_into_destination(destination_config, source_config)
  108. assert destination_config == {
  109. 'foo': ['source1'],
  110. 'bar': ['source2', 'source3'],
  111. 'baz': ['dest4'],
  112. }
  113. def test_merge_source_configuration_into_destination_inserts_sequence_of_maps():
  114. destination_config = {'foo': [{'first': 'dest1', 'second': 'dest2'}], 'bar': 'dest3'}
  115. source_config = {'foo': [{'first': 'source1'}, {'other': 'source2'}]}
  116. flexmock(module).should_receive('remove_commented_out_sentinel')
  117. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  118. module.merge_source_configuration_into_destination(destination_config, source_config)
  119. assert destination_config == {
  120. 'foo': [{'first': 'source1', 'second': 'dest2'}, {'other': 'source2'}],
  121. 'bar': 'dest3',
  122. }
  123. def test_merge_source_configuration_into_destination_without_source_does_nothing():
  124. original_destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  125. destination_config = dict(original_destination_config)
  126. module.merge_source_configuration_into_destination(destination_config, None)
  127. assert destination_config == original_destination_config