Exemplo n.º 1
0
    def _parse(cls, varname, item, problems):
        """Parse an item from the services: section."""
        service_type = None
        if is_string(item):
            service_type = item
            options = dict(type=service_type)
        elif isinstance(item, dict):
            service_type = item.get('type', None)
            if service_type is None:
                problems.append(
                    "Service {} doesn't contain a 'type' field.".format(
                        varname))
                return None
            options = deepcopy(item)
        else:
            problems.append(
                "Service {} should have a service type string or a dictionary as its value."
                .format(varname))
            return None

        if not EnvVarRequirement._parse_default(options, varname, problems):
            return None

        return dict(service_type=service_type,
                    env_var=varname,
                    options=options)
Exemplo n.º 2
0
def test_requirement_parse_default():
    null_default = dict(default=None)
    string_default = dict(default="foo")
    int_default = dict(default=42)
    float_default = dict(default=3.14)

    # invalid defaults
    bool_default = dict(default=True)
    list_default = dict(default=[])

    def type_error(value):
        return "default value for variable FOO must be null, a string, or a number, not {value}.".format(value=value)

    problems = []

    EnvVarRequirement._parse_default(null_default, "FOO", problems)
    assert null_default == dict()
    assert problems == []

    EnvVarRequirement._parse_default(string_default, "FOO", problems)
    assert string_default == dict(default="foo")
    assert problems == []

    EnvVarRequirement._parse_default(int_default, "FOO", problems)
    assert int_default == dict(default=42)
    assert problems == []

    EnvVarRequirement._parse_default(float_default, "FOO", problems)
    assert float_default == dict(default=3.14)
    assert problems == []

    EnvVarRequirement._parse_default(bool_default, "FOO", problems)
    assert problems == [type_error(True)]

    problems = []
    EnvVarRequirement._parse_default(list_default, "FOO", problems)
    assert problems == [type_error([])]