From 1cf2ab5efb8db2d7d8fdb2e2733166fa17a03ec7 Mon Sep 17 00:00:00 2001 From: vermeul <swen@ethz.ch> Date: Fri, 15 Dec 2017 16:38:47 +0100 Subject: [PATCH] experiment tests --- src/python/PyBis/pybis/pybis.py | 11 +++-- src/python/PyBis/tests/test_experiment.py | 51 +++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/python/PyBis/tests/test_experiment.py diff --git a/src/python/PyBis/pybis/pybis.py b/src/python/PyBis/pybis/pybis.py index 966e2bb903f..67ddb1d10e3 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,6 +3623,7 @@ class Experiment(OpenBisObject): # the list of possible methods/attributes displayed # when invoking TAB-completition return [ + 'permId', 'identifier', 'props', 'space', 'project', 'project', 'tags', 'attachments', 'data', 'get_datasets()', 'get_samples()', diff --git a/src/python/PyBis/tests/test_experiment.py b/src/python/PyBis/tests/test_experiment.py new file mode 100644 index 00000000000..cd102af4279 --- /dev/null +++ b/src/python/PyBis/tests/test_experiment.py @@ -0,0 +1,51 @@ +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 + e_new.save() + + assert e_new.permId is not None + assert e_new.code == new_code + assert e_identifier == '/DEFAULT/DEFAULT/'+new_code + + + #project=o.new_project(space=space, code='illegal title contains spaces') + #with pytest.raises(ValueError): + # project.save() + # assert "should not have been created" is None + + #project_name = 'project_'+timestamp + #project=o.new_project(space=space, code=project_name) + #project.save() + + #project_exists=o.get_project(project_name) + #assert project_exists is not None + #project_exists.delete('test project on '+timestamp) + # + #with pytest.raises(ValueError): + # project_no_longer_exists=o.get_project(project_name) + # assert "project {} should have been deleted".format(project_name) is None -- GitLab