Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

step2: upload coverage reports to Codecov #133

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: API workflow

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
name: Test python API
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v2
with:
python-version: '3.11'
- 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@v3
35 changes: 35 additions & 0 deletions api/app.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file added api/calculator/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions api/calculator/calculator.py
Original file line number Diff line number Diff line change
@@ -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