Skip to content

Commit

Permalink
Use patch decorators instead of context managers
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb531 committed Jun 1, 2015
1 parent 41dac1b commit b2dff6b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 74 deletions.
93 changes: 46 additions & 47 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ def test_create_dir_structure():


@nose.with_setup(before_each, after_each)
def test_create_dir_structure_silent_fail():
@patch('src.local.os.makedirs', side_effect=OSError)
def test_create_dir_structure_silent_fail(makedirs):
'''should fail silently if intermediate directories already exist'''
config = get_test_config()
with patch('src.local.os.makedirs', side_effect=OSError):
swb.back_up(config)
swb.os.makedirs.assert_called_with(
os.path.expanduser(os.path.dirname(TEST_BACKUP_PATH)))
swb.back_up(config)
makedirs.assert_called_with(os.path.expanduser(
os.path.dirname(TEST_BACKUP_PATH)))


@nose.with_setup(before_each, after_each)
Expand Down Expand Up @@ -113,49 +113,48 @@ def test_purge_empty_dirs():


@nose.with_setup(before_each, after_each)
def test_keep_nonempty_dirs():
@patch('src.local.os.rmdir', side_effect=OSError)
def test_keep_nonempty_dirs(rmdir):
'''should not purge nonempty timestamped directories'''
config = get_test_config()
config.set('paths', 'local_backup', TEST_BACKUP_PATH_TIMESTAMPED)
with patch('src.local.os.rmdir', side_effect=OSError):
swb.back_up(config)
for path in mock_backups[:-3]:
swb.os.rmdir.assert_any_call(path)
swb.back_up(config)
for path in mock_backups[:-3]:
rmdir.assert_any_call(path)


@nose.with_setup(before_each, after_each)
def test_main_back_up():
@patch('src.local.back_up')
def test_main_back_up(back_up):
'''should call back_up() when config path is passed to main()'''
config = get_test_config()
args = [swb.__file__, TEST_CONFIG_PATH]
with patch('src.local.sys.argv', args, create=True):
with patch('src.local.back_up'):
swb.main()
swb.back_up.assert_called_with(config, stdout=None, stderr=None)
swb.main()
back_up.assert_called_with(config, stdout=None, stderr=None)


@nose.with_setup(before_each, after_each)
def test_missing_shlex_quote():
@patch('src.local.shlex')
def test_missing_shlex_quote(shlex):
'''should use pipes.quote() if shlex.quote() is missing (<3.3)'''
with patch('src.local.shlex'):
del swb.shlex.quote
config = get_test_config()
swb.back_up(config)
swb.subprocess.Popen.assert_any_call(
# Only check if path is quoted (ignore preceding arguments)
([ANY] * 6) + ['~/\'backups/mysite.sql.bz2\''],
stdin=ANY, stdout=None, stderr=None)
swb.shlex.quote = shlex.quote
del shlex.quote
config = get_test_config()
swb.back_up(config)
swb.subprocess.Popen.assert_any_call(
# Only check if path is quoted (ignore preceding arguments)
([ANY] * 6) + ['~/\'backups/mysite.sql.bz2\''],
stdin=ANY, stdout=None, stderr=None)


@nose.with_setup(before_each, after_each)
def test_ssh_error():
@patch('src.local.sys.exit')
def test_ssh_error(exit):
'''should exit if SSH process returns non-zero exit code'''
config = get_test_config()
swb.subprocess.Popen.return_value.returncode = 3
with patch('src.local.sys.exit'):
swb.back_up(config)
swb.sys.exit.assert_called_with(3)
swb.back_up(config)
exit.assert_called_with(3)


@nose.with_setup(before_each, after_each)
Expand All @@ -174,43 +173,43 @@ def test_quiet_mode():


@nose.with_setup(before_each, after_each)
def test_main_restore():
@patch('src.local.restore')
def test_main_restore(restore):
'''should call restore() when config path is passed to main()'''
config = get_test_config()
args = [swb.__file__, TEST_CONFIG_PATH, '-r', TEST_BACKUP_PATH]
with patch('src.local.sys.argv', args, create=True):
with patch('src.local.restore'):
swb.main()
swb.input.assert_called_with(ANY)
swb.restore.assert_called_with(config, TEST_BACKUP_PATH,
stdout=None, stderr=None)
swb.main()
swb.input.assert_called_with(ANY)
restore.assert_called_with(config, TEST_BACKUP_PATH,
stdout=None, stderr=None)


@nose.with_setup(before_each, after_each)
def test_force_mode():
@patch('src.local.restore')
def test_force_mode(restore):
'''should bypass restore confirmation in force mode'''
config = get_test_config()
args = [swb.__file__, '-f', TEST_CONFIG_PATH, '-r', TEST_BACKUP_PATH]
with patch('src.local.sys.argv', args, create=True):
with patch('src.local.restore'):
swb.main()
nose.assert_equal(swb.input.call_count, 0)
swb.restore.assert_called_with(config, TEST_BACKUP_PATH,
stdout=None, stderr=None)
swb.main()
nose.assert_equal(swb.input.call_count, 0)
restore.assert_called_with(config, TEST_BACKUP_PATH,
stdout=None, stderr=None)


