test_schema.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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'],
  37. **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_parse_type_maps_schema_type_to_python_type():
  72. assert module.parse_type('boolean') is bool
  73. def test_parse_type_with_unknown_schema_type_raises():
  74. with pytest.raises(ValueError):
  75. module.parse_type('what')
  76. def test_parse_type_respect_overrides_when_mapping_types():
  77. assert module.parse_type('boolean', boolean=int) is int
  78. @pytest.mark.parametrize(
  79. 'schema_type,target_types,match,expected_result',
  80. (
  81. (
  82. 'string',
  83. {'integer', 'string', 'boolean'},
  84. None,
  85. True,
  86. ),
  87. (
  88. 'string',
  89. {'integer', 'boolean'},
  90. None,
  91. False,
  92. ),
  93. (
  94. 'string',
  95. {'integer', 'string', 'boolean'},
  96. all,
  97. True,
  98. ),
  99. (
  100. 'string',
  101. {'integer', 'boolean'},
  102. all,
  103. False,
  104. ),
  105. (
  106. ['string', 'array'],
  107. {'integer', 'string', 'boolean'},
  108. None,
  109. True,
  110. ),
  111. (
  112. ['string', 'array'],
  113. {'integer', 'boolean'},
  114. None,
  115. False,
  116. ),
  117. (
  118. ['string', 'array'],
  119. {'integer', 'string', 'boolean', 'array'},
  120. all,
  121. True,
  122. ),
  123. (
  124. ['string', 'array'],
  125. {'integer', 'string', 'boolean'},
  126. all,
  127. False,
  128. ),
  129. (
  130. ['string', 'array'],
  131. {'integer', 'boolean'},
  132. all,
  133. False,
  134. ),
  135. ),
  136. )
  137. def test_compare_types_returns_whether_schema_type_matches_target_types(
  138. schema_type,
  139. target_types,
  140. match,
  141. expected_result,
  142. ):
  143. if match:
  144. assert module.compare_types(schema_type, target_types, match) == expected_result
  145. else:
  146. assert module.compare_types(schema_type, target_types) == expected_result