File size: 10,452 Bytes
6086e71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Tests for the ML Intent Classifier.
Validates model loading, inference, confidence thresholds, and graceful fallback.
"""

import sys
import os
import json
import pytest
from unittest.mock import patch, MagicMock

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

# Check if ML classifier can actually run (model + sentence_transformers)
_MODEL_PATH = os.path.join(os.path.dirname(__file__), "..", "app", "agents", "models", "intent_model.joblib")
try:
    import sentence_transformers  # noqa: F401
    _ML_READY = os.path.exists(_MODEL_PATH)
except ImportError:
    _ML_READY = False


class TestMLClassifierFallback:
    """Test that the system gracefully falls back when ML model is unavailable."""

    def test_classifier_returns_none_when_model_missing(self):
        """When model file doesn't exist, classify() should return None."""
        from app.agents.ml_classifier import MLIntentClassifier
        clf = MLIntentClassifier(model_path="/nonexistent/path/model.joblib")
        assert clf.available is False
        assert clf.classify("hello") is None

    def test_classifier_returns_none_when_disabled_by_env(self):
        """When DISABLE_ML_INTENT is set, classify() should return None."""
        from app.agents.ml_classifier import MLIntentClassifier
        with patch.dict(os.environ, {"DISABLE_ML_INTENT": "true"}):
            clf = MLIntentClassifier()
            assert clf.available is False
            assert clf.classify("hello") is None

    def test_bridge_returns_none_when_disabled_by_env(self):
        """When DISABLE_ML_INTENT is set, the bridge should bypass ML classification."""
        from app.agents.intent_classifier import _try_ml_classification
        with patch.dict(os.environ, {"DISABLE_ML_INTENT": "true"}):
            result = _try_ml_classification("hello")
            assert result is None

    def test_heuristic_still_works_without_ml(self):
        """Intent classifier should work even when ML model is not available."""
        from app.agents.intent_classifier import classify_intent
        # These should all work via heuristic regardless of ML availability
        result = classify_intent("hello")
        assert result.intent == "chat"

        result = classify_intent("show top 5 employees by salary")
        assert result.intent == "sql"

    def test_greeting_still_chat_without_ml(self):
        from app.agents.intent_classifier import classify_intent
        for greeting in ["hi", "hey", "thanks", "bye", "what can you do"]:
            result = classify_intent(greeting)
            assert result.intent == "chat", f"'{greeting}' should be chat, got {result.intent}"

    def test_sql_queries_still_work_without_ml(self):
        from app.agents.intent_classifier import classify_intent
        sql_queries = [
            "total sales revenue by region",
            "how many employees are in each department",
            "show products with low stock",
        ]
        for q in sql_queries:
            result = classify_intent(q)
            assert result.intent == "sql", f"'{q}' should be sql, got {result.intent}"


class TestMLClassifierIntegration:
    """
    Integration tests that run when the model file exists.
    These are skipped if the model hasn't been trained yet.
    """

    @staticmethod
    def _model_exists():
        model_path = os.path.join(
            os.path.dirname(__file__), "..", "app", "agents", "models", "intent_model.joblib"
        )
        return os.path.exists(model_path)

    @staticmethod
    def _has_deps():
        try:
            import sentence_transformers  # noqa: F401
            return True
        except ImportError:
            return False

    @staticmethod
    def _can_run():
        """Model file exists AND sentence_transformers is installed."""
        model_path = os.path.join(
            os.path.dirname(__file__), "..", "app", "agents", "models", "intent_model.joblib"
        )
        if not os.path.exists(model_path):
            return False
        try:
            import sentence_transformers  # noqa: F401
            return True
        except ImportError:
            return False

    @pytest.mark.skipif(
        not _ML_READY,
        reason="ML model not trained or sentence_transformers not installed",
    )
    def test_ml_model_loads(self):
        """Verify the trained model loads successfully."""
        from app.agents.ml_classifier import MLIntentClassifier
        clf = MLIntentClassifier()
        assert clf.available is True

    @pytest.mark.skipif(
        not _ML_READY,
        reason="ML model not trained or sentence_transformers not installed",
    )
    def test_ml_classifies_greeting_as_chat(self):
        from app.agents.ml_classifier import MLIntentClassifier
        clf = MLIntentClassifier()
        result = clf.classify("hello how are you")
        assert result is not None
        assert result.intent == "chat"
        assert result.confidence > 0.5

    @pytest.mark.skipif(
        not _ML_READY,
        reason="ML model not trained or sentence_transformers not installed",
    )
    def test_ml_classifies_sql_query(self):
        from app.agents.ml_classifier import MLIntentClassifier
        clf = MLIntentClassifier()
        result = clf.classify("show top 5 employees by salary")
        assert result is not None
        assert result.intent == "sql"
        assert result.confidence > 0.5

    @pytest.mark.skipif(
        not _ML_READY,
        reason="ML model not trained or sentence_transformers not installed",
    )
    def test_ml_classifies_meta_query(self):
        from app.agents.ml_classifier import MLIntentClassifier
        clf = MLIntentClassifier()
        result = clf.classify("what tables are in the database")
        assert result is not None
        assert result.intent == "meta_query"

    @pytest.mark.skipif(
        not _ML_READY,
        reason="ML model not trained or sentence_transformers not installed",
    )
    def test_ml_classification_has_confidence(self):
        from app.agents.ml_classifier import MLIntentClassifier
        clf = MLIntentClassifier()
        result = clf.classify("total revenue by region")
        assert result is not None
        assert 0.0 <= result.confidence <= 1.0
        assert result.method == "ml"


