Spaces:
Sleeping
Sleeping
Download tests/test_api.py from Rahul4112/BackDropStudio: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/spaces/Rahul4112/BackDropStudio/resolve/main/tests/test_api.py
- Command line
-
hf download hf://spaces/Rahul4112/BackDropStudio/tests/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/Rahul4112/BackDropStudio/resolve/main/tests/test_api.py
3.07 kB
| 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 | |
| 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() | |