Skip to content

Commit beb87f3

Browse files
Init test for submitting color image.
1 parent 2a614c5 commit beb87f3

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ __pycache__/
1313
# Keras Models
1414
*model/*
1515

16+
# Images Downloaded for Testing
17+
*.jpg
18+
*.jpeg
19+
*.png
20+
1621
# Distribution / packaging
1722
.Python
1823
build/

app/__init__.py

Whitespace-only changes.

app/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
predictor = ImagePredictor.init_from_config_path(predictor_config_path)
1616

1717

18-
@app.post("/classify-image/", tags=["Detect Fire in an Image"])
19-
def create_upload_file(file: UploadFile = File(...)):
18+
@app.post("/detect-fire/", tags=["Detect Fire in an Image"])
19+
def make_prediction(file: UploadFile = File(...)):
2020
"""Predicts the possibility that a RBG image contains fire."""
2121
return predictor.predict_from_file(file.file)

app/main_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
3+
from fastapi.testclient import TestClient
4+
import tensorflow as tf
5+
6+
from app.main import app
7+
8+
client = TestClient(app)
9+
10+
class ClassifierAPITest(unittest.TestCase):
11+
12+
TEST_COLOR_IMG_URL = "https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg"
13+
14+
def test_make_prediction_normal(self):
15+
'''Client submits a RGB image and gets a valid response.'''
16+
# A: get image
17+
image_path = tf.keras.utils.get_file(
18+
origin=self.TEST_COLOR_IMG_URL,
19+
cache_dir=".", cache_subdir="."
20+
)
21+
with open(image_path, 'rb') as f:
22+
# B: format a mock request
23+
files = {"file": (f.name, f, "multipart/form-data")}
24+
# C: make a request, and verify a valid prediction is returned
25+
response = client.post(
26+
'/detect-fire', files=files
27+
)
28+
assert response.status_code == 307

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ fastapi==0.49.0
44
opencv-python-headless==4.5.3.56
55
PyYAML==5.3
66
uvicorn
7+
pytest==6.2.5
78
python-multipart

0 commit comments

Comments
 (0)