from flask import Flask, Response, request from threading import Thread import json import os import shutil import logging from .jform_data import mock_jform_data from ..routine import sync_jform, process_overdue from ..umbrellas import Umbrellas from ..jform import JForm from ..admin_log import AdminLog from ..config import * """Set up logging. logging.basicConfig( filename="/tmp/jimbrella.log", encoding="utf-8", level=logging.INFO, format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s", )""" """Spin up a simple Flask app to mimic jForm.""" jform = Flask(__name__) takeaway, giveback = mock_jform_data() EMPTY_RESPONSE = Response( json.dumps( { "success": True, "message": "ok", "data": { "rows": [], "total": 0, }, "code": 0, }, ), content_type="application/json", ) @jform.route("/") def api(endpoint): params = request.args.get("params") if ( params and "current" in json.loads(params) and json.loads(params)["current"] != 1 ): return EMPTY_RESPONSE if endpoint == "takeaway": return Response(takeaway, content_type="application/json") elif endpoint == "giveback": return Response(giveback, content_type="application/json") Thread(target=jform.run, kwargs={"port": 5001}).start() if __name__ == "__main__": logging.info("----- BEGIN ROUTINE TEST -----") """Initialize Database and JForm.""" TEST_DATABASE_PATH = "/tmp/jimbrella.db" TEST_ADMIN_LOG_PATH = "/tmp/jimbrella.admin.log" shutil.copyfile(DATABASE_PATH, TEST_DATABASE_PATH) db = Umbrellas(TEST_DATABASE_PATH) takeaway_jform = JForm("takeaway", "http://localhost:5001/takeaway", "/tmp") giveback_jform = JForm("giveback", "http://localhost:5001/giveback", "/tmp") admin = AdminLog(TEST_ADMIN_LOG_PATH) sync_jform(takeaway_jform, giveback_jform, db, admin) process_overdue(db, admin) """Cleanup""" os.remove(takeaway_jform._bookmark_fp) os.remove(giveback_jform._bookmark_fp) logging.info("----- END ROUTINE TEST -----")