import json from datetime import datetime, timedelta 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 = zip(TENANT_NAMES, TENANT_IDS, TENANT_PHONES) ALICE, BOB, CAROL, DAVE, EVE, FRANK = tuple(TENANTS) def mock_answer_sheet( tenant_name: str, tenant_id: str, tenant_phone: str, key_no: int, sheet_id: int, time: datetime, ) -> dict: return { "answers": [ {"answer": tenant_name, "question": {"id": 9957460, "title": "1. 姓名Name"}}, { "answer": tenant_id, "question": {"id": 9957461, "title": "2. 学工号Student/Faculty number"}, }, { "answer": tenant_phone, "question": {"id": 9957462, "title": "3. 联系方式Phone"}, }, { "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": "密西根学院"}, } def mock_jform_data() -> tuple: """Generate mock, serialized jForm data for tests. Result should describe this flow (- is borrow, + is return): - Alice borrowed umbrella #1 7 days ago - Bob borrowed umbrella #2 6 days ago + Alice returned umbrella #1 5 days ago - Carol borrowed umbrella #1 1 hour after Alice returned it + Carol returned umbrella #1 4 days ago - Dave borrowed umbrella #3 4 days ago, but would not return it - Eve borrowed umbrella #4 3 days and 1 hour ago - Eve attempted to borrow #4 again, 1 minute later - Frank borrowed umbrella #5 yesterday + Bob (finally) returned umbrella #2 today At this stage: - Umbrella #1 and #2 are available - Umbrella #3 is overdue in the hands of Dave - Umbrella #4 is overdue in the hands of Eve - Umbrella #5 is lent to Frank Value is returned as: ( , ) """ takeaway = { "success": True, "message": "ok", "data": { "rows": [], "total": 0, }, "code": 0, } giveback = { "success": True, "message": "ok", "data": { "rows": [], "total": 0, }, "code": 0, } db = Umbrellas(DATABASE_PATH) umbrellas = db.read() key_numbers = [umb["id"] for umb in umbrellas] now = datetime.now() takeaway_rows = [ mock_answer_sheet(*ALICE, 1, 1, now - timedelta(days=7)), mock_answer_sheet(*BOB, 2, 2, now - timedelta(days=6)), mock_answer_sheet(*CAROL, 1, 4, now - timedelta(days=4, hours=23)), mock_answer_sheet(*DAVE, 3, 6, now - timedelta(days=4)), mock_answer_sheet(*EVE, 4, 7, now - timedelta(days=3, hours=1)), mock_answer_sheet(*EVE, 4, 8, now - timedelta(days=3, minutes=59)), mock_answer_sheet(*FRANK, 5, 9, now - timedelta(days=1)), ] giveback_rows = [ mock_answer_sheet(*ALICE, 1, 3, now - timedelta(days=5)), mock_answer_sheet(*CAROL, 1, 5, now - timedelta(days=4)), mock_answer_sheet(*BOB, 2, 10, now - timedelta(hours=1)), ] takeaway["data"]["rows"] = takeaway_rows takeaway["data"]["total"] = len(takeaway_rows) giveback["data"]["rows"] = giveback_rows giveback["data"]["total"] = len(giveback_rows) return (json.dumps(takeaway), json.dumps(giveback))