File size: 1,459 Bytes
0cdb4e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Validate incremental /upload-room (one file per request + session_id).

Mimics the mobile native uploader against a RUNNING server.
Run: python scripts/upload_test.py
"""
from __future__ import annotations

import io
import sys

import requests
from PIL import Image

BASE = "http://127.0.0.1:8000"


def jpg(color: tuple[int, int, int]) -> bytes:
    b = io.BytesIO()
    Image.new("RGB", (640, 480), color).save(b, format="JPEG")
    return b.getvalue()


def main() -> int:
    # First file, no session -> creates a session with 1 image.
    r = requests.post(
        f"{BASE}/upload-room",
        files={"files": ("a.jpg", jpg((150, 140, 130)), "image/jpeg")},
        timeout=30,
    )
    print("call 1:", r.status_code, r.text[:160])
    r.raise_for_status()
    d = r.json()
    sid = d["session_id"]
    assert len(d["images"]) == 1, d

    # Second file, with session_id form field -> appends (cumulative 2).
    r = requests.post(
        f"{BASE}/upload-room",
        files={"files": ("b.jpg", jpg((120, 130, 140)), "image/jpeg")},
        data={"session_id": sid},
        timeout=30,
    )
    print("call 2:", r.status_code, r.text[:160])
    r.raise_for_status()
    d2 = r.json()
    assert d2["session_id"] == sid, d2
    assert len(d2["images"]) == 2, d2

    print(f"OK: session {sid[:8]} now has {len(d2['images'])} images")
    print("UPLOAD INCREMENTAL TEST PASSED")
    return 0


if __name__ == "__main__":
    sys.exit(main())