Skip to content
Closed

Step4 #266

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
63946fe
step0: add python dependencies
dfulmer Jun 9, 2025
5996181
Merge pull request #1 from dfulmer/step0
dfulmer Jun 9, 2025
c649142
step1: add calculator backend and tests
dfulmer Jun 9, 2025
fd7d44d
Merge pull request #2 from dfulmer/step1
dfulmer Jun 9, 2025
26d4cd5
step2: upload coverage reports to Codecov
dfulmer Jun 11, 2025
19aff39
step2: upload coverage reports to Codecov Try 2
dfulmer Jun 11, 2025
cc1b049
Merge pull request #4 from dfulmer/step2
dfulmer Jun 11, 2025
139833b
step3: add project status check target
dfulmer Jun 11, 2025
8039290
Merge pull request #5 from dfulmer/step3
dfulmer Jun 11, 2025
e578cac
step3.1: taking out a test
dfulmer Jun 11, 2025
eb8b53c
Merge pull request #6 from dfulmer/step3
dfulmer Jun 11, 2025
ebfb242
step3: adjusting the codecov.yml
dfulmer Jun 11, 2025
3ea6074
Merge pull request #7 from dfulmer/step3
dfulmer Jun 11, 2025
996f06d
step3: adjusting the codecov.yml a second time
dfulmer Jun 11, 2025
a5756af
step3: putting back codecov.yml the way it was
dfulmer Jun 11, 2025
080bcb5
Merge pull request #8 from dfulmer/step3
dfulmer Jun 11, 2025
db76feb
step3: adding square method
dfulmer Jun 11, 2025
b5bf4c0
Merge pull request #9 from dfulmer/step3
dfulmer Jun 11, 2025
5baef44
step3: cover divide by 0 case
dfulmer Jun 11, 2025
371bb17
Merge pull request #10 from dfulmer/step3
dfulmer Jun 11, 2025
f0cd83a
step3: add Codecov badge
dfulmer Jun 11, 2025
b41c6d2
Merge pull request #11 from dfulmer/step3
dfulmer Jun 11, 2025
bd29a93
step4: add smiles
dfulmer Jun 11, 2025
ce1332d
step4: add Codecov Component
dfulmer Jun 11, 2025
a3ac404
step4: aggregate tests and split Component
dfulmer Jun 11, 2025
3e1754d
step4: aggregate tests and split Component-fixing the api.yml
dfulmer Jun 11, 2025
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
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
- name: Upload coverage reports to Codecov with GitHub Action
uses: codecov/codecov-action@v5
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
coverage.xml
node_modules/
coverage/
venv/*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
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
33 changes: 33 additions & 0 deletions api/calculator/test_calculator.py
Original file line number Diff line number Diff line change
@@ -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'
14 changes: 14 additions & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Empty file added api/smiles/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions api/smiles/smiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Smiles:
def smile(self):
return ":)"

def frown(self):
return ":("
4 changes: 4 additions & 0 deletions api/smiles/test_smiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .smiles import Smiles

def test_smile():
assert Smiles().smile() == ":)"
19 changes: 19 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -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%