Skip to content

Commit

Permalink
fixing leftover datasets when post_dataset fails to link it to a pr…
Browse files Browse the repository at this point in the history
…oject (#87)

* deleting created dataset if it cannot be linked to project

* removed naked except and bad type tests

* flake8

* removing print
  • Loading branch information
erickmartins authored Oct 2, 2023
1 parent 9d2f03f commit f4774c2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
17 changes: 13 additions & 4 deletions ezomero/_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from omero.gateway import MapAnnotationWrapper, OriginalFileWrapper
from omero.gateway import CommentAnnotationWrapper
from omero.rtypes import rstring, rint, rdouble
from omero import SecurityViolation
from .rois import Point, Line, Rectangle, Ellipse
from .rois import Polygon, Polyline, Label
import importlib.util
Expand Down Expand Up @@ -96,8 +97,16 @@ def post_dataset(conn: BlitzGateway, dataset_name: str,
dataset.save()

if project_id is not None:
link_datasets_to_project(conn, [dataset.getId()], project_id)
return dataset.getId()
try:
link_datasets_to_project(conn, [dataset.getId()], project_id)
return dataset.getId()
except SecurityViolation:
logging.warning('You do not have permission to create new '
f'datasets in project {project_id}.')
conn.deleteObject("Dataset", dataset.getId())
return None
else:
return dataset.getId()


def post_image(conn: BlitzGateway, image: np.ndarray, image_name: str,
Expand Down Expand Up @@ -689,7 +698,7 @@ def create_columns(table: Any,
headers: bool) -> List[Column]:
"""Helper function to create the correct column types from a table"""
cols = []
if type(table) == list:
if isinstance(table, list):
if headers:
titles = table[0]
data = table[1:]
Expand All @@ -712,7 +721,7 @@ def create_columns(table: Any,
max_size = len(max(data[i], key=len))
cols.append(StringColumn(titles[i], '',
max_size, data[i]))
elif type(table) == pd.core.frame.DataFrame:
elif isinstance(table, pd.core.frame.DataFrame):
df = table.convert_dtypes()
ints = df.select_dtypes(include='int')
for col in ints:
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def omero_params(request):
web_host = request.config.getoption("--omero-web-host")
port = request.config.getoption("--omero-port")
secure = request.config.getoption("--omero-secure")
return(user, password, host, web_host, port, secure)
return (user, password, host, web_host, port, secure)


@pytest.fixture(scope='session')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gets.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def test_get_shape_and_get_shape_ids(conn, project_structure,
shape = ezomero.get_shape(conn, shape_ids[i])
assert hasattr(shape, 'label')
for pre_shape in shapes:
if type(shape) == type(pre_shape):
if type(shape) is type(pre_shape):
assert shape.fill_color == pre_shape.fill_color
assert shape.stroke_color == pre_shape.stroke_color
assert shape.stroke_width == pre_shape.stroke_width
Expand Down
4 changes: 4 additions & 0 deletions tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def test_post_dataset(conn, project_structure, users_groups, timestamp):
ds_test_name3 = 'test_post_dataset3_' + timestamp
pid = 99999999
did3 = ezomero.post_dataset(conn, ds_test_name3, project_id=pid)
ds_ids = ezomero.get_dataset_ids(conn)
assert did3 is None
assert len(ds_ids) == 2

# Dataset in cross-group project, valid permissions
username = users_groups[1][0][0] # test_user1
Expand All @@ -62,6 +64,8 @@ def test_post_dataset(conn, project_structure, users_groups, timestamp):
project_info = project_structure[0]
pid = project_info[1][1] # proj1 (in test_group_1)
did5 = ezomero.post_dataset(current_conn, ds_test_name5, project_id=pid)
ds_ids = ezomero.get_dataset_ids(current_conn)
assert len(ds_ids) == 1
current_conn.close()
assert did5 is None

Expand Down

0 comments on commit f4774c2

Please sign in to comment.