示例#1
0
def test_getters(builtin_pkg):
    req = build_req_tuple(
        {'int': '1', 'empty_int': '', 'str': 'hello', 'empty_str': '', 'bool': 'true', 'float': '1.1'}
    )
    val = utils.get_str_arg(req, 'str')
    assert isinstance(val, str) and val == 'hello'
    val = utils.get_str_arg(req, 'str_def', default='def')
    assert val == 'def'
    val = utils.get_str_arg(req, 'empty_str')
    assert val is None
    with mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
        val = utils.get_str_arg(req, 'str', default='def')
        assert val == 'def'
    val = utils.get_int_arg(req, 'int')
    assert isinstance(val, int) and val == 1
    val = utils.get_int_arg(req, 'int_def', default=2)
    assert val == 2
    val = utils.get_int_arg(req, 'empty_int')
    assert val is None
    with mock.patch('{}.int'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
        val = utils.get_int_arg(req, 'int', default=2)
        assert val == 2
    val = utils.get_bool_arg(req, 'bool')
    assert isinstance(val, bool) and val

    val = utils.get_float_arg(req, 'float')
    assert isinstance(val, float) and val == 1.1
    val = utils.get_float_arg(req, 'int_def', default=2.0)
    assert val == 2.0
    val = utils.get_float_arg(req, 'empty_float')
    assert val is None
    with mock.patch('{}.float'.format(builtin_pkg), mock.Mock(side_effect=Exception)):
        val = utils.get_float_arg(req, 'float', default=2.0)
        assert val == 2
示例#2
0
def test_getters(builtin_pkg):
    req = build_req_tuple({
        "int": "1",
        "empty_int": "",
        "str": "hello",
        "empty_str": "",
        "bool": "true",
        "float": "1.1",
    })
    val = utils.get_str_arg(req, "str")
    assert isinstance(val, str) and val == "hello"
    val = utils.get_str_arg(req, "str_def", default="def")
    assert val == "def"
    val = utils.get_str_arg(req, "empty_str")
    assert val is None
    with mock.patch("{}.str".format(builtin_pkg),
                    mock.Mock(side_effect=Exception)):
        val = utils.get_str_arg(req, "str", default="def")
        assert val == "def"
    val = utils.get_int_arg(req, "int")
    assert isinstance(val, int) and val == 1
    val = utils.get_int_arg(req, "int_def", default=2)
    assert val == 2
    val = utils.get_int_arg(req, "empty_int")
    assert val is None
    with mock.patch("{}.int".format(builtin_pkg),
                    mock.Mock(side_effect=Exception)):
        val = utils.get_int_arg(req, "int", default=2)
        assert val == 2
    val = utils.get_bool_arg(req, "bool")
    assert isinstance(val, bool) and val

    val = utils.get_float_arg(req, "float")
    assert isinstance(val, float) and val == 1.1
    val = utils.get_float_arg(req, "int_def", default=2.0)
    assert val == 2.0
    val = utils.get_float_arg(req, "empty_float")
    assert val is None
    with mock.patch("{}.float".format(builtin_pkg),
                    mock.Mock(side_effect=Exception)):
        val = utils.get_float_arg(req, "float", default=2.0)
        assert val == 2