diff --git a/src/caosadvancedtools/models/data_model.py b/src/caosadvancedtools/models/data_model.py index 266414893bcdf1ab45ee1345fc549e15f4a66250..1390cf2e45b83c7fd715e674706bc98173db4ebf 100644 --- a/src/caosadvancedtools/models/data_model.py +++ b/src/caosadvancedtools/models/data_model.py @@ -111,8 +111,11 @@ class DataModel(dict): existing_entities = db.Container().extend( DataModel.entities_without( self.values(), [e.name.lower() for e in non_existing_entities])) + self.sync_ids_by_name(tmp_exist) + + if len(non_existing_entities) > 0: if verbose: print("New entities:") @@ -174,7 +177,7 @@ class DataModel(dict): Args ---- entities : iterable - The entities to be retrieved. This object will not be moidified. + The entities to be retrieved. This object will not be modified. Raises ------ diff --git a/src/caosadvancedtools/models/parser.py b/src/caosadvancedtools/models/parser.py index 964cf1f5679b4b9bcdb0654393b21d8cee9b45ad..4fa529aae410b2d5fafd1bd57dfd7c6194fe8124 100644 --- a/src/caosadvancedtools/models/parser.py +++ b/src/caosadvancedtools/models/parser.py @@ -341,37 +341,38 @@ debug : bool, optional f"invalid keyword in line {entity['__line__']}:", 1) raise ValueError(err_str, *err.args[1:]) from err + # TODO: functionality commented out, to be able to test failing test first. # Update properties that are part of record types: # e.g. add their datatypes, units etc.. # Otherwise comparison of existing models and the parsed model become difficult. - for name, ent in self.model.items(): - if not isinstance(ent, db.RecordType): - continue - props = ent.get_properties() - for prop in props: - if prop.name in self.model: - model_prop = self.model[prop.name] - # The information must be missing, we don't want to overwrite it accidentally: - if prop.datatype is not None and prop.datatype != model_prop.datatype: - # breakpoint() - raise RuntimeError("datatype must not be set, here. This is probably a bug.") - if prop.unit is not None and prop.unit != model_prop.unit: - # continue - raise RuntimeError("unit must not be set, here. This is probably a bug.") - if prop.description is not None and prop.description != model_prop.description: - # continue - raise RuntimeError("description must not be set, here. This is probably a bug.") - - # If this property has a more detailed definition in the model, - # copy over the information: - - if isinstance(model_prop, db.RecordType): - # in this case the datatype equals the name of the record type: - prop.datatype = prop.name - else: - prop.datatype = model_prop.datatype - prop.unit = model_prop.unit - prop.description = model_prop.description + # for name, ent in self.model.items(): + # if not isinstance(ent, db.RecordType): + # continue + # props = ent.get_properties() + # for prop in props: + # if prop.name in self.model: + # model_prop = self.model[prop.name] + # # The information must be missing, we don't want to overwrite it accidentally: + # if prop.datatype is not None and prop.datatype != model_prop.datatype: + # # breakpoint() + # raise RuntimeError("datatype must not be set, here. This is probably a bug.") + # if prop.unit is not None and prop.unit != model_prop.unit: + # # continue + # raise RuntimeError("unit must not be set, here. This is probably a bug.") + # if prop.description is not None and prop.description != model_prop.description: + # # continue + # raise RuntimeError("description must not be set, here. This is probably a bug.") + # + # # If this property has a more detailed definition in the model, + # # copy over the information: + # + # if isinstance(model_prop, db.RecordType): + # # in this case the datatype equals the name of the record type: + # prop.datatype = prop.name + # else: + # prop.datatype = model_prop.datatype + # prop.unit = model_prop.unit + # prop.description = model_prop.description return DataModel(self.model.values()) diff --git a/unittests/test_yaml_model_parser.py b/unittests/test_yaml_model_parser.py index 574b41da25e40984c0a1eef62f39080c88f1d1e0..56c8672b0e073101a38329037d7006e99b88a359 100644 --- a/unittests/test_yaml_model_parser.py +++ b/unittests/test_yaml_model_parser.py @@ -27,6 +27,8 @@ from caosadvancedtools.models.parser import (TwiceDefinedException, parse_model_from_string, parse_model_from_yaml) +from unittests.mock import Mock + from linkahead.apiutils import compare_entities @@ -645,7 +647,7 @@ RT2: }""" -def test_comparison_yaml_model(): +def test_comparison_yaml_model(capfd): """ Test for this issue: https://gitlab.indiscale.com/caosdb/src/caosdb-advanced-user-tools/-/issues/130 @@ -707,7 +709,24 @@ test_reference: c2 = compare_entities(model["RT1"], entities[1]) c3 = compare_entities(model["RT2"], entities[2]) c4 = compare_entities(model["test_reference"], entities[3]) + for cs in (c1, c2, c3, c4): + assert "id" not in cs[0] + assert "id" in cs[1] + + mq = Mock() + def mq_init(self, query): + self.query = query + + def mq_execute(self, unique=True): + pass + + mq.__init__ = mq_init + mq.execute.side_effect = mq_execute + caosadvancedtools.models.parser.db.Query = mq + model.sync_data_model(True, True) + stdout, stderr = capfd.readouterr() + # TODO: test that there were no changes required