Example #1
0
def test_equal_values():
    value_a = c_uamqp.null_value()
    value_b = c_uamqp.null_value()
    value_c = c_uamqp.int_value(42)
    value_d = c_uamqp.int_value(42)
    value_e = c_uamqp.string_value(b'Test')

    assert value_a == value_b
    assert value_c == value_d
    assert value_a != value_c
    assert value_d != value_e
def test_body_sequence():
    message = c_uamqp.create_message()
    message.add_body_sequence(c_uamqp.int_value(1))
    message.add_body_sequence(c_uamqp.int_value(2))
    message.add_body_sequence(c_uamqp.int_value(3))

    assert message.count_body_sequence() == 3
    assert message.body_type == c_uamqp.MessageBodyType.SequenceType

    seq_value = message.get_body_sequence(2)
    assert seq_value.type == c_uamqp.AMQPType.IntValue
    assert seq_value.value == 3
Example #3
0
 def _c_wrapper(self, value):
     try:
         return c_uamqp.int_value(int(value))
     except TypeError:
         raise ValueError("Value must be an integer")
     except OverflowError:
         raise ValueError("Value {} is too large for an Int value.".format(value))
Example #4
0
def data_factory(value, encoding='UTF-8'):
    """Wrap a Python type in the equivalent C AMQP type.
    If the Python type has already been wrapped in a ~uamqp.types.AMQPType
    object - then this will be used to select the appropriate C type.
    - bool => c_uamqp.BoolValue
    - int => c_uamqp.IntValue
    - str => c_uamqp.StringValue
    - bytes => c_uamqp.BinaryValue
    - str (char) => c_uamqp.CharValue
    - list/set/tuple => c_uamqp.ListValue
    - dict => c_uamqp.DictValue (AMQP map)
    - float => c_uamqp.DoubleValue
    - uuid.UUID => c_uamqp.UUIDValue

    :param value: The value to wrap.
    :returns: c_uamqp.AMQPValue
    """
    result = None
    if value is None:
        result = c_uamqp.null_value()
    elif isinstance(value, types.AMQPType):
        result = value.c_data
    elif isinstance(value, c_uamqp.AMQPValue):
        result = value
    elif isinstance(value, bool):
        result = c_uamqp.bool_value(value)
    elif isinstance(value, str) and len(value) == 1:
        result = c_uamqp.char_value(value.encode(encoding))
    elif isinstance(value, str) and len(value) > 1:
        result = c_uamqp.string_value(value.encode(encoding))
    elif isinstance(value, bytes):
        result = c_uamqp.string_value(value)
    elif isinstance(value, uuid.UUID):
        result = c_uamqp.uuid_value(value)
    elif isinstance(value, bytearray):
        result = c_uamqp.binary_value(value)
    elif isinstance(value, float):
        result = c_uamqp.double_value(value)
    elif isinstance(value, int):
        result = c_uamqp.int_value(value)
    elif isinstance(value, dict):
        wrapped_dict = c_uamqp.dict_value()
        for key, item in value.items():
            wrapped_dict[data_factory(key, encoding=encoding)] = data_factory(item, encoding=encoding)
        result = wrapped_dict
    elif isinstance(value, (list, set, tuple)):
        wrapped_list = c_uamqp.list_value()
        wrapped_list.size = len(value)
        for index, item in enumerate(value):
            wrapped_list[index] = data_factory(item, encoding=encoding)
        result = wrapped_list
    return result
Example #5
0
def _convert_py_number(value):
    """Convert a Python integer value into equivalent C object.
    Will attempt to use the smallest possible conversion, starting with int, then long
    then double.
    """
    try:
        return c_uamqp.int_value(value)
    except OverflowError:
        pass
    try:
        return c_uamqp.long_value(value)
    except OverflowError:
        pass
    return c_uamqp.double_value(value)
Example #6
0
def test_dict_value():
    value = c_uamqp.dict_value()
    assert value.type == c_uamqp.AMQPType.DictValue

    one = c_uamqp.string_value(b'One')
    two = c_uamqp.string_value(b'Two')
    i_one = c_uamqp.int_value(1)
    i_two = c_uamqp.int_value(2)
    value[one] = i_one
    value[two] = i_two

    assert len(value) == 2
    assert value[one].value == 1
    assert value[two].value == 2
    with pytest.raises(KeyError):
        value[c_uamqp.null_value()]

    assert value.get(0) == (one, i_one)
    assert value.get(1) == (two, i_two)
    with pytest.raises(IndexError):
        value.get(2)
    
    assert value.value == {b"One": 1, b"Two": 2}
    assert str(value) == "{[One:1],[Two:2]}"
Example #7
0
def test_int_value():
    value = c_uamqp.int_value(255)
    assert value.value == 255
    assert value.type == c_uamqp.AMQPType.IntValue
    assert str(value) == "255"