test_schema.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import pytest
  2. from borgmatic.config import schema as module
  3. def test_get_properties_with_simple_object():
  4. schema = {
  5. 'type': 'object',
  6. 'properties': dict(
  7. [
  8. ('field1', {'example': 'Example'}),
  9. ]
  10. ),
  11. }
  12. assert module.get_properties(schema) == schema['properties']
  13. def test_get_properties_merges_oneof_list_properties():
  14. schema = {
  15. 'type': 'object',
  16. 'oneOf': [
  17. {
  18. 'properties': dict(
  19. [
  20. ('field1', {'example': 'Example 1'}),
  21. ('field2', {'example': 'Example 2'}),
  22. ]
  23. ),
  24. },
  25. {
  26. 'properties': dict(
  27. [
  28. ('field2', {'example': 'Example 2'}),
  29. ('field3', {'example': 'Example 3'}),
  30. ]
  31. ),
  32. },
  33. ],
  34. }
  35. assert module.get_properties(schema) == dict(
  36. schema['oneOf'][0]['properties'], **schema['oneOf'][1]['properties']
  37. )
  38. def test_get_properties_interleaves_oneof_list_properties():
  39. schema = {
  40. 'type': 'object',
  41. 'oneOf': [
  42. {
  43. 'properties': dict(
  44. [
  45. ('field1', {'example': 'Example 1'}),
  46. ('field2', {'example': 'Example 2'}),
  47. ('field3', {'example': 'Example 3'}),
  48. ]
  49. ),
  50. },
  51. {
  52. 'properties': dict(
  53. [
  54. ('field4', {'example': 'Example 4'}),
  55. ('field5', {'example': 'Example 5'}),
  56. ]
  57. ),
  58. },
  59. ],
  60. }
  61. assert module.get_properties(schema) == dict(
  62. [
  63. ('field1', {'example': 'Example 1'}),
  64. ('field4', {'example': 'Example 4'}),
  65. ('field2', {'example': 'Example 2'}),
  66. ('field5', {'example': 'Example 5'}),
  67. ('field3', {'example': 'Example 3'}),
  68. ]
  69. )
  70. def test_parse_type_maps_schema_type_to_python_type():
  71. module.parse_type('boolean') == bool
  72. def test_parse_type_with_unknown_schema_type_raises():
  73. with pytest.raises(ValueError):
  74. module.parse_type('what')
  75. def test_parse_type_respect_overrides_when_mapping_types():
  76. module.parse_type('boolean', boolean=int) == int
  77. @pytest.mark.parametrize(
  78. 'schema_type,target_types,match,expected_result',
  79. (
  80. (
  81. 'string',
  82. {'integer', 'string', 'boolean'},
  83. None,
  84. True,
  85. ),
  86. (
  87. 'string',
  88. {'integer', 'boolean'},
  89. None,
  90. False,
  91. ),
  92. (
  93. 'string',
  94. {'integer', 'string', 'boolean'},
  95. all,
  96. True,
  97. ),
  98. (
  99. 'string',
  100. {'integer', 'boolean'},
  101. all,
  102. False,
  103. ),
  104. (
  105. ['string', 'array'],
  106. {'integer', 'string', 'boolean'},
  107. None,
  108. True,
  109. ),
  110. (
  111. ['string', 'array'],
  112. {'integer', 'boolean'},
  113. None,
  114. False,
  115. ),
  116. (
  117. ['string', 'array'],
  118. {'integer', 'string', 'boolean', 'array'},
  119. all,
  120. True,
  121. ),
  122. (
  123. ['string', 'array'],
  124. {'integer', 'string', 'boolean'},
  125. all,
  126. False,
  127. ),
  128. (
  129. ['string', 'array'],
  130. {'integer', 'boolean'},
  131. all,
  132. False,
  133. ),
  134. ),
  135. )
  136. def test_compare_types_returns_whether_schema_type_matches_target_types(
  137. schema_type, target_types, match, expected_result
  138. ):
  139. if match:
  140. assert module.compare_types(schema_type, target_types, match) == expected_result
  141. else:
  142. assert module.compare_types(schema_type, target_types) == expected_result