summaryrefslogtreecommitdiff
path: root/jimbrella/test/jform_data.py
blob: 756a9ae820cec404bb3d725046b52839e52aad42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
from datetime import datetime, timedelta
from ..umbrellas import Umbrellas

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:
    (
        <jForm API response body for takeaway questionnaire>,
        <jForm API response body for giveback questionnaire>
    )
    """
    takeaway = {
        "success": True,
        "message": "ok",
        "data": {
            "rows": [],
            "total": 0,
        },
        "code": 0,
    }
    giveback = {
        "success": True,
        "message": "ok",
        "data": {
            "rows": [],
            "total": 0,
        },
        "code": 0,
    }

    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))