Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: simple_calculator unit test

permissions:
actions: write
contents: write

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with Ruff
run: |
pip install ruff
ruff --format=github --target-version=py310 .
continue-on-error: true
- name: Test with pytest
run: |
coverage run -m pytest tests/test_1b.py -v -s
- name: Generate Coverage Report
run: |
coverage report -m
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python-envs.defaultEnvManager": "ms-python.python:system"
}
5 changes: 3 additions & 2 deletions labs/lab_1/lab_1a.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
lab_1a.py
This is to simulate a change made on a robot: robot_speed = 5 # m/s

The first lab in the BWSI CSS course. To complete this lab, fill out the variable on line 10
with your name. Then, save the code, add it to the staging area, and commit it to the Git tree.
Expand All @@ -8,9 +9,9 @@
def main():
print("Hello World!")

name = "" # TODO: Insert your name between the double quotes
name = "Yasho" # TODO: Insert your name between the double quotes

print(f"{name}, Welcome to the CSS course!")

print(f"I'm {name} and a fun fact about me is that I like to break my record on solving Rubik's Cube!")
if __name__ == "__main__":
main()
6 changes: 4 additions & 2 deletions labs/lab_1/lab_1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float:
float: The result of the operation.
"""

operation = operation.strip().lower() # Normalize the operation string

if not isinstance(num1, (int, float)) or not isinstance(num2, (int, float)):
raise ValueError("Both num1 and num2 must be numeric values.")
if operation == "add":
return num1 + num2
elif operation == "subtract":
Expand All @@ -39,8 +43,6 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float:

def main():

print(f"===== Simple Calculator =====")

# Ask the user for sample input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
Expand Down
20 changes: 20 additions & 0 deletions tests/tests_1b.py → tests/test_1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
tests_1b.py

This module contains unit tests for the simple_calculator function defined in lab_1b.py.
The tests cover various scenarios for addition, subtraction, multiplication, and division operations
including edge cases such as division by zero and invalid operations. The tests are implemented using the pytest framework. Dummy comment to make commit
"""

import pytest
Expand Down Expand Up @@ -37,5 +39,23 @@ def test_invalid_operation():
with pytest.raises(ValueError, match="Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'."):
simple_calculator("", 5, 3) # Test for empty operation

def test_operation_case_insensitivity():
assert simple_calculator("ADD", 5, 3) == 8 # Test for uppercase operation
assert simple_calculator("Subtract", 5, 3) == 2 # Test for mixed case operation
assert simple_calculator("MULTIPLY", 5, 3) == 15 # Test for uppercase operation
assert simple_calculator("DIVIDE", 6, 3) == 2 # Test for uppercase operation

def test_operation_with_whitespace():
assert simple_calculator(" add ", 5, 3) == 8 # Test for operation with leading and trailing whitespace
assert simple_calculator(" subtract ", 5, 3) == 2 # Test for operation with leading and trailing whitespace
assert simple_calculator(" multiply ", 5, 3) == 15 # Test for operation with leading and trailing whitespace
assert simple_calculator(" divide ", 6, 3) == 2 # Test for operation with leading and trailing whitespace

def test_operation_with_non_numeric_input():
with pytest.raises(ValueError):
simple_calculator("add", "five", 3) # Test for non-numeric input for num1
with pytest.raises(ValueError):
simple_calculator("subtract", 5, "three") # Test for non-numeric input for num2

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