Beispiel #1
0
def test_field_constructor():
    type_ = Type('bool')
    field = Field(type_, 'foo')
    assert field.type == type_
    assert field.name == 'foo'
    assert field.default_value is None

    field = Field(type_, 'foo', '1')
    assert field.default_value

    with pytest.raises(TypeError):
        Field('type', 'foo')

    with pytest.raises(NameError):
        Field(type_, 'foo bar')

    type_ = Type('bool[2]')
    field = Field(type_, 'foo', '[false, true]')
    assert field.default_value == [False, True]

    type_ = Type('bool[]')
    field = Field(type_, 'foo', '[false, true, false]')
    assert field.default_value == [False, True, False]

    type_ = Type('bool[3]')
    with pytest.raises(InvalidValue):
        Field(type_, 'foo', '[false, true]')
def test_parse_primitive_value_string_float():
    for float_type in ['float32', 'float64']:
        value = parse_primitive_value_string(Type(float_type), '0')
        assert value == 0
        value = parse_primitive_value_string(Type(float_type), '0.0')
        assert value == 0
        value = parse_primitive_value_string(Type(float_type), '3.14')
        assert value == 3.14

        with pytest.raises(InvalidValue):
            parse_primitive_value_string(Type(float_type), 'value')
def test_validate_field_types():
    msg_spec = MessageSpecification('pkg', 'Foo', [], [])
    known_msg_type = []
    validate_field_types(msg_spec, known_msg_type)

    msg_spec.fields.append(Field(Type('bool'), 'foo'))
    validate_field_types(msg_spec, known_msg_type)

    msg_spec.fields.append(Field(Type('pkg/Bar'), 'bar'))
    with pytest.raises(UnknownMessageType):
        validate_field_types(msg_spec, known_msg_type)

    known_msg_type.append(BaseType('pkg/Bar'))
    validate_field_types(msg_spec, known_msg_type)
def test_message_specification_constructor():
    msg_spec = MessageSpecification('pkg', 'Foo', [], [])
    assert msg_spec.base_type.pkg_name == 'pkg'
    assert msg_spec.base_type.type == 'Foo'
    assert len(msg_spec.fields) == 0
    assert len(msg_spec.constants) == 0

    with pytest.raises(TypeError):
        MessageSpecification('pkg', 'Foo', None, [])
    with pytest.raises(TypeError):
        MessageSpecification('pkg', 'Foo', [], None)
    with pytest.raises(TypeError):
        MessageSpecification('pkg', 'Foo', ['field'], [])
    with pytest.raises(TypeError):
        MessageSpecification('pkg', 'Foo', [], ['constant'])

    field = Field(Type('bool'), 'foo', '1')
    constant = Constant('bool', 'BAR', '1')
    msg_spec = MessageSpecification('pkg', 'Foo', [field], [constant])
    assert len(msg_spec.fields) == 1
    assert msg_spec.fields[0] == field
    assert len(msg_spec.constants) == 1
    assert msg_spec.constants[0] == constant

    with pytest.raises(ValueError):
        MessageSpecification('pkg', 'Foo', [field, field], [])
    with pytest.raises(ValueError):
        MessageSpecification('pkg', 'Foo', [], [constant, constant])
def test_parse_primitive_value_string_bool():
    valid_bool_string_values = {
        'true': True,
        'TrUe': True,
        'TRUE': True,
        '1': True,
        'false': False,
        'FaLsE': False,
        'FALSE': False,
        '0': False,
    }
    for string_value, expected_value in valid_bool_string_values.items():
        value = parse_primitive_value_string(Type('bool'), string_value)
        assert value == expected_value

    with pytest.raises(InvalidValue):
        parse_primitive_value_string(Type('bool'), '5')
    with pytest.raises(InvalidValue):
        parse_primitive_value_string(Type('bool'), 'true ')
def test_message_specification_methods():
    field = Field(Type('bool'), 'foo', '1')
    constant = Constant('bool', 'BAR', '1')
    msg_spec = MessageSpecification('pkg', 'Foo', [field], [constant])

    assert msg_spec != 'spec'
    assert msg_spec == MessageSpecification('pkg', 'Foo', [field], [constant])
    assert msg_spec != MessageSpecification('pkg', 'Bar', [], [])
    assert msg_spec != MessageSpecification('pkg', 'Foo', [field], [])
    assert msg_spec != MessageSpecification('pkg', 'Foo', [], [constant])
