def test_normal_search_with_single_usage(session, strict_inline_search, user): """Test correct score calculation for sticker set titles with a single sticker usage.""" # Simple search which should get nearly all stickers from both sets context = Context('awesome', '', user) used_sticker = session.query(Sticker).get('sticker_00') assert used_sticker is not None sticker_usage = StickerUsage(user, used_sticker) sticker_usage.usage_count = 1 session.add(sticker_usage) session.commit() matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers( session, context) assert len(matching_stickers) == 40 assert len(fuzzy_matching_stickers) == 0 for i, result in enumerate(matching_stickers): if i == 0: assert result[0] == 'sticker_00' assert result[1] == 1.0 assert result[2] == 'z_mega_awesome' else: # Also do this little workaround to prevent fucky number sorting here as well if i < 10: i = f'0{i}' assert result[0] == f'sticker_{i}' assert result[1] == 0.75 assert result[2] == 'z_mega_awesome'
def test_search_with_usage_from_another_user(session, strict_inline_search, user): """Test correct score calculation for sticker set titles with a single sticker usage.""" # Simple search which should get nearly all stickers from both sets context = Context("awesome", "", user) other_user = user_factory(session, 100, "other_user") used_sticker = session.query(Sticker).filter( Sticker.file_id == "sticker_00").one() sticker_usage = StickerUsage(other_user, used_sticker) sticker_usage.usage_count = 1 session.add(sticker_usage) session.commit() matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers( session, context) assert len(matching_stickers) == 40 assert len(fuzzy_matching_stickers) == 0 for i, result in enumerate(matching_stickers): # Also do this little workaround to prevent fucky number sorting here as well if i < 10: i = f"0{i}" assert result[0] == f"sticker_{i}" assert result[1] == "z_mega_awesome" assert result[2] == 0.75
def upgrade(): op.create_table( "sticker_usage", sa.Column("sticker_file_id", sa.String(), nullable=False), sa.Column("user_id", sa.BigInteger(), nullable=False), sa.Column("usage_count", sa.Integer(), nullable=False), sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False), sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False), sa.ForeignKeyConstraint( ["sticker_file_id"], ["sticker.file_id"], onupdate="cascade", ondelete="cascade", deferrable=True, ), sa.ForeignKeyConstraint( ["user_id"], ["user.id"], onupdate="cascade", ondelete="cascade", deferrable=True, ), sa.PrimaryKeyConstraint("sticker_file_id", "user_id"), ) op.create_index( op.f("ix_sticker_usage_sticker_file_id"), "sticker_usage", ["sticker_file_id"], unique=False, ) op.create_index(op.f("ix_sticker_usage_user_id"), "sticker_usage", ["user_id"], unique=False) session = Session(bind=op.get_bind()) usages = (session.query(User, func.count( InlineQuery.sticker_file_id)).join(InlineQuery).join( Sticker).add_entity(Sticker).filter( InlineQuery.sticker_file_id.isnot(None)).group_by( User, Sticker).all()) for usage in usages: user = usage[0] count = usage[1] sticker = usage[2] sticker_usage = StickerUsage(user, sticker) sticker_usage.usage_count = count session.add(sticker_usage) session.commit()
def upgrade(): op.create_table( 'sticker_usage', sa.Column('sticker_file_id', sa.String(), nullable=False), sa.Column('user_id', sa.BigInteger(), nullable=False), sa.Column('usage_count', sa.Integer(), nullable=False), sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), sa.ForeignKeyConstraint(['sticker_file_id'], ['sticker.file_id'], onupdate='cascade', ondelete='cascade', deferrable=True), sa.ForeignKeyConstraint(['user_id'], ['user.id'], onupdate='cascade', ondelete='cascade', deferrable=True), sa.PrimaryKeyConstraint('sticker_file_id', 'user_id'), ) op.create_index(op.f('ix_sticker_usage_sticker_file_id'), 'sticker_usage', ['sticker_file_id'], unique=False) op.create_index(op.f('ix_sticker_usage_user_id'), 'sticker_usage', ['user_id'], unique=False) session = Session(bind=op.get_bind()) usages = session.query(User, func.count(InlineQuery.sticker_file_id)) \ .join(InlineQuery) \ .join(Sticker) \ .add_entity(Sticker) \ .filter(InlineQuery.sticker_file_id.isnot(None)) \ .group_by(User, Sticker) \ .all() for usage in usages: user = usage[0] count = usage[1] sticker = usage[2] sticker_usage = StickerUsage(user, sticker) sticker_usage.usage_count = count session.add(sticker_usage) session.commit()
def handle_chosen_inline_result(bot, update, session, user): """Save the chosen inline result.""" result = update.chosen_inline_result splitted = result.result_id.split(':') # This is a result from a banned user if len(splitted) < 2: return [search_id, file_id] = splitted inline_query = session.query(InlineQuery).get(search_id) # This happens, if the user clicks on a link in sticker set search. sticker = session.query(Sticker).get(file_id) if sticker is None: return inline_query.sticker_file_id = file_id sticker_usage = StickerUsage.get_or_create(session, inline_query.user, sticker) sticker_usage.usage_count += 1
def handle_chosen_inline_result(update, context): session = get_session() """Save the chosen inline result.""" result = update.chosen_inline_result splitted = result.result_id.split(":") # This is a result from a banned user if len(splitted) < 2: return [search_id, sticker_id] = splitted # In sticker set search, the second parameter is the md5 of the set's name # We're not interested in this data, thereby simply drop it if len(sticker_id) == 32: return inline_query = session.query(InlineQuery).get(search_id) # Clean all cache values as soon as the user selects a result cache = context.bot_data["query_cache"] if inline_query.id in cache: del cache[inline_query.id] sticker = session.query(Sticker).filter( Sticker.id == sticker_id).one_or_none() # This happens, if the user clicks on a link in sticker set search. if sticker is None: return inline_query.sticker_file_unique_id = sticker.file_unique_id inline_query.sticker_file_unique_id = sticker.file_unique_id sticker_usage = StickerUsage.get_or_create(session, inline_query.user, sticker) sticker_usage.usage_count += 1 session.commit()
def handle_chosen_inline_result(update, context): session = get_session() """Save the chosen inline result.""" result = update.chosen_inline_result splitted = result.result_id.split(":") # This is a result from a banned user if len(splitted) < 2: return [search_id, sticker_id] = splitted inline_query = session.query(InlineQuery).get(search_id) # This happens, if the user clicks on a link in sticker set search. sticker = session.query(Sticker).filter( Sticker.id == sticker_id).one_or_none() if sticker is None: return inline_query.sticker_file_id = sticker.file_id sticker_usage = StickerUsage.get_or_create(session, inline_query.user, sticker) sticker_usage.usage_count += 1