test_constants.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import constants as module
  4. @pytest.mark.parametrize(
  5. 'value,expected_value',
  6. (
  7. ('3', 3),
  8. ('0', 0),
  9. ('-3', -3),
  10. ('1234', 1234),
  11. ('true', True),
  12. ('True', True),
  13. ('false', False),
  14. ('False', False),
  15. ('thing', 'thing'),
  16. ({}, {}),
  17. ({'foo': 'bar'}, {'foo': 'bar'}),
  18. ([], []),
  19. (['foo', 'bar'], ['foo', 'bar']),
  20. ),
  21. )
  22. def test_coerce_scalar_converts_value(value, expected_value):
  23. assert module.coerce_scalar(value) == expected_value
  24. def test_apply_constants_with_empty_constants_passes_through_value():
  25. assert module.apply_constants(value='thing', constants={}) == 'thing'
  26. @pytest.mark.parametrize(
  27. 'value,expected_value',
  28. (
  29. (None, None),
  30. ('thing', 'thing'),
  31. ('{foo}', 'bar'),
  32. ('abc{foo}', 'abcbar'),
  33. ('{foo}xyz', 'barxyz'),
  34. ('{foo}{baz}', 'barquux'),
  35. ('{int}', '3'),
  36. ('{bool}', 'True'),
  37. (['thing', 'other'], ['thing', 'other']),
  38. (['thing', '{foo}'], ['thing', 'bar']),
  39. (['{foo}', '{baz}'], ['bar', 'quux']),
  40. ({'key': 'value'}, {'key': 'value'}),
  41. ({'key': '{foo}'}, {'key': 'bar'}),
  42. (3, 3),
  43. (True, True),
  44. (False, False),
  45. ),
  46. )
  47. def test_apply_constants_makes_string_substitutions(value, expected_value):
  48. flexmock(module).should_receive('coerce_scalar').replace_with(lambda value: value)
  49. constants = {'foo': 'bar', 'baz': 'quux', 'int': 3, 'bool': True}
  50. assert module.apply_constants(value, constants) == expected_value