diff --git a/src/python/PyBis/pybis/pybis.py b/src/python/PyBis/pybis/pybis.py index 966e2bb903fb640487ce11930989f7fb9999b704..cb6f865dda07b50fa5d0e5bbce3760872140e6c0 100644 --- a/src/python/PyBis/pybis/pybis.py +++ b/src/python/PyBis/pybis/pybis.py @@ -1167,12 +1167,13 @@ class Openbis: data = resp[id] ) - def new_experiment(self, type, code, props=None, **kwargs): + def new_experiment(self, type, code, project, props=None, **kwargs): """ Creates a new experiment of a given experiment type. """ return Experiment( openbis_obj = self, type = self.get_experiment_type(type), + project = project, data = None, props = props, code = code, @@ -3003,7 +3004,7 @@ class AttrHolder(): try: if self._type['autoGeneratedCode']: raise KeyError("This {}Type has auto-generated code. You cannot set a code".format(self.entity)) - except AttributeError: + except KeyError: pass self.__dict__['_code'] = value @@ -3577,7 +3578,7 @@ class Experiment(OpenBisObject): """ """ - def __init__(self, openbis_obj, type, data=None, props=None, code=None, **kwargs): + def __init__(self, openbis_obj, type, project=None, data=None, props=None, code=None, **kwargs): self.__dict__['openbis'] = openbis_obj self.__dict__['type'] = type self.__dict__['p'] = PropertyHolder(openbis_obj, type) @@ -3586,6 +3587,9 @@ class Experiment(OpenBisObject): if data is not None: self._set_data(data) + if project is not None: + setattr(self, 'project', project) + if props is not None: for key in props: setattr(self.p, key, props[key]) @@ -3619,7 +3623,9 @@ class Experiment(OpenBisObject): # the list of possible methods/attributes displayed # when invoking TAB-completition return [ - 'props', 'space', 'project', + 'code', 'permId', 'identifier', + 'type', 'project', + 'props.', 'project', 'tags', 'attachments', 'data', 'get_datasets()', 'get_samples()', 'set_tags()', 'add_tags()', 'del_tags()', diff --git a/src/python/PyBis/tests/test_experiment.py b/src/python/PyBis/tests/test_experiment.py new file mode 100644 index 0000000000000000000000000000000000000000..10ba05ef05618db415a6034a04d1d98a914245e0 --- /dev/null +++ b/src/python/PyBis/tests/test_experiment.py @@ -0,0 +1,45 @@ +import json +import random +import re + +import pytest +import time +from pybis import DataSet +from pybis import Openbis + + +def test_create_delete_project(space): + o=space.openbis + timestamp = time.strftime('%a_%y%m%d_%H%M%S').upper() + new_code='test_experiment_'+timestamp + + with pytest.raises(TypeError): + # experiments must be assigned to a project + e_new = o.new_experiment( + code=new_code, + type='DEFAULT_EXPERIMENT', + ) + + e_new = o.new_experiment( + code=new_code, + project='DEFAULT', + type='DEFAULT_EXPERIMENT', + ) + assert e_new.project is not None + assert e_new.permId is None + + e_new.save() + + assert e_new.permId is not None + assert e_new.code == new_code.upper() + assert e_new.identifier == '/DEFAULT/DEFAULT/'+new_code.upper() + + e_exists = o.get_experiment('/DEFAULT/DEFAULT/'+new_code.upper()) + assert e_exists is not None + + e_new.delete('delete test experiment '+new_code.upper()) + + with pytest.raises(ValueError): + e_no_longer_exists = o.get_experiment('/DEFAULT/DEFAULT/'+new_code.upper()) + +