示例#1
0
def test_verify_key__multiple__allowed():
    """There are some keys where multiple types are allowed."""

    conf = Config()
    conf.foo = 3.14
    conf._verify_key("foo", (list, str))
    assert conf.foo == [3.14]

    conf.foo = 3.14
    conf._verify_key("foo", (str, list))  # assert order matters
    assert conf.foo == "3.14"
示例#2
0
def test_verify_key__multiple__allowed():
    """There are some keys where multiple types are allowed."""

    conf = Config()
    conf.foo = 3.14
    conf._verify_key("foo", (list, str))
    assert conf.foo == [3.14]

    conf.foo = 3.14
    conf._verify_key("foo", (str, list))  # assert order matters
    assert conf.foo == "3.14"
示例#3
0
def test_verify_key__dict_types():
    """If a dict is provided with types, the key/values should be coerced."""

    conf = Config()
    conf.foo = {3.14: "500.123"}
    conf._verify_key("foo", {str: float})
    assert conf.foo == {"3.14": 500.123}
示例#4
0
def test_verify_key__str_list_values():
    """If the type is list, the items inside should be strings."""

    conf = Config()
    conf.foo = ["bar", 3.14, "baz"]
    conf._verify_key("foo", list)
    assert conf.foo == ["bar", "3.14", "baz"]
示例#5
0
def test_verify_key__failure_coerce():
    """Should try to coerce values into their types when not list or dict."""

    conf = Config()
    conf.foo = 3.14
    conf._verify_key("foo", str)
    assert conf.foo == "3.14"
示例#6
0
def test_verify_key__list_object():
    """If an attribute should be a list, it should be listed."""

    conf = Config()
    conf.foo = "not a list"
    conf._verify_key("foo", list)
    assert conf.foo == ["not a list"]
示例#7
0
def test_verify_key__dict_types():
    """If a dict is provided with types, the key/values should be coerced."""

    conf = Config()
    conf.foo = {3.14: "500.123"}
    conf._verify_key("foo", {str: float})
    assert conf.foo == {"3.14": 500.123}
示例#8
0
def test_verify_key__str_list_values():
    """If the type is list, the items inside should be strings."""

    conf = Config()
    conf.foo = ["bar", 3.14, "baz"]
    conf._verify_key("foo", list)
    assert conf.foo == ["bar", "3.14", "baz"]
示例#9
0
def test_verify_key__failure_coerce():
    """Should try to coerce values into their types when not list or dict."""

    conf = Config()
    conf.foo = 3.14
    conf._verify_key("foo", str)
    assert conf.foo == "3.14"
示例#10
0
def test_verify_key__list_object():
    """If an attribute should be a list, it should be listed."""

    conf = Config()
    conf.foo = "not a list"
    conf._verify_key("foo", list)
    assert conf.foo == ["not a list"]
示例#11
0
def test_verify_key__failure():
    """If unable to coerce into the expected type, raise TypeError."""

    conf = Config()
    conf.foo = "something"
    with pytest.raises(TypeError) as error:
        conf._verify_key("foo", float)
    assert error.value.args[0] == "foo should be a float, not str!"
示例#12
0
def test_verify_key__dict_failure():
    """Do not try to coerce something that should be a dict into one."""

    conf = Config()
    conf.foo = "not a dict"
    with pytest.raises(TypeError) as error:
        conf._verify_key("foo", {})
    assert error.value.args[0] == "foo should be a dict, not str!"
示例#13
0
def test_verify_key__failure():
    """If unable to coerce into the expected type, raise TypeError."""

    conf = Config()
    conf.foo = "something"
    with pytest.raises(TypeError) as error:
        conf._verify_key("foo", float)
    assert error.value.args[0] == "foo should be a float, not str!"
示例#14
0
def test_verify_key__dict_failure():
    """Do not try to coerce something that should be a dict into one."""

    conf = Config()
    conf.foo = "not a dict"
    with pytest.raises(TypeError) as error:
        conf._verify_key("foo", {})
    assert error.value.args[0] == "foo should be a dict, not str!"
示例#15
0
def test_verify_key__multiple__failure():
    """When unable to coerce to any type, raise TypeError."""

    conf = Config()
    conf.foo = mock.MagicMock(spec=False)
    conf.foo.__float__ = mock.Mock(side_effect=ValueError)
    conf.foo.__str__ = mock.Mock(side_effect=ValueError)
    with pytest.raises(TypeError) as err:
        conf._verify_key("foo", (float, str))
    assert err.value.args[0] == "foo should be a float or str, not MagicMock!"
示例#16
0
def test_verify_key__multiple__failure():
    """When unable to coerce to any type, raise TypeError."""

    conf = Config()
    conf.foo = mock.MagicMock(spec=False)
    conf.foo.__float__ = mock.Mock(side_effect=ValueError)
    conf.foo.__str__ = mock.Mock(side_effect=ValueError)
    with pytest.raises(TypeError) as err:
        conf._verify_key("foo", (float, str))
    assert err.value.args[0] == "foo should be a float or str, not MagicMock!"