Exemplo n.º 1
0
 def __test_valid_values(self, **kwargs):
     expected_values = {'min': float('-inf'), 'max': float('+inf'), 'default': None}
     for k in kwargs:
         expected_values[k] = kwargs[k]
         
     expected_values['absolute_min'] = expected_values['min']
     expected_values['absolute_max'] = expected_values['max']
         
     if isinstance(expected_values['absolute_min'], ParameterDeclaration):
         expected_values['absolute_min'] = expected_values['absolute_min'].absolute_min_value
     if isinstance(expected_values['absolute_max'], ParameterDeclaration):
         expected_values['absolute_max'] = expected_values['absolute_max'].absolute_max_value
     
     decl = ParameterDeclaration('test', **kwargs)
     
     self.assertEqual('test', decl.name)
     self.assertEqual(expected_values['min'], decl.min_value)
     self.assertEqual(expected_values['max'], decl.max_value)
     self.assertEqual(expected_values['default'], decl.default_value)
     self.assertEqual(expected_values['absolute_min'], decl.absolute_min_value)
     self.assertEqual(expected_values['absolute_max'], decl.absolute_max_value)
     
     decl = ParameterDeclaration('test', default=expected_values['default'])
     if 'min' in kwargs:
         decl.min_value = kwargs['min']
     if 'max' in kwargs:
         decl.max_value = kwargs['max']
         
     self.assertEqual(expected_values['min'], decl.min_value)
     self.assertEqual(expected_values['max'], decl.max_value)
     self.assertEqual(expected_values['default'], decl.default_value)
     self.assertEqual(expected_values['absolute_min'], decl.absolute_min_value)
     self.assertEqual(expected_values['absolute_max'], decl.absolute_max_value)
Exemplo n.º 2
0
 def test_get_value(self) -> None:
     decl = ParameterDeclaration('foo')
     foo_param = ConstantParameter(2.1)
     self.assertEqual(foo_param.get_value(), decl.get_value({'foo': foo_param}))
     self.assertRaises(ParameterNotProvidedException, decl.get_value, {})
     
     decl = ParameterDeclaration('foo', default=2.7)
     self.assertEqual(decl.default_value, decl.get_value({}))
Exemplo n.º 3
0
class ParameterDeclarationSerializationTests(unittest.TestCase):

    def setUp(self) -> None:
        self.serializer = DummySerializer()
        self.declaration = ParameterDeclaration('foo')
        self.expected_data = dict(name='foo', type=self.serializer.get_type_identifier(self.declaration))

    def test_get_serialization_data_all_default(self) -> None:
        self.expected_data['min_value'] = float('-inf')
        self.expected_data['max_value'] = float('+inf')
        self.expected_data['default_value'] = None
        self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer))

    def test_get_serialization_data_all_floats(self) -> None:
        self.declaration = ParameterDeclaration('foo', min=-3.1, max=4.3, default=0.2)
        self.expected_data['min_value'] = -3.1
        self.expected_data['max_value'] = 4.3
        self.expected_data['default_value'] = 0.2
        self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer))

    def test_get_serialization_data_min_max_references(self) -> None:
        bar_min = ParameterDeclaration('bar_min')
        bar_max = ParameterDeclaration('bar_max')
        self.declaration.min_value = bar_min
        self.declaration.max_value = bar_max
        self.expected_data['min_value'] = 'bar_min'
        self.expected_data['max_value'] = 'bar_max'
        self.expected_data['default_value'] = None
        self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer))

    def test_deserialize_all_default(self) -> None:
        data = dict(min_value=float('-inf'), max_value=float('+inf'), default_value=None, name='foo')
        declaration = ParameterDeclaration.deserialize(self.serializer, **data)
        self.assertEqual(data['min_value'], declaration.min_value)
        self.assertEqual(data['max_value'], declaration.max_value)
        self.assertEqual(data['default_value'], declaration.default_value)
        self.assertEqual(data['name'], declaration.name)
        self.assertIsNone(declaration.identifier)

    def test_deserialize_all_floats(self) -> None:
        data = dict(min_value=33.3, max_value=44, default_value=41.1, name='foo')
        declaration = ParameterDeclaration.deserialize(self.serializer, **data)
        self.assertEqual(data['min_value'], declaration.min_value)
        self.assertEqual(data['max_value'], declaration.max_value)
        self.assertEqual(data['default_value'], declaration.default_value)
        self.assertEqual(data['name'], declaration.name)
        self.assertIsNone(declaration.identifier)

    def test_deserialize_min_max_references(self) -> None:
        data = dict(min_value='bar_min', max_value='bar_max', default_value=-23.5, name='foo')
        declaration = ParameterDeclaration.deserialize(self.serializer, **data)
        self.assertEqual(float('-inf'), declaration.min_value)
        self.assertEqual(float('+inf'), declaration.max_value)
        self.assertEqual(data['default_value'], declaration.default_value)
        self.assertEqual(data['name'], declaration.name)
        self.assertIsNone(declaration.identifier)
Exemplo n.º 4
0
 def test_deserialize_min_max_references(self) -> None:
     data = dict(min_value='bar_min', max_value='bar_max', default_value=-23.5, name='foo')
     declaration = ParameterDeclaration.deserialize(self.serializer, **data)
     self.assertEqual(float('-inf'), declaration.min_value)
     self.assertEqual(float('+inf'), declaration.max_value)
     self.assertEqual(data['default_value'], declaration.default_value)
     self.assertEqual(data['name'], declaration.name)
     self.assertIsNone(declaration.identifier)
