Skip to content

Commit

Permalink
Merge pull request #50 from MIT-Emerging-Talent/challenge/sum_two_num…
Browse files Browse the repository at this point in the history
…bers

challenge/sum_two_numbers
  • Loading branch information
danielluyi authored Jan 10, 2025
2 parents 1663391 + fb574df commit becec91
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
30 changes: 30 additions & 0 deletions solutions/sum_two_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def sum_two_numbers(num1: int | float, num2: int | float) -> str:
"""
This function sums up two given numbers.
Parameters:
num1 (int | float): The first number to add.
num2 (int | float): The second number to add.
Returns:
str: A string indicating the sum of num1 and num2.
Raises:
TypeError: If either argument is not an integer or float.
ValueError: If either argument is None or an empty value.
Example:
>>> sum_two_numbers(7, 10)
'The sum of 7 and 10 is 17'
"""
if num1 is None or num2 is None or num1 == "" or num2 == "":
raise ValueError("Enter two numbers to use this function")

if not isinstance(num1, (int, float)) or not isinstance(num2, (int, float)):
raise TypeError("Both arguments must be int or float.")

summation = num1 + num2
return f"The sum of {num1} and {num2} is {round(summation, 2)}"


# print(sum_two_numbers(3))
155 changes: 155 additions & 0 deletions solutions/tests/test_sum_two_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import unittest
from solutions.sum_two_numbers import sum_two_numbers


"""
Test module for the sum_two_numbers function.
This file includes unittest to verify the correctness of the sum_two_numbers function.
It tests the function with standard cases, edge cases, and exception handling.
Test categories:
- Standard cases: Correct summation of integers and floats.
- Edge cases: Handling of zero values and mixed types.
- Exception handling: Tests for ValueError and TypeError.
Created on: 09 January 2025
Author: Hope Udoh
"""


class TestSumTwoNumbers(unittest.TestCase):
"""
Test suite for the sum_two_numbers function.
"""

# Standard Cases
def test_integer_sum(self):
"""
Standard Case: Test that the function correctly sums two integers.
Example:
>>> sum_two_numbers(7, 10)
'The sum of 7 and 10 is 17'
"""
self.assertEqual(sum_two_numbers(7, 10), "The sum of 7 and 10 is 17")

def test_float_sum(self):
"""
Standard Case: Test that the function correctly sums two floats.
Example:
>>> sum_two_numbers(2.5, 3.1)
'The sum of 2.5 and 3.1 is 5.6'
"""
self.assertEqual(sum_two_numbers(2.5, 3.1), "The sum of 2.5 and 3.1 is 5.6")

def test_integer_float_sum(self):
"""
Standard Case: Test that the function correctly sums an integer and a float.
Example:
>>> sum_two_numbers(3, 4.75)
'The sum of 3 and 4.75 is 7.75'
"""
self.assertEqual(sum_two_numbers(3, 4.75), "The sum of 3 and 4.75 is 7.75")

# Edge Cases
def test_zero_sum(self):
"""
Edge Case: Test that the function correctly sums when one or both values are zero.
Example:
>>> sum_two_numbers(0, 5)
'The sum of 0 and 5 is 5'
"""
self.assertEqual(sum_two_numbers(0, 5), "The sum of 0 and 5 is 5")
self.assertEqual(sum_two_numbers(0, 0), "The sum of 0 and 0 is 0")

# Exception Handling
def test_none_input(self):
"""
Exception Case: Test that the function raises a ValueError when either argument is None.
Raises:
ValueError: If either argument is None.
Example:
>>> sum_two_numbers(None, 5)
Traceback (most recent call last):
...
ValueError: Enter two numbers to use this function
"""
with self.assertRaises(ValueError):
sum_two_numbers(None, 5)

def test_string_input(self):
"""
Exception Case: Test that the function raises a TypeError when either argument is a string.
Raises:
TypeError: If either argument is a string.
Example:
>>> sum_two_numbers("10", 5)
Traceback (most recent call last):
...
TypeError: Both arguments must be int or float.
"""
with self.assertRaises(TypeError):
sum_two_numbers("10", 5)
with self.assertRaises(TypeError):
sum_two_numbers(10, "5")

def test_list_input(self):
"""
Exception Case: Test that the function raises a TypeError when either argument is a list.
Raises:
TypeError: If either argument is a list.
Example:
>>> sum_two_numbers([1, 2], 5)
Traceback (most recent call last):
...
TypeError: Both arguments must be int or float.
"""
with self.assertRaises(TypeError):
sum_two_numbers([1, 2], 5)

def test_dict_input(self):
"""
Exception Case: Test that the function raises a TypeError when either argument is a dictionary.
Raises:
TypeError: If either argument is a dictionary.
Example:
>>> sum_two_numbers({"a": 1}, 5)
Traceback (most recent call last):
...
TypeError: Both arguments must be int or float.
"""
with self.assertRaises(TypeError):
sum_two_numbers({"a": 1}, 5)

def test_empty_input(self):
"""
Exception Case: Test that the function raises a ValueError when either argument is empty.
Raises:
ValueError: If either argument is empty.
Example:
>>> sum_two_numbers("", 5)
Traceback (most recent call last):
...
ValueError: Enter two numbers to use this function
"""
with self.assertRaises(ValueError):
sum_two_numbers("", 5)
with self.assertRaises(ValueError):
sum_two_numbers(5, "")


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

0 comments on commit becec91

Please sign in to comment.