test_generate.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_map')
  8. schema = {
  9. 'map': OrderedDict(
  10. [
  11. ('section1', {'map': {'field1': OrderedDict([('example', 'Example 1')])}}),
  12. (
  13. 'section2',
  14. {
  15. 'map': OrderedDict(
  16. [
  17. ('field2', {'example': 'Example 2'}),
  18. ('field3', {'example': 'Example 3'}),
  19. ]
  20. )
  21. },
  22. ),
  23. ]
  24. )
  25. }
  26. config = module._schema_to_sample_configuration(schema)
  27. assert config == OrderedDict(
  28. [
  29. ('section1', OrderedDict([('field1', 'Example 1')])),
  30. ('section2', OrderedDict([('field2', 'Example 2'), ('field3', 'Example 3')])),
  31. ]
  32. )
  33. def test_schema_to_sample_configuration_generates_config_sequence_of_strings_with_example():
  34. flexmock(module.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  35. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  36. schema = {'seq': [{'type': 'str'}], 'example': ['hi']}
  37. config = module._schema_to_sample_configuration(schema)
  38. assert config == ['hi']
  39. def test_schema_to_sample_configuration_generates_config_sequence_of_maps_with_examples():
  40. flexmock(module.yaml.comments).should_receive('CommentedSeq').replace_with(list)
  41. flexmock(module).should_receive('add_comments_to_configuration_sequence')
  42. schema = {
  43. 'seq': [
  44. {
  45. 'map': OrderedDict(
  46. [('field1', {'example': 'Example 1'}), ('field2', {'example': 'Example 2'})]
  47. )
  48. }
  49. ]
  50. }
  51. config = module._schema_to_sample_configuration(schema)
  52. assert config == [OrderedDict([('field1', 'Example 1'), ('field2', 'Example 2')])]
  53. def test_schema_to_sample_configuration_with_unsupported_schema_raises():
  54. schema = {'gobbledygook': [{'type': 'not-your'}]}
  55. with pytest.raises(ValueError):
  56. module._schema_to_sample_configuration(schema)