def test_component(app, dataxz, dataxyz):
    # setup
    state = DummyState()
    helper = ComponentIDComboHelper(state, 'x_att', app.data_collection)
    helper.append_data(dataxz)
    state.helper = helper

    # main object we test
    dropdown = LinkedDropdown(state, 'x_att', 'x test attribute')

    # simple sanity tests
    assert dropdown.description == 'x test attribute'
    assert [item[0] for item in dropdown.options] == ['x', 'z']

    # initial state
    assert state.x_att is dataxz.id['x']
    assert dropdown.value is dataxz.id['x']

    # glue state -> ui
    state.x_att = dataxz.id['z']
    assert dropdown.value is dataxz.id['z']

    # ui -> glue state
    dropdown.value = dataxz.id['x']
    assert state.x_att is dataxz.id['x']

    # same, but now be ok with strings
    state.x_att = 'z'
    assert dropdown.value is dataxz.id['z']

    state.x_att = 'x'
    assert dropdown.value is dataxz.id['x']
def test_component_default_index(app, dataxz, dataxyz):

    # Regression test for a bug that caused the incorrect element to be selected
    # when default_index is involved.

    # setup
    state = DummyState()
    helper = ComponentIDComboHelper(state, 'y_att', app.data_collection)
    state.helper = helper
    dropdown = LinkedDropdown(state, 'y_att', 'y test attribute')
    assert [item[0] for item in dropdown.options] == []

    helper.append_data(dataxz)
    assert [item[0] for item in dropdown.options] == ['x', 'z']

    assert state.y_att is dataxz.id['z']
    assert dropdown.value is dataxz.id['z']
Example #3
0
def test_component_material(app, dataxz, dataxyz):
    # setup
    state = DummyState()
    helper = ComponentIDComboHelper(state, 'x_att', app.data_collection)
    helper.append_data(dataxz)
    state.helper = helper

    # main object we test
    dropdown = LinkedDropdownMaterial(state, 'x_att', 'x test attribute')

    # simple sanity tests
    assert dropdown.widget_input_label.description == 'x test attribute'
    items = getattr(type(state), 'x_att').get_choice_labels(state)
    assert len(dropdown.widget_select.children) == len(items)
    assert [item.description
            for item in dropdown.widget_select.children] == ['x', 'z']

    # initial state
    assert str(state.x_att) == 'x'
    assert dropdown.widget_select.value == 0

    # glue state -> ui
    state.x_att = dataxz.id['z']
    assert dropdown.widget_select.value == 1

    # ui -> glue state
    assert str(state.x_att) == 'z'
    assert dropdown.widget_select.value == 1
    dropdown.widget_select.value = 0
    assert str(state.x_att) == 'x'

    # same, but now be ok with strings
    assert dropdown.widget_select.value == 0
    assert str(state.x_att) == 'x'

    state.x_att = 'z'
    assert dropdown.widget_select.value == 1

    state.x_att = 'x'
    assert dropdown.widget_select.value == 0
