IndoHoaxDetector / test_model.py
theonegareth's picture
Improve IndoHoaxDetector repo: add comprehensive README, model card, examples, evaluation script, tests, license, and fix app.py for hoax detection
af7b60b
#!/usr/bin/env python3
"""
Unit tests for IndoHoaxDetector model components
Run with: python -m pytest test_model.py
"""
import pytest
import pickle
import os
from app import detect_hoax, model
from example import load_model, predict_hoax
class TestModelLoading:
"""Test model loading functionality."""
def test_model_file_exists(self):
"""Test that the model file exists."""
assert os.path.exists('logreg_model.pkl'), "Model file logreg_model.pkl not found"
def test_model_loading(self):
"""Test that model can be loaded successfully."""
loaded_model = load_model('logreg_model.pkl')
assert loaded_model is not None, "Model failed to load"
def test_model_has_predict_method(self):
"""Test that loaded model has predict method."""
loaded_model = load_model('logreg_model.pkl')
assert hasattr(loaded_model, 'predict'), "Model missing predict method"
def test_model_has_predict_proba_method(self):
"""Test that loaded model has predict_proba method."""
loaded_model = load_model('logreg_model.pkl')
assert hasattr(loaded_model, 'predict_proba'), "Model missing predict_proba method"
class TestPrediction:
"""Test prediction functionality."""
@pytest.fixture
def sample_model(self):
"""Fixture to load model for tests."""
return load_model('logreg_model.pkl')
def test_prediction_on_legitimate_text(self, sample_model):
"""Test prediction on legitimate news text."""
text = "Presiden mengumumkan kebijakan baru untuk meningkatkan ekonomi nasional."
result = predict_hoax(text, sample_model)
assert 'prediction' in result
assert 'confidence' in result
assert 'probabilities' in result
assert result['prediction'] in ['Legitimate', 'Hoax']
assert 0 <= result['confidence'] <= 1
def test_prediction_on_hoax_text(self, sample_model):
"""Test prediction on hoax text."""
text = "Ditemukan cara instan kaya raya tanpa kerja keras."
result = predict_hoax(text, sample_model)
assert 'prediction' in result
assert 'confidence' in result
assert 'probabilities' in result
assert result['prediction'] in ['Legitimate', 'Hoax']
assert 0 <= result['confidence'] <= 1
def test_prediction_probabilities_sum_to_one(self, sample_model):
"""Test that prediction probabilities sum to 1."""
text = "Contoh berita untuk testing."
result = predict_hoax(text, sample_model)
probs = result['probabilities']
assert abs(probs['legitimate'] + probs['hoax'] - 1.0) < 1e-6, "Probabilities don't sum to 1"
def test_empty_text_handling(self, sample_model):
"""Test handling of empty input text."""
result = predict_hoax("", sample_model)
# Should still return a result, though prediction may be default
assert 'prediction' in result
class TestGradioApp:
"""Test Gradio app functionality."""
def test_detect_hoax_function_exists(self):
"""Test that detect_hoax function exists."""
assert callable(detect_hoax), "detect_hoax function not callable"
def test_detect_hoax_with_valid_input(self):
"""Test detect_hoax with valid input."""
text = "Berita tentang politik Indonesia."
result = detect_hoax(text)
assert isinstance(result, str), "Result should be string"
assert len(result) > 0, "Result should not be empty"
def test_detect_hoax_with_empty_input(self):
"""Test detect_hoax with empty input."""
result = detect_hoax("")
assert "Please enter some text" in result, "Should handle empty input"
def test_detect_hoax_output_format(self):
"""Test that detect_hoax output contains expected elements."""
text = "Contoh berita."
result = detect_hoax(text)
# Check for markdown formatting
assert "**Prediction:" in result, "Should contain prediction"
assert "**Confidence:" in result, "Should contain confidence"
assert "**Probabilities:**" in result, "Should contain probabilities"
class TestModelMetadata:
"""Test model metadata."""
def test_metadata_file_exists(self):
"""Test that metadata file exists."""
assert os.path.exists('model_metadata.txt'), "Metadata file not found"
def test_metadata_content(self):
"""Test metadata file content."""
with open('model_metadata.txt', 'r') as f:
content = f.read()
assert 'Accuracy' in content, "Accuracy not found in metadata"
assert 'Model trained on' in content, "Training date not found in metadata"
if __name__ == "__main__":
pytest.main([__file__])