test_sqlite.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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) == [
  30. 'process',
  31. 'process',
  32. ]
  33. def test_dump_databases_does_not_dump_if_dry_run():
  34. databases = [{'path': '/path/to/database', 'name': 'database'}]
  35. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  36. flexmock(module).should_receive('dump.make_database_dump_filename').and_return(
  37. '/path/to/dump/database'
  38. )
  39. flexmock(module.os.path).should_receive('exists').and_return(False)
  40. flexmock(logging).should_receive('info')
  41. flexmock(logging).should_receive('warning')
  42. flexmock(module).should_receive('dump.create_parent_directory_for_dump')
  43. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  44. def test_restore_database_dump_restores_database():
  45. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  46. extract_process = flexmock(stdout=flexmock())
  47. flexmock(module).should_receive('execute_command_with_processes').and_return('process')
  48. module.restore_database_dump(
  49. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  50. )
  51. def test_restore_database_dump_does_not_restore_database_if_dry_run():
  52. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  53. extract_process = flexmock(stdout=flexmock())
  54. flexmock(module).should_receive('execute_command_with_processes').never()
  55. module.restore_database_dump(
  56. database_config, 'test.yaml', {}, dry_run=True, extract_process=extract_process
  57. )
  58. def test_restore_database_dump_raises_error_if_database_config_is_invalid():
  59. database_config = []
  60. extract_process = flexmock(stdout=flexmock())
  61. with pytest.raises(ValueError):
  62. module.restore_database_dump(
  63. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  64. )