| import pytest |
| from fastapi.testclient import TestClient |
| from unittest.mock import Mock, patch |
| import json |
|
|
| def test_analyze_skin_with_image(client: TestClient, auth_headers: dict, sample_image: str): |
| """ |
| Test skin analysis with image |
| """ |
| response = client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "image_base64": sample_image, |
| "user_id": "test_user" |
| } |
| ) |
| |
| assert response.status_code == 200 |
| data = response.json() |
| assert "id" in data |
| assert "conditions" in data |
| assert "pins" in data |
| assert "skin_tone" in data |
| assert "recommendations" in data |
| assert "disclaimer" in data |
|
|
| def test_analyze_skin_with_conditions(client: TestClient, auth_headers: dict): |
| """ |
| Test skin analysis with manual conditions |
| """ |
| response = client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "conditions": ["blackheads", "oily_skin"], |
| "user_id": "test_user" |
| } |
| ) |
| |
| assert response.status_code == 200 |
| data = response.json() |
| assert len(data["conditions"]) == 2 |
| assert data["conditions"][0]["condition"] == "blackheads" |
|
|
| def test_update_analysis_pins(client: TestClient, auth_headers: dict): |
| """ |
| Test updating analysis pins |
| """ |
| |
| create_response = client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "conditions": ["blackheads"], |
| "user_id": "test_user" |
| } |
| ) |
| analysis_id = create_response.json()["id"] |
| |
| |
| response = client.post( |
| f"/api/analysis/update-pins?analysis_id={analysis_id}", |
| headers=auth_headers, |
| json=[ |
| { |
| "condition": "hyperpigmentation", |
| "position": {"x": 0.5, "y": 0.5}, |
| "severity": "moderate" |
| } |
| ] |
| ) |
| |
| assert response.status_code == 200 |
| data = response.json() |
| assert len(data["pins"]) == 1 |
| assert data["pins"][0]["condition"] == "hyperpigmentation" |
|
|
| def test_get_analysis_history(client: TestClient, auth_headers: dict): |
| """ |
| Test getting analysis history |
| """ |
| |
| for i in range(3): |
| client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "conditions": [f"condition_{i}"], |
| "user_id": "test_user" |
| } |
| ) |
| |
| response = client.get( |
| "/api/analysis/history?limit=5", |
| headers=auth_headers |
| ) |
| |
| assert response.status_code == 200 |
| data = response.json() |
| assert "history" in data |
| assert len(data["history"]) >= 3 |
|
|
| def test_get_specific_analysis(client: TestClient, auth_headers: dict): |
| """ |
| Test getting specific analysis by ID |
| """ |
| |
| create_response = client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "conditions": ["blackheads"], |
| "user_id": "test_user" |
| } |
| ) |
| analysis_id = create_response.json()["id"] |
| |
| |
| response = client.get( |
| f"/api/analysis/{analysis_id}", |
| headers=auth_headers |
| ) |
| |
| assert response.status_code == 200 |
| data = response.json() |
| assert data["id"] == analysis_id |
|
|
| def test_delete_analysis(client: TestClient, auth_headers: dict): |
| """ |
| Test deleting analysis |
| """ |
| |
| create_response = client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "conditions": ["blackheads"], |
| "user_id": "test_user" |
| } |
| ) |
| analysis_id = create_response.json()["id"] |
| |
| |
| response = client.delete( |
| f"/api/analysis/{analysis_id}", |
| headers=auth_headers |
| ) |
| |
| assert response.status_code == 200 |
| assert response.json()["message"] == "Analysis deleted successfully" |
|
|
| def test_analysis_error_handling(client: TestClient, auth_headers: dict): |
| """ |
| Test error handling in analysis |
| """ |
| |
| response = client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "image_base64": "invalid_base64", |
| "user_id": "test_user" |
| } |
| ) |
| |
| assert response.status_code == 500 |
|
|
| def test_analysis_with_pagination(client: TestClient, auth_headers: dict): |
| """ |
| Test pagination in analysis history |
| """ |
| |
| for i in range(15): |
| client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "conditions": [f"condition_{i}"], |
| "user_id": "test_user" |
| } |
| ) |
| |
| |
| response = client.get( |
| "/api/analysis/history?limit=10", |
| headers=auth_headers |
| ) |
| |
| assert response.status_code == 200 |
| data = response.json() |
| assert len(data["history"]) == 10 |
|
|
| def test_analysis_with_filters(client: TestClient, auth_headers: dict): |
| """ |
| Test analysis with date filters |
| """ |
| from datetime import datetime, timedelta |
| |
| |
| with patch('datetime.datetime') as mock_datetime: |
| mock_datetime.utcnow.return_value = datetime.utcnow() - timedelta(days=5) |
| |
| client.post( |
| "/api/analysis/analyze-skin", |
| headers=auth_headers, |
| json={ |
| "conditions": ["old_analysis"], |
| "user_id": "test_user" |
| } |
| ) |
| |
| |
| response = client.get( |
| "/api/analysis/history?days=3", |
| headers=auth_headers |
| ) |
| |
| assert response.status_code == 200 |
| |
| for analysis in response.json()["history"]: |
| assert analysis["conditions"][0]["condition"] != "old_analysis" |