def test_parse_primitive_value_string_integer():
    integer_types = {
        'byte': [8, True],
        'char': [8, False],
        'int8': [8, False],
        'uint8': [8, True],
        'int16': [16, False],
        'uint16': [16, True],
        'int32': [32, False],
        'uint32': [32, True],
        'int64': [64, False],
        'uint64': [64, True],
    }
    for integer_type, (bits, is_unsigned) in integer_types.items():
        lower_bound = 0 if is_unsigned else -(2**(bits - 1))
        upper_bound = (2**(bits if is_unsigned else (bits - 1))) - 1

        value = parse_primitive_value_string(Type(integer_type),
                                             str(lower_bound))
        assert value == lower_bound
        value = parse_primitive_value_string(Type(integer_type), str(0))
        assert value == 0
        value = parse_primitive_value_string(Type(integer_type),
                                             str(upper_bound))
        assert value == upper_bound

        with pytest.raises(InvalidValue):
            parse_primitive_value_string(Type(integer_type), 'value')
        with pytest.raises(InvalidValue):
            parse_primitive_value_string(Type(integer_type),
                                         str(lower_bound - 1))
        with pytest.raises(InvalidValue):
            parse_primitive_value_string(Type(integer_type),
                                         str(upper_bound + 1))
Beispiel #8
0
def test_type_constructor():
    type_ = Type('bool')
    assert type_.pkg_name is None
    assert type_.type == 'bool'
    assert type_.string_upper_bound is None
    assert not type_.is_array
    assert type_.array_size is None
    assert not type_.is_upper_bound

    type_ = Type('bool[]')
    assert type_.pkg_name is None
    assert type_.type == 'bool'
    assert type_.string_upper_bound is None
    assert type_.is_array
    assert type_.array_size is None
    assert not type_.is_upper_bound

    with pytest.raises(TypeError):
        Type('bool]')

    type_ = Type('bool[5]')
    assert type_.pkg_name is None
    assert type_.type == 'bool'
    assert type_.string_upper_bound is None
    assert type_.is_array
    assert type_.array_size == 5
    assert not type_.is_upper_bound

    type_ = Type('bool[<=5]')
    assert type_.pkg_name is None
    assert type_.type == 'bool'
    assert type_.string_upper_bound is None
    assert type_.is_array
    assert type_.array_size == 5
    assert type_.is_upper_bound

    with pytest.raises(TypeError):
        Type('bool[size]')
    with pytest.raises(TypeError):
        Type('bool[0]')
    with pytest.raises(TypeError):
        Type('bool[<=size]')
    with pytest.raises(TypeError):
        Type('bool[<=0]')
def test_parse_primitive_value_string_string():
    value = parse_primitive_value_string(Type('string'), 'foo')
    assert value == 'foo'

    value = parse_primitive_value_string(Type('string'), '"foo"')
    assert value == 'foo'

    value = parse_primitive_value_string(Type('string'), "'foo'")
    assert value == 'foo'

    value = parse_primitive_value_string(Type('string'), '"\'foo\'"')
    assert value == "'foo'"

    value = parse_primitive_value_string(Type('string'), '"foo ')
    assert value == '"foo '

    value = parse_primitive_value_string(Type('string<=3'), 'foo')
    assert value == 'foo'

    with pytest.raises(InvalidValue):
        parse_primitive_value_string(Type('string<=3'), 'foobar')

    with pytest.raises(InvalidValue):
        parse_primitive_value_string(Type('string'), r"""'foo''""")

    with pytest.raises(InvalidValue):
        parse_primitive_value_string(Type('string'),
                                     r'''"foo"bar\"baz"''')  # noqa: Q001

    value = parse_primitive_value_string(Type('string'), '"foo')
    assert value == '"foo'

    value = parse_primitive_value_string(Type('string'), '"foo')
    assert value == '"foo'

    value = parse_primitive_value_string(Type('string'), '\'foo')
    assert value == "'foo"

    value = parse_primitive_value_string(Type('string'), '"foo')
    assert value == '"foo'

    value = parse_primitive_value_string(Type('string'), '\'fo\'o')
    assert value == "'fo'o"

    value = parse_primitive_value_string(Type('string'), '"fo"o')
    assert value == '"fo"o'

    value = parse_primitive_value_string(Type('string'),
                                         r'''"'foo'"''')  # noqa: Q001
    assert value == "'foo'"

    value = parse_primitive_value_string(Type('string'), r"""'"foo"'""")
    assert value == '"foo"'
def test_parse_primitive_value_string_invalid():
    with pytest.raises(ValueError):
        parse_primitive_value_string(Type('pkg/Foo'), '')
    with pytest.raises(ValueError):
        parse_primitive_value_string(Type('bool[]'), '')
