Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qubixes committed Feb 6, 2024
1 parent 5893d80 commit 9a0bc3e
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 25 deletions.
1 change: 1 addition & 0 deletions docker/irods_client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ RUN apt-get install -y python3 git
# python3-pip \
# git

# Work around for a bug in ubunto 22.04: https://github.com/pypa/setuptools/issues/3269#ref-issue-1217398136
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python3 get-pip.py
RUN mkdir -p /ibridges/integration_test
Expand Down
7 changes: 7 additions & 0 deletions docker/irods_client/testdata/example.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
example{
writeLine("stderr", "Test output for errors.");
writeLine("stdout", "Input for this rule: *in");
}

input *in="This is a string or a path or etc"
output ruleExecOut
27 changes: 27 additions & 0 deletions docker/irods_client/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
import tomli

from ibridges import Session
from ibridges.irodsconnector.data_operations import (
create_collection,
get_collection,
get_dataobject,
upload,
)
from ibridges.irodsconnector.permissions import Permissions
from ibridges.utils.path import IrodsPath

# def pytest_addoption(parser):
# parser.addoption("--configdir", action="store")
Expand Down Expand Up @@ -40,10 +48,29 @@ def config(config_dir):
@pytest.fixture(scope="session")
def session(irods_env, config):
session = Session(irods_env=irods_env, password=config["password"])
ipath = IrodsPath(session, "~")
perm = Permissions(session, get_collection(session, ipath))
perm.set("own")
yield session
del session


@pytest.fixture(scope="session")
def testdata():
return Path("/tmp/testdata")


@pytest.fixture(scope="session")
def collection(session):
return create_collection(session, IrodsPath(session, "~", "test_collection"))


@pytest.fixture(scope="session")
def dataobject(session, testdata):
ipath = IrodsPath(session, "~", "bunny.rtf")
upload(session, testdata/"bunny.rtf", IrodsPath(session, "~"), overwrite=True)
data_obj = get_dataobject(session, ipath)
perm = Permissions(session, data_obj)
perm.set("own")
yield data_obj
ipath.remove()
10 changes: 5 additions & 5 deletions docker/irods_client/tests/test_data_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@


def test_upload_download_dataset(session, testdata):
ipath = IrodsPath(session, "~", "bunny.rtf")
ipath = IrodsPath(session, "~", "plant.rtf")
ipath.remove()
upload(session, testdata/"bunny.rtf", IrodsPath(session, "~"))
upload(session, testdata/"plant.rtf", IrodsPath(session, "~"))
data_obj = get_dataobject(session, ipath)
assert is_dataobject(data_obj)
assert not is_collection(data_obj)
with pytest.raises(ValueError):
get_collection(session, ipath)
print(session)
download(session, ipath, testdata/"bunny.rtf.copy", overwrite=True)
download(session, ipath, testdata/"plant.rtf.copy", overwrite=True)
# with open(data_obj, "r") as handle:
# data_irods = handle.read()
with open(testdata/"bunny.rtf.copy") as handle:
with open(testdata/"plant.rtf.copy") as handle:
data_redownload = handle.read()
with open(testdata/"bunny.rtf") as handle:
with open(testdata/"plant.rtf") as handle:
data_original = handle.read()
assert data_original == data_redownload
# assert data_irods == data_original
Expand Down
22 changes: 6 additions & 16 deletions docker/irods_client/tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import pytest
from pytest import mark

from ibridges.irodsconnector.data_operations import get_collection, get_dataobject, upload
# from ibridges.irodsconnector.data_operations import get_collection, get_dataobject, upload
from ibridges.irodsconnector.meta import MetaData
from ibridges.utils.path import IrodsPath


@pytest.fixture(scope="module")
def collection(session):
return get_collection(session, IrodsPath(session, "~"))


@pytest.fixture(scope="module")
def dataobject(session, testdata):
ipath = IrodsPath(session, "~", "bunny.rtf")
upload(session, testdata/"bunny.rtf", IrodsPath(session, "~"), overwrite=True)
yield get_dataobject(session, ipath)
ipath.remove()
# from ibridges.utils.path import IrodsPath


@mark.parametrize("item_name", ["collection", "dataobject"])
Expand Down Expand Up @@ -67,10 +55,12 @@ def test_meta(item_name, request):
meta.delete("x", "z")
assert len(list(meta)) == 1

meta.delete("x")
meta.delete("x", None)
assert len(list(meta)) == 0

