|
import unittest
|
|
import pandas as pd
|
|
import numpy as np
|
|
import os
|
|
import sys
|
|
|
|
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
from src.models.loan_model import LoanApprovalModel
|
|
|
|
class TestLoanModel(unittest.TestCase):
|
|
def setUp(self):
|
|
"""Set up test fixtures"""
|
|
self.model = LoanApprovalModel()
|
|
|
|
|
|
self.sample_data = pd.DataFrame({
|
|
"income": [20000, 50000, 80000, 30000],
|
|
"credit_score": [580, 720, 800, 650],
|
|
"debt_to_income": [40, 25, 15, 30],
|
|
"loan_amount": [150000, 200000, 300000, 180000],
|
|
"loan_term": [30, 30, 15, 30],
|
|
"employment_length": [2, 5, 10, 3],
|
|
"home_ownership": ["RENT", "OWN", "OWN", "RENT"],
|
|
"loan_purpose": ["HOME", "HOME", "HOME", "HOME"]
|
|
})
|
|
|
|
def test_predict(self):
|
|
"""Test prediction functionality"""
|
|
predictions = self.model.predict(self.sample_data)
|
|
|
|
|
|
self.assertTrue(all(pred in [0, 1] for pred in predictions))
|
|
|
|
|
|
self.assertEqual(predictions[2], 1, "High income and credit score should lead to approval")
|
|
|
|
def test_predict_proba(self):
|
|
"""Test probability prediction functionality"""
|
|
probabilities = self.model.predict_proba(self.sample_data)
|
|
|
|
|
|
self.assertTrue(all(0 <= prob[1] <= 1 for prob in probabilities))
|
|
|
|
|
|
self.assertGreater(probabilities[1][1], probabilities[0][1],
|
|
"Better application should have higher approval probability")
|
|
|
|
def test_feature_importance(self):
|
|
"""Test feature importance functionality"""
|
|
importance = self.model.feature_importance()
|
|
|
|
|
|
self.assertEqual(len(importance), len(self.sample_data.columns))
|
|
|
|
|
|
self.assertAlmostEqual(sum(importance.values()), 1.0, places=5)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |