Skip to content

Commit

Permalink
feat: adds ira calculator with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasqueiroz23 committed Aug 10, 2024
1 parent 556df6c commit 88b8f5c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
37 changes: 37 additions & 0 deletions api/utils/ira_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class IraCalculator:
def __init__(self) -> None:
self.mencaoMap = {
'SS': 5,
'MS': 4,
'MM': 3,
'MI': 2,
'II': 1,
'SR': 0,
}

def get_ira_value(self, mencoes):
"""
Obter o valor do IRA a partir de um conjunto de menções.
:param mencoes: A lista de menções. Uma menção está no formato {'mencao': string, 'qtdCreditos': int, 'semestre': int}.
"""
valor_final = 0
for mencao in mencoes:

if not (1 <= mencao['semestre'] <= 6):
raise ValueError

if mencao['mencao'] not in self.mencaoMap.keys():
raise ValueError

numerador = self.mencaoMap[mencao['mencao']] * \
mencao['qtdCreditos'] * \
mencao['semestre']

denominador = mencao['qtdCreditos'] * mencao['semestre']
valor_final += numerador/denominador

return valor_final



52 changes: 52 additions & 0 deletions api/utils/tests/test_ira_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.test import TestCase
from utils.ira_calculator import IraCalculator


class IraTestCase(TestCase):
def setUp(self):
self.ira_calc = IraCalculator()

def test_one_discipline_with_MM(self):
args = [
{
'mencao': 'MM',
'semestre': 1,
'qtdCreditos': 2,
}
]

self.assertEqual(self.ira_calc.get_ira_value(args), 3)

def test_discipline_with_right_out_of_bounds_semester_value(self):
args = [
{
'mencao': 'MM',
'semestre': 7,
'qtdCreditos': 2,
},
]

self.assertRaises(ValueError, self.ira_calc.get_ira_value, args)

def test_discipline_with_left_out_of_bounds_semester_value(self):
args = [
{
'mencao': 'MM',
'semestre': 0,
'qtdCreditos': 2,
},
]

self.assertRaises(ValueError, self.ira_calc.get_ira_value, args)


def test_inexistent_grade(self):
args = [
{
'mencao': 'NE',
'semestre': 3,
'qtdCreditos': 2,
},
]
self.assertRaises(ValueError, self.ira_calc.get_ira_value, args)

0 comments on commit 88b8f5c

Please sign in to comment.