Example #4
0
    def __init__(self,
                 function,
                 data1=None,
                 data2=None,
                 cids1=None,
                 cids2=None,
                 names1=None,
                 names2=None,
                 display=None,
                 description=None):

        super(EditableLinkFunctionState, self).__init__()

        if isinstance(function, ComponentLink):
            self._function = function.get_using()
            self._inverse = function.get_inverse()
            self._helper_class = None
            cids1 = function.get_from_ids()
            cids2 = function.get_to_ids()
            data1 = cids1[0].parent
            data2 = cids2[0].parent
            self.display = self._function.__name__
            self.description = function.description
        elif isinstance(function, LinkCollection):
            self._function = None
            self._helper_class = function.__class__
            cids1 = function.cids1
            cids2 = function.cids2
            data1 = cids1[0].parent

            # To be backward-compatible with cases where @link_helper doesn't
            # include output labels, we need to assume cids2 can be empty
            # in which case we look for the second dataset inside cids1
            if len(cids2) > 0:
                data2 = cids2[0].parent
            else:
                for cid in cids1[1:]:
                    if cid.parent is not data1:
                        data2 = cid.parent
                        break
                else:
                    raise ValueError(
                        "Could not determine second dataset in link")

            self.display = function.display
            self.description = function.description
            self._mode = 'helper'
        elif type(function) is type and issubclass(function, LinkCollection):
            self._function = None
            self._helper_class = function
            self.display = function.display
            self.description = function.description
        elif isinstance(function, (FunctionType, MethodType)):
            self._function = function
            self._inverse = None
            self._helper_class = None
            self.inverse = None
            self.display = display
            self.description = description
        else:
            raise TypeError("Unexpected type for 'function': {0}".format(
                type(function)))

        self.data1 = data1
        self.data2 = data2

        for name in self.names1:
            helper = ComponentIDComboHelper(self,
                                            name,
                                            pixel_coord=True,
                                            world_coord=True)
            helper.append_data(data1)
            if len(self.names2) == 0:  # legacy mode for old link helpers
                helper.append_data(data2)

            setattr(self, '_' + name + '_helper', helper)

        for name in self.names2:
            helper = ComponentIDComboHelper(self,
                                            name,
                                            pixel_coord=True,
                                            world_coord=True)
            helper.append_data(data2)
            setattr(self, '_' + name + '_helper', helper)

        if cids1 is not None:
            for name, cid in zip(self.names1, cids1):
                setattr(self, name, cid)

        if cids2 is not None:
            for name, cid in zip(self.names2, cids2):
                setattr(self, name, cid)
Example #5
0
    def __init__(self, function, data_in=None, data_out=None, cids_in=None,
                 cids_out=None, input_names=None, output_names=None,
                 display=None, description=None):

        super(EditableLinkFunctionState, self).__init__()

        if isinstance(function, ComponentLink):
            self._function = function.get_using()
            self._inverse = function.get_inverse()
            self._helper_class = None
            cids_in = function.get_from_ids()
            cids_out = function.get_to_ids()
            data_in = cids_in[0].parent
            data_out = cids_out[0].parent
            self.display = self._function.__name__
            self.description = function.description
        elif isinstance(function, LinkCollection):
            self._function = None
            self._helper_class = function.__class__
            cids_in = function.cids1
            cids_out = function.cids2
            data_in = cids_in[0].parent

            # To be backward-compatible with cases where @link_helper doesn't
            # include output labels, we need to assume cids_out can be empty
            # in which case we look for the second dataset inside cids_in
            if len(cids_out) > 0:
                data_out = cids_out[0].parent
            else:
                for cid in cids_in[1:]:
                    if cid.parent is not data_in:
                        data_out = cid.parent
                        break
                else:
                    raise ValueError("Could not determine second dataset in link")

            self.display = function.display
            self.description = function.description
            self._mode = 'helper'
        elif type(function) is type and issubclass(function, LinkCollection):
            self._function = None
            self._helper_class = function
            self.display = function.display
            self.description = function.description
        elif isinstance(function, (FunctionType, MethodType)):
            self._function = function
            self._inverse = None
            self._helper_class = None
            self.inverse = None
            self.display = display
            self.description = description
        else:
            raise TypeError("Unexpected type for 'function': {0}".format(type(function)))

        self.data_in = data_in
        self.data_out = data_out

        for name in self.input_names:
            helper = ComponentIDComboHelper(self, name,
                                            pixel_coord=True, world_coord=True)
            helper.append_data(data_in)
            helper.append_data(data_out)

            setattr(self, '_' + name + '_helper', helper)

        for name in self.output_names:
            helper = ComponentIDComboHelper(self, name,
                                            pixel_coord=True, world_coord=True)
            helper.append_data(data_out)
            helper.append_data(data_in)
            setattr(self, '_' + name + '_helper', helper)

        if cids_in is not None:
            for name, cid in zip(self.input_names, cids_in):
                setattr(self, name, cid)

        if cids_out is not None:
            for name, cid in zip(self.output_names, cids_out):
                setattr(self, name, cid)