File size: 1,714 Bytes
9ca0b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from fastapi.testclient import TestClient
from app.main import app

client = TestClient(app)

# Données valides
valid_data = {
    "NumberofFloors": 50,
    "NumberofBuildings": 1,
    "GFAPerFloor": 500,
    "PropertyGFATotal": 500000,
    "GFA_Prison_Incarceration": 0,
    "GFA_College_University": 0,
    "GFA_Office": 0,
    "GFA_Parking": 0,
    "GFA_Medical_Office": 0,
    "GFA_Indoor_Arena": 0,
    "GFA_Hospital_General_Medical_Surgical": 0,
    "GFA_Data_Center": 0,
    "GFA_Laboratory": 0,
    "GFA_Supermarket_Grocery_Store": 0,
    "GFA_Urgent_Care_Clinic_Other_Outpatient": 0,
    "BuildingType_Nonresidential_WA": 0,
    "ZipCode_infrequent_sklearn": 0,
    "EPAPropertyType_infrequent_sklearn": 0
}

def test_predict_valid():
    response = client.post("/predict", json=valid_data)
    assert response.status_code == 200
    json_resp = response.json()
    assert "prediction" in json_resp
    assert isinstance(json_resp["prediction"], float)

def test_predict_missing_column():
    invalid_data = valid_data.copy()
    del invalid_data["NumberofFloors"]  # Supprime une colonne
    response = client.post("/predict", json=invalid_data)
    assert response.status_code == 422
    json_resp = response.json()
    assert any("Feature manquante" in msg for msg in json_resp["detail"])

def test_predict_wrong_type():
    invalid_data = valid_data.copy()
    invalid_data["NumberofFloors"] = "cinquante"
    response = client.post("/predict", json=invalid_data)
    assert response.status_code == 422
    json_resp = response.json()
    # Vérifie le message Pydantic plutôt que "Type incorrect"
    assert any("Input should be a valid integer" in msg for msg in json_resp["detail"])