def test_get_context(): bot = Minette(prepare_table=True) with bot.connection_provider.get_connection() as connection: # register context for test ctx = bot.context_store.get( channel="get_context_test", channel_user_id=user_id, connection=connection) ctx.data["unixtime"] = date_to_unixtime(now) bot.context_store.save(ctx, connection) ctx_group = bot.context_store.get( channel="get_context_test", channel_user_id="group_" + user_id, connection=connection) ctx_group.data["unixtime"] = date_to_unixtime(now) bot.context_store.save(ctx_group, connection) ctx_detail = bot.context_store.get( channel="get_context_test_detail", channel_user_id=user_id, connection=connection) ctx_detail.data["unixtime"] = date_to_unixtime(now) bot.context_store.save(ctx_detail, connection) # without detail request = Message( text="hello", channel="get_context_test", channel_user_id=user_id) context = bot._get_context(request, connection) assert context.channel == "get_context_test" assert context.channel_user_id == user_id assert context.data["unixtime"] == date_to_unixtime(now) # group without group request = Message( text="hello", channel="get_context_test", channel_user_id=user_id) request.group = Group(id="group_" + user_id) context = bot._get_context(request, connection) assert context.channel == "get_context_test" assert context.channel_user_id == "group_" + user_id assert context.data["unixtime"] == date_to_unixtime(now) # with detail bot.config.confg_parser.set( "minette", "context_scope", "channel_detail") request = Message( text="hello", channel="get_context_test", channel_detail="detail", channel_user_id=user_id) context = bot._get_context(request, connection) assert context.channel == "get_context_test_detail" assert context.channel_user_id == user_id assert context.data["unixtime"] == date_to_unixtime(now)
def test_save_context(): bot = Minette(prepare_table=True) with bot.connection_provider.get_connection() as connection: # register context for test ctx = bot.context_store.get( channel="save_context_test", channel_user_id=user_id, connection=connection) ctx.data["unixtime"] = date_to_unixtime(now) # save ctx.topic.keep_on = True bot._save_context(ctx, connection) # check request = Message( text="hello", channel="save_context_test", channel_user_id=user_id) context = bot._get_context(request, connection) assert context.channel == "save_context_test" assert context.channel_user_id == user_id assert context.data["unixtime"] == date_to_unixtime(now)