|
| 1 | +# pylint: disable=redefined-outer-name, too-many-arguments, too-many-positional-arguments |
| 2 | + |
| 3 | +"""Unit tests for RagasMetrics.""" |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
| 8 | +from pytest_mock import MockerFixture |
| 9 | + |
| 10 | +from lightspeed_evaluation.core.embedding.manager import ( |
| 11 | + EmbeddingError, |
| 12 | + EmbeddingManager, |
| 13 | +) |
| 14 | +from lightspeed_evaluation.core.metrics.ragas import RagasMetrics |
| 15 | +from lightspeed_evaluation.core.models import EmbeddingConfig, EvaluationScope, TurnData |
| 16 | +from lightspeed_evaluation.core.system.exceptions import ( |
| 17 | + ConfigurationError, |
| 18 | + EvaluationError, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def mock_ragas_deps(mocker: MockerFixture) -> dict[str, Any]: |
| 24 | + """Mock all heavy dependencies needed to construct RagasMetrics.""" |
| 25 | + mock_llm_manager = mocker.MagicMock() |
| 26 | + mock_llm_config = mocker.MagicMock() |
| 27 | + mock_llm_config.cache_enabled = False |
| 28 | + mock_llm_manager.get_config.return_value = mock_llm_config |
| 29 | + |
| 30 | + mock_embedding_manager = mocker.MagicMock(spec=EmbeddingManager) |
| 31 | + mock_embedding_manager.config = EmbeddingConfig( |
| 32 | + provider="openai", model="text-embedding-3-small", cache_enabled=False |
| 33 | + ) |
| 34 | + |
| 35 | + mocker.patch("lightspeed_evaluation.core.metrics.ragas.RagasLLMManager") |
| 36 | + |
| 37 | + return { |
| 38 | + "llm_manager": mock_llm_manager, |
| 39 | + "embedding_manager": mock_embedding_manager, |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def ragas_metrics(mock_ragas_deps: dict[str, Any]) -> RagasMetrics: |
| 45 | + """Create RagasMetrics with mocked dependencies.""" |
| 46 | + return RagasMetrics(**mock_ragas_deps) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def turn_scope() -> EvaluationScope: |
| 51 | + """Create a turn-level evaluation scope.""" |
| 52 | + return EvaluationScope( |
| 53 | + turn_idx=0, |
| 54 | + turn_data=TurnData( |
| 55 | + turn_id="t1", |
| 56 | + query="What is Python?", |
| 57 | + response="A programming language.", |
| 58 | + expected_response="A programming language.", |
| 59 | + ), |
| 60 | + is_conversation=False, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +class TestLazyEmbeddingManagerProperty: |
| 65 | + """Test lazy initialization of the embedding_manager property.""" |
| 66 | + |
| 67 | + def test_initialized_on_first_access( |
| 68 | + self, ragas_metrics: RagasMetrics, mocker: MockerFixture |
| 69 | + ) -> None: |
| 70 | + """First property access should create and return RagasEmbeddingManager.""" |
| 71 | + mock_cls = mocker.patch( |
| 72 | + "lightspeed_evaluation.core.metrics.ragas.RagasEmbeddingManager", |
| 73 | + ) |
| 74 | + |
| 75 | + result = ragas_metrics.embedding_manager |
| 76 | + |
| 77 | + assert result is mock_cls.return_value |
| 78 | + mock_cls.assert_called_once() |
| 79 | + |
| 80 | + def test_cached_after_first_access( |
| 81 | + self, ragas_metrics: RagasMetrics, mocker: MockerFixture |
| 82 | + ) -> None: |
| 83 | + """Subsequent accesses should return the cached instance.""" |
| 84 | + mock_cls = mocker.patch( |
| 85 | + "lightspeed_evaluation.core.metrics.ragas.RagasEmbeddingManager", |
| 86 | + ) |
| 87 | + |
| 88 | + first = ragas_metrics.embedding_manager |
| 89 | + second = ragas_metrics.embedding_manager |
| 90 | + |
| 91 | + assert first is second |
| 92 | + mock_cls.assert_called_once() |
| 93 | + |
| 94 | + |
| 95 | +class TestEvaluateExceptionHandling: |
| 96 | + """Test that evaluate() catches the expected exception types.""" |
| 97 | + |
| 98 | + @pytest.mark.parametrize( |
| 99 | + "exception_class,exception_msg", |
| 100 | + [ |
| 101 | + (EvaluationError, "base evaluation error"), |
| 102 | + (ConfigurationError, "unknown provider xyz"), |
| 103 | + (EmbeddingError, "unsupported embedding provider"), |
| 104 | + (RuntimeError, "unexpected runtime failure"), |
| 105 | + (ValueError, "invalid value"), |
| 106 | + (TypeError, "type mismatch"), |
| 107 | + (ImportError, "missing module"), |
| 108 | + ], |
| 109 | + ) |
| 110 | + def test_catches_exception_gracefully( |
| 111 | + self, |
| 112 | + ragas_metrics: RagasMetrics, |
| 113 | + turn_scope: EvaluationScope, |
| 114 | + mocker: MockerFixture, |
| 115 | + exception_class: type, |
| 116 | + exception_msg: str, |
| 117 | + ) -> None: |
| 118 | + """evaluate() should return (None, error_message) for caught exceptions.""" |
| 119 | + ragas_metrics.supported_metrics["faithfulness"] = mocker.MagicMock( |
| 120 | + side_effect=exception_class(exception_msg) |
| 121 | + ) |
| 122 | + |
| 123 | + score, reason = ragas_metrics.evaluate( |
| 124 | + "faithfulness", mocker.MagicMock(), turn_scope |
| 125 | + ) |
| 126 | + |
| 127 | + assert score is None |
| 128 | + assert exception_msg in reason |
| 129 | + assert "evaluation failed" in reason |
| 130 | + |
| 131 | + def test_unsupported_metric_returns_none( |
| 132 | + self, |
| 133 | + ragas_metrics: RagasMetrics, |
| 134 | + turn_scope: EvaluationScope, |
| 135 | + mocker: MockerFixture, |
| 136 | + ) -> None: |
| 137 | + """Unsupported metric name should return (None, message) without raising.""" |
| 138 | + score, reason = ragas_metrics.evaluate( |
| 139 | + "nonexistent_metric", mocker.MagicMock(), turn_scope |
| 140 | + ) |
| 141 | + |
| 142 | + assert score is None |
| 143 | + assert "Unsupported Ragas metric" in reason |
0 commit comments