Skip to content

Commit

Permalink
Merge pull request #131 from MIT-Emerging-Talent/hourly-to-monthly
Browse files Browse the repository at this point in the history
Modified convert salary
  • Loading branch information
Papsik09 authored Jan 12, 2025
2 parents e8e8de0 + 710292b commit 9229398
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
29 changes: 23 additions & 6 deletions solutions/convert_salary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Module for converting an hourly wage to its equivalent monthly and yearly salaries.
Module contents:
- convert_salary: A function to convert an hourly wage and
calculates the monthly and yearly salaries.
Created on 2024-01-06
@author: Henry Ogoe
Expand All @@ -16,31 +22,42 @@ def convert_salary(hourly: float, hours_per_week: float = 40) -> tuple:
hours_per_week: Amount of hours per week is set to 40. Added for flexibility.
Returns:
float: Monthly and yearly salary.
tuple: Monthly and yearly salary rounded to 2 decimals.
Raises:
AssertionError: If the input is not a float or salary is negative.
>>> convert_salary(16.5)
2857.8 34320.0
(2857.8, 34320.0)
>>> convert_salary(25)
4333.0 51960.0
(4330.0, 52000)
>>> convert_salary(1000000)
(173200000.0, 2080000000)
>>> convert_salary(0.1)
(17.32, 208.0)
>>> convert_salary(16.5, 100)
(7144.5, 85800.0)
>>> convert_salary(-40)
>>> convert_salary(-8)
Traceback (most recent call last):
AssertionError: Salary cannot be negative.
AssertionError: Salary can not be negative.
>>> convert_salary("7")
Traceback (most recent call last):
AssertionError: Inputs must be float.
"""

assert isinstance(hourly, float), "Inputs must be float."
assert isinstance(hourly, (float, int)), "Inputs must be float."
assert hourly > 0, "Salary can not be negative."

monthly_salary = hourly * hours_per_week * 4.33
# 4.33 is approximate number of weeks in month
yearly_salary = hourly * hours_per_week * 52
# We have 52 weeks in a year

return (round(monthly_salary, 2), round(yearly_salary, 2))
50 changes: 50 additions & 0 deletions solutions/tests/test_convert_salary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test module for convert an hourly wage to its equivalent monthly and yearly salaries.
Created 2025-01-04
@author: Henry Ogoe
"""

import unittest
from solutions.convert_salary import convert_salary


class TestConvertSalary(unittest.TestCase):
"""The test for testing convert_salary function."""

def test_standard_case(self):
"""It should normally proceed standard numbers"""
self.assertEqual(convert_salary(16.5), (2857.8, 34320.0))

def test_different_hourly_rate(self):
"""It should accept different common hourly rates"""
self.assertEqual(convert_salary(25), (4330.0, 52000))

def test_large_salary(self):
"""It should correctly proceed big salaries"""
self.assertEqual(convert_salary(1000000), (173200000.0, 2080000000))

def test_low_salary(self):
"""It should correctly proceed very low salaries for proper rounding"""
self.assertEqual(convert_salary(0.1), (17.32, 208.0))

def test_custom_hours(self):
"""It should be able to use different weekly hours"""
self.assertEqual(convert_salary(16.5, 100), (7144.5, 85800.0))

def test_negative_salary(self):
"""It should raise error on negative salary"""
with self.assertRaises(AssertionError):
convert_salary(-8)

def test_invalid_type(self):
"""It should raise error on non float input"""
with self.assertRaises(AssertionError):
convert_salary("7")


if __name__ == "__main__":
unittest.main()

0 comments on commit 9229398

Please sign in to comment.