def test_set_uses_keyquery_correctly(self, res): state = State(res.value_2) query = KeyQuery(lambda substate: substate.index({"key_2": 5})) state.set("value", ["key_1", query, "key_2"]) assert state.get(["key_1", 1, "key_2"]) == "value"
def test_invalid_necessary_defaults_raises_error(self, res): state = State(res.value_2) # Invalid defaults provided and used assert pytest.raises(TypeError, state.set, "value", ["key_1", "wrong_key_type", "key_2"], [[], []]) # Invalid defaults provided but not used state.set("value", ["key_1", 0, "key_2"], [[], []]) assert state.get(["key_1", 0, "key_2"], [[], [], 0]) == "value"
def test_fewer_defaults_than_necessary_raises_error(self, res): state = State(res.value_2) # Not enough defaults provided assert pytest.raises(ValueError, state.get, ["key_1", 0, "nonexistent_key"], [[], {}]) assert pytest.raises(ValueError, state.set, "value", ["key_1", "wrong_key_type", "key_2"], [[]]) # Enough defaults provided state.set("value", ["key_1", 1, "key_2"], [[], {}]) # Set operation only needs one less path key assert state.get(["key_1", "wrong_key_type", "key_2"], [[], {}, 0]) == 0
def test_registered_set_no_pathkeys(self, res): state = State.with_extensions(Registrar)(res.value_1) state.register("test_label", []) state.registered_set(8, "test_label") assert state.get() == 8
def test_registered_get_uses_correct_defaults(self, res): state = State.with_extensions(Registrar)(res.value_2) state.register("test_label", ["key_1", "wrong_key_type", "key_2"], [[], {}, 0]) assert state.registered_get("test_label") == 0
def test_invalid_set_no_defaults_raises_error(self, res): state = State(res.value_2) assert pytest.raises(ValueError, state.set, "value", ["key_1", "wrong_key_type", "key_2"]) assert pytest.raises(ValueError, state.set, "value", ["nonexistent_key", 1, "key_2"])
def test_registered_set_uses_correct_path_keys(self, res): state = State.with_extensions(Registrar)(res.value_2) state.register("test_label", ["key_1", 1, "key_2"]) state.registered_set("value", "test_label") assert state.get() == {"key_1": [{}, {"key_2": "value"}, {}]}
def test_registered_get_with_partialquery(self, res): state = State.with_extensions(Registrar)(res.value_2) query = PartialQuery(lambda args: KeyQuery(lambda substate: args)) state.register("label", ["key_1", query, "key_2"]) assert state.registered_get("label", (1,)) == 5
def test_partialquery_without_args_raises_error(self, res): state = State.with_extensions(Registrar)(res.value_2) query = PartialQuery(lambda args: 0) state.register("label", ["key_1", query]) assert pytest.raises(IndexError, state.registered_get, "label") assert pytest.raises(IndexError, state.registered_set, "value", "label")
def test_registered_set_last_key_partialquery(self, res): state = State.with_extensions(Registrar)(res.value_2) query = PartialQuery(lambda args: KeyQuery(lambda substate: "key"+args)) state.register("label", ["key_1", 0, query]) state.registered_set("value", "label", ("_2",)) assert state.get(["key_1", 0, "key_2"]) == "value"
def test_listener_added_twice_is_only_called_once(self, res, listeners_obj): state = State.with_extensions(Listeners)(res.value_1) state.add_listener("get", listeners_obj.add_get_path_keys_to_list) state.add_listener("get", listeners_obj.add_get_path_keys_to_list) state.get(["key_1"]) assert listeners_obj.list == [["key_1"]]
def test_removed_listener_is_not_called(self, res, listeners_obj): state = State.with_extensions(Listeners)(res.value_1) state.add_listener("get", listeners_obj.add_get_path_keys_to_list) state.add_listener("get", listeners_obj.add_get_path_keys_to_list) state.remove_listener("get", listeners_obj.add_get_path_keys_to_list) state.get(["key_1"]) assert listeners_obj.list == []
def test_set_with_path_keys(self, res): state = State(res.value_2) state.set([13, 37], [ "key_1", 0, "key_2" ]) # Setting to a key that does not yet exist should add the new value assert state.get(["key_1", 0, "key_2"]) == [13, 37] state.set("value", [ "key_1", 0, "key_2" ]) # Setting to a key that exists should replace the existing value assert state.get(["key_1", 0, "key_2"]) == "value"
def test_added_listener_is_correctly_called(self, res, listeners_obj): state = State.with_extensions(Listeners)(res.value_1) state.add_listener("get", listeners_obj.add_get_path_keys_to_list) state.add_listener("set", listeners_obj.add_set_value_to_list) assert listeners_obj.list == [] state.get(["key_1"]) assert listeners_obj.list == [["key_1"]] state.set("value", ["key_1"]) assert listeners_obj.list == [["key_1"], "value"]
def test_registered_set_uses_correct_defaults(self, res): state = State.with_extensions(Registrar)(res.value_3) state.register("test_label", ["key_1", "nonexistent_key", "key_4"], [{}, {}, 0]) state.registered_set("value", "test_label") assert state.get() == { "key_1": { "key_2": [], "key_3": { "key_4": 8 }, "nonexistent_key": { "key_4": "value" } } }
def test_get_uses_attributename_correctly(self, res): state = State(res.value_4) assert state.get(["key_2", AttributeName("attribute_1"), "key_3"]) == 7
def test_registered_get_uses_correct_path_keys(self, res): state = State.with_extensions(Registrar)(res.value_2) state.register("test_label", ["key_1", 1, "key_2"]) assert state.registered_get("test_label") == 5
def test_set_applies_attributename_correctly(self, res): state = State(res.value_4) state.set("value", ["key_2", AttributeName("attribute_2")]) assert state.get(["key_2", AttributeName("attribute_2")]) == "value"
def test_get_with_path_keys(self, res): state = State(res.value_2) assert state.get(["key_1", 1, "key_2"]) == 5
def test_set_with_defaults(self, res): state = State(res.value_2) state.set(6, ["key_1", 0, "key_2"], [[], {}, 0]) assert state.get(["key_1", 0, "key_2"]) == 6
def test_state_is_correctly_set(self, res): state = State() assert state.get() == {} state = State(res.value_1) assert state.get() == res.value_1 state = State() state.set(res.value_2) assert state.get() == res.value_2