예제 #1
0
def test_lookup_nested_absent_raise():
    """lookup raises KeyError for absent items by default"""
    data = {"a": {"b": {"c": 123}}}

    with pytest.raises(KeyError) as error:
        lookup(data, "a.b.d")

    assert "a.b.d" in str(error.value)
예제 #2
0
    def _data_to_init_args(cls, data):
        # maps from raw Pulp dict to a kwargs dict used to initialize
        # a new object of this class.
        #
        # The default implementation looks at defined attributes and metadata
        # (PULP2_FIELD).  If this is not sufficient, subclasses can override
        # this, and can also call super() to reuse this as needed.
        out = {}
        fields = attr.fields(cls)
        absent = object()

        for field in fields:
            pulp_field = field.metadata.get(PULP2_FIELD)
            if pulp_field:
                value = lookup(data, pulp_field, absent)
                if value is not absent:
                    converter = get_converter(field, value)
                    value = converter(value)
                    out[field.name] = value

        return out
예제 #3
0
def test_lookup_nested_found():
    """lookup can find elements in nested dicts"""
    data = {"a": {"b": {"c": 123}}}

    assert lookup(data, "a.b.c") == 123
예제 #4
0
def test_lookup_nested_absent_default():
    """lookup returns given default value for failed lookup"""
    data = {"a": {"b": {"c": 123}}}

    assert lookup(data, "a.d.c", "my-default") == "my-default"