Exemplo n.º 1
0
 def test_initialization_with_mutable_mapping(self, mutable_mapping):
     d = DotDict(mutable_mapping)
     assert "chris" not in d
     assert "another" in d
Exemplo n.º 2
0
 def test_setitem_nonstring_key(self):
     d = DotDict()
     d[1] = 1
     assert d[1] == 1
     d[1] = 2
     assert d[1] == 2
Exemplo n.º 3
0
 def test_eq_empty(self):
     assert DotDict() == DotDict()
Exemplo n.º 4
0
 def test_del_with_getitem(self):
     d = DotDict(data=5)
     del d["data"]
     assert "data" not in d
     assert len(d) == 0
Exemplo n.º 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"
Exemplo n.º 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)
Exemplo n.º 7
0
 def test_reserved_attrs_raise_error_on_init(self, key):
     with pytest.raises(ValueError):
         d = DotDict({key: 5})
Exemplo n.º 8
0
 def test_setdefault_works(self):
     d = DotDict(chris="best")
     d.setdefault("data", 5)
     assert d["data"] == 5
     assert d["chris"] == "best"
Exemplo n.º 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")}
Exemplo n.º 10
0
 def test_attributeerror_is_thrown_when_accessing_nonexistent_attr(self):
     d = DotDict(data=5)
     with pytest.raises(AttributeError):
         d.nothing
Exemplo n.º 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"}
Exemplo n.º 12
0
 def test_keyerror_is_thrown_when_accessing_nonexistent_key(self):
     d = DotDict(data=5)
     with pytest.raises(KeyError):
         d["nothing"]
Exemplo n.º 13
0
 def test_copy(self):
     d = DotDict(a=1, b=2, c=3)
     assert d == d.copy()
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 16
0
 def test_dotdict_splats(self):
     d = DotDict(data=5)
     identity = lambda **kwargs: kwargs
     assert identity(**d) == {"data": 5}
Exemplo n.º 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
Exemplo n.º 18
0
    def test_dotdict_is_not_json_serializable_with_default_encoder(self):

        with pytest.raises(TypeError):
            json.dumps(DotDict(x=1))
Exemplo n.º 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
Exemplo n.º 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": {}}}
Exemplo n.º 21
0
 def test_reserved_attrs_raise_error_on_set(self, key):
     with pytest.raises(ValueError):
         d = DotDict(data=5)
         d.__setattr__(key, "value")
Exemplo n.º 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": {}}]}}
Exemplo n.º 23
0
 def test_del_with_attr(self):
     d = DotDict(data=5)
     del d.data
     assert "data" not in d
     assert len(d) == 0
Exemplo n.º 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
Exemplo n.º 25
0
 def test_setitem(self):
     d = DotDict()
     d["a"] = 1
     assert d["a"] == 1
     d["a"] = 2
     assert d["a"] == 2
Exemplo n.º 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
Exemplo n.º 27
0
 def test_initialize_from_nonstring_keys(self):
     d = DotDict({1: 1, "a": 2})
     assert d[1] == 1 and d["a"] == 2
Exemplo n.º 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)
Exemplo n.º 29
0
 def test_eq_empty_dict(self):
     assert DotDict() == {}
Exemplo n.º 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})