예제 #1
0
def test_boolean_value():
    false_value = c_uamqp.bool_value(False)
    assert false_value.value == False
    assert str(false_value) == "false"

    true_value = c_uamqp.bool_value(True)
    assert true_value.value == True
    assert true_value.type == c_uamqp.AMQPType.BoolValue
    assert str(true_value) == "true"
def test_annotations():
    test_value = c_uamqp.bool_value(True)
    value = c_uamqp.create_annotations(test_value)

    # TODO
    #a_map = value.value
    #assert a_map.type == c_uamqp.AMQPType.DictValue

    new_value = value.clone()
    assert new_value is not value
예제 #3
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, LongValue, DoubleValue
    - str => c_uamqp.StringValue
    - bytes => c_uamqp.BinaryValue
    - 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.
    :type value: ~uamqp.types.AMQPType
    :rtype: uamqp.c_uamqp.AMQPValue
    """
    result = None
    if value is None:
        result = c_uamqp.null_value()
    elif hasattr(value, 'c_data'):
        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, six.text_type):
        result = c_uamqp.string_value(value.encode(encoding))
    elif isinstance(value, six.binary_type):
        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, six.integer_types):
        result = _convert_py_number(value)
    elif isinstance(value, float):
        result = c_uamqp.double_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
    elif isinstance(value, datetime):
        timestamp = int((time.mktime(value.utctimetuple()) * 1000) +
                        (value.microsecond / 1000))
        result = c_uamqp.timestamp_value(timestamp)
    return result
예제 #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
예제 #5
0
def test_list_value():
    value = c_uamqp.list_value()
    assert value.type == c_uamqp.AMQPType.ListValue
    assert value.size == 0

    value.size = 2
    assert len(value) == 2
    assert value.size == 2

    val_1 = c_uamqp.bool_value(True)
    val_2 = c_uamqp.ubyte_value(125)

    value[0] = val_1
    value[1] = val_2
    with pytest.raises(IndexError):
        value[2] = c_uamqp.null_value()

    assert value[0].value == True
    assert value[1].value == 125
    assert value.value == [True, 125]
    assert str(value) == "{true,125}"
def test_message_annotations():
    test_value = c_uamqp.bool_value(True)
    value = c_uamqp.create_message_annotations(test_value)
def test_delivery_annotations():
    test_value = c_uamqp.bool_value(True)
    value = c_uamqp.create_delivery_annotations(test_value)