File size: 2,186 Bytes
e1c010e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | """
Tests for the Object Mapper — synonym normalization and fuzzy matching.
"""
import pytest
from src.utils.object_mapper import (
normalize_label,
find_best_match,
SYNONYM_MAP,
)
class TestNormalizeLabel:
"""Tests for normalize_label()."""
def test_known_synonym(self):
assert normalize_label("mug") == "cup"
def test_known_synonym_case_insensitive(self):
assert normalize_label("MUG") == "cup"
def test_unknown_label_passthrough(self):
assert normalize_label("bottle") == "bottle"
def test_whitespace_stripped(self):
assert normalize_label(" mug ") == "cup"
def test_container_to_bottle(self):
assert normalize_label("container") == "bottle"
def test_smartphone_to_cell_phone(self):
assert normalize_label("smartphone") == "cell phone"
class TestFindBestMatch:
"""Tests for find_best_match()."""
@pytest.fixture
def detected_labels(self) -> list[str]:
return ["bottle", "cup", "box", "person", "laptop"]
def test_exact_match(self, detected_labels):
result = find_best_match("bottle", detected_labels)
assert result == "bottle"
def test_synonym_match(self, detected_labels):
"""'mug' should resolve to 'cup' via synonym normalization."""
result = find_best_match("mug", detected_labels)
assert result == "cup"
def test_container_matches_bottle(self, detected_labels):
result = find_best_match("container", detected_labels)
assert result == "bottle"
def test_substring_match(self, detected_labels):
result = find_best_match("lap", detected_labels)
assert result == "laptop"
def test_no_match_returns_none(self, detected_labels):
result = find_best_match("xyznonexistent", detected_labels)
assert result is None
def test_empty_query(self, detected_labels):
result = find_best_match("", detected_labels)
# Empty normalizes to empty, no match expected
assert result is None or isinstance(result, str)
def test_empty_detected_labels(self):
result = find_best_match("bottle", [])
assert result is None
|