def test_encode_tagset_values(values, expected): """Test that we can properly encode data into the expected format""" header = encode_tagset_values(values) assert expected == header # Ensure what we generate also parses correctly assert values == decode_tagset_string(header)
def test_encode_tagset_values_strip_spaces(): """Test that leading and trailing spaces are stripped from keys and values""" # Leading/trailing spaces are striped values = {" key ": " value "} res = encode_tagset_values(values) assert "key=value" == res assert {"key": "value"} == decode_tagset_string(res)
def test_encode_tagset_values_max_size(): """Test that exceeding the max size raises an exception""" # DEV: Use OrderedDict to ensure consistent iteration for encoding values = OrderedDict([ # Short values we know will pack into the final result ("a", "1"), ("b", "2"), ("somereallylongkey", "somereallyreallylongvalue"), ]) with pytest.raises(TagsetMaxSizeError) as ex_info: encode_tagset_values(values, max_size=10) ex = ex_info.value assert ex.values == values assert ex.max_size == 10 assert ex.current_results == "a=1,b=2"
def test_encode_tagset_values_invalid_type(): """ encode_tagset_values accepts `values` as an `object` instead of `dict` so we can allow subclasses like `collections.OrderedDict` ensure that we properly raise a `TypeError` when a non-dict is passed """ # Allowed encode_tagset_values({}) encode_tagset_values(OrderedDict()) # Not allowed, AttributeError for values in (None, True, 10, object(), []): with pytest.raises(AttributeError): encode_tagset_values(values)
def test_encode_tagset_values_malformed(values): """Test that invalid data raises a TagsetEncodeError""" with pytest.raises(TagsetEncodeError): encode_tagset_values(values)