def save(self): if self.obj is None: # create #test_definition = ValidationTestDefinition.from_uri(self.data["test_definition"], self.client, api="nexus") test_definition = self.data["test_definition"] self.obj = ValidationScript( name="Implementation of {}, version '{}'".format( test_definition.name, self.data["version"]), date_created=datetime.now(), repository=IRI(self.data["repository"]), version=self.data["version"], description=self.data.get("description", ""), parameters=self.data.get("parameters", None), test_class=self.data["path"], test_definition=test_definition) else: # update if "repository" in self.data: self.obj.repository = IRI(self.data["repository"]) if "version" in self.data: self.obj.version = self.data["version"] if "description" in self.data: self.obj.description = self.data["description"] if "parameters" in self.data: self.obj.parameters = self.data["parameters"] if "path" in self.data: self.obj.test_class = self.data["path"] self.obj.save(self.client) return self.obj
def migrate_validation_code(self): test_code_objects = ValidationTestCode.objects.all() for tco in test_code_objects: test_name = tco.test_definition.name test_definition = ValidationTestDefinitionKG.by_name( test_name, NAR_client) if not test_definition: raise Exception("not found") script_obj = ValidationScript( name="Implementation of {}, version {}".format( test_name, tco.version), date_created=tco.timestamp, repository=tco.repository, version=tco.version, description=tco.description, parameters=tco.parameters, test_class=tco.path, test_definition=test_definition, old_uuid=str(tco.id)) script_obj.save(NAR_client) logger.info("ValidationScript saved: %s", script_obj)
def _get_test_instance_by_id(instance_id, token): test_instance = ValidationScript.from_uuid(str(instance_id), kg_client, api="nexus") if test_instance is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Test instance with ID '{instance_id}' not found.", ) test_definition = test_instance.test_definition.resolve(kg_client, api="nexus") # todo: in case of a dangling test instance, where the parent test_definition # has been deleted but the instance wasn't, we could get a None here # which we need to deal with return test_instance
async def delete_test_instance_by_id( test_instance_id: UUID, token: HTTPAuthorizationCredentials = Depends(auth)): # todo: handle non-existent UUID, inconsistent test_id and test_instance_id test_script = ValidationScript.from_uuid(str(test_instance_id), kg_client, api="nexus") if not await is_admin(token.credentials): # todo: replace this check with a group membership check for Collab v2 raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Deleting test instances is restricted to admins", ) test_script.delete(kg_client)
class ValidationTestCodeKGSerializer(BaseKGSerializer): def is_valid(self): if not self.data.get("version"): self.errors.append("Test code implementation version missing") return False if not self.data.get("repository"): self.errors.append("Test code location / repository missing") return False return True # to finish def serialize(self, obj): # todo: rewrite all this using KG Query API, to avoid doing all the individual resolves. test_definition = obj.test_definition.resolve(self.client, api="nexus") if isinstance(obj.repository, dict): repo = obj.repository["@id"] elif isinstance(obj.repository, IRI): repo = obj.repository.value else: repo = obj.repository data = { "uri": obj.id, "id": obj.uuid, "old_uuid": obj.old_uuid, "repository": repo, "version": obj.version, "description": obj.description, "parameters": obj.parameters, "path": obj.test_class, "timestamp": obj.date_created, 'test_definition_id': test_definition.uuid, "test_definition": ValidationTestDefinitionKGSerializer(test_definition, self.client).data } return data def save(self): if self.obj is None: # create #test_definition = ValidationTestDefinition.from_uri(self.data["test_definition"], self.client, api="nexus") test_definition = self.data["test_definition"] self.obj = ValidationScript( name="Implementation of {}, version '{}'".format( test_definition.name, self.data["version"]), date_created=datetime.now(), repository=IRI(self.data["repository"]), version=self.data["version"], description=self.data.get("description", ""), parameters=self.data.get("parameters", None), test_class=self.data["path"], test_definition=test_definition) else: # update if "repository" in self.data: self.obj.repository = IRI(self.data["repository"]) if "version" in self.data: self.obj.version = self.data["version"] if "description" in self.data: self.obj.description = self.data["description"] if "parameters" in self.data: self.obj.parameters = self.data["parameters"] if "path" in self.data: self.obj.test_class = self.data["path"] self.obj.save(self.client) return self.obj