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 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 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_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 test_null_value(): value = c_uamqp.null_value() assert value.value == None assert value.type == c_uamqp.AMQPType.NullValue assert str(value) == "NULL"