test_generate.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import generate as module
  4. def test_get_properties_with_simple_object():
  5. schema = {
  6. 'type': 'object',
  7. 'properties': dict(
  8. [
  9. ('field1', {'example': 'Example'}),
  10. ]
  11. ),
  12. }
  13. assert module.get_properties(schema) == schema['properties']
  14. def test_get_properties_merges_oneof_list_properties():
  15. schema = {
  16. 'type': 'object',
  17. 'oneOf': [
  18. {
  19. 'properties': dict(
  20. [
  21. ('field1', {'example': 'Example 1'}),
  22. ('field2', {'example': 'Example 2'}),
  23. ]
  24. ),
  25. },
  26. {
  27. 'properties': dict(
  28. [
  29. ('field2', {'example': 'Example 2'}),
  30. ('field3', {'example': 'Example 3'}),
  31. ]
  32. ),
  33. },
  34. ],
  35. }
  36. assert module.get_properties(schema) == dict(
  37. schema['oneOf'][0]['properties'], **schema['oneOf'][1]['properties']
  38. )
  39. def test_get_properties_interleaves_oneof_list_properties():
  40. schema = {
  41. 'type': 'object',
  42. 'oneOf': [
  43. {
  44. 'properties': dict(
  45. [
  46. ('field1', {'example': 'Example 1'}),
  47. ('field2', {'example': 'Example 2'}),
  48. ('field3', {'example': 'Example 3'}),
  49. ]
  50. ),
  51. },
  52. {
  53. 'properties': dict(
  54. [
  55. ('field4', {'example': 'Example 4'}),
  56. ('field5', {'example': 'Example 5'}),
  57. ]
  58. ),
  59. },
  60. ],
  61. }
  62. assert module.get_properties(schema) == dict(
  63. [
  64. ('field1', {'example': 'Example 1'}),
  65. ('field4', {'example': 'Example 4'}),
  66. ('field2', {'example': 'Example 2'}),
  67. ('field5', {'example': 'Example 5'}),
  68. ('field3', {'example': 'Example 3'}),
  69. ]
  70. )
  71. def test_schema_to_sample_configuration_generates_config_map_with_examples():
  72. schema = {
  73. 'type': 'object',
  74. 'properties': dict(
  75. [
  76. ('field1', {'example': 'Example 1'}),
  77. ('field2', {'example': 'Example 2'}),
  78. ('field3', {'example': 'Example 3'}),
  79. ]
  80. ),
  81. }
  82. flexmock(module).should_receive('get_properties').and_return(schema['properties'])
  83. flexmock(module.ruamel.yaml.comments).should_receive('CommentedMap').replace_with(dict)
  84. flexmock(module).should_receive('add_comments_to_configuration_object')
  85. config = module.schema_to_sample_configuration(schema)
  86. assert config == dict(
  87. [
  88. ('field1', 'Example 1'),
  89. ('field2', 'Example 2'),
  90. ('field3', 'Example 3'),
  91. ]
  92. )
  93. def test_schema_to_sample_configuration_generates_config_sequence_of_strings_with_example():
  94. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  95. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  96. schema = {'type': 'array', 'items': {'type': 'string'}, 'example': ['hi']}
  97. config = module.schema_to_sample_configuration(schema)
  98. assert config == ['hi']
  99. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_examples():
  100. schema = {
  101. 'type': 'array',
  102. 'items': {
  103. 'type': 'object',
  104. 'properties': dict(
  105. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  106. ),
  107. },
  108. }
  109. flexmock(module).should_receive('get_properties').and_return(schema['items']['properties'])
  110. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  111. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  112. flexmock(module).should_receive('add_comments_to_configuration_object')
  113. config = module.schema_to_sample_configuration(schema)
  114. assert config == [dict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  115. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_multiple_types():
  116. schema = {
  117. 'type': 'array',
  118. 'items': {
  119. 'type': ['object', 'null'],
  120. 'properties': dict(
  121. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  122. ),
  123. },
  124. }
  125. flexmock(module).should_receive('get_properties').and_return(schema['items']['properties'])
  126. flexmock(module.ruamel.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  127. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  128. flexmock(module).should_receive('add_comments_to_configuration_object')
  129. config = module.schema_to_sample_configuration(schema)
  130. assert config == [dict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  131. def test_schema_to_sample_configuration_with_unsupported_schema_raises():
  132. schema = {'gobbledygook': [{'type': 'not-your'}]}
  133. with pytest.raises(ValueError):
  134. module.schema_to_sample_configuration(schema)
  135. def test_merge_source_configuration_into_destination_inserts_map_fields():
  136. destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  137. source_config = {'foo': 'source1', 'baz': 'source2'}
  138. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  139. module.merge_source_configuration_into_destination(destination_config, source_config)
  140. assert destination_config == {'foo': 'source1', 'bar': 'dest2', 'baz': 'source2'}
  141. def test_merge_source_configuration_into_destination_inserts_nested_map_fields():
  142. destination_config = {'foo': {'first': 'dest1', 'second': 'dest2'}, 'bar': 'dest3'}
  143. source_config = {'foo': {'first': 'source1'}}
  144. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  145. module.merge_source_configuration_into_destination(destination_config, source_config)
  146. assert destination_config == {'foo': {'first': 'source1', 'second': 'dest2'}, 'bar': 'dest3'}
  147. def test_merge_source_configuration_into_destination_inserts_sequence_fields():
  148. destination_config = {'foo': ['dest1', 'dest2'], 'bar': ['dest3'], 'baz': ['dest4']}
  149. source_config = {'foo': ['source1'], 'bar': ['source2', 'source3']}
  150. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  151. module.merge_source_configuration_into_destination(destination_config, source_config)
  152. assert destination_config == {
  153. 'foo': ['source1'],
  154. 'bar': ['source2', 'source3'],
  155. 'baz': ['dest4'],
  156. }
  157. def test_merge_source_configuration_into_destination_inserts_sequence_of_maps():
  158. destination_config = {'foo': [{'first': 'dest1', 'second': 'dest2'}], 'bar': 'dest3'}
  159. source_config = {'foo': [{'first': 'source1'}, {'other': 'source2'}]}
  160. flexmock(module).should_receive('ruamel.yaml.comments.CommentedSeq').replace_with(list)
  161. module.merge_source_configuration_into_destination(destination_config, source_config)
  162. assert destination_config == {
  163. 'foo': [{'first': 'source1', 'second': 'dest2'}, {'other': 'source2'}],
  164. 'bar': 'dest3',
  165. }
  166. def test_merge_source_configuration_into_destination_without_source_does_nothing():
  167. original_destination_config = {'foo': 'dest1', 'bar': 'dest2'}
  168. destination_config = dict(original_destination_config)
  169. module.merge_source_configuration_into_destination(destination_config, None)
  170. assert destination_config == original_destination_config