meta.add("x", "y")
meta.add("y", "z")
meta.set("y", "x")
assert "x" not in meta
assert "x" in meta
assert ("y", "z") not in meta
assert ("y", "x") in meta
7 changes: 7 additions & 0 deletions docker/irods_client/tests/test_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ibridges.irodsconnector.rules import execute_rule


def test_rules(session, testdata):
rule_fp = str(testdata/"example.r")
execute_rule(session, rule_fp, {})
execute_rule(session, rule_fp, {'*in': 4, '*another_val': '"Value"'})
34 changes: 34 additions & 0 deletions docker/irods_client/tests/test_ticket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from datetime import datetime

import irods
import pytest
from pytest import mark

from ibridges.irodsconnector.tickets import Tickets


@mark.parametrize("item_name", ["collection", "dataobject"])
@mark.parametrize("ticket_type", ["read", "write"])
def test_tickets(item_name, ticket_type, session, request):
item = request.getfixturevalue(item_name)
ipath = item.path
tickets = Tickets(session)
tickets.clear()
assert len(tickets.all_tickets(update=True)) == 0

exp_date = datetime.today().strftime('%Y-%m-%d.%H:%M:%S')
tickets.create_ticket(str(ipath), ticket_type=ticket_type, expiry_string=exp_date)
assert len(tickets.all_tickets(update=True)) == 1
ticket_str = tickets.all_ticket_strings[0]
tick = tickets.get_ticket(ticket_str)
assert isinstance(tick, irods.ticket.Ticket)
ticket_data = tickets.all_tickets(update=True)[0]
assert ticket_data[0] == ticket_str
assert ticket_data[1] == ticket_type
assert ticket_data[2] == str(ipath)
# assert ticket_data[3] == exp_date
assert isinstance(ticket_data[3], str)
tickets.delete_ticket(tick)
assert len(tickets.all_tickets(update=True)) == 0
with pytest.raises(KeyError):
tickets.delete_ticket(tick, check=True)
4 changes: 3 additions & 1 deletion ibridges/irodsconnector/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __contains__(self, val: Union[str, Sequence]) -> bool:
for meta in self:
n_same = 0
for i_attr, attr in enumerate(all_attrs):
if getattr(meta, attr) == val[i_attr]:
if getattr(meta, attr) == val[i_attr] or val[i_attr] is None:
n_same += 1
else:
break
Expand Down Expand Up @@ -62,6 +62,8 @@ def add(self, key: str, value: str, units: Optional[str] = None):
CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME
"""
try:
if (key, value, units) in self:
raise irods.exception.CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME()
self.item.metadata.add(key, value, units)
except irods.exception.CATALOG_ALREADY_HAS_ITEM_BY_THAT_NAME as error:
raise ValueError("ADD META: Metadata already present") from error
Expand Down
13 changes: 10 additions & 3 deletions ibridges/irodsconnector/permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" permission operations """
from collections import defaultdict
from typing import Iterator
from typing import Iterator, Optional

import irods.access
import irods.collection
Expand Down Expand Up @@ -38,10 +38,17 @@ def __str__(self) -> str:
@property
def available_permissions(self) -> dict:
"""Get available permissions"""
return self.session.irods_session.available_permissions
return self.session.irods_session.available_permissions.codes

def set(self, perm: str, user: str = '', zone: str = '',
def set(self, perm: str, user: Optional[str] = None, zone: Optional[str] = None,
recursive: bool = False, admin: bool = False) -> None:
"""Set permissions (ACL) for an iRODS collection or data object."""
if user is None:
user = self.session.username
if zone is None:
zone = self.session.zone
if perm == "null" and user == self.session.username and zone == self.session.zone:
raise ValueError("Cannot set your own permissions to null, because you would lose "
"access to the object/collection.")
acl = irods.access.iRODSAccess(perm, self.item.path, user, zone)
self.session.irods_session.acls.set(acl, recursive=recursive, admin=admin)
7 changes: 7 additions & 0 deletions ibridges/irodsconnector/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def all_tickets(self, update: bool = False) -> list[tuple[str, str, str, str]]:
))
return self._all_tickets

def clear(self):
self.all_tickets(update=True)
for tick_data in self._all_tickets:
tick_str = tick_data[0]
tick = self.get_ticket(tick_str)
self.delete_ticket(tick)

def _id_to_path(self, itemid: str) -> str:
"""
Given an iRODS item id (data object or collection) from the
Expand Down

0 comments on commit 9a0bc3e

Please sign in to comment.