Example #1
0
def test_invalid__feedback(capfd):
    """The user should receive some feedback when the value doesn't coerce."""

    class TestObject(object):
        def __init__(self):
            self.str_count = 0

        def __str__(self):
            self.str_count += 1
            if self.str_count == 1:
                raise IOError
            else:
                return "test object"

    conf = Config(name="foo")
    coerce_patch = mock.patch.object(
        configure,
        "coerce_to_expected",
        return_value=TestObject(),
    )

    with mock.patch.object(configure, "ask_for_value"):
        with coerce_patch:
            configure.set_value_in_config("name", conf, conf._KEYS)

    out, err = capfd.readouterr()
    assert "name as test object failed to verify. should be str" in err
    assert "name set to: 'test object'\n" == out
Example #2
0
def test_invalid__feedback(capfd):
    """The user should receive some feedback when the value doesn't coerce."""
    class TestObject(object):
        def __init__(self):
            self.str_count = 0

        def __str__(self):
            self.str_count += 1
            if self.str_count == 1:
                raise IOError
            else:
                return "test object"

    conf = Config(name="foo")
    coerce_patch = mock.patch.object(
        configure,
        "coerce_to_expected",
        return_value=TestObject(),
    )

    with mock.patch.object(configure, "ask_for_value"):
        with coerce_patch:
            configure.set_value_in_config("name", conf, conf._KEYS)

    out, err = capfd.readouterr()
    assert "name as test object failed to verify. should be str" in err
    assert "name set to: 'test object'\n" == out
Example #3
0
def test_value_in_config__not_classifiers(capfd):
    """Any other key should be the same."""

    conf = Config()
    with mock.patch.object(configure, "ask_for_value", return_value="Frank"):
        configure.set_value_in_config("author", conf, conf._KEYS)
    out, err = capfd.readouterr()
    assert out == "author set to: 'Frank'\n"
    assert not err
Example #4
0
def test_value_in_config__classifiers(capfd):
    """The key 'classifiers' should be handled."""

    conf = Config()
    with mock.patch.object(configure, "handle_classifiers", return_value="ok"):
        configure.set_value_in_config("classifiers", conf, conf._KEYS)
    out, err = capfd.readouterr()
    assert out == "classifiers set to: ['ok']\n"
    assert not err
Example #5
0
def test_value_in_config__not_classifiers(capfd):
    """Any other key should be the same."""

    conf = Config()
    with mock.patch.object(configure, "ask_for_value", return_value="Frank"):
        configure.set_value_in_config("author", conf, conf._KEYS)
    out, err = capfd.readouterr()
    assert out == "author set to: 'Frank'\n"
    assert not err
Example #6
0
def test_value_in_config__classifiers(capfd):
    """The key 'classifiers' should be handled."""

    conf = Config()
    with mock.patch.object(configure, "handle_classifiers", return_value="ok"):
        configure.set_value_in_config("classifiers", conf, conf._KEYS)
    out, err = capfd.readouterr()
    assert out == "classifiers set to: ['ok']\n"
    assert not err
Example #7
0
def test_value_in_config__invalid_try_again():
    """If the return from the user is invalid, they should be re-prompted."""

    conf = Config()
    user_input_patch = mock.patch.object(
        configure,
        "ask_for_value",
        side_effect=iter([
            "{'what': 12.3",  # malformed input
            "{'what': 1 / 4}",  # valid input, but not for the spec
            "{'what': ['ok']}",  # valid input
        ]))
    with user_input_patch:
        configure.set_value_in_config("entry_points", conf, conf._KEYS)

    assert conf.entry_points == {"what": ["ok"]}
Example #8
0
def test_value_in_config__invalid_try_again():
    """If the return from the user is invalid, they should be re-prompted."""

    conf = Config()
    user_input_patch = mock.patch.object(
        configure,
        "ask_for_value",
        side_effect=iter([
            "{'what': 12.3",     # malformed input
            "{'what': 1 / 4}",   # valid input, but not for the spec
            "{'what': ['ok']}",  # valid input
        ])
    )
    with user_input_patch:
        configure.set_value_in_config("entry_points", conf, conf._KEYS)

    assert conf.entry_points == {"what": ["ok"]}