| from django.test import TestCase | |
| from core.models import MutualFund, Stock | |
| from core.mfrating.score_calculator import MutualFundScorer, MFRating | |
| from core.tests.data import test_data | |
| class MutualFundScorerTestCase(TestCase): | |
| """ | |
| Test case for the MutualFundScorer class to test scores. | |
| """ | |
| def setUp(self): | |
| self.stock_data = [ | |
| {"isin_number": "INE040A01034", "rank": 10}, | |
| {"isin_number": "INE090A01021", "rank": 21}, | |
| {"isin_number": "INE002A01018", "rank": 131}, | |
| {"isin_number": "INE154A01025", "rank": 99}, | |
| {"isin_number": "INE018A01030", "rank": 31}, | |
| {"isin_number": "INE280A01028", "rank": 2}, | |
| ] | |
| self.mutual_fund_data = [ | |
| { | |
| "isin_number": "ISIN1", | |
| "fund_name": "Testing Fund 1", | |
| "rank": 1, | |
| "aum": 837.3, | |
| "crisil_rank": 4, | |
| "security_id": "SEC1", | |
| "data": test_data[1], | |
| }, | |
| { | |
| "isin_number": "ISIN2", | |
| "fund_name": "Testing Fund 2", | |
| "rank": 2, | |
| "aum": 210.3, | |
| "crisil_rank": 1, | |
| "security_id": "SEC2", | |
| "data": test_data[2], | |
| }, | |
| { | |
| "isin_number": "ISIN3", | |
| "fund_name": "Testing Fund 3", | |
| "rank": 3, | |
| "aum": 639.3, | |
| "crisil_rank": 3, | |
| "security_id": "SEC3", | |
| "data": test_data[3], | |
| }, | |
| { | |
| "isin_number": "ISIN4", | |
| "fund_name": "Testing Fund 4", | |
| "rank": 4, | |
| "aum": 410.3, | |
| "crisil_rank": 2, | |
| "security_id": "SEC4", | |
| "data": test_data[4], | |
| }, | |
| { | |
| "isin_number": "ISIN5", | |
| "fund_name": "Testing Fund 5", | |
| "rank": 5, | |
| "aum": 1881.3, | |
| "crisil_rank": 5, | |
| "security_id": "SEC5", | |
| "data": test_data[5], | |
| }, | |
| ] | |
| self.create_stock_objects() | |
| self.create_mutual_fund_objects() | |
| self.mf_scorer = MutualFundScorer() | |
| def create_stock_objects(self): | |
| """ | |
| Create stock objects using the predefined stock data. | |
| """ | |
| self.stock_objects = [Stock.objects.create(**data) for data in self.stock_data] | |
| def create_mutual_fund_objects(self): | |
| """ | |
| Create mutual fund objects using the predefined mutual fund data. | |
| """ | |
| self.mutual_fund_objects = [ | |
| MutualFund.objects.create(**data) for data in self.mutual_fund_data | |
| ] | |
| def test_get_scores_returns_sorted_list(self): | |
| """ | |
| Test whether the get_scores method returns a sorted list of scores. | |
| """ | |
| scores = self.mf_scorer.get_scores() | |
| self.assertEqual(len(scores), 5) | |
| self.assertEqual( | |
| scores, sorted(scores, key=lambda x: x["overall_score"], reverse=True) | |
| ) | |
| expected_scores = [0.4263, 0.3348, 0.2962, 0.2447, 0.2101] | |
| for i, expected_score in enumerate(expected_scores): | |
| self.assertAlmostEqual( | |
| scores[i]["overall_score"], expected_score, delta=1e-4 | |
| ) | |