Skip to content

Commit 230361f

Browse files
committed
Add FastAPI app with Docker, CI/CD, and linter checking flake8 and pytest
1 parent 2786465 commit 230361f

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

test/test_api.py

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from unittest.mock import MagicMock, patch
21
import sys
32
import os
43
import warnings
4+
from unittest.mock import MagicMock, patch
55

66
warnings.filterwarnings("ignore", category=DeprecationWarning)
77

8+
# Add src folder to sys.path so 'api' can be imported
89
sys.path.insert(
910
0,
1011
os.path.abspath(
@@ -19,43 +20,48 @@
1920
mock_model = MagicMock()
2021
mock_model.predict_proba.return_value = [[0.3, 0.7]]
2122

22-
with patch('mlflow.sklearn.load_model', return_value=mock_model):
23-
from fastapi.testclient import TestClient
24-
from api.main import app
25-
26-
client = TestClient(app)
27-
28-
def test_predict():
29-
sample_data = {
30-
"Recency": 1,
31-
"Frequency": 127,
32-
"Monetary": 489358,
33-
"Transaction_Hour": 8,
34-
"FraudResult": 0,
35-
"Average_Transaction_Amount": 2424.58,
36-
"Transaction_Day": 12,
37-
"ChannelId_ChannelId_2": False,
38-
"ChannelId_ChannelId_3": True,
39-
"ChannelId_ChannelId_5": False,
40-
"ProviderId_ProviderId_2": False,
41-
"ProviderId_ProviderId_3": False,
42-
"ProviderId_ProviderId_4": False,
43-
"ProviderId_ProviderId_5": False,
44-
"ProviderId_ProviderId_6": True,
45-
"PricingStrategy_1": False,
46-
"PricingStrategy_2": True,
47-
"PricingStrategy_4": False,
48-
"ProductCategory_data_bundles": False,
49-
"ProductCategory_financial_services": False,
50-
"ProductCategory_movies": False,
51-
"ProductCategory_other": False,
52-
"ProductCategory_ticket": False,
53-
"ProductCategory_transport": False,
54-
"ProductCategory_tv": False,
55-
"ProductCategory_utility_bill": False,
56-
}
57-
58-
response = client.post("/predict", json=sample_data)
59-
assert response.status_code == 200
60-
assert "risk_probability" in response.json()
61-
assert abs(response.json()["risk_probability"] - 0.7) < 1e-6
23+
patcher = patch('mlflow.sklearn.load_model', return_value=mock_model)
24+
patcher.start()
25+
26+
from fastapi.testclient import TestClient # noqa: E402
27+
from api.main import app # noqa: E402
28+
client = TestClient(app)
29+
30+
31+
def test_predict():
32+
sample_data = {
33+
"Recency": 1,
34+
"Frequency": 127,
35+
"Monetary": 489358,
36+
"Transaction_Hour": 8,
37+
"FraudResult": 0,
38+
"Average_Transaction_Amount": 2424.58,
39+
"Transaction_Day": 12,
40+
"ChannelId_ChannelId_2": False,
41+
"ChannelId_ChannelId_3": True,
42+
"ChannelId_ChannelId_5": False,
43+
"ProviderId_ProviderId_2": False,
44+
"ProviderId_ProviderId_3": False,
45+
"ProviderId_ProviderId_4": False,
46+
"ProviderId_ProviderId_5": False,
47+
"ProviderId_ProviderId_6": True,
48+
"PricingStrategy_1": False,
49+
"PricingStrategy_2": True,
50+
"PricingStrategy_4": False,
51+
"ProductCategory_data_bundles": False,
52+
"ProductCategory_financial_services": False,
53+
"ProductCategory_movies": False,
54+
"ProductCategory_other": False,
55+
"ProductCategory_ticket": False,
56+
"ProductCategory_transport": False,
57+
"ProductCategory_tv": False,
58+
"ProductCategory_utility_bill": False,
59+
}
60+
61+
response = client.post("/predict", json=sample_data)
62+
assert response.status_code == 200
63+
assert "risk_probability" in response.json()
64+
assert abs(response.json()["risk_probability"] - 0.7) < 1e-6
65+
66+
67+
patcher.stop()

0 commit comments

Comments
 (0)