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")
Bài viết liên quan
__init__.py
__init__.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/__init__.py).
Đọc bài viết →auth.py
auth.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/auth.py).
Đọc bài viết →routes.py
routes.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/routes.py).
Đọc bài viết →config.py
config.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/config.py).
Đọc bài viết →admin_ingest.py
admin_ingest.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_ingest.py).
Đọc bài viết →admin_stats.py
admin_stats.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_stats.py).
Đọc bài viết →