|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +from sklearn.metrics import accuracy_score |
| 4 | +from sklearn.model_selection import train_test_split |
| 5 | +from spam_detector_ai.classifiers.classifier_types import ClassifierType |
| 6 | +from spam_detector_ai.logger_config import init_logging |
| 7 | +from spam_detector_ai.prediction import SpamDetector |
| 8 | +from spam_detector_ai.training import ModelTrainer |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope="module") |
| 12 | +def test_model(): |
| 13 | + classifier_types = [ClassifierType.NAIVE_BAYES, ClassifierType.RANDOM_FOREST, ClassifierType.SVM] |
| 14 | + logger = init_logging() |
| 15 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 16 | + base_dir = os.path.dirname(current_dir) |
| 17 | + data_path = os.path.join(base_dir, 'data/spam.csv') |
| 18 | + initial_trainer = ModelTrainer(data_path=data_path, classifier_type=None, logger=logger) |
| 19 | + processed_data = initial_trainer._preprocess_data() |
| 20 | + _, X_test, _, y_test = train_test_split(processed_data['processed_text'], processed_data['label'], |
| 21 | + test_size=0.2, random_state=0) |
| 22 | + return classifier_types, X_test, y_test |
| 23 | + |
| 24 | + |
| 25 | +class TestClassifiers: |
| 26 | + def test_classifier_accuracy(self, test_model): |
| 27 | + classifier_types, X_test, y_test = test_model |
| 28 | + for ct in classifier_types: |
| 29 | + detector = SpamDetector(model_type=ct) |
| 30 | + y_pred = [detector.test_is_spam(message) for message in X_test] |
| 31 | + assert accuracy_score(y_test, y_pred) > 0.85 |
0 commit comments