Solution: Flask persistent counter



examples/flask/persistent-counter/app.py
from flask import Flask
import os
app = Flask(__name__)

def get_counter_file():
    data_dir = os.path.dirname(os.path.abspath(__file__))
    data_dir = os.environ.get('COUNTER_DATA_DIR', data_dir)
    counter_file = os.path.join(data_dir, 'counter.txt')
    return counter_file

@app.route("/")
def main():
    counter_file = get_counter_file()
    counter = 0
    if os.path.exists(counter_file):
        with open(counter_file) as fh:
            counter = int(fh.read())

    counter += 1
    with open(counter_file, 'w') as fh:
        fh.write(str(counter))

    return str(counter)

examples/flask/persistent-counter/test_app.py
import app
import os

def test_app(tmpdir):
    os.environ['COUNTER_DATA_DIR'] = str(tmpdir) # str expected, not LocalPath

    web1 = app.app.test_client()
    rv = web1.get('/')
    assert rv.status == '200 OK'
    assert rv.data == b'1'

    rv = web1.get('/')
    assert rv.status == '200 OK'
    assert rv.data == b'2'

    web2 = app.app.test_client()
    rv = web2.get('/')
    assert rv.status == '200 OK'
    assert rv.data == b'3'