test_sqlite.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.dump).should_receive('make_database_dump_filename').and_return(
  9. '/path/to/dump/database'
  10. )
  11. flexmock(module.os.path).should_receive('exists').and_return(True)
  12. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == []
  13. def test_dump_databases_dumps_each_database():
  14. databases = [
  15. {'path': '/path/to/database1', 'name': 'database1'},
  16. {'path': '/path/to/database2', 'name': 'database2'},
  17. ]
  18. processes = [flexmock(), flexmock()]
  19. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  20. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  21. '/path/to/dump/database'
  22. )
  23. flexmock(module.os.path).should_receive('exists').and_return(False)
  24. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  25. flexmock(module).should_receive('execute_command').and_return(processes[0]).and_return(
  26. processes[1]
  27. )
  28. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  29. def test_dumping_database_with_name_all_warns_and_dumps_all_databases():
  30. databases = [
  31. {'path': '/path/to/database1', 'name': 'all'},
  32. ]
  33. processes = [flexmock()]
  34. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  35. flexmock(logging).should_receive('warning')
  36. flexmock(module.dump).should_receive('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(module.dump).should_receive('create_parent_directory_for_dump')
  41. flexmock(module).should_receive('execute_command').and_return(processes[0])
  42. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  43. def test_dump_databases_does_not_dump_if_dry_run():
  44. databases = [{'path': '/path/to/database', 'name': 'database'}]
  45. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  46. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  47. '/path/to/dump/database'
  48. )
  49. flexmock(module.os.path).should_receive('exists').and_return(False)
  50. flexmock(module.dump).should_receive('create_parent_directory_for_dump').never()
  51. flexmock(module).should_receive('execute_command').never()
  52. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  53. def test_restore_database_dump_restores_database():
  54. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  55. extract_process = flexmock(stdout=flexmock())
  56. flexmock(module).should_receive('execute_command_with_processes').once()
  57. flexmock(module).should_receive('execute_command').once()
  58. module.restore_database_dump(
  59. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  60. )
  61. def test_restore_database_dump_does_not_restore_database_if_dry_run():
  62. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  63. extract_process = flexmock(stdout=flexmock())
  64. flexmock(module).should_receive('execute_command_with_processes').never()
  65. module.restore_database_dump(
  66. database_config, 'test.yaml', {}, dry_run=True, extract_process=extract_process
  67. )
  68. def test_restore_database_dump_raises_error_if_database_config_is_invalid():
  69. database_config = []
  70. extract_process = flexmock(stdout=flexmock())
  71. with pytest.raises(ValueError):
  72. module.restore_database_dump(
  73. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  74. )