def test_replace_ref_nodes_with_names(): modelb = ModelB() modelb.name = "modelbname" modela = ModelA() modela.int_field = 2 modela.ref_field = modelb modela.ref_field2 = "user_set_name" model_list = [modelb, modela] schema._replace_ref_nodes_with_names(modela, model_list) assert modela.ref_field == "modelbname" assert modela.ref_field2 == "user_set_name"
def test_replace_ref_nodes_with_names_dicts(): """Tests that it works with lists.""" class Model(optplan.ProblemGraphNodeSchema): type = types.StringType(default="Model") value = types.DictType( optplan.ReferenceType(optplan.ProblemGraphNodeSchema)) modelb1 = ModelB(name="m1", int_field=1) modelb2 = ModelB(name="m2", int_field=2) model = Model(name="m3", value={"1": modelb1, "2": modelb2}) model_list = [modelb1, modelb2, model] schema._replace_ref_nodes_with_names(model, model_list) assert model.value == {"1": modelb1.name, "2": modelb2.name}
def test_replace_ref_nodes_with_names_nested(): class OuterModel(optplan.ProblemGraphNode.Schema): type = types.StringType(default="Model") value = optplan.ReferenceType(optplan.ProblemGraphNode.Schema) class InnerModel(optplan.ProblemGraphNode.Schema): type = types.StringType(default="Model2") value = optplan.ReferenceType(optplan.ProblemGraphNode.Schema) modelb = ModelB(name="m1") inner_model = InnerModel(name="m2", value=modelb) outer_model = OuterModel(name="m3", value=inner_model) model_list = [outer_model, inner_model, modelb] schema._replace_ref_nodes_with_names(outer_model, model_list) assert outer_model.value == inner_model.name assert inner_model.value == modelb.name