@nose.with_setup(before_each, after_each)
def test_restore_confirm_cancel():
@patch('src.local.input')
def test_restore_confirm_cancel(input):
'''should exit script when user cancels restore confirmation'''
config = get_test_config()
args = [swb.__file__, TEST_CONFIG_PATH, '-r', TEST_BACKUP_PATH]
with patch('src.local.sys.argv', args, create=True):
with patch('src.local.input') as mock_input:
responses = ['n', 'N', ' n ', '']
for response in responses:
mock_input.return_value = response
with nose.assert_raises(Exception):
swb.main()
responses = ['n', 'N', ' n ', '']
for response in responses:
input.return_value = response
with nose.assert_raises(Exception):
swb.main()


@nose.with_setup(before_each, after_each)
Expand Down
54 changes: 27 additions & 27 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ def run_restore(wordpress_path=TEST_WP_PATH,


@nose.with_setup(before_each, after_each)
def test_route_back_up():
@patch('src.remote.back_up')
def test_route_back_up(back_up):
'''should call back_up() with args if respective action is passed'''
with patch('src.remote.back_up'):
swb.sys.argv = [swb.__file__, 'back-up', 'a', 'b', 'c']
swb.main()
swb.back_up.assert_called_once_with('a', 'b', 'c')
swb.sys.argv = [swb.__file__, 'back-up', 'a', 'b', 'c']
swb.main()
back_up.assert_called_once_with('a', 'b', 'c')


@nose.with_setup(before_each, after_each)
def test_route_restore():
@patch('src.remote.restore')
def test_route_restore(restore):
'''should call restore() with args if respective action is passed'''
with patch('src.remote.restore'):
swb.sys.argv = [swb.__file__, 'restore', 'a', 'b', 'c']
swb.main()
swb.restore.assert_called_once_with('a', 'b', 'c')
swb.sys.argv = [swb.__file__, 'restore', 'a', 'b', 'c']
swb.main()
restore.assert_called_once_with('a', 'b', 'c')


@nose.with_setup(before_each, after_each)
def test_route_purge_backup():
@patch('src.remote.purge_downloaded_backup')
def test_route_purge_backup(purge):
'''should call purge_backup() with args if respective action is passed'''
with patch('src.remote.purge_downloaded_backup'):
swb.sys.argv = [swb.__file__, 'purge-backup', 'a', 'b', 'c']
swb.main()
swb.purge_downloaded_backup.assert_called_once_with('a', 'b', 'c')
swb.sys.argv = [swb.__file__, 'purge-backup', 'a', 'b', 'c']
swb.main()
purge.assert_called_once_with('a', 'b', 'c')


@nose.with_setup(before_each, after_each)
Expand All @@ -66,12 +66,12 @@ def test_create_dir_structure():
swb.os.makedirs.assert_called_with(os.path.expanduser('~/backups'))


@patch('src.remote.os.makedirs', side_effect=OSError)
@nose.with_setup(before_each, after_each)
def test_create_dir_structure_silent_fail():
def test_create_dir_structure_silent_fail(makedirs):
'''should fail silently if intermediate directories already exist'''
with patch('src.remote.os.makedirs', side_effect=OSError):
run_back_up()
swb.os.makedirs.assert_called_with(os.path.expanduser('~/backups'))
run_back_up()
makedirs.assert_called_with(os.path.expanduser('~/backups'))


@nose.with_setup(before_each, after_each)
Expand All @@ -84,11 +84,11 @@ def test_dump_db():


@nose.with_setup(before_each, after_each)
def test_corrupted_backup():
@patch('src.remote.os.path.getsize', return_value=20)
def test_corrupted_backup(getsize):
'''should raise OSError if backup is corrupted'''
with patch('src.remote.os.path.getsize', return_value=20):
with nose.assert_raises(OSError):
run_back_up()
with nose.assert_raises(OSError):
run_back_up()


@nose.with_setup(before_each, after_each)
Expand Down Expand Up @@ -132,11 +132,11 @@ def test_purge_restored_backup():


@nose.with_setup(before_each, after_each)
def test_purge_restored_backup_silent_fail():
@patch('src.remote.os.remove', side_effect=OSError)
def test_purge_restored_backup_silent_fail(remove):
'''should fail silently if remote files do not exist after restore'''
with patch('src.remote.os.remove', side_effect=OSError):
run_restore()
swb.os.remove.assert_called_once_with(os.path.expanduser(TEST_DB_PATH))
run_restore()
remove.assert_called_once_with(os.path.expanduser(TEST_DB_PATH))


@nose.with_setup(before_each, after_each)
Expand Down

0 comments on commit b2dff6b

Please sign in to comment.