File size: 3,071 Bytes
0d7bef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest
import zipfile
from pathlib import Path
from unittest.mock import patch

try:
    from fastapi.testclient import TestClient

    from app.main import app
    from app.processor import ProcessResult

    FASTAPI_IMPORT_ERROR = None
except Exception as exc:  # pragma: no cover
    TestClient = None
    app = None
    FASTAPI_IMPORT_ERROR = exc


@unittest.skipIf(TestClient is None, f"FastAPI test client unavailable: {FASTAPI_IMPORT_ERROR}")
class ApiTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(app)

    def test_process_rejects_non_zip_uploads(self):
        response = self.client.post(
            "/api/process",
            files={"file": ("photo.jpg", b"not a zip", "image/jpeg")},
            data={"bg_color": "241,242,242", "folder_name": "Grey"},
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["detail"], "Please upload a .zip file.")

    def test_status_returns_not_found_for_unknown_job(self):
        response = self.client.get("/api/status/missing-job")

        self.assertEqual(response.status_code, 404)

    def test_process_accepts_new_quality_fields(self):
        def fake_process_zip(
            zip_path,
            output_zip_path,
            folder_name,
            bg_rgb,
            *,
            quality_profile,
            output_mode,
            progress_callback,
        ):
            self.assertEqual(folder_name, "Grey")
            self.assertEqual(bg_rgb, (241, 242, 242))
            self.assertEqual(quality_profile, "pro")
            self.assertEqual(output_mode, "lossless_png")
            progress_callback(1, 0, [])
            with zipfile.ZipFile(output_zip_path, "w") as archive:
                archive.writestr("Grey/", b"")
                archive.writestr("Grey/SKU-100.png", b"fake")
            progress_callback(1, 1, [])
            return ProcessResult(Path(output_zip_path), 1, 1, [])

        with patch("app.main.process_zip", side_effect=fake_process_zip):
            response = self.client.post(
                "/api/process",
                files={"file": ("input.zip", b"PK\x05\x06" + b"\x00" * 18, "application/zip")},
                data={
                    "bg_color": "#F1F2F2",
                    "folder_name": "Grey",
                    "quality_profile": "pro",
                    "output_mode": "lossless_png",
                },
            )

        self.assertEqual(response.status_code, 200)
        status = self.client.get(f"/api/status/{response.json()['job_id']}")
        self.assertEqual(status.json()["status"], "done")
        self.assertEqual(status.json()["failed_files"], [])

    def test_process_rejects_invalid_quality_profile(self):
        response = self.client.post(
            "/api/process",
            files={"file": ("input.zip", b"PK\x05\x06" + b"\x00" * 18, "application/zip")},
            data={"quality_profile": "ultra"},
        )

        self.assertEqual(response.status_code, 400)


if __name__ == "__main__":
    unittest.main()