diff --git a/.github/workflows/api.yml b/.github/workflows/api.yml new file mode 100644 index 000000000..9fd271126 --- /dev/null +++ b/.github/workflows/api.yml @@ -0,0 +1,21 @@ +name: API workflow + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + name: Test python API + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v2 + with: + python-version: '3.10' + - name: Install requirements + run: pip install -r api/requirements.txt + - name: Run tests and collect coverage + run: pytest --cov=api.calculator --cov-report=xml + - name: Upload coverage reports to Codecov with GitHub Action + uses: codecov/codecov-action@v5 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file diff --git a/api/app.py b/api/app.py new file mode 100644 index 000000000..d91792d1b --- /dev/null +++ b/api/app.py @@ -0,0 +1,35 @@ +from flask import ( + Flask, + request, +) + +from calculator.calculator import Calculator + +app = Flask(__name__) + +@app.route('/api/add', methods=['POST']) +def add(): + return operation('add', 2) + +@app.route('/api/subtract', methods=['POST']) +def subtract(): + return operation('subtract', 2) + +@app.route('/api/multiply', methods=['POST']) +def multiply(): + return operation('multiply', 2) + +@app.route('/api/divide', methods=['POST']) +def divide(): + return operation('divide', 2) + +def operation(method, num_factors): + factors = [] + if num_factors == 2: + factors.append(float(request.json.get('x'))) + factors.append(float(request.json.get('y'))) + + return str(getattr(Calculator, method)(*factors)) + + +app.run(host='0.0.0.0', port=8080) \ No newline at end of file diff --git a/api/calculator/__init__.py b/api/calculator/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api/calculator/calculator.py b/api/calculator/calculator.py new file mode 100644 index 000000000..eac4e7acb --- /dev/null +++ b/api/calculator/calculator.py @@ -0,0 +1,14 @@ +class Calculator: + def add(x, y): + return x + y + + def subtract(x, y): + return x - y + + def multiply(x, y): + return x * y + + def divide(x, y): + if y == 0: + return 'Cannot divide by 0' + return x * 1.0 / y \ No newline at end of file diff --git a/api/calculator/test_calculator.py b/api/calculator/test_calculator.py new file mode 100644 index 000000000..c170712ac --- /dev/null +++ b/api/calculator/test_calculator.py @@ -0,0 +1,33 @@ +from .calculator import Calculator + + +def test_add(): + assert Calculator.add(1, 2) == 3.0 + assert Calculator.add(1.0, 2.0) == 3.0 + assert Calculator.add(0, 2.0) == 2.0 + assert Calculator.add(2.0, 0) == 2.0 + assert Calculator.add(-4, 2.0) == -2.0 + +def test_subtract(): + assert Calculator.subtract(1, 2) == -1.0 + assert Calculator.subtract(2, 1) == 1.0 + assert Calculator.subtract(1.0, 2.0) == -1.0 + assert Calculator.subtract(0, 2.0) == -2.0 + assert Calculator.subtract(2.0, 0.0) == 2.0 + assert Calculator.subtract(-4, 2.0) == -6.0 + +def test_multiply(): + assert Calculator.multiply(1, 2) == 2.0 + assert Calculator.multiply(1.0, 2.0) == 2.0 + assert Calculator.multiply(0, 2.0) == 0.0 + assert Calculator.multiply(2.0, 0.0) == 0.0 + assert Calculator.multiply(-4, 2.0) == -8.0 + +# def test_divide(): +# assert Calculator.divide(1, 2) == 0.5 +# assert Calculator.divide(1.0, 2.0) == 0.5 +# assert Calculator.divide(0, 2.0) == 0 +# assert Calculator.divide(-4, 2.0) == -2.0 + +# def test_divide_by_0(): +# assert Calculator.divide(2.0, 0) == 'Cannot divide by 0' \ No newline at end of file diff --git a/api/requirements.txt b/api/requirements.txt new file mode 100644 index 000000000..192d94da7 --- /dev/null +++ b/api/requirements.txt @@ -0,0 +1,13 @@ +blinker==1.9.0 +click==8.1.8 +coverage==7.7.1 +Flask==3.1.0 +iniconfig==2.1.0 +itsdangerous==2.2.0 +Jinja2==3.1.6 +MarkupSafe==3.0.2 +packaging==24.2 +pluggy==1.5.0 +pytest==8.3.5 +pytest-cov==6.0.0 +Werkzeug==3.1.3 \ No newline at end of file diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 000000000..b6ac064fd --- /dev/null +++ b/codecov.yml @@ -0,0 +1,6 @@ +coverage: + status: + project: + default: + target: 100% + threshold: 1% \ No newline at end of file