mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-11-25 08:39:30 +08:00
261 lines
11 KiB
Python
261 lines
11 KiB
Python
from typing import (
|
|
Any,
|
|
)
|
|
|
|
import pytest
|
|
|
|
from galaxy import model
|
|
from galaxy.app_unittest_utils import galaxy_mock
|
|
from galaxy.tools.parameters.basic import ParameterValueError
|
|
from galaxy.tools.parameters.pagination import DEFAULT_OPTIONS_PAGE_SIZE
|
|
from .util import BaseParameterTestCase
|
|
|
|
|
|
class TestDataToolParameter(BaseParameterTestCase):
|
|
def test_to_python_none_values(self):
|
|
assert self.param.to_python(None, self.app) is None
|
|
assert self.param.to_python("None", self.app) is None
|
|
assert self.param.to_python("", self.app) is None
|
|
|
|
def test_to_python_hda(self):
|
|
hda = self._new_hda()
|
|
as_python = self.param.to_python(hda.id, self.app)
|
|
assert hda == as_python
|
|
|
|
def test_to_python_multi_hdas(self):
|
|
hda1 = self._new_hda()
|
|
hda2 = self._new_hda()
|
|
as_python = self.param.to_python(f"{hda1.id},{hda2.id}", self.app)
|
|
assert as_python == [hda1, hda2]
|
|
|
|
def test_to_python_multi_none(self):
|
|
self.multiple = True
|
|
hda = self._new_hda()
|
|
# Selection is Optional. may be selected with other stuff,
|
|
# not sure the UI should really allow this but easy enough
|
|
# to just filter it out.
|
|
assert [hda] == self.param.to_python(f"{hda.id},None", self.app)
|
|
|
|
def test_from_json_rejects_src_prefixed_string(self):
|
|
bogus = "hda:f9cad7b01a472135e2c8f5464c5c5ecb"
|
|
with pytest.raises(ParameterValueError, match="invalid dataset id"):
|
|
self.param.from_json([bogus], self.trans)
|
|
|
|
def test_from_json_rejects_garbage_string(self):
|
|
with pytest.raises(ParameterValueError, match="invalid dataset id"):
|
|
self.param.from_json("not-an-id", self.trans)
|
|
|
|
def test_from_json_multi_tolerates_none(self):
|
|
# An unset optional data input connected to a multiple data parameter
|
|
# arrives as a null list element; it must be filtered out rather than
|
|
# rejected as a malformed id (regression from #22617, surfaced as a
|
|
# workflow run erroring on an unset optional data input).
|
|
self.multiple = True
|
|
self.optional = True
|
|
assert self.param.from_json([None], self.trans) == []
|
|
assert self.param.from_json(["None"], self.trans) == []
|
|
|
|
def test_field_filter_on_types(self):
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda2 = MockHistoryDatasetAssociation(name="hda2", id=2)
|
|
self.stub_active_datasets(hda1, hda2)
|
|
field = self._simple_field()
|
|
assert len(field["options"]["hda"]) == 2
|
|
assert field["options"]["hda"][0]["name"] == "hda2"
|
|
assert field["options"]["hda"][1]["name"] == "hda1"
|
|
|
|
hda2.extension = "data"
|
|
hda2.conversion_destination = (False, None, None)
|
|
field = self._simple_field()
|
|
assert len(field["options"]["hda"]) == 1, field
|
|
assert field["options"]["hda"][0]["name"] == "hda1"
|
|
|
|
def test_field_display_hidden_hdas_only_if_selected(self):
|
|
hda1 = self._new_hda(name="hda1")
|
|
hda2 = self._new_hda(name="hda2")
|
|
hda1.visible = False
|
|
hda2.visible = False
|
|
self.stub_active_datasets(hda1, hda2)
|
|
field = self._simple_field(other_values={"data2": hda2})
|
|
assert len(field["options"]["hda"]) == 1 # hda1 not an option, not visible or selected
|
|
assert field["options"]["hda"][0]["name"] == "(hidden) hda2"
|
|
|
|
def test_field_display_deleted_hdas_only_if_selected(self):
|
|
hda1 = self._new_hda(name="hda1")
|
|
hda2 = self._new_hda(name="hda2")
|
|
hda1.visible = False
|
|
hda2.deleted = True
|
|
self.stub_active_datasets(hda1, hda2)
|
|
field = self._simple_field(other_values={"data2": hda2})
|
|
assert len(field["options"]["hda"]) == 1 # hda1 not an option, not visible or selected
|
|
assert field["options"]["hda"][0]["name"] == "(deleted) hda2"
|
|
|
|
def test_field_implicit_conversion_new(self):
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda1.extension = "data"
|
|
hda1.conversion_destination = (False, "tabular", None)
|
|
self.stub_active_datasets(hda1)
|
|
field = self._simple_field()
|
|
assert len(field["options"]["hda"]) == 1
|
|
assert field["options"]["hda"][0]["name"] == "hda1 (as tabular)"
|
|
assert field["options"]["hda"][0]["hid"] == 1
|
|
|
|
def test_field_implicit_conversion_existing(self):
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda1.extension = "data"
|
|
hda1.conversion_destination = (False, "tabular", MockHistoryDatasetAssociation(name="hda1converted", id=2))
|
|
self.stub_active_datasets(hda1)
|
|
field = self._simple_field()
|
|
assert len(field["options"]["hda"]) == 1
|
|
assert field["options"]["hda"][0]["name"] == "hda1 (as tabular)"
|
|
# This is difference with previous test, value is existing
|
|
# hda id not new one.
|
|
assert field["options"]["hda"][0]["hid"] == 2
|
|
|
|
def test_field_multiple(self):
|
|
self.multiple = True
|
|
field = self._simple_field()
|
|
assert field["multiple"]
|
|
|
|
def test_field_empty_selection(self):
|
|
field = self._simple_field()
|
|
assert len(field["options"]["hda"]) == 0
|
|
assert len(field["options"]["hdca"]) == 0
|
|
|
|
def test_field_empty_selection_optional(self):
|
|
self.optional = True
|
|
field = self._simple_field()
|
|
assert field["optional"]
|
|
|
|
def test_get_initial_value(self):
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda2 = MockHistoryDatasetAssociation(name="hda2", id=2)
|
|
self.stub_active_datasets(hda1, hda2)
|
|
assert hda2 == self.param.get_initial_value(self.trans, {})
|
|
|
|
def test_get_initial_value_is_none_if_no_match(self):
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda1.visible = False
|
|
hda2 = MockHistoryDatasetAssociation(name="hda2", id=2)
|
|
hda2.visible = False
|
|
self.stub_active_datasets(hda1, hda2)
|
|
assert self.param.get_initial_value(self.trans, {}) is None
|
|
|
|
def test_get_initial_none_when_optional(self):
|
|
self.optional = True
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda2 = MockHistoryDatasetAssociation(name="hda2", id=2)
|
|
self.stub_active_datasets(hda1, hda2)
|
|
assert self.param.get_initial_value(self.trans, {}) is None
|
|
|
|
def test_get_initial_with_previously_converted_data(self):
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda1.extension = "data"
|
|
converted = MockHistoryDatasetAssociation(name="hda1converted", id=2)
|
|
hda1.conversion_destination = (False, "tabular", converted)
|
|
self.stub_active_datasets(hda1)
|
|
assert converted == self.param.get_initial_value(self.trans, {})
|
|
|
|
def test_get_initial_with_to_be_converted_data(self):
|
|
hda1 = MockHistoryDatasetAssociation(name="hda1", id=1)
|
|
hda1.extension = "data"
|
|
hda1.conversion_destination = (False, "tabular", None)
|
|
self.stub_active_datasets(hda1)
|
|
assert hda1 == self.param.get_initial_value(self.trans, {}), hda1
|
|
|
|
def _new_hda(self, name="Test Dataset"):
|
|
hda = model.HistoryDatasetAssociation()
|
|
hda.visible = True
|
|
hda.name = name
|
|
hda.extension = "txt"
|
|
hda.dataset = model.Dataset()
|
|
hda.dataset.state = model.Dataset.states.OK
|
|
session = self.app.model.context
|
|
session.add(hda)
|
|
session.commit()
|
|
return hda
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.test_history = model.History()
|
|
session = self.app.model.context
|
|
session.add(self.test_history)
|
|
session.commit()
|
|
self.trans = galaxy_mock.MockTrans(history=self.test_history)
|
|
self.multiple = False
|
|
self.optional = False
|
|
self._param = None
|
|
|
|
def stub_active_datasets(self, *hdas):
|
|
self.test_history._active_datasets_and_roles = [h for h in hdas if not h.deleted]
|
|
visible = [h for h in hdas if not h.deleted and h.visible]
|
|
self.test_history._active_visible_datasets_and_roles = visible
|
|
|
|
# Stub the paginated query helper used by ``DataToolParameter.to_dict``.
|
|
# The fakes mock ``find_conversion_destination`` at the HDA level rather
|
|
# than at the datatypes-registry level, so we deliberately ignore the
|
|
# SQL ``extensions``/``valid_states``/``tag`` filters here and return
|
|
# all visible HDAs — the matcher path then exercises the per-HDA mocked
|
|
# conversion logic. We sort by ``hid`` descending to mirror the real
|
|
# query's ordering (newest-first), which ``get_initial_value`` relies
|
|
# on to pick the most recent matching HDA.
|
|
visible_desc = sorted(visible, key=lambda h: h.hid, reverse=True)
|
|
|
|
def _paginated(
|
|
*, extensions=None, valid_states=None, tag=None, search=None, offset=0, limit=DEFAULT_OPTIONS_PAGE_SIZE
|
|
):
|
|
return visible_desc[offset : offset + limit], len(visible_desc)
|
|
|
|
self.test_history.paginated_active_visible_datasets = _paginated # type: ignore[method-assign]
|
|
|
|
def _simple_field(self, **kwds):
|
|
return self.param.to_dict(trans=self.trans, **kwds)
|
|
|
|
@property
|
|
def param(self):
|
|
if not self._param:
|
|
multi_text = ""
|
|
if self.multiple:
|
|
multi_text = 'multiple="True"'
|
|
optional_text = ""
|
|
if self.optional:
|
|
optional_text = 'optional="True"'
|
|
param_str = f"""<param name="data2" type="data" format="txt" {multi_text} {optional_text}></param>"""
|
|
self._param = self._parameter_for(tool=self.mock_tool, xml=param_str)
|
|
|
|
return self._param
|
|
|
|
|
|
class MockHistoryDatasetAssociation:
|
|
"""Fake HistoryDatasetAssociation that stubs find_conversion_destination
|
|
so conversion tests can run without a datatype registry.
|
|
"""
|
|
|
|
def __init__(self, test_dataset=None, name="Test Dataset", id=1):
|
|
if not test_dataset:
|
|
test_dataset = model.Dataset()
|
|
test_dataset.state = model.Dataset.states.OK
|
|
self.states = model.HistoryDatasetAssociation.states
|
|
self.deleted = False
|
|
self.dataset = test_dataset
|
|
self.visible = True
|
|
self.conversion_destination: tuple[bool, str | None, Any | None] = (True, None, None)
|
|
self.extension = "txt"
|
|
self.dbkey = "hg19"
|
|
self.implicitly_converted_parent_datasets = False
|
|
self.name = name
|
|
self.hid = id
|
|
self.id = id
|
|
self.tags = []
|
|
self.metadata = None
|
|
|
|
@property
|
|
def state(self):
|
|
return self.dataset.state
|
|
|
|
def get_dbkey(self):
|
|
return self.dbkey
|
|
|
|
def find_conversion_destination(self, formats):
|
|
return self.conversion_destination
|