예제 #1
0
def test_invalid_serialization():
    """Test errors for invalid serializations."""
    doc = {
        'name': '0000',
        'dtype': PARA_LIST,
        'index': 0,
        'label': 'Record',
        'isRequired': True,
        'para': Bool('0001').to_dict()
    }
    # Ensure that the original document is valid.
    Array.from_dict(doc, validate=True)
    # Removing a 'para' name will not raise a InvalidParameterError if we do not
    # validate the document but it raises a KeyError when an attempt is made to
    # access the 'para' name.
    del doc['para']
    with pytest.raises(KeyError):
        Array.from_dict(doc, validate=False)
    with pytest.raises(err.InvalidParameterError):
        Array.from_dict(doc, validate=True)
    with pytest.raises(ValueError):
        Array.from_dict(doc={
            'name': '0000',
            'dtype': 'unknown',
            'index': 0,
            'label': 'Record',
            'isRequired': True,
            'para': Bool('0001').to_dict()
        },
                        validate=True)
def test_boolean_parameter_from_dict():
    """Test generating a Boolean parameter declaration from a dictionary
    serialization.
    """
    para = Bool.from_dict(
        Bool.to_dict(
            Bool.from_dict({
                'name': '0000',
                'dtype': PARA_BOOL,
                'label': 'Agree',
                'index': 10,
                'help': 'Do you agree?',
                'defaultValue': False,
                'isRequired': True,
                'group': 'contract'
            })))
    assert para.is_bool()
    assert para.name == '0000'
    assert para.dtype == PARA_BOOL
    assert para.label == 'Agree'
    assert para.index == 10
    assert para.help == 'Do you agree?'
    assert not para.default
    assert para.required
    assert para.group == 'contract'
예제 #3
0
def test_read_boolean_parameters():
    """Test reading lists of boolean parameters."""
    parameters = [
        Bool(name='A', index=0),
        Bool(name='B', index=1, default=False),
        Bool(name='C', index=2, default=False),
    ]
    sc = Scanner(reader=ListReader(['true', 'xyz', '', 'True']))
    arguments = cli.read(parameters, sc)
    assert len(arguments) == 3
    assert arguments['A']
    assert not arguments['B']
    assert arguments['C']
예제 #4
0
def test_list_parameter_from_dict():
    """Test generating a list parameter declaration from a dictionary
    serialization.
    """
    doc = {
        'name': '0000',
        'dtype': PARA_LIST,
        'index': 1,
        'label': 'List',
        'help': 'List of values',
        'defaultValue': [1, 2, 3],
        'isRequired': True,
        'para': Bool('f1').to_dict(),
        'group': 'arrays'
    }
    para = Array.from_dict(Array.to_dict(Array.from_dict(doc)))
    assert para.is_list()
    assert para.name == '0000'
    assert para.dtype == PARA_LIST
    assert para.label == 'List'
    assert para.index == 1
    assert para.help == 'List of values'
    assert para.default == [1, 2, 3]
    assert para.required
    assert para.group == 'arrays'
    assert para.para.name == 'f1'
    assert para.para.is_bool()
예제 #5
0
def test_record_parameter_from_dict():
    """Test generating an parameter record declaration from a dictionary
    serialization.
    """
    doc = {
        'name': '0000',
        'dtype': PARA_RECORD,
        'index': 1,
        'label': 'Record',
        'help': 'List of fields',
        'defaultValue': {'f1': True},
        'isRequired': True,
        'fields': [Bool('f1').to_dict(), Int('f2').to_dict()],
        'group': 'recs'
    }
    para = Record.from_dict(Record.to_dict(Record.from_dict(doc)))
    assert para.is_record()
    assert para.name == '0000'
    assert para.dtype == PARA_RECORD
    assert para.label == 'Record'
    assert para.index == 1
    assert para.help == 'List of fields'
    assert para.default == {'f1': True}
    assert para.required
    assert para.group == 'recs'
    assert len(para.fields) == 2
    assert 'f1' in para.fields
    assert para.fields['f1'].is_bool()
    assert 'f2' in para.fields
    assert para.fields['f2'].is_int()
