File size: 3,380 Bytes
38171fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
            )