test_json.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import pytest
  2. from borgmatic.actions import json as module
  3. def test_parse_json_loads_json_from_string():
  4. assert module.parse_json('{"repository": {"id": "foo"}}', label=None) == {
  5. 'repository': {'id': 'foo', 'label': ''}
  6. }
  7. def test_parse_json_skips_non_json_warnings_and_loads_subsequent_json():
  8. assert module.parse_json(
  9. '/non/existent/path: stat: [Errno 2] No such file or directory: /non/existent/path\n{"repository":\n{"id": "foo"}}',
  10. label=None,
  11. ) == {'repository': {'id': 'foo', 'label': ''}}
  12. def test_parse_json_skips_with_invalid_json_raises():
  13. with pytest.raises(module.json.JSONDecodeError):
  14. module.parse_json('this is not valid JSON }', label=None)
  15. def test_parse_json_injects_label_into_parsed_data():
  16. assert module.parse_json('{"repository": {"id": "foo"}}', label='bar') == {
  17. 'repository': {'id': 'foo', 'label': 'bar'}
  18. }
  19. def test_parse_json_injects_nothing_when_repository_missing():
  20. assert module.parse_json('{"stuff": {"id": "foo"}}', label='bar') == {'stuff': {'id': 'foo'}}