Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download tests/test_api/test_foundation_api.py from DanielKretschmer/lighting-mvp: direct link, hf CLI and curl.
- Browser
- Download file 8.16 kB
-
https://huggingface.co/spaces/DanielKretschmer/lighting-mvp/resolve/main/tests/test_api/test_foundation_api.py
- Command line
-
hf download hf://spaces/DanielKretschmer/lighting-mvp/tests/test_api/test_foundation_api.py
-
curl -L -o test_foundation_api.py https://huggingface.co/spaces/DanielKretschmer/lighting-mvp/resolve/main/tests/test_api/test_foundation_api.py
8.16 kB
| import io | |
| async def test_healthcheck(client): | |
| response = await client.get("/health") | |
| assert response.status_code == 200 | |
| assert response.json() == {"status": "ok"} | |
| async def test_default_config_seeded_and_fetchable(client): | |
| response = await client.get("/api/v1/configs/v1.0") | |
| assert response.status_code == 200 | |
| payload = response.json() | |
| assert payload["config"]["config_version"] == "v1.0" | |
| assert payload["config"]["matching"]["require_exact_category"] is True | |
| async def test_put_config_create_only(client): | |
| create_response = await client.put( | |
| "/api/v1/configs/v2.0", | |
| json={"config": {"config_version": "v2.0"}}, | |
| ) | |
| assert create_response.status_code == 200 | |
| assert create_response.json() == {"config_version": "v2.0"} | |
| conflict_response = await client.put( | |
| "/api/v1/configs/v2.0", | |
| json={"config": {"config_version": "v2.0"}}, | |
| ) | |
| assert conflict_response.status_code == 409 | |
| assert conflict_response.json()["detail"]["code"] == "CONFIG_ALREADY_EXISTS" | |
| async def test_create_job_and_upload_file_creates_ingestion_artifact(client): | |
| create_job_response = await client.post( | |
| "/api/v1/jobs", | |
| json={"job_name": "Example project", "config_version": "v1.0", "created_by": "tester"}, | |
| ) | |
| assert create_job_response.status_code == 201 | |
| job_id = create_job_response.json()["job_id"] | |
| upload_response = await client.post( | |
| f"/api/v1/jobs/{job_id}/files", | |
| files={"files": ("schedule.csv", io.BytesIO(b"fixture,data\nA,1\n"), "text/csv")}, | |
| ) | |
| assert upload_response.status_code == 200 | |
| upload_payload = upload_response.json() | |
| assert upload_payload["job_id"] == job_id | |
| assert len(upload_payload["files"]) == 1 | |
| status_response = await client.get(f"/api/v1/jobs/{job_id}") | |
| assert status_response.status_code == 200 | |
| status_payload = status_response.json() | |
| assert status_payload["job_id"] == job_id | |
| ingestion_stage = next(stage for stage in status_payload["stages"] if stage["stage"] == "ingestion") | |
| assert ingestion_stage["status"] == "completed" | |
| artifacts_response = await client.get(f"/api/v1/jobs/{job_id}/artifacts") | |
| assert artifacts_response.status_code == 200 | |
| artifacts = artifacts_response.json()["artifacts"] | |
| assert len(artifacts) == 1 | |
| artifact = artifacts[0] | |
| assert artifact["stage"] == "ingestion" | |
| assert artifact["status"] == "completed" | |
| assert artifact["payload"]["file_manifest"][0]["filename"] == "schedule.csv" | |
| artifact_response = await client.get(f"/api/v1/artifacts/{artifact['artifact_id']}") | |
| assert artifact_response.status_code == 200 | |
| assert artifact_response.json()["artifact_id"] == artifact["artifact_id"] | |
| async def test_run_stage_creates_pending_artifact_using_previous_stage_output(client): | |
| create_job_response = await client.post( | |
| "/api/v1/jobs", | |
| json={"job_name": "Run stage project", "config_version": "v1.0"}, | |
| ) | |
| job_id = create_job_response.json()["job_id"] | |
| await client.post( | |
| f"/api/v1/jobs/{job_id}/files", | |
| files={"files": ("schedule.pdf", io.BytesIO(b"%PDF-1.4"), "application/pdf")}, | |
| ) | |
| response = await client.post( | |
| f"/api/v1/jobs/{job_id}/stages/extraction/run", | |
| json={"input_artifact_ids": [], "config_overrides": {}}, | |
| ) | |
| assert response.status_code == 202 | |
| payload = response.json() | |
| assert payload["status"] == "pending" | |
| artifacts_response = await client.get(f"/api/v1/jobs/{job_id}/artifacts") | |
| artifacts = artifacts_response.json()["artifacts"] | |
| extraction_artifact = next(artifact for artifact in artifacts if artifact["stage"] == "extraction") | |
| assert extraction_artifact["status"] == "pending" | |
| assert len(extraction_artifact["input_artifact_ids"]) == 1 | |
| async def test_corpus_normalize_and_lookup_product(client, tmp_path): | |
| csv_path = tmp_path / "exo.csv" | |
| csv_path.write_text( | |
| "\n".join( | |
| [ | |
| "Stock Code,Description,Wattage,Lumens,Distribution,CCT,Material,Diffuser,Reflector,Colour,IP Rating,Dimming,CRI,Length,Width,Height,Diameter,Mounting,Cut-Out,X_emg_class,X_emg_viewdistance", | |
| "DL-01,Recessed Downlight,12,900,Wide,3000,Aluminium,Opal,,White,IP44,DALI-2,80,120,120,90,120,Recessed,110,,", | |
| ] | |
| ), | |
| encoding="utf-8", | |
| ) | |
| normalize_response = await client.post( | |
| "/api/v1/corpus/normalize", | |
| params={"source_uri": str(csv_path)}, | |
| ) | |
| assert normalize_response.status_code == 200 | |
| assert normalize_response.json()["rows_normalized"] == 1 | |
| product_response = await client.get("/api/v1/corpus/products/DL-01") | |
| assert product_response.status_code == 200 | |
| assert product_response.json()["product_category"] == "downlight" | |
| async def test_corpus_products_persist_beyond_in_memory_catalog(client, tmp_path): | |
| csv_path = tmp_path / "exo.csv" | |
| csv_path.write_text( | |
| "\n".join( | |
| [ | |
| "Stock Code,Description,Wattage,Lumens,Distribution,CCT,Material,Diffuser,Reflector,Colour,IP Rating,Dimming,CRI,Length,Width,Height,Diameter,Mounting,Cut-Out,X_emg_class,X_emg_viewdistance", | |
| "DL-01,Recessed Downlight,12,900,Wide,3000,Aluminium,Opal,,White,IP44,DALI-2,80,120,120,90,120,Recessed,110,,", | |
| ] | |
| ), | |
| encoding="utf-8", | |
| ) | |
| normalize_response = await client.post( | |
| "/api/v1/corpus/normalize", | |
| params={"source_uri": str(csv_path)}, | |
| ) | |
| assert normalize_response.status_code == 200 | |
| client._transport.app.state.corpus_catalog = {} | |
| product_response = await client.get("/api/v1/corpus/products/DL-01") | |
| assert product_response.status_code == 200 | |
| assert product_response.json()["product_code"] == "DL-01" | |
| async def test_run_all_executes_pipeline_through_explanation(client, tmp_path): | |
| corpus_path = tmp_path / "exo.csv" | |
| corpus_path.write_text( | |
| "\n".join( | |
| [ | |
| "Stock Code,Description,Wattage,Lumens,Distribution,CCT,Material,Diffuser,Reflector,Colour,IP Rating,Dimming,CRI,Length,Width,Height,Diameter,Mounting,Cut-Out,X_emg_class,X_emg_viewdistance", | |
| "DL-01,Recessed Downlight,12,900,Wide,3000,Aluminium,Opal,,White,IP44,DALI-2,80,120,120,90,120,Recessed,110,,", | |
| ] | |
| ), | |
| encoding="utf-8", | |
| ) | |
| normalize_response = await client.post( | |
| "/api/v1/corpus/normalize", | |
| params={"source_uri": str(corpus_path)}, | |
| ) | |
| assert normalize_response.status_code == 200 | |
| client._transport.app.state.corpus_catalog = {} | |
| create_job_response = await client.post( | |
| "/api/v1/jobs", | |
| json={"job_name": "Pipeline job", "config_version": "v1.0"}, | |
| ) | |
| job_id = create_job_response.json()["job_id"] | |
| schedule_path = tmp_path / "schedule.csv" | |
| schedule_path.write_text( | |
| "\n".join( | |
| [ | |
| "Description,Type,Qty,CCT,CRI,Lumens,Dimming,Mounting,IP", | |
| "Lobby downlight,downlight,1,3000k,CRI80,900lm,DALI,recessed mount,IP44", | |
| ] | |
| ), | |
| encoding="utf-8", | |
| ) | |
| upload_response = await client.post( | |
| f"/api/v1/jobs/{job_id}/files", | |
| files={"files": ("schedule.csv", schedule_path.read_bytes(), "text/csv")}, | |
| ) | |
| assert upload_response.status_code == 200 | |
| run_all_response = await client.post( | |
| f"/api/v1/jobs/{job_id}/run-all", | |
| json={"config_overrides": {}}, | |
| ) | |
| assert run_all_response.status_code == 202 | |
| artifact_ids = run_all_response.json()["artifact_ids"] | |
| assert "explanation" in artifact_ids | |
| artifacts_response = await client.get(f"/api/v1/jobs/{job_id}/artifacts") | |
| assert artifacts_response.status_code == 200 | |
| artifacts = artifacts_response.json()["artifacts"] | |
| artifact_stages = [artifact["stage"] for artifact in artifacts] | |
| assert "matching" in artifact_stages | |
| assert "explanation" in artifact_stages | |
| matching_artifact = next(artifact for artifact in artifacts if artifact["stage"] == "matching") | |
| assert matching_artifact["payload"]["matches"][0]["selected_product_code"] == "DL-01" | |