diff --git a/.github/workflows/api.yml b/.github/workflows/api.yml new file mode 100644 index 000000000..2c2f08c71 --- /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 + - 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/.gitignore b/.gitignore index cfa4ab2ef..85aada097 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ coverage.xml node_modules/ coverage/ +venv/* \ No newline at end of file diff --git a/README.md b/README.md index 2c27a415d..b80e4068a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ +[![codecov](https://codecov.io/gh/dfulmer/codecov-demo/graph/badge.svg?token=Y9KAYHKTQ5)](https://codecov.io/gh/dfulmer/codecov-demo) # codecov-demo This repository is meant to show Codecov's features and functionalities. You can follow along [here](https://docs.codecov.com/docs/codecov-tutorial). 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..4c0e68150 --- /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..b7029c42e --- /dev/null +++ b/api/requirements.txt @@ -0,0 +1,14 @@ +blinker==1.9.0 +click==8.2.1 +coverage==7.8.2 +Flask==3.1.1 +iniconfig==2.1.0 +itsdangerous==2.2.0 +Jinja2==3.1.6 +MarkupSafe==3.0.2 +packaging==25.0 +pluggy==1.6.0 +Pygments==2.19.1 +pytest==8.4.0 +pytest-cov==6.1.1 +Werkzeug==3.1.3 diff --git a/api/smiles/__init__.py b/api/smiles/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api/smiles/smiles.py b/api/smiles/smiles.py new file mode 100644 index 000000000..765cabf0e --- /dev/null +++ b/api/smiles/smiles.py @@ -0,0 +1,6 @@ +class Smiles: + def smile(self): + return ":)" + + def frown(self): + return ":(" \ No newline at end of file diff --git a/api/smiles/test_smiles.py b/api/smiles/test_smiles.py new file mode 100644 index 000000000..39e188f16 --- /dev/null +++ b/api/smiles/test_smiles.py @@ -0,0 +1,4 @@ +from .smiles import Smiles + +def test_smile(): + assert Smiles().smile() == ":)" \ No newline at end of file diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 000000000..ae8f3f21d --- /dev/null +++ b/codecov.yml @@ -0,0 +1,19 @@ +comment: + layout: "condensed_header, diff, flags, components" + +component_management: + individual_components: + - component_id: api-calculator # this is an identifier that should not be changed + name: calculator # this is a display name, and can be changed freely + paths: + - api/calculator/ + - component_id: api-smiles # this is an identifier that should not be changed + name: smiles # this is a display name, and can be changed freely + paths: + - api/smiles/ +coverage: + status: + project: + default: + target: 100% + threshold: 1% \ No newline at end of file