Skip to content

Commit

Permalink
fix issues with ruff linter
Browse files Browse the repository at this point in the history
  • Loading branch information
RamonColmenares committed Jan 1, 2025
1 parent 95857ea commit f88df23
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
19 changes: 11 additions & 8 deletions solutions/basic_calculator/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


"""
A module for performing basic arithmetic operations: addition, subtraction,
multiplication, and division.
Expand All @@ -11,11 +9,13 @@
Author: Ana Isabel Murillo
"""


def calculate(operation: str, num1: float, num2: float) -> float:
"""Performs the specified arithmetic operation on two numbers.
Parameters:
operation: str, the operation to perform ('add', 'subtract', 'multiply', 'divide')
operation: str, the operation to perform
('add', 'subtract', 'multiply', 'divide')
num1: float, the first number
num2: float, the second number
Expand All @@ -38,15 +38,18 @@ def calculate(operation: str, num1: float, num2: float) -> float:
...
ValueError: Cannot divide by zero.
"""
if operation == 'add':
if operation == "add":
return num1 + num2
elif operation == 'subtract':
elif operation == "subtract":
return num1 - num2
elif operation == 'multiply':
elif operation == "multiply":
return num1 * num2
elif operation == 'divide':
elif operation == "divide":
if num2 == 0:
raise ValueError("Cannot divide by zero.")
return num1 / num2
else:
raise ValueError(f"Invalid operation '{operation}'. Valid operations are: add, subtract, multiply, divide.")
raise ValueError(
f"Invalid operation '{operation}'. Valid operations are: "
"add, subtract, multiply, divide."
)
31 changes: 16 additions & 15 deletions solutions/tests/test_basic_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,46 @@
Author: Ana Isabel Murillo
"""

from solutions.basic_calculator.main import calculate
import unittest

from solutions.basic_calculator.main import calculate


class TestCalculate(unittest.TestCase):
"""Test suite for the calculate function."""

def test_addition(self):
"""It should correctly add two numbers."""
self.assertEqual(calculate('add', 5, 3), 8.0)
self.assertEqual(calculate('add', -5, -3), -8.0)
self.assertEqual(calculate('add', 0, 5), 5.0)
self.assertEqual(calculate("add", 5, 3), 8.0)
self.assertEqual(calculate("add", -5, -3), -8.0)
self.assertEqual(calculate("add", 0, 5), 5.0)

def test_subtraction(self):
"""It should correctly subtract the second number from the first."""
self.assertEqual(calculate('subtract', 10, 4), 6.0)
self.assertEqual(calculate('subtract', -5, -5), 0.0)
self.assertEqual(calculate('subtract', 0, 5), -5.0)
self.assertEqual(calculate("subtract", 10, 4), 6.0)
self.assertEqual(calculate("subtract", -5, -5), 0.0)
self.assertEqual(calculate("subtract", 0, 5), -5.0)

def test_multiplication(self):
"""It should correctly multiply two numbers."""
self.assertEqual(calculate('multiply', 7, 3), 21.0)
self.assertEqual(calculate('multiply', -2, 3), -6.0)
self.assertEqual(calculate('multiply', 0, 10), 0.0)
self.assertEqual(calculate("multiply", 7, 3), 21.0)
self.assertEqual(calculate("multiply", -2, 3), -6.0)
self.assertEqual(calculate("multiply", 0, 10), 0.0)

def test_division(self):
"""It should correctly divide the first number by the second."""
self.assertEqual(calculate('divide', 9, 3), 3.0)
self.assertEqual(calculate('divide', -6, 3), -2.0)
self.assertAlmostEqual(calculate('divide', 7, 2), 3.5)
self.assertEqual(calculate("divide", 9, 3), 3.0)
self.assertEqual(calculate("divide", -6, 3), -2.0)
self.assertAlmostEqual(calculate("divide", 7, 2), 3.5)

def test_division_by_zero(self):
"""It should raise a ValueError when attempting to divide by zero."""
with self.assertRaises(ValueError) as context:
calculate('divide', 5, 0)
calculate("divide", 5, 0)
self.assertEqual(str(context.exception), "Cannot divide by zero.")

def test_invalid_operation(self):
"""It should raise a ValueError for an invalid operation."""
with self.assertRaises(ValueError) as context:
calculate('invalid_op', 5, 3)
calculate("invalid_op", 5, 3)
self.assertIn("Invalid operation", str(context.exception))

0 comments on commit f88df23

Please sign in to comment.