def test_string_parameter_from_dict():
    """Test generating a string parameter declaration from a dictionary
    serialization.
    """
    para = String.from_dict(
        String.to_dict(
            String.from_dict({
                'name': '0000',
                'dtype': PARA_STRING,
                'label': 'Firstname',
                'index': 1,
                'help': 'Your first name',
                'defaultValue': 'Alice',
                'isRequired': True,
                'group': 'person'
            })))
    assert para.is_string()
    assert para.name == '0000'
    assert para.dtype == PARA_STRING
    assert para.label == 'Firstname'
    assert para.index == 1
    assert para.help == 'Your first name'
    assert para.default == 'Alice'
    assert para.required
    assert para.group == 'person'
def test_parameter_index_serialization():
    """Test generating parameter index from serializations."""
    p1 = String(name='0', label='P1', index=1)
    p2 = String(name='1', label='P2', index=0)
    doc = ParameterIndex.from_dict([p1.to_dict(), p2.to_dict()]).to_dict()
    parameters = ParameterIndex.from_dict(doc)
    assert len(parameters) == 2
    assert '0' in parameters
    assert '1' in parameters
    assert [p.name for p in parameters.sorted()] == ['1', '0']
    # Error case: Duplicate parameter.
    with pytest.raises(err.InvalidTemplateError):
        ParameterIndex.from_dict([p1.to_dict(), p1.to_dict()])
    # Error case: Unknown parameter type.
    doc = p1.to_dict()
    doc['dtype'] = 'unknown'
    with pytest.raises(err.InvalidParameterError):
        ParameterIndex.from_dict([doc])