File tree Expand file tree Collapse file tree 4 files changed +77
-0
lines changed
Expand file tree Collapse file tree 4 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ import pytest
2+ from starlette .config import environ
3+ from starlette .testclient import TestClient
4+
5+ environ ["API_KEY" ] = "a1279d26-63ac-41f1-8266-4ef3702ad7cb"
6+ environ ["DEFAULT_MODEL_PATH" ] = "./sample_model/lin_reg_california_housing_model.joblib"
7+
8+ from fastapi_scaffolding .main import get_app # noqa: E402
9+
10+
11+ @pytest .fixture ()
12+ def test_client ():
13+ app = get_app ()
14+ with TestClient (app ) as test_client :
15+ yield test_client
Original file line number Diff line number Diff line change 1+ from fastapi_scaffolding .core import messages
2+
3+
4+ def test_auth_using_prediction_api_no_apikey_header (test_client ) -> None :
5+ response = test_client .post ('/api/model/predict' )
6+ assert response .status_code == 400
7+ assert response .json () == {"detail" : messages .NO_API_KEY }
8+
9+
10+ def test_auth_using_prediction_api_wrong_apikey_header (test_client ) -> None :
11+ response = test_client .post (
12+ '/api/model/predict' ,
13+ json = {"image" : "test" },
14+ headers = {"token" : "WRONG_TOKEN" }
15+ )
16+ assert response .status_code == 401
17+ assert response .json () == {"detail" : messages .AUTH_REQ }
Original file line number Diff line number Diff line change 1+ from fastapi_scaffolding .main import get_app
2+
3+ app = get_app ()
4+
5+
6+ def test_heartbeat (test_client ) -> None :
7+ response = test_client .get ('/api/health/heartbeat' )
8+ assert response .status_code == 200
9+ assert response .json () == {"is_alive" : True }
10+
11+
12+ def test_default_route (test_client ) -> None :
13+ response = test_client .get ('/' )
14+ assert response .status_code == 404
Original file line number Diff line number Diff line change 1+ from fastapi_scaffolding .core import config
2+
3+
4+ def test_prediction (test_client ) -> None :
5+ response = test_client .post (
6+ "/api/model/predict" ,
7+ json = {
8+ "median_income_in_block" : 8.3252 ,
9+ "median_house_age_in_block" : 41 ,
10+ "average_rooms" : 6 ,
11+ "average_bedrooms" : 1 ,
12+ "population_per_block" : 322 ,
13+ "average_house_occupancy" : 2.55 ,
14+ "block_latitude" : 37.88 ,
15+ "block_longitude" : - 122.23
16+ },
17+ headers = {"token" : str (config .API_KEY )}
18+ )
19+ assert response .status_code == 200
20+ assert "median_house_value" in response .json ()
21+ assert "currency" in response .json ()
22+
23+
24+ def test_prediction_nopayload (test_client ) -> None :
25+ response = test_client .post (
26+ "/api/model/predict" ,
27+ json = {},
28+ headers = {"token" : str (config .API_KEY )}
29+ )
30+ assert response .status_code == 422
31+
You can’t perform that action at this time.
0 commit comments