Exemplo n.º 5
0
 def test_deserialize_all_floats(self) -> None:
     data = dict(min_value=33.3, max_value=44, default_value=41.1, name='foo')
     declaration = ParameterDeclaration.deserialize(self.serializer, **data)
     self.assertEqual(data['min_value'], declaration.min_value)
     self.assertEqual(data['max_value'], declaration.max_value)
     self.assertEqual(data['default_value'], declaration.default_value)
     self.assertEqual(data['name'], declaration.name)
     self.assertIsNone(declaration.identifier)
Exemplo n.º 6
0
 def test_deserialize_all_default(self) -> None:
     data = dict(min_value=float('-inf'), max_value=float('+inf'), default_value=None, name='foo')
     declaration = ParameterDeclaration.deserialize(self.serializer, **data)
     self.assertEqual(data['min_value'], declaration.min_value)
     self.assertEqual(data['max_value'], declaration.max_value)
     self.assertEqual(data['default_value'], declaration.default_value)
     self.assertEqual(data['name'], declaration.name)
     self.assertIsNone(declaration.identifier)
Exemplo n.º 7
0
 def __min_assignment(self, decl: ParameterDeclaration, value: Union[ParameterDeclaration, float]) -> None:
     decl.min_value = value
Exemplo n.º 8
0
 def test_get_serialization_data_all_floats(self) -> None:
     self.declaration = ParameterDeclaration('foo', min=-3.1, max=4.3, default=0.2)
     self.expected_data['min_value'] = -3.1
     self.expected_data['max_value'] = 4.3
     self.expected_data['default_value'] = 0.2
     self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer))
Exemplo n.º 9
0
 def setUp(self) -> None:
     self.serializer = DummySerializer()
     self.declaration = ParameterDeclaration('foo')
     self.expected_data = dict(name='foo', type=self.serializer.get_type_identifier(self.declaration))
Exemplo n.º 10
0
 def test_is_parameter_valid_min_max_bound(self) -> None:
     decl = ParameterDeclaration('foobar', min=-0.1, max=13.2)
     params = [(False, -0.5), (True, -0.1), (True, 0), (True, 7.9), (True, 13.2), (False, 17.3)]
     for expected, param in params:
         self.assertEqual(expected, decl.is_parameter_valid(param))
Exemplo n.º 11
0
 def test_is_parameter_valid_no_bounds(self) -> None:
     decl = ParameterDeclaration('foo')
     param = ConstantParameter(2.4)
     self.assertTrue(decl.is_parameter_valid(param))
Exemplo n.º 12
0
 def __assign_min_value(self, left_value: ParameterDeclaration, right_value: ParameterDeclaration) -> None:
     left_value.min_value = right_value
Exemplo n.º 13
0
 def test_check_parameter_set_valid(self) -> None:
     min_decl = ParameterDeclaration('min', min=1.2, max=2.3)
     max_decl = ParameterDeclaration('max', min=1.2, max=5.1)
     
     min_param = ConstantParameter(1.3)
     max_param = ConstantParameter(2.3)
     parameters = {'min': min_param, 'max': max_param}
     
     decl = ParameterDeclaration('foo', min=1.3)
     self.assertTrue(decl.check_parameter_set_valid({'foo': ConstantParameter(1.4)}))
     self.assertTrue(decl.check_parameter_set_valid({'foo': ConstantParameter(1.3)}))
     self.assertFalse(decl.check_parameter_set_valid({'foo': ConstantParameter(1.1)}))
     
     decl = ParameterDeclaration('foo', max=2.3)
     self.assertTrue(decl.check_parameter_set_valid({'foo': ConstantParameter(1.4)}))
     self.assertTrue(decl.check_parameter_set_valid({'foo': ConstantParameter(2.3)}))
     self.assertFalse(decl.check_parameter_set_valid({'foo': ConstantParameter(3.1)}))
     
     decl = ParameterDeclaration('foo', min=1.3, max=2.3)
     self.assertFalse(decl.check_parameter_set_valid({'foo': ConstantParameter(0.9)}))
     self.assertTrue(decl.check_parameter_set_valid({'foo': ConstantParameter(1.3)}))
     self.assertTrue(decl.check_parameter_set_valid({'foo': ConstantParameter(1.4)}))
     self.assertTrue(decl.check_parameter_set_valid({'foo': ConstantParameter(2.3)}))
     self.assertFalse(decl.check_parameter_set_valid({'foo': ConstantParameter(3.1)}))
     
     decl = ParameterDeclaration('foo', min=min_decl, max=max_decl)
     self.assertFalse(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(0.9)}))
     self.assertFalse(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.2)}))
     self.assertFalse(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.25)}))
     self.assertTrue(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.3)}))
     self.assertTrue(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.7)}))
     self.assertTrue(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(2.3)}))
     self.assertFalse(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(3.5)}))
     self.assertFalse(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(5.1)}))
     self.assertFalse(decl.check_parameter_set_valid({'min': min_param, 'max': max_param, 'foo': ConstantParameter(17.2)}))