def test_context_loads_secrets_from_config(monkeypatch): secrets_dict = DotDict(password="******") config = DotDict(context=DotDict(secrets=secrets_dict)) monkeypatch.setattr(prefect.utilities.context, "config", config) fresh_context = Context() assert "secrets" in fresh_context assert fresh_context.secrets == secrets_dict
def test_context_loads_values_from_config(monkeypatch): subsection = DotDict(password="******") config = DotDict(context=DotDict(subsection=subsection, my_key="my_value")) monkeypatch.setattr(prefect.utilities.context, "config", config) fresh_context = Context() assert "subsection" in fresh_context assert fresh_context.my_key == "my_value" assert fresh_context.subsection == subsection
def test_len(self): d = DotDict({"chris": 10, "attr": "string", "other": lambda x: {}}) assert len(d) == 3 a = DotDict() assert len(a) == 0 a.update(new=4) assert len(a) == 1 del d["chris"] assert len(d) == 2
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
def test_dotdict_query_parsing(): verify( query=DotDict(query=DotDict(books={"id"})), expected=""" query { books { id } } """, )
def test_repr_sorts_mixed_keys(self): d = DotDict() assert repr(d) == "<DotDict>" d["a"] = 1 d[1] = 1 d["b"] = 1 assert repr(d) == "<DotDict: 'a', 'b', 1>"
def test_pass_dotdicts_as_args(): verify( query={ "query": { with_args("books", DotDict(author=DotDict(name=DotDict(first="first")))): {"id"} } }, expected=""" query { books(author: { name: { first: "first" } }) { id } } """, )
def test_attr_updates_and_key_updates_agree(self): d = DotDict(data=5) d.data += 1 assert d["data"] == 6 d["new"] = "value" assert d.new == "value" d.another_key = "another_value" assert d["another_key"] == "another_value"
def test_oneofschema_handles_unknown_fields(self): class ChildSchema(marshmallow.Schema): x = marshmallow.fields.Integer() class ParentSchema(OneOfSchema): type_schemas = {"Child": ChildSchema} child = ParentSchema().load(DotDict(type="Child", x="5", y="6")) assert child["x"] == 5 assert not hasattr(child, "y")
def test_getattr_default(self): with Flow(name="test") as f: x = Parameter("x") a = GetAttr(default="foo")(x, "missing") b = GetAttr()(x, "missing", "bar") c = GetAttr()(x, "missing", default="baz") state = f.run(parameters=dict(x=DotDict(a=1, b=2, c=3))) assert state.result[a].result == "foo" assert state.result[b].result == "bar" assert state.result[c].result == "baz"
def test_clear_clears_keys_and_attrs(self): d = DotDict(data=5, chris="best") assert "data" in d d.clear() assert "data" not in d assert len(d) == 0 d.new_key = 63 assert "new_key" in d d.clear() assert len(d) == 0 assert "new_key" not in d
def test_oneofschema_load_dotdict(self): """ Tests that modified OneOfSchema can load data from a DotDict (standard can not) """ class ChildSchema(marshmallow.Schema): x = marshmallow.fields.Integer() class ParentSchema(OneOfSchema): type_schemas = {"Child": ChildSchema} child = ParentSchema().load(DotDict(type="Child", x="5")) assert child["x"] == 5
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
def test_dotdict_splats(self): d = DotDict(data=5) identity = lambda **kwargs: kwargs assert identity(**d) == {"data": 5}
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})
def test_dotdict_is_not_json_serializable_with_default_encoder(self): with pytest.raises(TypeError): json.dumps(DotDict(x=1))
def test_iter(self): d = DotDict(data=5, chris="best") res = set() for item in d: res.add(item) assert res == {"data", "chris"}
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
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": {}}}
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
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")}
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": {}}]}}
def test_protect_critical_default_true(): x = DotDict() assert x.__protect_critical_keys__
def test_protect_critical_keys_active(): x = DotDict() with pytest.raises(ValueError): x.update = 1
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
def test_setdefault_works(self): d = DotDict(chris="best") d.setdefault("data", 5) assert d["data"] == 5 assert d["chris"] == "best"
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)
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_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
def test_initialization_with_mutable_mapping(self, mutable_mapping): d = DotDict(mutable_mapping) assert "chris" not in d assert "another" in d