diff options
author | Frederick Yin <fkfd@fkfd.me> | 2021-10-22 22:06:45 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2021-10-22 22:10:13 +0800 |
commit | 3044005c2e45b6b5513b45a7e229ccdad3d8068c (patch) | |
tree | 3811a132e0c6a15de5bb415f07366e48e335725c /jimbrella/test/test_routine.py | |
parent | 3b16f04f4f3c6085ffef2ac058fca0a17fcb49ca (diff) |
Tests: jForm mock data generator and routine tests
Diffstat (limited to 'jimbrella/test/test_routine.py')
-rw-r--r-- | jimbrella/test/test_routine.py | 58 |
1 files changed, 58 insertions, 0 deletions
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("/<endpoint>") +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) |