test_sqlite.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import sqlite as module
  5. def test_dump_databases_logs_and_skips_if_dump_already_exists():
  6. databases = [{'path': '/path/to/database', 'name': 'database'}]
  7. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  8. flexmock(module).should_receive('dump.make_database_dump_filename').and_return(
  9. '/path/to/dump/database'
  10. )
  11. flexmock(module.os.path).should_receive('exists').and_return(True)
  12. flexmock(logging).should_receive('info')
  13. flexmock(logging).should_receive('warning')
  14. assert module.dump_databases(databases, 'test.yaml', {}, dry_run = False) == []
  15. def test_dump_databases_dumps_each_database():
  16. databases = [
  17. {'path': '/path/to/database1', 'name': 'database1'},
  18. {'path': '/path/to/database2', 'name': 'database2'},
  19. ]
  20. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  21. flexmock(module).should_receive('dump.make_database_dump_filename').and_return(
  22. '/path/to/dump/database'
  23. )
  24. flexmock(module.os.path).should_receive('exists').and_return(False)
  25. flexmock(logging).should_receive('info')
  26. flexmock(logging).should_receive('warning')
  27. flexmock(module).should_receive('dump.create_parent_directory_for_dump')
  28. flexmock(module).should_receive('execute_command').and_return('process')
  29. assert module.dump_databases(databases, 'test.yaml', {}, dry_run = False) == ['process', 'process']
  30. def test_dump_databases_does_not_dump_if_dry_run():
  31. databases = [{'path': '/path/to/database', 'name': 'database'}]
  32. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  33. flexmock(module).should_receive('dump.make_database_dump_filename').and_return(
  34. '/path/to/dump/database'
  35. )
  36. flexmock(module.os.path).should_receive('exists').and_return(False)
  37. flexmock(logging).should_receive('info')
  38. flexmock(logging).should_receive('warning')
  39. flexmock(module).should_receive('dump.create_parent_directory_for_dump')
  40. assert module.dump_databases(databases, 'test.yaml', {}, dry_run = True) == []
  41. def test_restore_database_dump_restores_database():
  42. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  43. extract_process = flexmock(stdout = flexmock())
  44. flexmock(module).should_receive('execute_command_with_processes').and_return('process')
  45. module.restore_database_dump(database_config, 'test.yaml', {}, dry_run = False, extract_process = extract_process)
  46. def test_restore_database_dump_does_not_restore_database_if_dry_run():
  47. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  48. extract_process = flexmock(stdout = flexmock())
  49. flexmock(module).should_receive('execute_command_with_processes').never()
  50. module.restore_database_dump(database_config, 'test.yaml', {}, dry_run = True, extract_process = extract_process)
  51. def test_restore_database_dump_raises_error_if_database_config_is_invalid():
  52. database_config = []
  53. extract_process = flexmock(stdout = flexmock())
  54. with pytest.raises(ValueError):
  55. module.restore_database_dump(database_config, 'test.yaml', {}, dry_run = False, extract_process = extract_process)