From 15d01c22447f1ad7f98d84c83300b72f7f168507 Mon Sep 17 00:00:00 2001 From: JRoot Junior Date: Mon, 22 Apr 2024 13:26:19 +0300 Subject: [PATCH] Cover FSM key builder (business_connection_id --- aiogram/fsm/storage/redis.py | 11 ++++-- tests/test_fsm/storage/test_redis.py | 58 ++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/aiogram/fsm/storage/redis.py b/aiogram/fsm/storage/redis.py index eae71eec..a818a831 100644 --- a/aiogram/fsm/storage/redis.py +++ b/aiogram/fsm/storage/redis.py @@ -44,7 +44,10 @@ class DefaultKeyBuilder(KeyBuilder): Simple Redis key builder with default prefix. Generates a colon-joined string with prefix, chat_id, user_id, - optional bot_id and optional destiny. + optional bot_id, business_connection_id and destiny. + + Format: + :code:`::::::` """ def __init__( @@ -61,7 +64,7 @@ class DefaultKeyBuilder(KeyBuilder): :param separator: separator :param with_bot_id: include Bot id in the key :param with_business_connection_id: include business connection id - :param with_destiny: include destiny key + :param with_destiny: include a destiny key """ self.prefix = prefix self.separator = separator @@ -73,12 +76,12 @@ class DefaultKeyBuilder(KeyBuilder): parts = [self.prefix] if self.with_bot_id: parts.append(str(key.bot_id)) + if self.with_business_connection_id and key.business_connection_id: + parts.append(str(key.business_connection_id)) parts.append(str(key.chat_id)) if key.thread_id: parts.append(str(key.thread_id)) parts.append(str(key.user_id)) - if self.with_business_connection_id and key.business_connection_id: - parts.append(str(key.business_connection_id)) if self.with_destiny: parts.append(key.destiny) elif key.destiny != DEFAULT_DESTINY: diff --git a/tests/test_fsm/storage/test_redis.py b/tests/test_fsm/storage/test_redis.py index adca384a..18c143e3 100644 --- a/tests/test_fsm/storage/test_redis.py +++ b/tests/test_fsm/storage/test_redis.py @@ -12,26 +12,62 @@ BOT_ID = 42 CHAT_ID = -1 USER_ID = 2 THREAD_ID = 3 +BUSINESS_CONNECTION_ID = "4" FIELD = "data" class TestRedisDefaultKeyBuilder: @pytest.mark.parametrize( - "with_bot_id,with_destiny,result", + "key_builder,result", [ - [False, False, f"{PREFIX}:{CHAT_ID}:{USER_ID}:{FIELD}"], - [True, False, f"{PREFIX}:{BOT_ID}:{CHAT_ID}:{USER_ID}:{FIELD}"], - [True, True, f"{PREFIX}:{BOT_ID}:{CHAT_ID}:{USER_ID}:{DEFAULT_DESTINY}:{FIELD}"], - [False, True, f"{PREFIX}:{CHAT_ID}:{USER_ID}:{DEFAULT_DESTINY}:{FIELD}"], + [ + DefaultKeyBuilder( + prefix=PREFIX, + with_bot_id=True, + with_destiny=True, + with_business_connection_id=True, + ), + f"{PREFIX}:{BOT_ID}:{BUSINESS_CONNECTION_ID}:{CHAT_ID}:{USER_ID}:{DEFAULT_DESTINY}:{FIELD}", + ], + [ + DefaultKeyBuilder(prefix=PREFIX, with_bot_id=True, with_destiny=True), + f"{PREFIX}:{BOT_ID}:{CHAT_ID}:{USER_ID}:{DEFAULT_DESTINY}:{FIELD}", + ], + [ + DefaultKeyBuilder( + prefix=PREFIX, with_bot_id=True, with_business_connection_id=True + ), + f"{PREFIX}:{BOT_ID}:{BUSINESS_CONNECTION_ID}:{CHAT_ID}:{USER_ID}:{FIELD}", + ], + [ + DefaultKeyBuilder(prefix=PREFIX, with_bot_id=True), + f"{PREFIX}:{BOT_ID}:{CHAT_ID}:{USER_ID}:{FIELD}", + ], + [ + DefaultKeyBuilder( + prefix=PREFIX, with_destiny=True, with_business_connection_id=True + ), + f"{PREFIX}:{BUSINESS_CONNECTION_ID}:{CHAT_ID}:{USER_ID}:{DEFAULT_DESTINY}:{FIELD}", + ], + [ + DefaultKeyBuilder(prefix=PREFIX, with_destiny=True), + f"{PREFIX}:{CHAT_ID}:{USER_ID}:{DEFAULT_DESTINY}:{FIELD}", + ], + [ + DefaultKeyBuilder(prefix=PREFIX, with_business_connection_id=True), + f"{PREFIX}:{BUSINESS_CONNECTION_ID}:{CHAT_ID}:{USER_ID}:{FIELD}", + ], + [DefaultKeyBuilder(prefix=PREFIX), f"{PREFIX}:{CHAT_ID}:{USER_ID}:{FIELD}"], ], ) - async def test_generate_key(self, with_bot_id: bool, with_destiny: bool, result: str): - key_builder = DefaultKeyBuilder( - prefix=PREFIX, - with_bot_id=with_bot_id, - with_destiny=with_destiny, + async def test_generate_key(self, key_builder: DefaultKeyBuilder, result: str): + key = StorageKey( + chat_id=CHAT_ID, + user_id=USER_ID, + bot_id=BOT_ID, + business_connection_id=BUSINESS_CONNECTION_ID, + destiny=DEFAULT_DESTINY, ) - key = StorageKey(chat_id=CHAT_ID, user_id=USER_ID, bot_id=BOT_ID, destiny=DEFAULT_DESTINY) assert key_builder.build(key, FIELD) == result async def test_destiny_check(self):