Ejemplo n.º 1
0
    def test_default(self):
        """Test the default value property for the InputPort."""
        port = InputPort('test', default=5)
        self.assertEqual(port.default, 5)

        with self.assertRaises(ValueError):
            InputPort('test', default=4, valid_type=str)
Ejemplo n.º 2
0
    def test_port_namespace_no_populate_defaults(self):
        """Verify that defaults are not populated for a `populate_defaults=False` namespace in `pre_process`."""
        port_namespace = PortNamespace('base')
        port_namespace_normal = port_namespace.create_port_namespace(
            'normal', populate_defaults=True)
        port_namespace_normal['with_default'] = InputPort('with_default',
                                                          default=1,
                                                          valid_type=int)
        port_namespace_normal['without_default'] = InputPort('without_default',
                                                             valid_type=int)

        inputs = {}
        pre_processed = port_namespace.pre_process(inputs)
        self.assertIn('normal', pre_processed)
        self.assertIn('with_default', pre_processed.normal)
        self.assertNotIn('without_default', pre_processed.normal)

        # Now repeat the test but with a "lazy" namespace where defaults are not populated if not explicitly specified
        port_namespace = PortNamespace('base')
        port_namespace_lazy = port_namespace.create_port_namespace(
            'lazy', populate_defaults=False, required=False)
        port_namespace_lazy['with_default'] = InputPort('with_default',
                                                        default=1,
                                                        valid_type=int)
        port_namespace_lazy['without_default'] = InputPort('without_default',
                                                           valid_type=int)

        inputs = {}
        pre_processed = port_namespace.pre_process(inputs)

        # Because the namespace is lazy and no inputs were passed, the defaults should not have been populated.
        self.assertEqual(pre_processed, {})
Ejemplo n.º 3
0
    def test_validator(self):
        """Test the validator functionality."""
        def integer_validator(value):
            if value < 0:
                return 'Only positive integers allowed'

        port = InputPort('test', validator=integer_validator)
        self.assertIsNone(port.validate(5))
        self.assertIsNotNone(port.validate(-5))
Ejemplo n.º 4
0
    def test_lambda_default(self):
        """Test a default with a lambda."""
        from plumpy.ports import UNSPECIFIED

        # The default should not be evaluated upon construction, so even if it will return an incorrect type, the
        # following call should not fail
        InputPort('lambda_default', default=lambda: 'string', valid_type=int)

        port = InputPort('test', default=lambda: 5)

        self.assertIsNone(port.validate(UNSPECIFIED))
        self.assertIsNone(port.validate(3))

        # Testing that passing an actual lambda as a value is alos possible
        port = InputPort('test',
                         valid_type=(types.FunctionType, int),
                         default=lambda: 5)
        some_lambda = lambda: 'string'
        self.assertIsNone(port.validate(some_lambda))
Ejemplo n.º 5
0
    def test_port_namespace_validation(self):
        """Test validate method of a `PortNamespace`."""
        def validator(port_values, port):
            assert isinstance(port, PortNamespace)
            if port_values['explicit'] < 0 or port_values['dynamic'] < 0:
                return 'Only positive integers allowed'

        self.port_namespace['explicit'] = InputPort('explicit', valid_type=int)
        self.port_namespace.validator = validator
        self.port_namespace.valid_type = int

        # The explicit ports will be validated first before the namespace validator is called.
        self.assertIsNone(
            self.port_namespace.validate({
                'explicit': 1,
                'dynamic': 5
            }))
        self.assertIsNotNone(self.port_namespace.validate({'dynamic': -5}))
Ejemplo n.º 6
0
    def test_port_namespace_lambda_defaults(self):
        """Verify that lambda defaults are accepted and properly evaluated."""
        port_namespace = PortNamespace('base')
        port_namespace['lambda_default'] = InputPort(
            'lambda_default',
            default=lambda: 1,
            valid_type=(types.FunctionType, int))

        inputs = port_namespace.pre_process({})
        self.assertEqual(inputs['lambda_default'], 1)
        self.assertIsNone(port_namespace.validate(inputs))

        inputs = port_namespace.pre_process({'lambda_default': 5})
        self.assertEqual(inputs['lambda_default'], 5)
        self.assertIsNone(port_namespace.validate(inputs))

        # When passing a lambda directly as the value, it should NOT be evaluated during pre_processing
        some_lambda = lambda: 5
        inputs = port_namespace.pre_process({'lambda_default': some_lambda})
        self.assertEqual(inputs['lambda_default'], some_lambda)
        self.assertIsNone(port_namespace.validate(inputs))
Ejemplo n.º 7
0
 def setUp(self):
     self.port = InputPort(self.BASE_PORT_NAME)
     self.port_namespace = PortNamespace(self.BASE_PORT_NAMESPACE_NAME)