def test_substitution_without_id(): """Test that trying to substitute links if uids haven't been assigned throws an error.""" mat = MaterialRun("A material with no id") meas = MeasurementRun("A measurement with no id", material=mat) with pytest.raises(ValueError): substitute_links( meas ), "subbed = substitute_links should fail if objects don't have uids" with pytest.raises(ValueError): substitute_links([meas, mat]), \ "subbed = substitute_links should fail if objects don't have uids" with pytest.raises(ValueError): substitute_links(meas.as_dict()), \ "subbed = substitute_links should fail if objects don't have uids" # Create a dictionary in which either the key or value is missing a uid meas.add_uid('id', str(uuid4())) with pytest.raises(ValueError): substitute_links({mat: meas}), \ "subbed = substitute_links should fail if objects don't have uids" with pytest.raises(ValueError): substitute_links({meas: mat}), \ "subbed = substitute_links should fail if objects don't have uids"
def ingest_material_run(data, material_spec=None, process_run=None): """Ingest material run with data, a material spec, and an originating process run.""" if isinstance(data, list): return [ingest_material_run(x, material_spec) for x in data] if not isinstance(data, dict): raise ValueError("This ingester operates on dict, but got {}".format(type(data))) material = MaterialRun() sample_id = data.get("sample_id") if sample_id: material.add_uid("given_sample_id", sample_id) tags = data.get("tags") if tags: material.tags = tags for experiment in data.get("experiments", []): measurement = MeasurementRun() for name in set(known_properties.keys()).intersection(experiment.keys()): prop = Property( name=name, template=known_properties[name], value=_parse_value(experiment[name]) ) measurement.properties.append(prop) for name in set(known_conditions.keys()).intersection(experiment.keys()): cond = Condition( name=name, template=known_conditions[name], value=_parse_value(experiment[name]) ) measurement.conditions.append(cond) for name in set(known_parameters.keys()).intersection(experiment.keys()): param = Parameter( name=name, template=known_parameters[name], value=_parse_value(experiment[name]) ) measurement.parameters.append(param) scan_id = experiment.get("scan_id") if scan_id: measurement.add_uid("given_scan_id", scan_id) tags = experiment.get("tags") if tags: measurement.tags = tags measurement.material = material if material_spec: material.material_spec = material_spec if process_run: material.process = process_run return material