Beispiel #11
0
def test_type_methods():
    assert Type('bool[5]') != 23

    assert Type('pkg/Foo') == Type('pkg/Foo')
    assert Type('pkg/Foo[]') == Type('pkg/Foo[]')
    assert Type('pkg/Foo[5]') == Type('pkg/Foo[5]')
    assert Type('pkg/Foo[<=5]') == Type('pkg/Foo[<=5]')

    assert Type('bool') != Type('pkg/Foo')
    assert Type('pkg/Foo[]') != Type('pkg/Foo[5]')
    assert Type('pkg/Foo[5]') != Type('pkg/Foo[<=5]')
    assert Type('pkg/Foo[<=5]') != Type('pkg/Foo[<=23]')

    {Type('pkg/Foo[5]'): None}

    assert str(Type('pkg/Foo[]')) == 'pkg/Foo[]'
    assert str(Type('pkg/Foo[5]')) == 'pkg/Foo[5]'
    assert str(Type('pkg/Foo[<=5]')) == 'pkg/Foo[<=5]'
Beispiel #12
0
def ros_to_tortoise_type(ros_type: RosType) -> str:
    """
    tortoise-orm types:
        BigInt
        Binary
        Boolean
        Char
        Date
        DateTime
        Decimal
        Float
        Int
        JSON
        SmallInt
        Text
        TimeDelta
        UUID
    normal mappings:
        bool: Boolean
        int8: SmallInt
        uint8: SmallInt
        int16: SmallInt
        uint16: SmallInt
        int32: Int
        uint32: Int
        int64: BigInt
        uint64: BigInt
        float: Float
        double: Float
        string: Text
        wstring: Binary
        Nested: JSON
    array mappings:
        int8: Binary
        uint8: Binary
        others: JSON
    special:
        builtin_interfaces/Time: DatetimeField (assumed to be utc)
    """
    if ros_type.is_primitive_type():
        if ros_type.is_array:
            if ros_type.type in ["int8", "uint8"]:
                return "fields.BinaryField()"
            else:
                return "fields.JSONField()"

        if ros_type.type == "bool":
            return "fields.BooleanField()"
        elif ros_type.type in ["int8", "uint8"]:
            return "fields.SmallIntField()"
        elif ros_type.type in ["int16", "uint16"]:
            return "fields.SmallIntField()"
        elif ros_type.type in ["int32", "uint32"]:
            return "fields.IntField()"
        elif ros_type.type in ["int64", "uint64"]:
            return "fields.BigIntField()"
        elif ros_type.type in ["float", "double", "float32", "float64"]:
            return "fields.FloatField()"
        elif ros_type.type == "string":
            return "fields.TextField()"
        elif ros_type.type == "wstring":
            return "fields.BinaryField()"
    else:
        if ros_type.__str__() == "builtin_interfaces/Time":
            return "fields.DatetimeField()"
        else:
            return "fields.JSONField()"
Beispiel #13
0
def test_parse_value_string_not_implemented():
    with pytest.raises(NotImplementedError):
        parse_value_string(Type('pkg/Foo[]'), '')
Beispiel #14
0
def test_parse_value_string():
    with pytest.raises(InvalidValue):
        parse_value_string(Type('bool[]'), '1')

    with pytest.raises(InvalidValue):
        parse_value_string(Type('bool[2]'), '[1]')
    with pytest.raises(InvalidValue):
        parse_value_string(Type('bool[<=1]'), '[1, 0]')

    with pytest.raises(InvalidValue):
        parse_value_string(Type('bool[]'), '[2]')

    value = parse_value_string(Type('bool[]'), '[1]')
    assert value

    value = parse_value_string(Type('string[]'), "['foo', 'bar']")
    assert value == ['foo', 'bar']

    with pytest.raises(InvalidValue):
        parse_value_string(Type('string[<=2]'), '[foo, bar, baz]')

    with pytest.raises(InvalidValue):
        parse_value_string(Type('string[<=2]'), """["foo", 'ba"r', "ba,z"]""")

    with pytest.raises(ValueError):
        parse_value_string(Type('string[<=3]'), """[,"foo", 'ba"r', "ba,z"]""")

    with pytest.raises(ValueError):
        parse_value_string(Type('string[<=3]'), """["foo", ,'ba"r', "ba,z"]""")

    parse_value_string(Type('string[<=2]'), """["fo'o\\\", bar", baz]""")
    assert value
Beispiel #15
0
def test_parse_value_string_primitive():
    parse_value_string(Type('bool'), '1')
Beispiel #16
0
def test_field_methods():
    assert Field(Type('bool'), 'foo') != 23

    assert (Field(Type('bool'), 'foo', '1') == Field(Type('bool'), 'foo',
                                                     'true'))
    assert (Field(Type('bool'), 'foo', '1') != Field(Type('bool'), 'foo',
                                                     'false'))
    assert (Field(Type('bool'), 'foo', '1') != Field(Type('bool'), 'bar', '1'))
    assert (Field(Type('bool'), 'foo', '1') != Field(Type('byte'), 'foo', '1'))

    assert str(Field(Type('bool'), 'foo', '1')) == 'bool foo True'

    assert str(Field(Type('string<=5'), 'foo', 'value')) == \
        "string<=5 foo 'value'"