예제 #6
0
def test_invalid_serialization():
    """Test errors for invalid serializations."""
    doc = {
        'name': '0000',
        'dtype': PARA_RECORD,
        'index': 0,
        'label': 'Record',
        'isRequired': True,
        'fields': [Bool('0001').to_dict()]
    }
    # Ensure that the original document is valid.
    Record.from_dict(doc, validate=True)
    # Invalid dtype value.
    with pytest.raises(ValueError):
        Record.from_dict(doc={
            'name': '0000',
            'dtype': 'unknown',
            'index': 0,
            'label': 'Record',
            'isRequired': True,
            'fields': [Bool('0001').to_dict()]
        }, validate=True)
    # Removing a field name will not raise a InvalidParameterError if we do not
    # validate the document but it raises a KeyError when an attempt is made to
    # access the field name.
    del doc['fields'][0]['name']
    with pytest.raises(KeyError):
        Record.from_dict(doc, validate=False)
    with pytest.raises(err.InvalidParameterError):
        Record.from_dict(doc, validate=True)
    # If the whole field declaration is removed a KeyError is raised when
    # deserializing without validation and a InvalidParameterError is raised
    # when deserializing with validation.
    del doc['fields']
    with pytest.raises(KeyError):
        Record.from_dict(doc, validate=False)
    with pytest.raises(err.InvalidParameterError):
        Record.from_dict(doc, validate=True)
    # Error for field lists with duplicate names.
    field = {'name': 'f1', 'para': Bool('0001').to_dict()}
    doc['fields'] = [field, field]
    with pytest.raises(err.InvalidParameterError):
        Record.from_dict(doc, validate=False)
    with pytest.raises(err.InvalidParameterError):
        Record('0', fields=[Bool('0001'), Bool('0001')])
예제 #7
0
def test_record_parameter_value():
    """Test getting argument value for a record parameter."""
    para = Record('R', fields=[Bool('f1', required=True), Int('f2', default=10), Int('f3')])
    value = [{'name': 'f1', 'value': True}, {'name': 'f3', 'value': '6'}]
    assert para.cast(value) == {'f1': True, 'f2': 10, 'f3': 6}
    value = [{'name': 'f1', 'value': True}]
    assert para.cast(value) == {'f1': True, 'f2': 10}
    with pytest.raises(err.InvalidArgumentError):
        para.cast([])
    with pytest.raises(err.InvalidArgumentError):
        para.cast('a')
    with pytest.raises(err.InvalidArgumentError):
        para.cast([{'name': 'f4', 'value': '5'}])
예제 #8
0
def test_list_parameter_value():
    """Test getting argument value for a list parameter."""
    para = Array('A',
                 para=Record('R', fields=[Bool('f1'),
                                          Int('f2', default=10)]))
    value = [[{
        'name': 'f1',
        'value': True
    }, {
        'name': 'f2',
        'value': '5'
    }], [{
        'name': 'f1',
        'value': False
    }, {
        'name': 'f2',
        'value': '10'
    }]]
    assert para.cast(value) == [{'f1': True, 'f2': 5}, {'f1': False, 'f2': 10}]
    with pytest.raises(err.InvalidArgumentError):
        para.cast('a')
def test_invalid_serialization():
    """Test errors for invalid serializations."""
    with pytest.raises(err.InvalidParameterError):
        Bool.from_dict({
            'name': '0000',
            'dtype': PARA_BOOL,
        })
    Bool.from_dict(
        {
            'name': '0000',
            'dtype': 'string',
            'index': 0,
            'label': 'Name',
            'isRequired': True
        },
        validate=False)
    with pytest.raises(ValueError):
        Bool.from_dict({
            'name': '0000',
            'dtype': 'unknown',
            'index': 0,
            'label': 'Name',
            'isRequired': True
        })
def test_boolean_parameter_value():
    """Test getting argument value for a boolean parameter."""
    para = Bool('0000', 0, 'name')
    # Values that convert to True.
    assert para.cast(True)
    assert para.cast(1)
    assert para.cast('1')
    assert para.cast('T')
    assert para.cast('true')
    # Values that convert to False
    assert not para.cast(False)
    assert not para.cast(0)
    assert not para.cast('0')
    assert not para.cast('')
    assert not para.cast('f')
    assert not para.cast('FALSE')
    assert not para.cast(None)
    # Values that raise errors
    with pytest.raises(err.InvalidArgumentError):
        para.cast('yes')
    with pytest.raises(err.InvalidArgumentError):
        para.cast(10)
    with pytest.raises(err.InvalidArgumentError):
        para.cast('   ')