class TestMLClassificationBridge:
    """Test the _try_ml_classification bridge in intent_classifier.py."""

    def test_bridge_returns_none_when_ml_unavailable(self):
        """When ML model doesn't load, bridge returns None and heuristic runs."""
        from app.agents.intent_classifier import _try_ml_classification
        # Force a fresh import with no model
        with patch("app.agents.ml_classifier.get_ml_classifier") as mock_get:
            mock_clf = MagicMock()
            mock_clf.available = False
            mock_get.return_value = mock_clf
            result = _try_ml_classification("hello")
            assert result is None

    def test_bridge_returns_none_on_low_confidence(self):
        """When ML confidence is below threshold, bridge returns None."""
        from app.agents.intent_classifier import _try_ml_classification
        from app.agents.ml_classifier import MLClassification

        with patch("app.agents.ml_classifier.get_ml_classifier") as mock_get:
            mock_clf = MagicMock()
            mock_clf.available = True
            mock_clf.classify.return_value = MLClassification(
                intent="chat", route_intent="chat", confidence=0.45
            )
            mock_get.return_value = mock_clf
            result = _try_ml_classification("maybe this is chat")
            assert result is None  # Below 0.70 threshold

    def test_bridge_returns_classification_on_high_confidence(self):
        """When ML confidence is above threshold, bridge returns classification."""
        from app.agents.intent_classifier import _try_ml_classification
        from app.agents.ml_classifier import MLClassification

        with patch("app.agents.ml_classifier.get_ml_classifier") as mock_get:
            mock_clf = MagicMock()
            mock_clf.available = True
            mock_clf.classify.return_value = MLClassification(
                intent="sql", route_intent="data_query", confidence=0.92
            )
            mock_get.return_value = mock_clf
            result = _try_ml_classification("show top 5 employees by salary")
            assert result is not None
            assert result.intent == "sql"
            assert "ml_model" in result.reason


class TestTrainingData:
    """Validate the training dataset structure."""

    def test_training_data_loads(self):
        data_path = os.path.join(
            os.path.dirname(__file__), "..", "app", "agents", "models", "training_data.json"
        )
        with open(data_path, "r") as f:
            data = json.load(f)
        assert len(data) > 100, f"Expected 100+ training examples, got {len(data)}"

    def test_all_labels_valid(self):
        data_path = os.path.join(
            os.path.dirname(__file__), "..", "app", "agents", "models", "training_data.json"
        )
        with open(data_path, "r") as f:
            data = json.load(f)
        valid_labels = {"chat", "sql", "ambiguous", "meta_query"}
        for item in data:
            assert "text" in item, f"Missing 'text' field: {item}"
            assert "label" in item, f"Missing 'label' field: {item}"
            assert item["label"] in valid_labels, f"Invalid label '{item['label']}' in: {item}"

    def test_all_classes_represented(self):
        data_path = os.path.join(
            os.path.dirname(__file__), "..", "app", "agents", "models", "training_data.json"
        )
        with open(data_path, "r") as f:
            data = json.load(f)
        labels = {item["label"] for item in data}
        assert labels == {"chat", "sql", "ambiguous", "meta_query"}

    def test_minimum_per_class(self):
        """Each class should have at least 15 examples for meaningful training."""
        data_path = os.path.join(
            os.path.dirname(__file__), "..", "app", "agents", "models", "training_data.json"
        )
        with open(data_path, "r") as f:
            data = json.load(f)
        from collections import Counter
        counts = Counter(item["label"] for item in data)
        for label, count in counts.items():
            assert count >= 15, f"Class '{label}' has only {count} examples, need 15+"