Solution: Calculator with path



examples/flask/path-int-add/app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def main():
    return 'Main'

@app.route('/<op>/<int:a>/<int:b>')
def calc(op, a, b):
    if op == 'add':
        return str(a+b)
    if op == 'mul':
        return str(a*b)

    return 'Invalid operator'

@app.route('/sum/<path:values>')
def sum_route(values):
    numbers = map(float, values.split('/'))
    total = sum(numbers)
    return str(total)

examples/flask/path-int-add/test_app.py
import app

def test_app():
    web = app.app.test_client()

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'Main' == rv.data

    rv = web.get('/add/2/3')
    assert rv.status == '200 OK'
    assert b'5' == rv.data

    rv = web.get('/mul/2/3')
    assert rv.status == '200 OK'
    assert b'6' == rv.data

    rv = web.get('/sum/2/3/4.1/-1')
    assert rv.status == '200 OK'
    print(rv.data)
    assert b'8.1' == rv.data