Exemple #1
0
    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'
Exemple #2
0
    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'
Exemple #3
0
    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'
Exemple #4
0
    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'
Exemple #5
0
    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'
Exemple #6
0
    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'
Exemple #7
0
    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
Exemple #8
0
    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'
Exemple #9
0
    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'
Exemple #10
0
    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