From 3044005c2e45b6b5513b45a7e229ccdad3d8068c Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Fri, 22 Oct 2021 22:06:45 +0800 Subject: Tests: jForm mock data generator and routine tests --- jimbrella/test/test_routine.py | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 jimbrella/test/test_routine.py (limited to 'jimbrella/test/test_routine.py') diff --git a/jimbrella/test/test_routine.py b/jimbrella/test/test_routine.py new file mode 100644 index 0000000..a3a5675 --- /dev/null +++ b/jimbrella/test/test_routine.py @@ -0,0 +1,58 @@ +from flask import Flask, Response, request +from threading import Thread +import json +import os +import shutil +from .jform_data import mock_jform_data +from ..routine import sync_database +from ..database import Database +from ..jform import JForm +from ..config import * + +"""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() + +"""Initialize Database and JForm.""" +TEST_DATABASE_PATH = "/tmp/jimbrella.test.db.csv" +shutil.copyfile(DATABASE_PATH, TEST_DATABASE_PATH) +db = Database(TEST_DATABASE_PATH) +takeaway_jform = JForm("takeaway", "http://localhost:5001/takeaway", "/tmp") +giveback_jform = JForm("giveback", "http://localhost:5001/giveback", "/tmp") +sync_database(takeaway_jform, giveback_jform, db) +os.remove(takeaway_jform._bookmark_fp) +os.remove(giveback_jform._bookmark_fp) -- cgit v1.2.3