Skip to content

Commit

Permalink
fixing pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
ak8000 committed Apr 30, 2024
1 parent 1f77209 commit bdc27ed
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
10 changes: 8 additions & 2 deletions machine-learning-client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ def analyze_image(img_path):
logging.info("Analyzing the image at path: %s", img_path)
result = DeepFace.analyze(img_path=img_path, actions=["age", "gender", "emotion", "race"])
logging.info("Analysis result: %s", result)
return [result[0]["age"], result[0]["dominant_gender"], result[0]["dominant_emotion"], result[0]["dominant_race"]]
# Updated to access dictionary values directly
return [
result["age"],
result["dominant_gender"],
result["dominant_emotion"],
result["dominant_race"]
]
except Exception as e:
logging.error("An error occurred during image analysis: %s", e)
raise
raise
6 changes: 3 additions & 3 deletions machine-learning-client/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
app = Flask(__name__)

serverOptions = {
"socketTimeoutMS": 600000, # 10 minutes
"connectTimeoutMS": 30000, # 30 seconds
"serverSelectionTimeoutMS": 30000, # 30 seconds
"socketTimeoutMS": 6000000, # 10 minutes
"connectTimeoutMS": 3000000, # 30 seconds
"serverSelectionTimeoutMS": 3000000, # 30 seconds
}

client = MongoClient("mongodb://mongodb:27017/", **serverOptions)
Expand Down
1 change: 1 addition & 0 deletions machine-learning-client/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ tf-keras
deepface
pymongo
pytest
pytest-mock
pylint
black
9 changes: 7 additions & 2 deletions machine-learning-client/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ def test_analyze_image():
Test to see if analyze image returns an age, not necessarily the correct one
"""
# Mock the DeepFace library's analyze method
api.DeepFace.analyze = MagicMock(return_value=[{"age": 30}])
api.DeepFace.analyze = MagicMock(return_value={
"age": 30,
"dominant_gender": "Male",
"dominant_emotion": "Happy",
"dominant_race": "Asian"
})

# Call the analyze_image function with the sample image path
result = api.analyze_image(IMAGE_PATH)

# Assert that the result is as expected
assert result == 30
assert result == [30, "Male", "Happy", "Asian"], "Test failed: The analyze_image function did not return expected results"
37 changes: 22 additions & 15 deletions machine-learning-client/tests/test_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import pytest
from api_server import app
from unittest.mock import patch


sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
Expand All @@ -31,21 +32,27 @@ def client_fixture(testing_app):
# Set up the Flask app for testing
return testing_app.test_client()

def test_analyze_success(client, mocker):
def test_analyze_success(client):
"""
Test the analyze endpoint with a successful analysis
"""
# Mock the analyze_image function to return a dummy result
mocker.patch("api_server.analyze_image", return_value={"age": 25})

# Create a temporary file and send it as part of the request
with open(IMAGE_PATH, "rb") as image_file:
data = {
"age": (None, "25"),
"image": (image_file, "tester_photo.png")
}
response = client.post("analyze", data=data)

# Check that the response status code is 200 and the result matches the expected value
assert response.status_code == 200
assert response.json == {"age": 25}
# Correctly patch the analyze_image function used within the Flask app
with patch("api.analyze_image", return_value=[25, "Male", "Happy", "Asian"]) as mock_analyze:
# Ensure you're sending the file under the correct form name expected by the Flask route
with open(IMAGE_PATH, "rb") as image_file:
data = {
"file": (image_file, "tester_photo.png") # Change "image" to "file" if that's what your Flask route expects
}
response = client.post("/analyze", data=data, content_type='multipart/form-data')

# Check that the response status code is 200 and the result matches the expected value
assert response.status_code == 200
assert response.json == {
"predicted_age": 25,
"predicted_gender": "Male",
"dominant_emotion": "Happy",
"predicted_race": "Asian"
}, "Test failed: The response did not match the expected JSON"

# Ensure the function was called once
mock_analyze.assert_called_once()
4 changes: 2 additions & 2 deletions web-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
# MongoDB connection
serverOptions = {
"socketTimeoutMS": 600000, # 10 minutes
"connectTimeoutMS": 30000, # 30 seconds
"serverSelectionTimeoutMS": 30000, # 30 seconds
"connectTimeoutMS": 300000, # 300 seconds
"serverSelectionTimeoutMS": 300000, # 300 seconds
}

client = MongoClient("mongodb://mongodb:27017/", **serverOptions)
Expand Down

0 comments on commit bdc27ed

Please sign in to comment.