mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Allow empty and zero parts in CallbackData (#646)
* enh: allow empty parts in CallbackData * enh: allow zero parts in CallbackData * new: tests for CallbackData
This commit is contained in:
parent
f6f2972a11
commit
580fa2e499
2 changed files with 39 additions and 4 deletions
|
|
@ -33,8 +33,6 @@ class CallbackData:
|
||||||
raise ValueError("Prefix can't be empty")
|
raise ValueError("Prefix can't be empty")
|
||||||
if sep in prefix:
|
if sep in prefix:
|
||||||
raise ValueError(f"Separator {sep!r} can't be used in prefix")
|
raise ValueError(f"Separator {sep!r} can't be used in prefix")
|
||||||
if not parts:
|
|
||||||
raise TypeError('Parts were not passed!')
|
|
||||||
|
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
self.sep = sep
|
self.sep = sep
|
||||||
|
|
@ -64,8 +62,6 @@ class CallbackData:
|
||||||
if value is not None and not isinstance(value, str):
|
if value is not None and not isinstance(value, str):
|
||||||
value = str(value)
|
value = str(value)
|
||||||
|
|
||||||
if not value:
|
|
||||||
raise ValueError(f"Value for part {part!r} can't be empty!'")
|
|
||||||
if self.sep in value:
|
if self.sep in value:
|
||||||
raise ValueError(f"Symbol {self.sep!r} is defined as the separator and can't be used in parts' values")
|
raise ValueError(f"Symbol {self.sep!r} is defined as the separator and can't be used in parts' values")
|
||||||
|
|
||||||
|
|
|
||||||
39
tests/test_utils/test_callback_data.py
Normal file
39
tests/test_utils/test_callback_data.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from aiogram.types import CallbackQuery
|
||||||
|
from aiogram.utils.callback_data import CallbackData
|
||||||
|
|
||||||
|
|
||||||
|
class TestCallbackData:
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cb(self):
|
||||||
|
cb = CallbackData('simple', 'action')
|
||||||
|
assert cb.new('x') == 'simple:x'
|
||||||
|
assert cb.new(action='y') == 'simple:y'
|
||||||
|
assert cb.new('') == 'simple:'
|
||||||
|
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='simple:'))) == {'callback_data': {'@': 'simple', 'action': ''}}
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='simple:x'))) == {'callback_data': {'@': 'simple', 'action': 'x'}}
|
||||||
|
assert (await cb.filter(action='y').check(CallbackQuery(data='simple:x'))) is False
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cb_double(self):
|
||||||
|
cb = CallbackData('double', 'pid', 'action')
|
||||||
|
assert cb.new('123', 'x') == 'double:123:x'
|
||||||
|
assert cb.new(pid=456, action='y') == 'double:456:y'
|
||||||
|
assert cb.new('', 'z') == 'double::z'
|
||||||
|
assert cb.new('789', '') == 'double:789:'
|
||||||
|
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='double::'))) == {'callback_data': {'@': 'double', 'pid': '', 'action': ''}}
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='double:x:'))) == {'callback_data': {'@': 'double', 'pid': 'x', 'action': ''}}
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='double::y'))) == {'callback_data': {'@': 'double', 'pid': '', 'action': 'y'}}
|
||||||
|
assert (await cb.filter(action='x').check(CallbackQuery(data='double:123:x'))) == {'callback_data': {'@': 'double', 'pid': '123', 'action': 'x'}}
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cb_zero(self):
|
||||||
|
cb = CallbackData('zero')
|
||||||
|
assert cb.new() == 'zero'
|
||||||
|
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='zero'))) == {'callback_data': {'@': 'zero'}}
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='zero:'))) is False
|
||||||
|
assert (await cb.filter().check(CallbackQuery(data='bla'))) is False
|
||||||
Loading…
Add table
Add a link
Reference in a new issue