diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..7564275 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,28 @@ +version: 2.1 + +orbs: + codecov: codecov/codecov@4.0.1 + +permissions: + id-token: write # This is required for requesting the JWT + contents: read # This is required for actions/checkout + +jobs: + test-api: + docker: + - image: cimg/python:3.10.2 + steps: + - checkout + - run: + name: Install requirements + command: pip install -r api/requirements.txt + - run: + name: Run tests and collect coverage + command: pytest --cov api.calculator + - codecov/upload + +workflows: + version: 2.1 + build-test: + jobs: + - test-api \ No newline at end of file diff --git a/.github/workflows/api.yml b/.github/workflows/api.yml new file mode 100644 index 0000000..5f24d2c --- /dev/null +++ b/.github/workflows/api.yml @@ -0,0 +1,25 @@ +name: API workflow + +on: [push, pull_request] + +permissions: + id-token: write # This is required for requesting the JWT + contents: read # This is required for actions/checkout + +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@v4.2.0 + with: + use_oidc: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cfa4ab2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.py[oc] +__pycache__ +.coverage +.pytest_cache/ +coverage.xml +node_modules/ +coverage/ diff --git a/README.md b/README.md index e0cad3c..1faf404 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# oidc-test \ No newline at end of file +# oidc-test +used pyenv virtualenv 3.12.2 temp \ No newline at end of file diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/app.py b/api/app.py new file mode 100644 index 0000000..046e026 --- /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) diff --git a/api/calculator/__init__.py b/api/calculator/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/calculator/calculator.py b/api/calculator/calculator.py new file mode 100644 index 0000000..ab6c338 --- /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 diff --git a/api/calculator/test_calculator.py b/api/calculator/test_calculator.py new file mode 100644 index 0000000..4164bee --- /dev/null +++ b/api/calculator/test_calculator.py @@ -0,0 +1,30 @@ +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 diff --git a/api/requirements.txt b/api/requirements.txt new file mode 100644 index 0000000..262d521 --- /dev/null +++ b/api/requirements.txt @@ -0,0 +1,13 @@ +blinker==1.8.2 +click==8.1.7 +coverage==7.6.1 +Flask==3.0.3 +iniconfig==2.0.0 +itsdangerous==2.2.0 +Jinja2==3.1.4 +MarkupSafe==2.1.5 +packaging==24.1 +pluggy==1.5.0 +pytest==8.3.3 +pytest-cov==5.0.0 +Werkzeug==3.0.4