mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Improve callback data serialization (#1163)
* Improve callback data serialization * Added tests and changelog
This commit is contained in:
parent
fb3076d40f
commit
aad2de4324
3 changed files with 37 additions and 25 deletions
4
CHANGES/1163.feature.rst
Normal file
4
CHANGES/1163.feature.rst
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
Improved CallbackData serialization.
|
||||||
|
|
||||||
|
- Minimized UUID (hex without dashes)
|
||||||
|
- Replaced bool values with int (true=1, false=0)
|
||||||
|
|
@ -67,7 +67,11 @@ class CallbackData(BaseModel):
|
||||||
return ""
|
return ""
|
||||||
if isinstance(value, Enum):
|
if isinstance(value, Enum):
|
||||||
return str(value.value)
|
return str(value.value)
|
||||||
if isinstance(value, (int, str, float, Decimal, Fraction, UUID)):
|
if isinstance(value, UUID):
|
||||||
|
return value.hex
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return str(int(value))
|
||||||
|
if isinstance(value, (int, str, float, Decimal, Fraction)):
|
||||||
return str(value)
|
return str(value)
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Attribute {key}={value!r} of type {type(value).__name__!r}"
|
f"Attribute {key}={value!r} of type {type(value).__name__!r}"
|
||||||
|
|
|
||||||
|
|
@ -49,34 +49,38 @@ class TestCallbackData:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"value,success,expected",
|
"value,expected",
|
||||||
[
|
[
|
||||||
[None, True, ""],
|
[None, ""],
|
||||||
[42, True, "42"],
|
[True, "1"],
|
||||||
["test", True, "test"],
|
[False, "0"],
|
||||||
[9.99, True, "9.99"],
|
[42, "42"],
|
||||||
[Decimal("9.99"), True, "9.99"],
|
["test", "test"],
|
||||||
[Fraction("3/2"), True, "3/2"],
|
[9.99, "9.99"],
|
||||||
[
|
[Decimal("9.99"), "9.99"],
|
||||||
UUID("123e4567-e89b-12d3-a456-426655440000"),
|
[Fraction("3/2"), "3/2"],
|
||||||
True,
|
[UUID("123e4567-e89b-12d3-a456-426655440000"), "123e4567e89b12d3a456426655440000"],
|
||||||
"123e4567-e89b-12d3-a456-426655440000",
|
[MyIntEnum.FOO, "1"],
|
||||||
],
|
[MyStringEnum.FOO, "FOO"],
|
||||||
[MyIntEnum.FOO, True, "1"],
|
|
||||||
[MyStringEnum.FOO, True, "FOO"],
|
|
||||||
[..., False, "..."],
|
|
||||||
[object, False, "..."],
|
|
||||||
[object(), False, "..."],
|
|
||||||
[User(id=42, is_bot=False, first_name="test"), False, "..."],
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_encode_value(self, value, success, expected):
|
def test_encode_value_positive(self, value, expected):
|
||||||
callback = MyCallback(foo="test", bar=42)
|
callback = MyCallback(foo="test", bar=42)
|
||||||
if success:
|
assert callback._encode_value("test", value) == expected
|
||||||
assert callback._encode_value("test", value) == expected
|
|
||||||
else:
|
@pytest.mark.parametrize(
|
||||||
with pytest.raises(ValueError):
|
"value",
|
||||||
assert callback._encode_value("test", value) == expected
|
[
|
||||||
|
...,
|
||||||
|
object,
|
||||||
|
object(),
|
||||||
|
User(id=42, is_bot=False, first_name="test"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_encode_value_negative(self, value):
|
||||||
|
callback = MyCallback(foo="test", bar=42)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
callback._encode_value("test", value)
|
||||||
|
|
||||||
def test_pack(self):
|
def test_pack(self):
|
||||||
with pytest.raises(ValueError, match="Separator symbol .+"):
|
with pytest.raises(ValueError, match="Separator symbol .+"):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue