예제 #1
0
 def test_initialization_with_mutable_mapping(self, mutable_mapping):
     d = DotDict(mutable_mapping)
     assert "chris" not in d
     assert "another" in d
예제 #2
0
 def test_setitem_nonstring_key(self):
     d = DotDict()
     d[1] = 1
     assert d[1] == 1
     d[1] = 2
     assert d[1] == 2
예제 #3
0
 def test_eq_empty(self):
     assert DotDict() == DotDict()
예제 #4
0
 def test_del_with_getitem(self):
     d = DotDict(data=5)
     del d["data"]
     assert "data" not in d
     assert len(d) == 0
예제 #5
0
 def test_get(self):
     d = DotDict(data=5)
     assert d.get("data") == 5
     assert d.get("no_data") is None
     assert d.get("no_data", "fallback") == "fallback"
예제 #6
0
    assert restored == {"a": "b"}


def test_restore_flattened_dict_with_dict_class():
    nested_dict = DotDict(a=DotDict(x=1), b=DotDict(y=2))
    flat = collections.dict_to_flatdict(nested_dict)
    restored = collections.flatdict_to_dict(flat)
    assert isinstance(restored, dict)

    restored_dotdict = collections.flatdict_to_dict(flat, dct_class=DotDict)
    assert isinstance(restored_dotdict, DotDict)
    assert isinstance(restored_dotdict.a, DotDict)
    assert restored_dotdict.a == nested_dict.a


@pytest.fixture(params=[dict(another=500), DotDict(another=500)])
def mutable_mapping(request):
    "MutableMapping objects to test with"
    return request.param


class TestDotDict:
    def test_initialization_with_kwargs(self):
        d = DotDict(chris=10, attr="string", other=lambda x: {})
        assert "another" not in d
        assert "chris" in d
        assert "attr" in d
        assert "other" in d

    def test_initialization_with_mutable_mapping(self, mutable_mapping):
        d = DotDict(mutable_mapping)
예제 #7
0
 def test_reserved_attrs_raise_error_on_init(self, key):
     with pytest.raises(ValueError):
         d = DotDict({key: 5})
예제 #8
0
 def test_setdefault_works(self):
     d = DotDict(chris="best")
     d.setdefault("data", 5)
     assert d["data"] == 5
     assert d["chris"] == "best"
예제 #9
0
 def test_items(self):
     d = DotDict(data=5, chris="best")
     res = set()
     for k, v in d.items():
         res.add((k, v))
     assert res == {("data", 5), ("chris", "best")}
예제 #10
0
 def test_attributeerror_is_thrown_when_accessing_nonexistent_attr(self):
     d = DotDict(data=5)
     with pytest.raises(AttributeError):
         d.nothing
예제 #11
0
 def test_iter(self):
     d = DotDict(data=5, chris="best")
     res = set()
     for item in d:
         res.add(item)
     assert res == {"data", "chris"}
예제 #12
0
 def test_keyerror_is_thrown_when_accessing_nonexistent_key(self):
     d = DotDict(data=5)
     with pytest.raises(KeyError):
         d["nothing"]
예제 #13
0
 def test_copy(self):
     d = DotDict(a=1, b=2, c=3)
     assert d == d.copy()
예제 #14
0
 def test_update_with_kwargs(self):
     d = DotDict(chris=10, attr="string", other=lambda x: {})
     assert "another" not in d
     d.update(another=500)
     assert "another" in d
     assert d["another"] == 500
예제 #15
0
 def test_getattr_nested(self):
     with Flow(name="test") as f:
         z = GetAttr()(Parameter("x"), "a.b.c")
     state = f.run(parameters=dict(x=DotDict(a=DotDict(b=DotDict(c=1)))))
     assert state.result[z].result == 1
예제 #16
0
 def test_dotdict_splats(self):
     d = DotDict(data=5)
     identity = lambda **kwargs: kwargs
     assert identity(**d) == {"data": 5}
예제 #17
0
 def test_getattr_dynamic(self):
     with Flow(name="test") as f:
         z = GetAttr()(Parameter("x"), Parameter("y"))
     state = f.run(parameters=dict(x=DotDict(a=1, b=2, c=3), y="b"))
     assert state.result[z].result == 2
예제 #18
0
    def test_dotdict_is_not_json_serializable_with_default_encoder(self):

        with pytest.raises(TypeError):
            json.dumps(DotDict(x=1))
예제 #19
0
 def test_update_with_mutable_mapping(self, mutable_mapping):
     d = DotDict({"chris": 10, "attr": "string", "other": lambda x: {}})
     assert "another" not in d
     d.update(mutable_mapping)
     assert "another" in d
예제 #20
0
 def test_dotdict_to_dict(self):
     d = DotDict(x=5, y=DotDict(z="zzz", qq=DotDict()))
     assert d.to_dict() == {"x": 5, "y": {"z": "zzz", "qq": {}}}
예제 #21
0
 def test_reserved_attrs_raise_error_on_set(self, key):
     with pytest.raises(ValueError):
         d = DotDict(data=5)
         d.__setattr__(key, "value")
예제 #22
0
 def test_dotdict_to_dict_with_lists_of_dicts(self):
     d = DotDict(x=5, y=DotDict(z=[DotDict(abc=10, qq=DotDict())]))
     assert d.to_dict() == {"x": 5, "y": {"z": [{"abc": 10, "qq": {}}]}}
예제 #23
0
 def test_del_with_attr(self):
     d = DotDict(data=5)
     del d.data
     assert "data" not in d
     assert len(d) == 0
예제 #24
0
 def test_initialization_with_kwargs(self):
     d = DotDict(chris=10, attr="string", other=lambda x: {})
     assert "another" not in d
     assert "chris" in d
     assert "attr" in d
     assert "other" in d
예제 #25
0
 def test_setitem(self):
     d = DotDict()
     d["a"] = 1
     assert d["a"] == 1
     d["a"] = 2
     assert d["a"] == 2
예제 #26
0
 def test_getattr_constant(self):
     with Flow(name="test") as f:
         z = GetAttr()(Parameter("x"), "b")
     state = f.run(parameters=dict(x=DotDict(a=1, b=2, c=3)))
     assert state.result[z].result == 2
예제 #27
0
 def test_initialize_from_nonstring_keys(self):
     d = DotDict({1: 1, "a": 2})
     assert d[1] == 1 and d["a"] == 2
예제 #28
0
 def test_getattr_missing(self):
     with Flow(name="test") as f:
         a = GetAttr()(Parameter("x"), "missing")
     state = f.run(parameters=dict(x=DotDict(a=1, b=2, c=3)))
     assert isinstance(state.result[a].result, AttributeError)
예제 #29
0
 def test_eq_empty_dict(self):
     assert DotDict() == {}
예제 #30
0
json_test_values = [
    1,
    [1, 2],
    "1",
    [1, "2"],
    {
        "x": 1
    },
    {
        "x": "1",
        "y": {
            "z": 3
        }
    },
    DotDict({
        "x": "1",
        "y": [DotDict(z=3)]
    }),
]


class Child(marshmallow.Schema):
    x = marshmallow.fields.String()


def get_child(obj, context):
    if obj.get("child key") is False:
        return marshmallow.missing
    else:
        return obj.get("child key", {"x": -1})