-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.py
82 lines (65 loc) · 2.52 KB
/
app_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pytest
import re
import numpy as np
import json
from flask import Flask, render_template, request
from werkzeug.datastructures import FileStorage
from PIL import Image
from app import prepare_image, predict
@pytest.fixture
def app():
app = Flask(__name__)
@app.route("/")
def home():
return render_template("view.html")
return app
def test_home_endpoint(app):
with app.test_client() as client:
response = client.get("/")
assert response.status_code == 200
assert response.mimetype == "text/html"
content = response.data.decode("utf-8")
# Check that the correct template is being used
assert content is not None
def test_home_content(app):
with app.test_client() as client:
response = client.get("/")
# Extract the HTML content
content = response.data.decode("utf-8")
# Verify that the form contains the expected elements
assert "<input type=" in content
def test_prepare_image_with_valid_image_path():
# Set up
image_path = "images/test/image.jpg"
# Call the function
img_array = prepare_image(image_path)
# Check the shape of the image array
expected_shape = (1, 224, 224, 3)
assert img_array.shape == expected_shape
# Check the type of the image array
expected_type = np.float32
assert img_array.dtype == expected_type
# Check the values of the image array
assert -2.0 <= img_array.min() <= 2.0
# assert img_array.max() == 1.0
def test_prepare_image_with_invalid_image_path():
# Set up
invalid_image_path = "images/invalid_image_path.jpg"
# Call the function
with pytest.raises(FileNotFoundError):
img_array = prepare_image(invalid_image_path)
def test_predict_endpoint(app):
# You may need to adjust the following based on your actual file path and image file
image_file_path = "images/test/image.jpg"
# Send a POST request to the /predict endpoint
with open(image_file_path, "rb") as image_file:
response = app.test_client().post("/predict", data={"image": (image_file, "image.jpg")})
# Check if the response status code is 200 (OK)
#assert response.status_code == 200
assert response.headers["Content-Type"] == "text/html; charset=utf-8"
print(response)
# assert b"Processing image..." in response
# assert b"Calling to model..." in response
# assert b"result.html" in response
# assert b"label" in response
# assert b"probability" in response