Beispiel #1
0
def test_scope_control():
    """Serializing a nested object should be identical to individually serializing each piece."""
    input_material = MaterialSpec()
    process = ProcessSpec()
    IngredientSpec(material=input_material, process=process)
    material = MaterialSpec(process=process)

    # Verify the default scope is there
    default_json = GEMDJson()
    default_text = default_json.dumps(material)
    assert "auto" in default_text
    assert "custom" not in default_text

    # Clear out ids
    input_material.uids = {}
    process.uids = {}
    process.ingredients[0].uids = {}
    input_material.uids = {}
    material.uids = {}

    # Verify the default scope is there
    custom_json = GEMDJson(scope='custom')
    custom_text = custom_json.dumps(material)
    assert "auto" not in custom_text
    assert "custom" in custom_text
Beispiel #2
0
    def register_all(self,
                     models: List[ResourceType],
                     dry_run=False) -> List[ResourceType]:
        """
        [ALPHA] Create or update each model in models.

        This method has the same behavior as `register`, except that all no models will be
        written if any one of them is invalid.

        Using this method should yield significant improvements to write speed over separate
        calls to `register`.

        Parameters
        ----------
        models: List[ResourceType]
            The objects to be written.
        dry_run: bool
            Whether to actually register the objects or run a dry run of the register operation.
            Dry run is intended to be used for validation. Default: false

        Returns
        -------
        List[ResourceType]
            Each object model as it now exists in the database. The order and number of models
            is guaranteed to be the same as originally specified.

        """
        if self.dataset_id is None:
            raise RuntimeError(
                "Must specify a dataset in order to register a data model object."
            )
        path = self._get_path()
        params = {'dry_run': dry_run}

        temp_scope = str(uuid4())
        scope = temp_scope if dry_run else CITRINE_SCOPE
        json = GEMDJson(scope=scope)
        [json.dumps(x) for x in models]  # This apparent no-op populates uids

        objects = [
            replace_objects_with_links(scrub_none(model.dump()))
            for model in models
        ]

        recursive_foreach(
            models, lambda x: x.uids.pop(temp_scope, None))  # Strip temp uids

        response_data = self.session.put_resource(path + '/batch',
                                                  json={'objects': objects},
                                                  params=params)
        return [self.build(obj) for obj in response_data['objects']]