eaedk commited on
Commit
013c3a3
1 Parent(s): c713165
Files changed (1) hide show
  1. tests/test_api_predict.py +65 -0
tests/test_api_predict.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import pandas as pd
3
+ import requests
4
+
5
+ # Define base URL for the FastAPI application
6
+ BASE_URL = "http://127.0.0.1:8000"
7
+
8
+ # Define sample device specifications
9
+ device_specs = {
10
+ "battery_power": 3000,
11
+ "blue": 1,
12
+ "clock_speed": 2.0,
13
+ "dual_sim": 0,
14
+ "fc": 5.0,
15
+ "four_g": 1,
16
+ "int_memory": 64.0,
17
+ "m_dep": 0.4,
18
+ "mobile_wt": 150.0,
19
+ "n_cores": 8.0,
20
+ "pc": 12.0,
21
+ "px_height": 1920.0,
22
+ "px_width": 1080.0,
23
+ "ram": 4.0,
24
+ "sc_h": 5.5,
25
+ "sc_w": 2.5,
26
+ "talk_time": 10.0,
27
+ "three_g": 1,
28
+ "touch_screen": 1,
29
+ "wifi": 1
30
+ }
31
+
32
+
33
+ def test_predict_price():
34
+ """
35
+ Test the predict price endpoint.
36
+
37
+ Steps:
38
+ 1. Define device specifications and device ID.
39
+ 2. Send a POST request to the predict price endpoint with the device specifications.
40
+ 3. Validate that the response status code is 200 (OK).
41
+ 4. Parse the response JSON and validate that it contains the expected fields.
42
+ 5. Print the predicted price.
43
+ """
44
+ # Define device ID
45
+ device_id = 1
46
+
47
+ # Send POST request to predict price
48
+ response = requests.post(f"{BASE_URL}/predict/{device_id}", json=device_specs)
49
+
50
+ # Check if request was successful (status code 200)
51
+ assert response.status_code == 200
52
+
53
+ # Parse response JSON
54
+ data = pd.DataFrame(response.json())
55
+
56
+ # Validate response fields
57
+ assert "device_id" in data.columns
58
+ assert "predicted_price" in data.columns
59
+
60
+ # Print predicted price
61
+ print(f"Predictions\n{data[['device_id', 'predicted_price']].to_markdown()} ")
62
+
63
+
64
+ if __name__ == "__main__":
65
+ pytest.main()