def test_delivery_rejected(): rej_val = c_uamqp.Messaging.delivery_rejected(b'Failed', b'Test failure') assert rej_val.type == c_uamqp.AMQPType.CompositeType assert rej_val.size == 1 assert rej_val[0].type == c_uamqp.AMQPType.CompositeType assert rej_val[0].size == 2 assert str(rej_val[0][0]) == 'Failed' assert str(rej_val[0][1]) == 'Test failure' error = c_uamqp.dict_value() error_key = c_uamqp.string_value(b"key123") error_value = c_uamqp.string_value(b"value456") error[error_key] = error_value error_info = c_uamqp.create_fields(error) rej_val = c_uamqp.Messaging.delivery_rejected(b'Failed', b'Test failure', error_info) assert rej_val.type == c_uamqp.AMQPType.CompositeType assert rej_val.size == 1 assert rej_val[0].type == c_uamqp.AMQPType.CompositeType assert rej_val[0].size == 3 assert str(rej_val[0][0]) == 'Failed' assert str(rej_val[0][1]) == 'Test failure' error_info = rej_val[0][2] assert error_info.type == c_uamqp.AMQPType.DictValue assert error_info[error_key] == error_value
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
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
def __init__(self, address, encoding='UTF-8'): address = address.encode(encoding) if isinstance(address, str) else address self.parsed_address = self._validate_address(address) self._encoding = encoding self._address = None addr = self.parsed_address.scheme + b"://" + self.parsed_address.hostname + self.parsed_address.path self._c_address = c_uamqp.string_value(addr)
def test_body_value(): message = c_uamqp.create_message() body_value = c_uamqp.string_value(b'TestBodyValue') message.set_body_value(body_value) assert message.body_type == c_uamqp.MessageBodyType.ValueType body = message.get_body_value() assert body.type == c_uamqp.AMQPType.StringValue
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_delivery_modified(): failed_val = c_uamqp.string_value(b"Failed") fields = c_uamqp.create_fields(failed_val) mod_val = c_uamqp.Messaging.delivery_modified(True, False, fields) assert mod_val.type == c_uamqp.AMQPType.CompositeType assert mod_val.size == 3 assert mod_val[0].type == c_uamqp.AMQPType.BoolValue assert mod_val[0].value == True #assert mod_val[1].type == c_uamqp.AMQPType.BoolValue #assert mod_val[1].value == False assert mod_val[2].type == c_uamqp.AMQPType.StringValue
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]}"
def set_filter(self, value, name=constants.STRING_FILTER): """Set a filter on the endpoint. Only one filter can be applied to an endpoint. :param value: The filter to apply to the endpoint. :type value: bytes or str :param name: The name of the filter. This will be encoded as an AMQP Symbol and will also be used as the filter descriptor. By default this is set to b'apache.org:selector-filter:string'. :type name: bytes """ value = value.encode(self._encoding) if isinstance(value, str) else value filter_set = c_uamqp.dict_value() filter_key = c_uamqp.symbol_value(name) descriptor = c_uamqp.symbol_value(name) filter_value = c_uamqp.string_value(value) described_filter_value = c_uamqp.described_value( descriptor, filter_value) self._filters.append((descriptor, filter_value)) filter_set[filter_key] = described_filter_value self._address.filter_set = filter_set
def __init__(self, address, encoding='UTF-8'): self.parsed_address = self._validate_address(address) self._encoding = encoding self._address = None self._c_address = c_uamqp.string_value( address.encode(encoding) if isinstance(address, str) else address)
def test_string_value(): value = c_uamqp.string_value('Test'.encode('utf-8')) assert value.value == b'Test' assert value.type == c_uamqp.AMQPType.StringValue assert str(value) == "Test"