S SmartDocs
Série: Ricky python 49 linhas · Atualizado 2026-03-23

test_retrieval_images.py

Ricky/RAG_project/tests/test_retrieval_images.py

"""
Unit tests for retrieval/images.py — pure functions, no network required.
"""
import pytest
from retrieval.images import score_image_path, rank_image_hits


class TestScoreImagePath:
    def test_exact_token_match(self):
        score = score_image_path("coolfire watch", "/images/coolfire watch.jpg")
        assert score > 0

    def test_product_hint_bonus(self):
        # "coolfire" triggers the source hint bonus
        score_matching = score_image_path("coolfire watch", "/images/Coolfire watch.jpg")
        score_other = score_image_path("coolfire watch", "/images/handyspring bottle.jpg")
        assert score_matching > score_other

    def test_stopwords_ignored(self):
        # "show me the image of" should be ignored
        score = score_image_path("show me the image of coolfire", "/images/coolfire watch.jpg")
        assert score > 0

    def test_zero_for_unrelated(self):
        score = score_image_path("keychain time", "/images/handyspring bottle.jpg")
        # Different product — may score 0 or a small positive but significantly less
        matching = score_image_path("keychain", "/images/FIVE SENSES talking key chain.jpg")
        assert matching > score

    def test_keychain_vs_key_chain(self):
        # Handles the "keychain" vs "key chain" spacing difference
        score_compact = score_image_path("keychain", "/images/key chain photo.jpg")
        assert score_compact > 0


class TestRankImageHits:
    def test_product_specific_hit_first(self):
        hits = [
            "/images/handyspring bottle.jpg",
            "/images/coolfire watch.jpg",
        ]
        ranked = rank_image_hits("coolfire watch", hits)
        assert "coolfire" in ranked[0].lower()

    def test_alphabetical_tiebreak(self):
        hits = ["/images/b_img.jpg", "/images/a_img.jpg"]
        ranked = rank_image_hits("unrelated query", hits)
        # Both score 0; alphabetical is the tiebreak
        assert ranked[0].endswith("a_img.jpg")

Artigos relacionados