summaryrefslogtreecommitdiff
path: root/jimbrella/test
diff options
context:
space:
mode:
Diffstat (limited to 'jimbrella/test')
-rw-r--r--jimbrella/test/jform_data.py14
-rw-r--r--jimbrella/test/test_routine.py40
2 files changed, 28 insertions, 26 deletions
diff --git a/jimbrella/test/jform_data.py b/jimbrella/test/jform_data.py
index 358da8a..5d235ce 100644
--- a/jimbrella/test/jform_data.py
+++ b/jimbrella/test/jform_data.py
@@ -1,14 +1,12 @@
import json
from datetime import datetime, timedelta
-from ..database import Database
+from ..umbrellas import Umbrellas
from ..config import *
TENANT_NAMES = ["Alice", "Bob", "Carol", "Dave", "Eve", "Frank"]
TENANT_IDS = ["01", "02", "03", "04", "05", "06"]
TENANT_PHONES = ["0001", "0002", "0003", "0004", "0005", "0006"]
-TENANTS = [
- (TENANT_NAMES[i], TENANT_IDS[i], TENANT_PHONES[i]) for i in range(len(TENANT_NAMES))
-]
+TENANTS = zip(TENANT_NAMES, TENANT_IDS, TENANT_PHONES)
ALICE, BOB, CAROL, DAVE, EVE, FRANK = tuple(TENANTS)
@@ -32,13 +30,15 @@ def mock_answer_sheet(
"question": {"id": 9957462, "title": "3. 联系方式Phone"},
},
{
- "answer": key_no,
+ "answer": str(key_no),
"question": {"id": 9957463, "title": "4. 钥匙编号Key's Number"},
},
],
"id": sheet_id,
"ip_address": "127.0.0.1",
"status": 0,
+ # NOTE: jForm omits trailing zeros in milliseconds,
+ # but datetime.datetime.isoformat does not
"submitted_at": time.isoformat(timespec="milliseconds") + "+08:00",
"tags": [],
"user": {"name": tenant_name, "organization": "密西根学院"},
@@ -91,9 +91,9 @@ def mock_jform_data() -> tuple:
"code": 0,
}
- db = Database(DATABASE_PATH)
+ db = Umbrellas(DATABASE_PATH)
umbrellas = db.read()
- key_numbers = [umb["serial"] for umb in umbrellas]
+ key_numbers = [umb["id"] for umb in umbrellas]
now = datetime.now()
takeaway_rows = [
mock_answer_sheet(*ALICE, 1, 1, now - timedelta(days=7)),
diff --git a/jimbrella/test/test_routine.py b/jimbrella/test/test_routine.py
index 0b15717..acb1f1e 100644
--- a/jimbrella/test/test_routine.py
+++ b/jimbrella/test/test_routine.py
@@ -5,20 +5,19 @@ import os
import shutil
import logging
from .jform_data import mock_jform_data
-from ..routine import sync_database, process_overdue
-from ..database import Database
+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."""
+"""Set up logging.
logging.basicConfig(
filename="/tmp/jimbrella.log",
encoding="utf-8",
level=logging.INFO,
format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s",
-)
-logging.info("----- BEGIN ROUTINE TEST -----")
+)"""
"""Spin up a simple Flask app to mimic jForm."""
jform = Flask(__name__)
@@ -58,18 +57,21 @@ def api(endpoint):
Thread(target=jform.run, kwargs={"port": 5001}).start()
-"""Initialize Database and JForm."""
-TEST_DATABASE_PATH = "/tmp/jimbrella.db.csv"
-TEST_ADMIN_LOG_PATH = "/tmp/jimbrella.admin.log"
-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")
-admin = AdminLog(TEST_ADMIN_LOG_PATH)
-sync_database(takeaway_jform, giveback_jform, db, admin)
-process_overdue(db)
+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 -----")
+ """Cleanup"""
+ os.remove(takeaway_jform._bookmark_fp)
+ os.remove(giveback_jform._bookmark_fp)
+ logging.info("----- END ROUTINE TEST -----")