Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds ira calculator with unit tests #229

Merged
merged 15 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
caio-felipee marked this conversation as resolved.
Show resolved Hide resolved

def get_ira_value(self, mencoes):
caio-felipee marked this conversation as resolved.
Show resolved Hide resolved
"""
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
caio-felipee marked this conversation as resolved.
Show resolved Hide resolved

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)

Loading