Exemplo n.º 1
0
    def test__create_value_wrapper(self):
        param = Parameter(name='test', default=[1, 2, 3, 4])

        # Bad ufunc
        with pytest.raises(TypeError) as err:
            param._create_value_wrapper(np.add, mk.MagicMock())
        assert str(
            err.value) == ("A numpy.ufunc used for Parameter getter/setter "
                           "may only take one input argument")
        # Good ufunc
        assert param._create_value_wrapper(np.negative,
                                           mk.MagicMock()) == np.negative

        # None
        assert param._create_value_wrapper(None, mk.MagicMock()) is None

        # wrapper with one argument
        def wrapper1(a):
            pass

        assert param._create_value_wrapper(wrapper1,
                                           mk.MagicMock()) == wrapper1

        # wrapper with two argument2
        def wrapper2(a, b):
            pass

        # model is None
        assert param._model_required is False
        assert param._create_value_wrapper(wrapper2, None) == wrapper2
        assert param._model_required is True
        # model is not None
        param._model_required = False
        model = mk.MagicMock()
        with mk.patch.object(functools, 'partial', autospec=True) as mkPartial:
            assert param._create_value_wrapper(wrapper2,
                                               model) == mkPartial.return_value

        # wrapper with more than 2 arguments
        def wrapper3(a, b, c):
            pass

        with pytest.raises(TypeError) as err:
            param._create_value_wrapper(wrapper3, mk.MagicMock())
        assert str(err.value) == ("Parameter getter/setter must be a function "
                                  "of either one or two arguments")
Exemplo n.º 2
0
    def test_model(self):
        param = Parameter(name='test', default=[1, 2, 3, 4])
        assert param.model == None == param._model
        assert param._model_required == False
        assert (param._value == [1, 2, 3, 4]).all()

        setter = mk.MagicMock()
        getter = mk.MagicMock()
        param._setter = setter
        param._getter = getter

        # No Model Required
        param._value = [5, 6, 7, 8]
        model0 = mk.MagicMock()
        setter0 = mk.MagicMock()
        getter0 = mk.MagicMock()
        with mk.patch.object(Parameter, '_create_value_wrapper',
                             side_effect=[setter0, getter0]) as mkCreate:
            param.model = model0
            assert param.model == model0 == param._model
            assert param._setter == setter0
            assert param._getter == getter0
            assert mkCreate.call_args_list == [
                mk.call(setter, model0),
                mk.call(getter, model0)
            ]
            assert param._value == [5, 6, 7, 8]

        param._setter = setter
        param._getter = getter

        # Model required
        param._model_required = True
        model1 = mk.MagicMock()
        setter1 = mk.MagicMock()
        getter1 = mk.MagicMock()
        setter1.return_value = [9, 10, 11, 12]
        getter1.return_value = [9, 10, 11, 12]
        with mk.patch.object(Parameter, '_create_value_wrapper',
                             side_effect=[setter1, getter1]) as mkCreate:
            param.model = model1
            assert param.model == model1 == param._model
            assert param._setter == setter1
            assert param._getter == getter1
            assert mkCreate.call_args_list == [
                mk.call(setter, model1),
                mk.call(getter, model1)
            ]
            assert (param.value == [9, 10, 11, 12]).all()

        param._setter = setter
        param._getter = getter
        param._default = None
        with mk.patch.object(Parameter, '_create_value_wrapper',
                             side_effect=[setter1, getter1]) as mkCreate:
            param.model = model1
            assert param.model == model1 == param._model
            assert param._setter == setter1
            assert param._getter == getter1
            assert mkCreate.call_args_list == [
                mk.call(setter, model1),
                mk.call(getter, model1)
            ]
            assert param._value is None