Skip to content

Commit

Permalink
part1
Browse files Browse the repository at this point in the history
  • Loading branch information
nora-codecov committed Sep 11, 2024
1 parent 962b2a0 commit 7323ed8
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: 2.1

orbs:
codecov: codecov/[email protected]

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
25 changes: 25 additions & 0 deletions .github/workflows/api.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
with:
use_oidc: true
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.py[oc]
__pycache__
.coverage
.pytest_cache/
coverage.xml
node_modules/
coverage/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# oidc-test
# oidc-test
used pyenv virtualenv 3.12.2 temp
Empty file added api/__init__.py
Empty file.
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
13 changes: 13 additions & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7323ed8

Please sign in to comment.