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 #214

Closed
wants to merge 10 commits into from
Closed

Step2 #214

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/.circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2.1

orbs:
codecov: codecov/codecov@4.0.1

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

21 changes: 21 additions & 0 deletions .github/workflows/api.yml
Original file line number Diff line number Diff line change
@@ -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@v4.2.0
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
40 changes: 40 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This is a basic workflow to help you get started with Actions

name: Upload coverage reports to Codecov

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: Adal3n3/codecov-demo

# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!

# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
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
30 changes: 30 additions & 0 deletions api/calculator/test_calculator.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
blinker==1.7.0
certifi==2024.2.2
click==8.1.7
coverage==7.4.4
distlib==0.3.8
filelock==3.13.4
Flask==3.0.3
iniconfig==2.0.0
itsdangerous==2.2.0
Jinja2==3.1.3
MarkupSafe==2.1.5
packaging==24.0
platformdirs==4.2.0
pluggy==1.4.0
pytest==8.1.1
pytest-cov==5.0.0
setuptools==69.5.1
virtualenv==20.25.3
Werkzeug==3.0.2
wheel==0.43.0
Loading