def test_prompt_input_validate_failure(self, capfd): # this is a bit complex, because our mocks need to do different things on first and second calls input_returns = ['hello', 'goodbye'] def input_se(*args): return input_returns.pop(0) input_mock = mock.MagicMock(side_effect=input_se) confirm_mock = mock.MagicMock() confirm_mock.return_value = True validate_returns = [None, 'eybdoog'] def validate_se(*args): return validate_returns.pop(0) validate_mock = mock.MagicMock(side_effect=validate_se) dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", validate_cb=validate_mock) assert input_mock.call_count == 2 out, err = capfd.readouterr() assert out == "ERROR: invalid response: hello\n" assert err == "" assert confirm_mock.call_count == 1 assert validate_mock.call_count == 2 assert foo == 'eybdoog'
def test_prompt_input_validate_failure(self, capfd): # this is a bit complex, because our mocks need to do different things on first and second calls input_returns = ['hello', 'goodbye'] def input_se(*args): return input_returns.pop(0) input_mock = mock.MagicMock(side_effect=input_se) confirm_mock = mock.MagicMock() confirm_mock.return_value = True validate_returns = [None, 'eybdoog'] def validate_se(*args): return validate_returns.pop(0) validate_mock = mock.MagicMock(side_effect=validate_se) dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", validate_cb=validate_mock) assert input_mock.call_count == 2 out, err = capfd.readouterr() assert out == "ERROR: invalid response: hello\n" assert err == "" assert confirm_mock.call_count == 1 assert validate_mock.call_count == 2 assert foo == 'eybdoog'
def test_prompt_input_default(self): input_mock = mock.MagicMock() input_mock.return_value = '' confirm_mock = mock.MagicMock() confirm_mock.return_value = True dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", default='bar') assert input_mock.call_count == 1 assert input_mock.call_args == mock.call("foo (default: bar): ") assert confirm_mock.call_count == 1 assert foo == 'bar'
def test_prompt_input_validate_success(self): input_mock = mock.MagicMock() input_mock.return_value = 'hello' confirm_mock = mock.MagicMock() confirm_mock.return_value = True validate_mock = mock.MagicMock() validate_mock.return_value = 'goodbye' dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", validate_cb=validate_mock) assert input_mock.call_count == 1 assert confirm_mock.call_count == 1 assert validate_mock.call_count == 1 assert foo == 'goodbye'
def test_prompt_input_default(self): input_mock = mock.MagicMock() input_mock.return_value = '' confirm_mock = mock.MagicMock() confirm_mock.return_value = True dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", default='bar') assert input_mock.call_count == 1 assert input_mock.call_args == mock.call("foo (default: bar): ") assert confirm_mock.call_count == 1 assert foo == 'bar'
def test_prompt_input_no_confirm(self): input_mock = mock.MagicMock() input_mock.return_value = 'hello' def confirm_se(*args): return confirm_returns.pop(0) confirm_returns = [False, True] confirm_mock = mock.MagicMock(side_effect=confirm_se) dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo") assert input_mock.call_count == 2 assert confirm_mock.call_count == 2 assert foo == 'hello'
def test_prompt_input_default_float(self): input_mock = mock.MagicMock() input_mock.return_value = '' confirm_mock = mock.MagicMock() confirm_mock.return_value = True validate_mock = mock.MagicMock() validate_mock.return_value = 123.456 dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", default=123.456, validate_cb=validate_mock) assert input_mock.call_count == 1 assert input_mock.call_args == mock.call("foo (default: 123.456): ") assert confirm_mock.call_count == 1 assert validate_mock.call_count == 1 assert validate_mock.call_args == mock.call('123.456') assert foo == 123.456
def test_prompt_input_validate_success(self): input_mock = mock.MagicMock() input_mock.return_value = 'hello' confirm_mock = mock.MagicMock() confirm_mock.return_value = True validate_mock = mock.MagicMock() validate_mock.return_value = 'goodbye' dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", validate_cb=validate_mock) assert input_mock.call_count == 1 assert confirm_mock.call_count == 1 assert validate_mock.call_count == 1 assert foo == 'goodbye'
def test_prompt_input_no_confirm(self): input_mock = mock.MagicMock() input_mock.return_value = 'hello' def confirm_se(*args): return confirm_returns.pop(0) confirm_returns = [False, True] confirm_mock = mock.MagicMock(side_effect=confirm_se) dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo") assert input_mock.call_count == 2 assert confirm_mock.call_count == 2 assert foo == 'hello'
def test_prompt_input_default_float(self): input_mock = mock.MagicMock() input_mock.return_value = '' confirm_mock = mock.MagicMock() confirm_mock.return_value = True validate_mock = mock.MagicMock() validate_mock.return_value = 123.456 dc = DnstestConfig() with mock.patch('pydnstest.config.DnstestConfig.input_wrapper', input_mock): with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_mock): foo = dc.prompt_input("foo", default=123.456, validate_cb=validate_mock) assert input_mock.call_count == 1 assert input_mock.call_args == mock.call("foo (default: 123.456): ") assert confirm_mock.call_count == 1 assert validate_mock.call_count == 1 assert validate_mock.call_args == mock.call('123.456') assert foo == 123.456