import unittest from loan_calc import loan_calc from loan_calc_class import LoanCalculator class TestLoanCalculatorAndHelper(unittest.TestCase): def test_loan_calculator_standard_emi(self): """Test standard EMI calculation with a normal interest rate.""" # Principal: 1,000,000, Annual Rate: 6%, Years: 5 calculator = LoanCalculator(principal=1_000_000, annual_rate=6.0, years=5) emi = calculator.calculate_EMI() # EMI for 1,000,000 at 6% for 5 years (60 months) is ~ 19,332.80 self.assertAlmostEqual(emi, 19332.80, places=2) def test_loan_calculator_zero_interest(self): """Test EMI calculation when the annual rate is 0%.""" # Principal: 120,000, Annual Rate: 0%, Years: 1 (12 months) # Should simply return principal / months = 120,000 / 12 = 10,000 calculator = LoanCalculator(principal=120_000, annual_rate=0.0, years=1) emi = calculator.calculate_EMI() self.assertEqual(emi, 10000.0) def test_loan_calc(self): """Test that loan_calc correctly instantiates and executes the method.""" loan_data = { "principal": 500_000, "annual_rate": 8.0, "years": 2 } # Call the helper function provided in sample.py[cite: 1] result = loan_calc( cls=LoanCalculator, attributes=loan_data, method_name="calculate_EMI" ) # Manually calculate expected value to verify helper execution expected_calc = LoanCalculator(**loan_data) expected_result = expected_calc.calculate_EMI() self.assertEqual(result, expected_result) if __name__ == '__main__': unittest.main()