File size: 3,131 Bytes
89b8989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
import pytest
import sys
import os
from pathlib import Path

# Add the project root to the Python path
project_root = Path(__file__).parent.parent
sys.path.append(str(project_root))

from unittest.mock import patch, MagicMock
from fastapi import HTTPException
from fastapi.testclient import TestClient

# Test the root endpoint
def test_read_root(test_app):
    response = test_app.get("/")
    assert response.status_code == 200
    assert response.json() == {"message": "Health Assistant API is running"}

# Test food recognition endpoint
@patch('backend.app.services.food_analyzer_service.HybridFoodAnalyzer.analyze_food')
def test_recognize_food(mock_analyze_food, test_app, sample_image):
    # Mock the response from the analyzer
    mock_analyze_food.return_value = {
        "food_name": "apple",
        "chinese_name": "蘋果",
        "confidence": 0.95,
        "success": True
    }
    
    # Create a test file
    files = {"file": ("test_image.jpg", sample_image, "image/jpeg")}
    
    # Make the request
    response = test_app.post("/recognize-food", files=files)
    
    # Check the response
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert "food_name" in data
    assert "chinese_name" in data
    assert "confidence" in data

# Test nutrition analysis endpoint
@patch('app.services.food_analyzer_service.HybridFoodAnalyzer.analyze_nutrition')
def test_analyze_nutrition(mock_analyze_nutrition, test_app):
    # Mock the response from the analyzer
    mock_analyze_nutrition.return_value = {
        "nutrition": {
            "calories": 95,
            "protein": 0.5,
            "fat": 0.3,
            "carbohydrates": 25.0
        },
        "success": True
    }
    
    # Test data
    test_data = {"food_name": "apple"}
    
    # Make the request
    response = test_app.post("/analyze-nutrition", json=test_data)
    
    # Check the response
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert "nutrition" in data
    assert "analysis" in data

# Test error handling for invalid image
def test_recognize_food_invalid_file(test_app):
    # Create an invalid file
    files = {"file": ("test.txt", b"not an image", "text/plain")}
    
    # Make the request
    response = test_app.post("/recognize-food", files=files)
    
    # Check the response
    assert response.status_code == 400
    data = response.json()
    assert data["detail"] == "Invalid image file"

# Test error handling for missing food name
@patch('app.services.food_analyzer_service.HybridFoodAnalyzer.analyze_nutrition')
def test_analyze_nutrition_missing_food(mock_analyze_nutrition, test_app):
    # Configure the mock to raise an exception
    mock_analyze_nutrition.side_effect = ValueError("Food name is required")
    
    # Test data with missing food name
    test_data = {}
    
    # Make the request
    response = test_app.post("/analyze-nutrition", json=test_data)
    
    # Check the response
    assert response.status_code == 400
    data = response.json()
    assert "detail" in data