def test_fuzzy_sticker_search(session, tg_context, strict_inline_search, user,
                              query, score):
    """Test fuzzy search for stickers."""
    context = Context(tg_context, query, "123:60:0", user)
    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(
        session, context)
    assert len(fuzzy_matching_stickers) == 40
    assert len(matching_stickers) == 0
    for i, result in enumerate(fuzzy_matching_stickers):
        # Also do this little workaround to prevent fucky number sorting here as well
        if i < 10:
            i = f"0{i}"
        assert result[1] == f"sticker_{i}"
        assert round(result[4], 2) == score

    # Make sure we instantly search for fuzzy stickers, if no normal stickers can be found on the first run
    context = Context(tg_context, query, "", user)
    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(
        session, context)
    assert len(matching_stickers) == 0
    assert len(fuzzy_matching_stickers) == 40

    # Make sure we instantly search for fuzzy stickers, if no normal stickers can be found on a normal offset
    context = Context(tg_context, query, "123:50", user)
    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(
        session, context)
    assert len(matching_stickers) == 0
    assert len(fuzzy_matching_stickers) == 40
def test_combined_sticker_search(session, tg_context, strict_inline_search,
                                 user):
    """Test whether combined search of fuzzy and strict search works."""
    context = Context(tg_context, "roflcpter unique-other", "", user)
    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(
        session, context)
    # Context properties have been properly set
    assert context.switched_to_fuzzy
    assert context.limit == 10
    assert context.fuzzy_offset == 0

    # Sticker result counts are correct
    assert len(matching_stickers) == 40
    assert len(fuzzy_matching_stickers) == 10

    for i, result in enumerate(matching_stickers):
        # Also do this little workaround to prevent fucky number sorting here as well
        print(result)
        if i < 10:
            i = f"0{i}"
        assert result[1] == f"sticker_{i}"
        assert round(result[4], 2) == 1

    for i, result in enumerate(fuzzy_matching_stickers):
        i += 40
        assert result[1] == f"sticker_{i}"
        assert round(result[4], 2) == 0.62
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
示例#4
0
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'
示例#5
0
def test_strict_sticker_search_set_order(session, strict_inline_search, user,
                                         query, first_score, second_score):
    """Test correct sticker set sorting order."""
    # Simple search which should get nearly all stickers from both sets
    context = Context(query, '', user)
    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(session, context)
    assert len(matching_stickers) == 50
    assert len(fuzzy_matching_stickers) == 0
    for i, result in enumerate(matching_stickers):
        # The stickers are firstly sorted by:
        # 1. score
        # 2. StickerSet.name
        # 3. Sticker.file_id
        #
        # Thereby we expect the `a_dumb_shit` set first (20 results), since the scores are the same
        # for both sets, but this set's name is higher in order.
        if i < 20:
            assert result[0] == f'sticker_{i+40}'
            assert result[1] == first_score
            assert result[2] == 'a_dumb_shit'
        # Next we get the second set in order of the file_ids
        elif i >= 20:
            # We need to subtract 21, since we now start to count file_ids from 0
            i = i-20
            # 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] == second_score
            assert result[2] == 'z_mega_awesome'

    # Context properties have not been changed
    assert not context.switched_to_fuzzy
    assert context.limit is None
    assert context.fuzzy_offset is None
示例#6
0
def test_strict_sticker_search_set_score(session, strict_inline_search, user):
    """Test correct score calculation for sticker set titles."""
    # Simple search which should get nearly all stickers from both sets
    context = Context('awesome', '', user)
    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] == 0.75
        assert result[2] == 'z_mega_awesome'
示例#7
0
def test_nsfw_search(session, strict_inline_search, user):
    """Test nsfw search for stickers."""
    context = Context('nsfw p**n roflcopter', '', user)

    sticker_set = strict_inline_search[0]
    sticker_set.nsfw = True

    # Add specific sticker to tag
    sticker = sticker_set.stickers[0]
    tag = Tag.get_or_create(session, 'p**n', True, False)
    sticker.tags.append(tag)
    session.commit()

    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(session, context)
    assert len(matching_stickers) == 1
    assert matching_stickers[0][0] == sticker.file_id
def test_similar_fuzzy_tags_search(session, tg_context, fuzzy_inline_search,
                                   user, query, score):
    """Test fuzzy search for stickers with similar tags.

    This test is here to ensure that stickers won't show up multiple times in search,
    if there are multiple tags on the sticker that match to a single search word.
    """
    context = Context(tg_context, query, "123:0:0", user)
    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(
        session, context)

    assert len(fuzzy_matching_stickers) == 20
    assert len(matching_stickers) == 0

    for i, result in enumerate(fuzzy_matching_stickers):
        # Also do this little workaround to prevent fucky number sorting here as well
        if i < 10:
            i = f"0{i}"
        assert result[1] == f"sticker_{i}"
        assert round(result[4], 2) == score
示例#9
0
def test_no_combined_on_full_strict(session, strict_inline_search, user):
    """Test fuzzy search for stickers."""
    context = Context('roflcpter unique_other', '', user)
    # Add ten more stickers to the strict matching set
    sticker_set = strict_inline_search[0]
    for i in range(60, 70):
        sticker = sticker_factory(session, f'sticker_{i}', ['testtag', 'unique_other'])
        sticker_set.stickers.append(sticker)
    session.commit()

    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(session, context)

    # Context properties have been properly set
    assert not context.switched_to_fuzzy
    assert context.limit is None
    assert context.fuzzy_offset is None

    # Sticker result counts are correct
    assert len(matching_stickers) == 50
    assert len(fuzzy_matching_stickers) == 0
示例#10
0
def test_combined_sticker_search(session, strict_inline_search, user):
    """Test fuzzy search for stickers."""
    context = Context('roflcpter unique_other', '', user)
    matching_stickers, fuzzy_matching_stickers, duration = get_matching_stickers(session, context)
    # Context properties have been properly set
    assert context.switched_to_fuzzy
    assert context.limit == 10
    assert context.fuzzy_offset == 0

    # Sticker result counts are correct
    assert len(matching_stickers) == 40
    assert len(fuzzy_matching_stickers) == 10

    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 round(result[1], 2) == 1

    for i, result in enumerate(fuzzy_matching_stickers):
        i += 40
        assert result[0] == f'sticker_{i}'
        assert round(result[1], 2) == 0.62