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
Esempio n. 2
0
def test_switched_to_fuzzy_offset(user):
    """We didn't get enough strict results and switched to fuzzy."""
    context = Context("test", "123:50", user)
    matching_stickers = range(0, 40)
    fuzzy_matching_stickers = range(0, 10)
    context.switch_to_fuzzy(10)

    next_offset = get_next_offset(context, matching_stickers,
                                  fuzzy_matching_stickers)
    assert next_offset == "123:90:10"
Esempio n. 3
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
Esempio n. 4
0
def test_sticker_set_search(user):
    """A simple normal search should result in default values on the context object."""
    context = Context('set pack test', '', user)

    assert context.mode == Context.STICKER_SET_MODE
    assert len(context.tags) == 1
    assert context.tags[0] == 'test'
Esempio n. 5
0
def test_done_set_offset(user):
    """Create a new set offset payload."""
    context = Context("test", "123:8", user)
    matching_set = range(0, 4)

    next_offset = get_next_set_offset(context, matching_set)
    assert next_offset == "done"
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
Esempio n. 7
0
def test_get_next_set_offset(user):
    """Create a new set offset payload."""
    context = Context("test", "123:0", user)
    matching_sets = range(0, 8)

    next_offset = get_next_set_offset(context, matching_sets)
    assert next_offset == "123:8"
Esempio n. 8
0
def test_get_next_strict_offset(user):
    """Create a new strict offset payload."""
    context = Context("test", "123:50", user)
    matching_stickers = range(0, 50)

    next_offset = get_next_offset(context, matching_stickers, [])
    assert next_offset == "123:100"
Esempio n. 9
0
def test_get_strict_finished_offset(user):
    """Create an offset payload that signals that strict search is done."""
    context = Context("test", "123:50", user)
    matching_stickers = range(0, 10)

    next_offset = get_next_offset(context, matching_stickers, [])
    assert next_offset == "done"
Esempio n. 10
0
def test_extract_strict_offset(user):
    """Extract data from an normal offset payload."""
    context = Context("test", "15235:50", user)

    assert context.inline_query_id == 15235
    assert context.offset == 50
    assert context.fuzzy_offset is None
Esempio n. 11
0
def test_extract_fuzzy_offset(user):
    """Extract data from an fuzzy offset payload."""
    context = Context("test", "15235:100:0", user)

    assert context.inline_query_id == 15235
    assert context.offset == 100
    assert context.fuzzy_offset == 0
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
Esempio n. 13
0
def test_default_context_search(user):
    """A simple normal search should result in default values on the context object."""
    context = Context('test', '', user)

    assert context.mode == Context.STICKER_MODE
    assert context.nsfw is False
    assert context.furry is False
Esempio n. 14
0
def test_done_set_offset(user):
    """Create a new set offset payload."""
    context = Context('test', '123:8', user)
    matching_set = range(0, 4)

    next_offset = get_next_set_offset(context, matching_set)
    assert next_offset == 'done'
Esempio n. 15
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'
Esempio n. 16
0
def test_extract_empty_offset(user):
    """Empyt offset should result in normal offset 0."""
    context = Context("test", "", user)

    assert context.inline_query_id is None
    assert context.offset == 0
    assert context.fuzzy_offset is None
Esempio n. 17
0
def test_get_next_fuzzy_offset(user):
    """Create a new fuzzy offset payload."""
    context = Context('test', '123:60:50', user)
    matching_stickers = []
    fuzzy_matching_stickers = range(0, 50)

    next_offset = get_next_offset(context, matching_stickers,
                                  fuzzy_matching_stickers)
    assert next_offset == '123:60:100'
Esempio n. 18
0
def test_special_flag_favorite_search(user):
    """A special search without any additional keywords results in a favorite search."""
    context = Context('nsfw fur furry', '', user)

    assert context.mode == Context.FAVORITE_MODE
    assert context.nsfw is True
    assert context.furry is True

    assert len(context.tags) == 0
Esempio n. 19
0
def test_done_offset(user):
    """Create a new fuzzy offset payload."""
    context = Context("test", "123:60:50", user)
    matching_stickers = []
    fuzzy_matching_stickers = range(0, 30)

    next_offset = get_next_offset(context, matching_stickers,
                                  fuzzy_matching_stickers)
    assert next_offset == "done"
Esempio n. 20
0
def test_special_flag_search(user):
    """A search with special keywords should result in a special search."""
    context = Context('nsfw fur furry test', '', user)

    assert context.mode == Context.STICKER_MODE
    assert context.nsfw is True
    assert context.furry is True

    assert len(context.tags) == 1
    assert context.tags[0] == 'test'
Esempio n. 21
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'
def test_strict_sticker_search_set_order(session, strict_inline_search, user):
    """Test correct sticker set sorting order."""
    # Simple search which should get nearly all stickers from both sets
    context = Context('testtag set', '', user)
    assert context.mode == Context.STICKER_SET_MODE

    matching_sets, duration = get_matching_sticker_sets(session, context)
    assert len(matching_sets) == 2

    first_set = matching_sets[0]
    assert first_set[0].name == 'z_mega_awesome'
    assert first_set[1] == 40

    second_set = matching_sets[1]
    assert second_set[0].name == 'a_dumb_shit'
    assert second_set[1] == 20
Esempio n. 23
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
Esempio n. 25
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
Esempio n. 26
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
Esempio n. 27
0
def test_favorite_search(user):
    """A simple normal search should result in default values on the context object."""
    context = Context('', '', user)
    assert context.mode == Context.FAVORITE_MODE