From c3d8560a87577f6a37c2d568622d8749b500ccbc Mon Sep 17 00:00:00 2001 From: fabiocat93 Date: Tue, 17 Sep 2024 09:36:59 +0200 Subject: [PATCH] playing with unint tests --- .../guide_notebooks_regression_ec2.yml | 18 +++++++++++ math_operations.py | 20 +++++++++++++ test_math_operations.py | 30 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 math_operations.py create mode 100644 test_math_operations.py diff --git a/.github/workflows/guide_notebooks_regression_ec2.yml b/.github/workflows/guide_notebooks_regression_ec2.yml index 55a1d53..626e008 100644 --- a/.github/workflows/guide_notebooks_regression_ec2.yml +++ b/.github/workflows/guide_notebooks_regression_ec2.yml @@ -31,9 +31,27 @@ jobs: name: Do the job on the runner needs: start-runner # required to start the main job when the runner is ready runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner + env: + RUN_GPU_TESTS: true steps: - name: Hello World run: echo 'Hello World!' + - uses: actions/checkout@v4 + with: # no need for the history + fetch-depth: 1 + - name: Set up Python 3.10 + uses: actions/setup-python@v5 + with: + python-version: 3.10 + - name: Install dependencies + run: | + pip install pytest + shell: bash + - name: Run unit tests + id: run-tests + run: > + pytest + shell: bash stop-runner: name: Stop self-hosted EC2 runner diff --git a/math_operations.py b/math_operations.py new file mode 100644 index 0000000..8f6f04a --- /dev/null +++ b/math_operations.py @@ -0,0 +1,20 @@ +def basic_math_operation(x, y, operation): + """Performs a basic math operation on two numbers. + + :param x: First number + :param y: Second number + :param operation: Operation to perform: 'add', 'subtract', 'multiply', or 'divide' + :return: Result of the operation + """ + if operation == 'add': + return x + y + elif operation == 'subtract': + return x - y + elif operation == 'multiply': + return x * y + elif operation == 'divide': + if y == 0: + raise ValueError("Cannot divide by zero") + return x / y + else: + raise ValueError("Unsupported operation") diff --git a/test_math_operations.py b/test_math_operations.py new file mode 100644 index 0000000..7b24177 --- /dev/null +++ b/test_math_operations.py @@ -0,0 +1,30 @@ +import pytest +import os +from math_operations import basic_math_operation + +def test_add(): + assert basic_math_operation(2, 3, 'add') == 5 + assert basic_math_operation(-2, 3, 'add') == 1 + assert basic_math_operation(0, 0, 'add') == 0 + +def test_subtract(): + assert basic_math_operation(5, 3, 'subtract') == 2 + assert basic_math_operation(2, 5, 'subtract') == -3 + assert basic_math_operation(0, 0, 'subtract') == 0 + +if os.getenv("RUN_GPU_TESTS") != "true": + def test_multiply(): + assert basic_math_operation(2, 3, 'multiply') == 6 + assert basic_math_operation(-2, 3, 'multiply') == -6 + assert basic_math_operation(0, 5, 'multiply') == 0 + + def test_divide(): + assert basic_math_operation(6, 3, 'divide') == 2 + assert basic_math_operation(7, 2, 'divide') == 3.5 + + with pytest.raises(ValueError): + basic_math_operation(6, 0, 'divide') + + def test_invalid_operation(): + with pytest.raises(ValueError): + basic_math_operation(2, 3, 'modulus')